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

Conversation

olivierphi
Copy link
Contributor

@olivierphi olivierphi commented Jun 14, 2022

By doing this the users get their original cursor style back when Rich gives control back to the terminal, rather than a "full size" one

Fixes #2333

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code. (new code is tested by existing "show/hide cursor" tests 馃檪 )
  • I accept that @willmcgugan may be pedantic in the code review.

Description

Before the fix:

We can see the cursor is no longer the same (converted to a "thick" one) after Rich gives control back to the terminal:

Command.Prompt.2022-06-14.15-39-25.mp4

After the fix:

Cursor-after-Rich is well and truly the same than cursor-before-Rich:

Command.Prompt.2022-06-14.15-38-47.mp4

(I also tested with Powershell, and the fix looks to work there too :-)

@olivierphi olivierphi force-pushed the windows-dont-override-cursor-info branch from 386e4fe to 49aa015 Compare June 14, 2022 15:49
@codecov-commenter
Copy link

codecov-commenter commented Jun 14, 2022

Codecov Report

Merging #2339 (08ea255) into master (14d47c9) will decrease coverage by 0.17%.
The diff coverage is 95.58%.

@@            Coverage Diff             @@
##           master    #2339      +/-   ##
==========================================
- Coverage   98.88%   98.71%   -0.18%     
==========================================
  Files          73       73              
  Lines        7629     7704      +75     
==========================================
+ Hits         7544     7605      +61     
- Misses         85       99      +14     
Flag Coverage 螖
unittests 98.71% <95.58%> (-0.18%) 猬囷笍

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage 螖
rich/_export_format.py 100.00% <酶> (酶)
rich/cells.py 100.00% <酶> (酶)
rich/live.py 97.90% <40.00%> (-2.10%) 猬囷笍
rich/_win32_console.py 89.94% <92.30%> (-1.54%) 猬囷笍
rich/console.py 98.29% <96.03%> (-0.49%) 猬囷笍
rich/_inspect.py 100.00% <100.00%> (酶)
rich/_wrap.py 100.00% <100.00%> (酶)
rich/box.py 100.00% <100.00%> (酶)
rich/control.py 100.00% <100.00%> (酶)
rich/progress.py 92.76% <100.00%> (+0.03%) 猬嗭笍
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
螖 = absolute <relative> (impact), 酶 = not affected, ? = missing data
Powered by Codecov. Last update d28e636...08ea255. Read the comment docs.

By doing this the users get their original cursor style back when Rich gives control back to the terminal, rather than a "full size" one
@olivierphi olivierphi force-pushed the windows-dont-override-cursor-info branch from 49aa015 to a7124ea Compare June 15, 2022 08:38
)
def test_cursor_position(_):
@pytest.fixture
def win32_console_getters():
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I replaced the 20+ repeated occurrences of @patch.object(_win32_console, "GetConsoleScreenBufferInfo", return_value=StubScreenBufferInfo) with a Pytest fixture, which should scale better if we add more patching-that-is-common-to-most-tests in the future .
At the moment this fixture is in charge of patching the previous GetConsoleScreenBufferInfo, but also the new GetConsoleCursorInfo 馃檪

) as GetConsoleScreenBufferInfo, mock.patch.object(
_win32_console, "GetConsoleCursorInfo", side_effect=stub_console_cursor_info
) as GetConsoleCursorInfo:
yield {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the fixture yields the created mocks, which is useful for tests that few need to override the default behaviour :-)

@olivierphi olivierphi marked this pull request as ready for review June 15, 2022 08:48
Copy link
Member

@darrenburns darrenburns left a comment

Choose a reason for hiding this comment

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

Nice work!

Copy link
Collaborator

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

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

Once change request...

@@ -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

@olivierphi olivierphi force-pushed the windows-dont-override-cursor-info branch from 3d7cea4 to 08ea255 Compare June 15, 2022 13:42
@willmcgugan willmcgugan merged commit 761cc8d into master Jun 15, 2022
@willmcgugan willmcgugan deleted the windows-dont-override-cursor-info branch June 15, 2022 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Console.show_cursor() (and displaying Rich progress bars) clobbers cursor style on Windows
4 participants