[BUG] ANSI sequences parsed incorrectly: \x1b(B\x1b[m
See original GitHub issue- Iโve checked docs and closed issues for possible solutions.
- I canโt find my issue in the FAQ.
Describe the bug
Some ANSI sequences (which I didnโt know about) are not correctly parsed. Here is a text example with such ANSI sequences:
src/failprint/capture.py:57: \x1b[1m\x1b[31merror:\x1b(B\x1b[m Property \x1b(B\x1b[m\x1b[1m"buffer"\x1b(B\x1b[m defined in \x1b(B\x1b[m\x1b[1m"TextIOWrapper"\x1b(B\x1b[m is read-only \x1b(B\x1b[m\x1b[33m[misc]\x1b(B\x1b[m
src/failprint/capture.py:57: \x1b[1m\x1b[31merror:\x1b(B\x1b[m Incompatible types in assignment (expression has type \x1b(B\x1b[m\x1b[1m"_BytesBuffer"\x1b(B\x1b[m, variable has type \x1b(B\x1b[m\x1b[1m"BinaryIO"\x1b(B\x1b[m) \x1b(B\x1b[m\x1b[33m[assignment]\x1b(B\x1b[m
src/failprint/capture.py:77: \x1b[1m\x1b[31merror:\x1b(B\x1b[m Argument 1 to \x1b(B\x1b[m\x1b[1m"StringIO"\x1b(B\x1b[m has incompatible type \x1b(B\x1b[m\x1b[1m"StringIO"\x1b(B\x1b[m; expected \x1b(B\x1b[m\x1b[1m"Optional[str]"\x1b(B\x1b[m \x1b(B\x1b[m\x1b[33m[arg-type]\x1b(B\x1b[m
src/failprint/runners.py:221: \x1b[1m\x1b[31merror:\x1b(B\x1b[m Incompatible return value type (got \x1b(B\x1b[m\x1b[1m"Union[str, int, None]"\x1b(B\x1b[m, expected \x1b(B\x1b[m\x1b[1m"int"\x1b(B\x1b[m) \x1b(B\x1b[m\x1b[33m[return-value]\x1b(B\x1b[m
\x1b[1m\x1b[31mFound 4 errors in 2 files (checked 18 source files)\x1b(B\x1b[m
This is the (ANSI colored) output of mypy.
Other libraries like ansimarkup
are able to print it.
from ansimarkup import ansiprint
ansiprint('\x1b[31mFound 4 errors in 2 files (checked 18 source files)\x1b(B\x1b[m\n')
from rich.text import Text
from rich import print as richprint
richprint(Text.from_ansi('\x1b[31mFound 4 errors in 2 files (checked 18 source files)\x1b(B\x1b[m\n'))
Note the trailing B
in the above screenshot.
Platform
Click to expand
What platform (Win/Linux/Mac) are you running on? What terminal software are you using?
I may ask you to copy and paste the output of the following commands. It may save some time if you do it now.
If youโre using Rich in a terminal:
python -m rich.diagnose
pip freeze | grep rich
โญโโโโโโโโโโโโโโโโโโโโโโโโโ <class 'rich.console.Console'> โโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ A high level console interface. โ
โ โ
โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ <console width=191 ColorSystem.TRUECOLOR> โ โ
โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ
โ โ
โ color_system = 'truecolor' โ
โ encoding = 'utf-8' โ
โ file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> โ
โ height = 19 โ
โ 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=191, height=19), โ
โ legacy_windows=False, โ
โ min_width=1, โ
โ max_width=191, โ
โ is_terminal=True, โ
โ encoding='utf-8', โ
โ max_height=19, โ
โ justify=None, โ
โ overflow=None, โ
โ no_wrap=False, โ
โ highlight=None, โ
โ markup=None, โ
โ height=None โ
โ ) โ
โ quiet = False โ
โ record = False โ
โ safe_box = True โ
โ size = ConsoleDimensions(width=191, height=19) โ
โ soft_wrap = False โ
โ stderr = False โ
โ style = None โ
โ tab_size = 8 โ
โ width = 191 โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โญโโโ <class 'rich._windows.WindowsConsoleFeatures'> โโโโโฎ
โ Windows features available. โ
โ โ
โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ WindowsConsoleFeatures(vt=False, truecolor=False) โ โ
โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ
โ โ
โ truecolor = False โ
โ vt = False โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โญโโโโโโ Environment Variables โโโโโโโโฎ
โ { โ
โ 'TERM': 'xterm-256color', โ
โ 'COLORTERM': 'truecolor', โ
โ 'CLICOLOR': None, โ
โ 'NO_COLOR': None, โ
โ 'TERM_PROGRAM': 'vscode', โ
โ 'COLUMNS': None, โ
โ 'LINES': None, โ
โ 'JUPYTER_COLUMNS': None, โ
โ 'JUPYTER_LINES': None, โ
โ 'JPY_PARENT_PID': None, โ
โ 'VSCODE_VERBOSE_LOGGING': None โ
โ } โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
platform="Linux"
rich==12.6.0
Issue Analytics
- State:
- Created 9 months ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Best Ansi Escape beginning - Stack Overflow
For parsers that support hexadecimal escapes ( \x ), then \x1b (character 0x1B) should work. ... stdin:1: invalid escape sequence near '\e'.
Read more >ANSI/VT \e[7m and \e[27m (reverse/negative) do not work ...
VT escape control sequences "\x1B[7m" and "\x1B[27m" are used to turn reverse video ("negative", but not technically negative) on and off.
Read more >Don't Trust This Title: Abusing Terminal Emulators with ANSI ...
In my case, I wanted to find a problem in the way the terminal parses text, which led me to learn about ANSI...
Read more >ANSI escape code - Wikipedia
ANSI escape sequences are a standard for in-band signaling to control cursor location, color, font styling, and other options on video text terminals...
Read more >Everything you never wanted to know about ANSI escape codes
ANSI escapes always start with \x1b , or \e , or \033 . ... So with that mental modelโreading escape sequences as functionย ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@jayaddison thanks for the investigation! On my end I verified that these codes really come from mypy, and they do; looking at their FancyFormatter, I ran this in a Python REPL:
Since Rich seems to render the right colors/style, it seems fine to me if it also strips these unfamiliar codes.
Here is the code in question. It is somewhat limited in the ansi codes it processes. It would be nice to handle what makes sense, but it should also strip out unfamiliar codes.