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

Can't show ansi color on windows if use git bash #575

Open
zhangxiaojun92 opened this issue Dec 25, 2024 · 0 comments
Open

Can't show ansi color on windows if use git bash #575

zhangxiaojun92 opened this issue Dec 25, 2024 · 0 comments

Comments

@zhangxiaojun92
Copy link

Description

When using fire in Git Bash on Windows, the initialize_or_disable function in the formatting_windows.py unnecessarily invokes colorama.init(wrap=True) even though Git Bash already supports native ANSI escape sequences. This behavior causes issues, such as double-wrapped output or incorrect handling of ANSI sequences, which can lead to degraded user experience or unexpected output.

Steps to Reproduce

  1. Use Git Bash on a Windows system.

  2. Run a script that uses fire with formatting enabled.

  3. Observe that the output is handled incorrectly due to the invocation of colorama.init(wrap=True).

Root Cause Analysis

The initialize_or_disable function currently does not differentiate between Git Bash and other Windows environments like cmd.exe or powershell.exe. Specifically:

  • Git Bash natively supports ANSI escape sequences.

  • The function does not explicitly check for Git Bash and thus assumes it must handle formatting via colorama.

  • This leads to colorama.init(wrap=True) being invoked unnecessarily, causing redundant processing and potential output issues.

Proposed Solution

Add an explicit check for Git Bash environments in the initialize_or_disable function. This can be achieved by inspecting environment variables such as MSYSTEM or TERM, which are typically set in Git Bash.

Additionally, provide a configuration option to control whether the formatting_windows functionality is enabled or disabled. This will allow users to explicitly manage how formatting is handled.

Suggested Changes

Modify initialize_or_disable to detect Git Bash:

def initialize_or_disable():
    is_git_bash = os.environ.get('MSYSTEM') or os.environ.get('TERM') in ['xterm', 'xterm-256color']
    if is_git_bash:
        print("Detected Git Bash, skipping colorama.init")
        return  # Skip colorama initialization for Git Bash
    
    # Existing logic for colorama and ANSI initialization

Add a configuration flag to enable or disable the formatting_windows logic explicitly. For example:

ENABLE_FORMATTING_WINDOWS = os.environ.get('ENABLE_FORMATTING_WINDOWS', '1') == '1'

if ENABLE_FORMATTING_WINDOWS:
    initialize_or_disable()
else:
    print("Formatting for Windows is disabled")

Please consider implementing the above solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant