Skip to content

Commit

Permalink
Optimize rendering. Don't output escape codes for hiding/showing the …
Browse files Browse the repository at this point in the history
…cursor if not needed. (#1968)

This fixes a regression that was introduced in
#1925
  • Loading branch information
jonathanslenders authored Jan 20, 2025
1 parent ace74db commit 76957c8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/prompt_toolkit/output/vt100.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions src/prompt_toolkit/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 76957c8

Please sign in to comment.