Skip to content

__terminal__ output reappears after clearing the element #913

Description

@CarstenGrohmann

Problem

Output written to __terminal__ cannot be cleared. Clearing the target element from application code removes the rendered text only. The next print puts everything back.

Cause

__Terminal__ keeps the output history in self.buffer and rewrites the whole element from that buffer on every call:

def print (self, *args, sep = ' ', end = '\n'):
self.buffer = '{}{}{}'.format (self.buffer, sep.join ([str (arg) for arg in args]), end) [-4096 : ]
if self.element:
self.element.innerHTML = self.buffer.replace ('\n', '<br>') .replace (' ', '&nbsp')

The class offers no method to reset self.buffer. Replacing the module-level instance does not help either, because print and input are bound to the original instance when the runtime module is loaded:

__terminal__ = __Terminal__ ()
print = __terminal__.print
input = __terminal__.input

Proposed fix

Add a clear() method that resets buffer and element together, so the buffer cannot restore stale output:

    def clear (self):
        self.buffer = ''

        if self.element:
            self.element.innerHTML = '_'

'_' is the initial content written by __init__, so clear() returns the terminal to its start state.

Workaround

Reset both parts by hand:

__terminal__.buffer = ''
__terminal__.element.innerHTML = ''

Version

Transcrypt 3.9.5

Contribution

I am happy to write the pull request for this change if the proposed approach is acceptable.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions