You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Colors are not working in default loguru configuration for mintty terminal (windows)
loguru starts with a default sink sys.stderr
loguru finds that sys.stderr has a callable write attribute and calls AnsiToWin32(...)
It's windows. AnsiToWin32 works without errors.
As result ANSI color codes are replaced with WinAPI calls and mintty is not able to handle it.
I was able to workaround this behavior by the code snippet below. Think it might be useful for somebody else. The trick is to configure loguru to use 'callable' sink instead of stream-like object.
importsysimportcoloramafromloguruimportloggerdefsetup_ansi_colors(suppress_colors):
convert_ansi_codes_to_win32_calls=Falseifos.name=='nt':
# Only need to init colorama with 'convert=True' when app is called# from 'cmd.exe', 'powershell' or 'git-bash via VS Code'convert_ansi_codes_to_win32_calls='TERM'notinos.environor \
os.environ.get('TERM_PROGRAM', None) =='vscode'if'CONVERT_ANSI_CODES_TO_WIN32_CALLS'inos.environ:
# explicit option is useful for cases when automatic guess fails (e.g. for Eclipse IDE)convert_ansi_codes_to_win32_calls=os.environ.get('CONVERT_ANSI_CODES_TO_WIN32_CALLS').lower() in ('true', '1')
colorama.init(strip=suppress_colors, convert=convert_ansi_codes_to_win32_calls)
setup_ansi_colors(suppress_colors=False)
logger.remove()
logger.add(sink=sys.stdout.write, colorize=True)
The text was updated successfully, but these errors were encountered:
I guess this is a common issue with apps using colorama. I need to see how this is handled by others developers, and see how this is usually fixed. Otherwise, I could probably integrate your terminal detection directly into loguru.
By refactoring the colorization process, there is now a better distinction between "sink supporting colors" and "terminal needing WinAPI calls".
Basically, this means that issues looking like yours can be solved simply with .add(..., colorize=True). This will output ansi colors codes without converting them using colorama, so they can be handled solely by the mintty terminal.
However, this is not needed here. I also added an automatic detection of mintty terminal, so if colorize=None (the default for auto detection), this should correctly output ansi code (if sink is stderr / stdout) without colorama.
If you encounter others problems like this one, please let me know!
Colors are not working in default
loguru
configuration formintty
terminal (windows)sys.stderr
sys.stderr
has a callablewrite
attribute and callsAnsiToWin32(...)
AnsiToWin32
works without errors.As result ANSI color codes are replaced with WinAPI calls and
mintty
is not able to handle it.I was able to workaround this behavior by the code snippet below. Think it might be useful for somebody else. The trick is to configure
loguru
to use 'callable' sink instead of stream-like object.The text was updated successfully, but these errors were encountered: