Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[win32] Use initial cursor size when hiding or showing it #2339

Merged
merged 2 commits into from Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix Rich clobbering cursor style on Windows https://github.com/Textualize/rich/pull/2339
- Fix text wrapping edge case https://github.com/Textualize/rich/pull/2296
- Allow exceptions that are raised while a Live is rendered to be displayed and/or processed https://github.com/Textualize/rich/pull/2305
- Fix crashes that can happen with `inspect` when docstrings contain some special control codes https://github.com/Textualize/rich/pull/2294
Expand Down
38 changes: 36 additions & 2 deletions rich/_win32_console.py
Expand Up @@ -263,6 +263,30 @@ def SetConsoleCursorPosition(
return bool(_SetConsoleCursorPosition(std_handle, coords))


_GetConsoleCursorInfo = windll.kernel32.GetConsoleCursorInfo
_GetConsoleCursorInfo.argtypes = [
wintypes.HANDLE,
ctypes.POINTER(CONSOLE_CURSOR_INFO),
]
_GetConsoleCursorInfo.restype = wintypes.BOOL


def GetConsoleCursorInfo(
std_handle: wintypes.HANDLE, cursor_info: CONSOLE_CURSOR_INFO
) -> bool:
"""Get the cursor info - used to get cursor visibility and width

Args:
std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
cursor_info (CONSOLE_CURSOR_INFO): CONSOLE_CURSOR_INFO ctype struct that receives information
about the console's cursor.

Returns:
bool: True if the function succeeds, otherwise False.
"""
return bool(_GetConsoleCursorInfo(std_handle, byref(cursor_info)))


_SetConsoleCursorInfo = windll.kernel32.SetConsoleCursorInfo
_SetConsoleCursorInfo.argtypes = [
wintypes.HANDLE,
Expand Down Expand Up @@ -340,6 +364,12 @@ 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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be storing the cursor size in the constructor. Consider if the user changes the cursor size after this object has been created (hardly likely, but possible). Now if they call hide_cursor or show_cursor the cursor size may be incorrect.

We should add this to hide_cursor and show_cursor. i.e. get the cursor info first, modify the visible flag, and then set the cursor info in a single operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! 馃檪 08ea255

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 @@ -523,12 +553,16 @@ def move_cursor_backward(self) -> None:

def hide_cursor(self) -> None:
"""Hide the cursor"""
invisible_cursor = CONSOLE_CURSOR_INFO(dwSize=100, bVisible=0)
invisible_cursor = CONSOLE_CURSOR_INFO(
dwSize=self._initial_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=100, bVisible=1)
visible_cursor = CONSOLE_CURSOR_INFO(
dwSize=self._initial_cursor_size, bVisible=1
)
SetConsoleCursorInfo(self._handle, cursor_info=visible_cursor)

def set_title(self, title: str) -> None:
Expand Down