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

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

我在 Windows 10 上使用 Git Bash 和 Windows 终端,对于这个 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
Example image

在浪费了一些时间测试之后,显然只使用 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

Result

编辑:

使用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()