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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
patrick-kidger opened this issue Sep 28, 2024 · 3 comments
Closed
2 tasks done

Comments

@patrick-kidger
Copy link

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"
Copy link

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
Copy link
Collaborator

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.

Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants