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

colors don't work out of the box for git-bash (mintty, windows) #104

Closed
aleksey-sergey opened this issue Jun 4, 2019 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@aleksey-sergey
Copy link

aleksey-sergey commented Jun 4, 2019

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.

import sys
import colorama
from loguru import logger


def setup_ansi_colors(suppress_colors):
    convert_ansi_codes_to_win32_calls = False

    if os.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' not in os.environ or \
                                            os.environ.get('TERM_PROGRAM', None) == 'vscode'

    if 'CONVERT_ANSI_CODES_TO_WIN32_CALLS' in os.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)
@Delgan
Copy link
Owner

Delgan commented Jun 4, 2019

Hey @aleksey-sergey, thanks for the detailed explanation!

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.

@Delgan Delgan added the bug Something isn't working label Jun 4, 2019
@Delgan
Copy link
Owner

Delgan commented Jun 29, 2019

This should be fixed in the 0.3.0 just released.

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!

@Delgan Delgan closed this as completed Jun 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants