-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
Describe the bug
When running a Python script using Rich with Git Bash on Windows, color is not outputted.
To Reproduce
Simply run the following code on a Git Bash terminal:
import rich
rich.print('[red]This text should be red[/red]')Forcing terminal and/or disabling legacy Windows mode do not work either:
import rich.console
console = rich.console.Console(force_terminal=True, legacy_windows=False)
console.print('[red]This text should be red[/red]')Platform
Windows 10, Git Bash (MINGW64)
Diagnose
$ python -m rich.diagnose
+---------------------- <class 'rich.console.Console'> -----------------------+
| A high level console interface. |
| |
| +-------------------------------------------------------------------------+ |
| | <console width=79 None> | |
| +-------------------------------------------------------------------------+ |
| |
| color_system = None |
| encoding = 'cp1252' |
| file = <colorama.ansitowin32.StreamWrapper object at |
| 0x0203EC88> |
| is_dumb_terminal = False |
| is_jupyter = False |
| is_terminal = False |
| legacy_windows = True |
| no_color = False |
| options = ConsoleOptions(legacy_windows=True, min_width=1, |
| max_width=79, is_terminal=False, encoding='cp1252', |
| justify=None, overflow=None, no_wrap=False, |
| highlight=None) |
| record = False |
| safe_box = True |
| size = ConsoleDimensions(width=79, height=24) |
| soft_wrap = False |
| stderr = False |
| style = None |
| tab_size = 8 |
| width = 79 |
+-----------------------------------------------------------------------------+
$ python -m rich._windows
platform="Windows"
WindowsConsoleFeatures(vt=False, truecolor=False)
$ pip freeze | grep rich
rich==9.10.0
Additional info
I've investigated the Rich coding and the problematic code seem to be related to the colorama.init() call.
If I comment out the call to colorama.init() and set force_terminal to True, then I get color on the output. Git Bash emulates BASH on Windows, so I assume it is expecting ANSI codes. However, detect_legacy_windows() is incorrectly returning True; therefore, colorama.init() is called and ANSI codes are not outputted.
I think the fix would be to make detect_legacy_windows() return False when running on Git Bash/MINGW, but I'm not sure how to do it.