Skip to content

Commit 60a6e5f

Browse files
Optimize rendering. Don't output escape codes for hiding/showing the cursor if not needed.
This fixes a regression that was introduced in #1925
1 parent ace74db commit 60a6e5f

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/prompt_toolkit/output/vt100.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,11 @@ def __init__(
436436
# default, we don't change them.)
437437
self._cursor_shape_changed = False
438438

439+
# Don't hide/show the cursor when this was already done.
440+
# (`None` means that we don't know whether the cursor is visible or
441+
# not.)
442+
self._cursor_visible: bool | None = None
443+
439444
@classmethod
440445
def from_pty(
441446
cls,
@@ -651,10 +656,14 @@ def cursor_backward(self, amount: int) -> None:
651656
self.write_raw("\x1b[%iD" % amount)
652657

653658
def hide_cursor(self) -> None:
654-
self.write_raw("\x1b[?25l")
659+
if self._cursor_visible in (True, None):
660+
self._cursor_visible = False
661+
self.write_raw("\x1b[?25l")
655662

656663
def show_cursor(self) -> None:
657-
self.write_raw("\x1b[?12l\x1b[?25h") # Stop blinking cursor and show.
664+
if self._cursor_visible in (False, None):
665+
self._cursor_visible = True
666+
self.write_raw("\x1b[?12l\x1b[?25h") # Stop blinking cursor and show.
658667

659668
def set_cursor_shape(self, cursor_shape: CursorShape) -> None:
660669
if cursor_shape == CursorShape._NEVER_CHANGE:

src/prompt_toolkit/renderer.py

+5
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,11 @@ def __init__(
353353
self.mouse_support = to_filter(mouse_support)
354354
self.cpr_not_supported_callback = cpr_not_supported_callback
355355

356+
# TODO: Move following state flags into `Vt100_Output`, similar to
357+
# `_cursor_shape_changed` and `_cursor_visible`. But then also
358+
# adjust the `Win32Output` to not call win32 APIs if nothing has
359+
# to be changed.
360+
356361
self._in_alternate_screen = False
357362
self._mouse_support_enabled = False
358363
self._bracketed_paste_enabled = False

0 commit comments

Comments
 (0)