Can't show ansi color on windows if use git bash
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 45/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- bash, python
- 领域
- cli, operating-systems
调研方向
从 formatting_windows.py 中的 initialize_or_disable 开始,检查 Windows 环境目前如何决定是否初始化 colorama。在 Git Bash 中复现该行为,然后验证 Git Bash 会避免不必要的包装,而其他 Windows shell 保留现有的处理方式。还需要确定所请求配置选项的作用范围和行为。
由索引模型根据 Issue 内容生成。
描述
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
-
Use Git Bash on a Windows system.
-
Run a script that uses
firewith formatting enabled. -
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
- 主要语言
- Python
- 星标
- 28.2k
- 派生
- 1.5k
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
google/python-fire 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 85/100
google/python-fire#693 ·
-
Release 0.7.2? 未关闭
难度 3/5 1-2 天 新手友好度 38/100
google/python-fire#698 ·
-
难度 3/5 1-2 天 新手友好度 58/100
google/python-fire#672 · 5 条评论 ·
-
难度 4/5 3-5 天 新手友好度 45/100
google/python-fire#665 · 2 条评论 ·
-
难度 4/5 3-5 天 新手友好度 55/100
google/python-fire#659 · 1 条评论 ·
查看 google/python-fire 的全部 Issue
相似的 Issue
-
enhancement
难度 2/5 1-3 小时 新手友好度 70/100
canonical/paas-charm#368 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
tech debt
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 1/5 1 小时以内 新手友好度 90/100
StevenBlack/hosts#3256 ·
-
难度 1/5 1 小时以内 新手友好度 90/100
qualcomm/qai-appbuilder#275 ·