Skip to content

Optimize rendering. Don't output escape codes for hiding/showing the cursor if not needed #1968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading