Skip to content

Commit

Permalink
[win32] Read the cursor size every time we want to hide or show it
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierphi committed Jun 15, 2022
1 parent a7124ea commit 3d7cea4
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions rich/_win32_console.py
Expand Up @@ -364,12 +364,6 @@ def __init__(self, file: "IO[str]") -> None:
default_text = GetConsoleScreenBufferInfo(handle).wAttributes
self._default_text = default_text

# Store the current cursor size
# @link https://docs.microsoft.com/en-us/windows/console/console-cursor-info-str
initial_cursor_info = CONSOLE_CURSOR_INFO()
GetConsoleCursorInfo(self._handle, cursor_info=initial_cursor_info)
self._initial_cursor_size = initial_cursor_info.dwSize

self._default_fore = default_text & 7
self._default_back = (default_text >> 4) & 7
self._default_attrs = self._default_fore | (self._default_back << 4)
Expand Down Expand Up @@ -553,16 +547,14 @@ def move_cursor_backward(self) -> None:

def hide_cursor(self) -> None:
"""Hide the cursor"""
invisible_cursor = CONSOLE_CURSOR_INFO(
dwSize=self._initial_cursor_size, bVisible=0
)
current_cursor_size = self._get_cursor_size()
invisible_cursor = CONSOLE_CURSOR_INFO(dwSize=current_cursor_size, bVisible=0)
SetConsoleCursorInfo(self._handle, cursor_info=invisible_cursor)

def show_cursor(self) -> None:
"""Show the cursor"""
visible_cursor = CONSOLE_CURSOR_INFO(
dwSize=self._initial_cursor_size, bVisible=1
)
current_cursor_size = self._get_cursor_size()
visible_cursor = CONSOLE_CURSOR_INFO(dwSize=current_cursor_size, bVisible=1)
SetConsoleCursorInfo(self._handle, cursor_info=visible_cursor)

def set_title(self, title: str) -> None:
Expand All @@ -574,6 +566,12 @@ def set_title(self, title: str) -> None:
assert len(title) < 255, "Console title must be less than 255 characters"
SetConsoleTitle(title)

def _get_cursor_size(self) -> int:
"""Get the percentage of the character cell that is filled by the cursor"""
cursor_info = CONSOLE_CURSOR_INFO()
GetConsoleCursorInfo(self._handle, cursor_info=cursor_info)
return cursor_info.dwSize


if __name__ == "__main__":
handle = GetStdHandle()
Expand Down

0 comments on commit 3d7cea4

Please sign in to comment.