Textualize / rich

Rich is a Python library for rich text and beautiful formatting in the terminal.
https://rich.readthedocs.io/en/latest/
MIT License
49.69k stars 1.73k forks source link

[BUG] Errors with ANSI escape codes have the code printed without escapes. #3505

Closed patrick-kidger closed 1 month ago

patrick-kidger commented 1 month ago

Describe the bug

from rich.traceback import install
install()

raise RuntimeError("\x1b[31mFoo")

produces:

❯ py tmp6.py            
╭───────────────────── Traceback (most recent call last) ─────────────────────╮
│ file.py:4 in <module>                                                       │
│                                                                             │
│   1 from rich.traceback import install                                      │
│   2 install()                                                               │
│   3                                                                         │
│ ❱ 4 raise RuntimeError("\x1b[31mFoo")                                       │
│   5                                                                         │
│   6                                                                         │
╰─────────────────────────────────────────────────────────────────────────────╯
RuntimeError: [31mFoo

Expected behaviour is for for either (a) the \x1b[31m escape code to be respected, or (b) at minimum for the escape codes to be removed and ignored.


Rich version 13.8.1 on Linux.

❯ python -m rich.diagnose 
╭────────────────────── <class 'rich.console.Console'> ───────────────────────╮
│ A high level console interface.                                             │
│                                                                             │
│ ╭─────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=79 ColorSystem.TRUECOLOR>                                │ │
│ ╰─────────────────────────────────────────────────────────────────────────╯ │
│                                                                             │
│     color_system = 'truecolor'                                              │
│         encoding = 'utf-8'                                                  │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w'              │
│                    encoding='utf-8'>                                        │
│           height = 45                                                       │
│    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=79, height=45),         │
│                        legacy_windows=False,                                │
│                        min_width=1,                                         │
│                        max_width=79,                                        │
│                        is_terminal=True,                                    │
│                        encoding='utf-8',                                    │
│                        max_height=45,                                       │
│                        justify=None,                                        │
│                        overflow=None,                                       │
│                        no_wrap=False,                                       │
│                        highlight=None,                                      │
│                        markup=None,                                         │
│                        height=None                                          │
│                    )                                                        │
│            quiet = False                                                    │
│           record = False                                                    │
│         safe_box = True                                                     │
│             size = ConsoleDimensions(width=79, height=45)                   │
│        soft_wrap = False                                                    │
│           stderr = False                                                    │
│            style = None                                                     │
│         tab_size = 8                                                        │
│            width = 79                                                       │
╰─────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ {                                  │
│     'TERM': 'tmux-256color',       │
│     'COLORTERM': 'truecolor',      │
│     'CLICOLOR': None,              │
│     'NO_COLOR': None,              │
│     'TERM_PROGRAM': 'tmux',        │
│     'COLUMNS': None,               │
│     'LINES': None,                 │
│     'JUPYTER_COLUMNS': None,       │
│     'JUPYTER_LINES': None,         │
│     'JPY_PARENT_PID': None,        │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
╰────────────────────────────────────╯
platform="Darwin"
github-actions[bot] commented 1 month ago

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

willmcgugan commented 1 month ago

Rich will generate escape sequences, but not parse them by default.

ANSI escape sequences in exceptions are a bad idea, because you never know what is going to consume them.

github-actions[bot] commented 1 month ago

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual