-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[BUG] [Windows] ANSI SGR sequences printed to streams which are not attached to TTYs. #3693
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
Comments
I think your issue is not related to #3647. In your code, Try to open a random file, and it does not print ANSI color code any more: from locale import getpreferredencoding
from os import devnull
from rich.console import Console
from rich.style import Style
# Change filename to 'foo'
blackhole = open( 'foo', 'w', encoding = getpreferredencoding( ) )
console = Console( file = blackhole )
with console.capture( ) as capture:
console.print( 'foo', style = Style( color = 'red' ) )
# Does not print ANSI color code any more
print( capture.get( ) ) |
The null device is not a TTY on POSIX systems: >>> sys.platform
'linux'
>>> f = open( os.devnull, 'w' )
>>> f.isatty( )
False However, you appear to be correct about the behavior on Windows: >>> sys.platform
'win32'
>>> f = open( os.devnull, 'w' )
>>> f.isatty( )
True That is very unintuitive. Will withdraw this issue. Thanks for the followup. |
I hope we solved your problem. If you like using Rich, you might also enjoy Textual |
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
On Windows, ANSI SGR sequences are emitted regardless of whether a stream is attached to a TTY or not.
The following simple reproducer will correctly print an uncolored "foo" on non-Windows platforms but will print a red "foo" on Windows:
The null device is definitely not a TTY, but
capture
on a Console print will show that ANSI C1 sequences are being emitted.Expected behavior is that a plain "foo" would be printed on all platforms.
Closely related to #3647. (And maybe fixed by #3648, though it is not clear that the terminal on the Github Actions Windows runners is a legacy terminal. But, it seems like the code path for legacy terminal detection is not different than that for non-terminal detection on Windows, so the fix may be valid. Would definitely like to see the fix or a maintainer-approved variant thereof merged.)
Possibly has some bearing on #2622 and #3082, even though they are the "opposite" problem.
Platforms
Platform 1: (exhibits bug)
Windows 10, CMD.EXE
╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── <class 'rich.console.Console'> ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ A high level console interface. │
│ │
│ ╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ │ │
│ ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│ │
│ color_system = 'truecolor' │
│ encoding = 'utf-8' │
│ file = <_io.TextIOWrapper name='' mode='w' encoding='utf-8'> │
│ height = 76 │
│ 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=270, height=76), legacy_windows=False, min_width=1, max_width=270, is_terminal=True, encoding='utf-8', max_height=76, justify=None, overflow=None, no_wrap=False, highlight=None, markup=None, height=None) │
│ quiet = False │
│ record = False │
│ safe_box = True │
│ size = ConsoleDimensions(width=270, height=76) │
│ soft_wrap = False │
│ stderr = False │
│ style = None │
│ tab_size = 8 │
│ width = 270 │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭── <class 'rich._windows.WindowsConsoleFeatures'> ───╮
│ Windows features available. │
│ │
│ ╭─────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=True, truecolor=True) │ │
│ ╰─────────────────────────────────────────────────╯ │
│ │
│ truecolor = True │
│ vt = True │
╰─────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Environment Variables ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ {'CLICOLOR': None, 'COLORTERM': None, 'COLUMNS': None, 'JPY_PARENT_PID': None, 'JUPYTER_COLUMNS': None, 'JUPYTER_LINES': None, 'LINES': None, 'NO_COLOR': None, 'TERM_PROGRAM': None, 'TERM': None, 'TTY_COMPATIBLE': None, 'VSCODE_VERBOSE_LOGGING': None} │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
platform="Windows"
rich==14.0.0
Platform 2: (exhibits bug)
Github Actions Windows runner (Windows 11?), Git Bash (mintty)?
Platform 3: (does not exhibit bug)
Ubuntu 22.04 (GNU/Linux)
The text was updated successfully, but these errors were encountered: