From 60a6e5f74f18bffbce82f7d21c48f0d123d1b2e0 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 20 Jan 2025 15:43:40 +0000 Subject: [PATCH] Optimize rendering. Don't output escape codes for hiding/showing the cursor if not needed. This fixes a regression that was introduced in https://github.com/prompt-toolkit/python-prompt-toolkit/pull/1925 --- src/prompt_toolkit/output/vt100.py | 13 +++++++++++-- src/prompt_toolkit/renderer.py | 5 +++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/prompt_toolkit/output/vt100.py b/src/prompt_toolkit/output/vt100.py index 069636b8c..90df21e55 100644 --- a/src/prompt_toolkit/output/vt100.py +++ b/src/prompt_toolkit/output/vt100.py @@ -436,6 +436,11 @@ def __init__( # default, we don't change them.) self._cursor_shape_changed = False + # Don't hide/show the cursor when this was already done. + # (`None` means that we don't know whether the cursor is visible or + # not.) + self._cursor_visible: bool | None = None + @classmethod def from_pty( cls, @@ -651,10 +656,14 @@ def cursor_backward(self, amount: int) -> None: self.write_raw("\x1b[%iD" % amount) def hide_cursor(self) -> None: - self.write_raw("\x1b[?25l") + if self._cursor_visible in (True, None): + self._cursor_visible = False + self.write_raw("\x1b[?25l") def show_cursor(self) -> None: - self.write_raw("\x1b[?12l\x1b[?25h") # Stop blinking cursor and show. + if self._cursor_visible in (False, None): + self._cursor_visible = True + self.write_raw("\x1b[?12l\x1b[?25h") # Stop blinking cursor and show. def set_cursor_shape(self, cursor_shape: CursorShape) -> None: if cursor_shape == CursorShape._NEVER_CHANGE: diff --git a/src/prompt_toolkit/renderer.py b/src/prompt_toolkit/renderer.py index d0590830d..8d5e03c19 100644 --- a/src/prompt_toolkit/renderer.py +++ b/src/prompt_toolkit/renderer.py @@ -353,6 +353,11 @@ def __init__( self.mouse_support = to_filter(mouse_support) self.cpr_not_supported_callback = cpr_not_supported_callback + # TODO: Move following state flags into `Vt100_Output`, similar to + # `_cursor_shape_changed` and `_cursor_visible`. But then also + # adjust the `Win32Output` to not call win32 APIs if nothing has + # to be changed. + self._in_alternate_screen = False self._mouse_support_enabled = False self._bracketed_paste_enabled = False