def clear_line(self): """ Entirely deletes the current line. This is useful if it needs to be redrawn, for instance if the window has been resized. """ # Calculate where the beginning of the line is (in terms of line wrapping). lines = (len(self.last_line) + len(self.input_buffer) - self.cursor_position) // context.window_size[1] if lines > 0: write(ansi.CUU(lines)) # Go to the line where the buffer starts. write(ansi.CUB(context.window_size[1]) + ansi.ED(0)) # Delete all after the caret.
def cursor_back(self, adjust_internal_cursor=True): """ Moves the on-screen caret back one position, going up to the previous line if needed. :param adjust_internal_cursor: Whether the internal cursor should be updated accordingly. This should always be True unless you have a very good reason not to do so. """ if self.cursor_position + 1 <= len(self.input_buffer): if not self.caret_at_sol(): write(ansi.CUB()) else: write(ansi.CUU() + ansi.CUF(context.window_size[1])) if adjust_internal_cursor: self.cursor_position += 1
def cursor_forward(self, adjust_internal_cursor=True): """ Moves the on-screen caret forward one position, going down to the next line if needed. :param adjust_internal_cursor: Whether the internal cursor should be updated accordingly. This should always be True unless you have a very good reason not to do so. """ if self.cursor_position > 0: if not self.caret_at_eol(): write(ansi.CUF()) else: write(ansi.CUD() + ansi.CUB(context.window_size[1])) if adjust_internal_cursor: self.cursor_position -= 1
def relative_caret_move(x, y): """ Move the caret relatively to its current position. :param x: The number of lines to move. Negative numbers go upwards. :param y: The number of columns to move. Negative numbers go backwards. """ command = b"" if x > 0: command += ansi.CUU(x) if x < 0: command += ansi.CUD(-x) if y > 0: command += ansi.CUF(y) if y < 0: command += ansi.CUB(-y) if command: write(command)