繁体   English   中英

ANSI 颜色不适用于使用 Windows 终端的 Windows 10 上的 Git Bash

[英]ANSI color not working for Git Bash on Windows 10 using Windows Terminal

我在带有 Windows 终端的 Windows 10 上使用 Git Bash,对于这个 Python 项目,ANSI 转义序列不起作用。

from colorama import init, Fore
from sys import stdout

init(convert=True)

# code ...

我试图打印一个测试文本

# The code above
stdout.write('{GREEN}Test{RESET}'.format(GREEN=Fore.GREEN, RESET=Fore.RESET)

这是输出:

←[32mTest←[0m

我确信我的终端支持 ANSI 序列,因为我已经用 Bash、TS (Deno) 和 JS (NodeJS) 测试过它。 他们都工作了。 我还在命令提示符上进行了测试,它适用于 Python。 也许这是 Git Bash 执行 Python 本身的问题?

我也试过直接编写十六进制代码,但仍然没有运气。 检查下面的代码
write.py
示例图像

浪费了一些时间测试后,显然只使用print作品

#!/usr/bin/env python3
from colorama import init, Fore
from sys import stdout

# Initialize
init()

# Using `Fore`
print(f'{Fore.GREEN}Test{Fore.RESET}')

# Using actuall hex values
print('\x1B[31mTest\x1B[0m')

# Using stdout.write
stdout.write(f'{Fore.GREEN}Test{Fore.RESET}\n')
stdout.write('\x1B[31mTest\x1B[0m\n')
Test
Test
←[32mTest←[39m
←[31mTest←[0m

结果

编辑:

使用input也会失败

# This will fail
xyz = input(f'{Fore.RED}Red text{Fore.RESET}')

# Consider using this as an alternative
print(f'{Fore.RED}Red text{Fore.RESET}', end='')
xyz = input()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM