rich icon indicating copy to clipboard operation
rich copied to clipboard

[BUG] Terminal settings aren't restored

Open nineteendo opened this issue 9 months ago • 2 comments

  • [X] I've checked docs and closed issues for possible solutions.
  • [X] I can't find my issue in the FAQ.

Describe the bug

On exit

Rich doesn't restore the terminal back to its original settings when the program terminates:

wannes@Stefans-iMac ~ % python -c "import rich.console; rich.console.Console().show_cursor(False)"
  • Before (cursor is shown) before
  • After (cursor is hidden) NOK after

On suspend

Rich doesn't restore the terminal back to its original settings when the program is suspended:

from rich.console import Console

console: Console = Console()
console.show_cursor(show=False)
input("Press enter")
console.show_cursor()
  • Before (cursor is shown) 1
  • Hide cursor 2
  • Suspend (cursor is hidden) NOK 3
  • Show cursor manually 4
  • Resume (cursor is shown) NOK 5
  • Show cursor 6

Platform

Click to expand
╭─────────────────────── <class 'rich.console.Console'> ───────────────────────╮
│ A high level console interface.                                              │
│                                                                              │
│ ╭──────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=80 ColorSystem.EIGHT_BIT>                                 │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
│                                                                              │
│     color_system = '256'                                                     │
│         encoding = 'utf-8'                                                   │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w'               │
│                    encoding='utf-8'>                                         │
│           height = 24                                                        │
│    is_alt_screen = False                                                     │
│ is_dumb_terminal = False                                                     │
│   is_interactive = True                                                      │
│       is_jupyter = False                                                     │
│      is_terminal = True                                                      │
│   legacy_windows = False                                                     │
│         no_color = False                                                     │
│          options = ConsoleOptions(                                           │
│                        size=ConsoleDimensions(width=80, height=24),          │
│                        legacy_windows=False,                                 │
│                        min_width=1,                                          │
│                        max_width=80,                                         │
│                        is_terminal=True,                                     │
│                        encoding='utf-8',                                     │
│                        max_height=24,                                        │
│                        justify=None,                                         │
│                        overflow=None,                                        │
│                        no_wrap=False,                                        │
│                        highlight=None,                                       │
│                        markup=None,                                          │
│                        height=None                                           │
│                    )                                                         │
│            quiet = False                                                     │
│           record = False                                                     │
│         safe_box = True                                                      │
│             size = ConsoleDimensions(width=80, height=24)                    │
│        soft_wrap = False                                                     │
│           stderr = False                                                     │
│            style = None                                                      │
│         tab_size = 8                                                         │
│            width = 80                                                        │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭──────── Environment Variables ────────╮
│ {                                     │
│     'TERM': 'xterm-256color',         │
│     'COLORTERM': None,                │
│     'CLICOLOR': None,                 │
│     'NO_COLOR': None,                 │
│     'TERM_PROGRAM': 'Apple_Terminal', │
│     'COLUMNS': None,                  │
│     'LINES': None,                    │
│     'JUPYTER_COLUMNS': None,          │
│     'JUPYTER_LINES': None,            │
│     'JPY_PARENT_PID': None,           │
│     'VSCODE_VERBOSE_LOGGING': None    │
│ }                                     │
╰───────────────────────────────────────╯
platform="Darwin"
rich==13.7.1

nineteendo avatar May 03 '24 18:05 nineteendo

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

github-actions[bot] avatar May 03 '24 18:05 github-actions[bot]

For reference, here's how I handled this in my own library: https://github.com/nineteendo/ansio/blob/125577e28362ed5395c3dafed38df763fa88b8c2/ansio/init.py

nineteendo avatar May 03 '24 18:05 nineteendo

is there any updates to this?

MrDaGree avatar Jun 04 '24 18:06 MrDaGree

Nope, this is one of the reasons why I don't use rich.

nineteendo avatar Jun 04 '24 18:06 nineteendo

The cursor state is something managed by the terminal. It can be modified outside of a Console object, which makes knowing how to "reset" it next to impossible. Additionally, to detect the cursor state, you would need to capture stdin, which Rich doesn't do as it would break the many project that do capture stdin.

If you want control over the cursor state, put it in a try / finally, or a context manager.

willmcgugan avatar Jun 04 '24 19:06 willmcgugan

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

github-actions[bot] avatar Jun 04 '24 19:06 github-actions[bot]

to detect the cursor state, you would need to capture stdin

Can I detect that without using ansi escape codes? My library will always show the cursor on exit if you chose to hide it.

nineteendo avatar Jun 04 '24 19:06 nineteendo

It can be modified outside of a Console object, which makes knowing how to "reset" it next to impossible.

In my project I'm not modifying the cursor at all. I've only used status, Live tables, and progress bars. I'd expect for using things the correct way it surely can manage its cursor itself.

MrDaGree avatar Jun 05 '24 12:06 MrDaGree

Don't call input when any of these objects are running. Input will block and write to the terminal at the same time as Rich, and there is no way for Rich to detect this.

If you want a UI where you can get user input and update the screen, look in to a TUI framework like Textual.

willmcgugan avatar Jun 05 '24 12:06 willmcgugan

When status, Live tables, or progress bars are shown there is no input being captured. In the case of status and the progress bars its being used for long waiting tasks to show that something is happening than just a blank terminal

MrDaGree avatar Jun 05 '24 12:06 MrDaGree