Windows 10上的Ansi颜色,有点不起作用 [英] Ansi colours on Windows 10, sort of not working
问题描述
带有颜色的控制台对于Windows来说是相当新颖和令人兴奋的:-)
Console with colours is fairly new and exciting for Windows :-)
我编写了一些使用ansi彩色转义打印的程序,一切都很好
...
...然后突然停止工作了.或者至少在我的笔记本电脑Windows 10.0.14393上停止了运行
... then suddenly it stopped working. Or at least stopped on my laptop, Windows 10.0.14393
在我的计算机上失败的应用程序是在其中nodejs应用程序已显示颜色的控制台中执行此操作的,同一二进制文件在另一台笔记本电脑上以正确的颜色运行.
The app that fails on my machine does so in a console that a nodejs app has displayed colour in, the same binary runs with the correct colour on another laptop.
golangfmt.Println("\ 033 [31mSome Text \ 033 [0m")
左上角,控制台节点输出,左下角,我的应用失败,右vscode
httpDump.exe是此演示的名称(以防混淆)
它确实在vscode集成终端窗口中正确显示.
It does display correctly in the vscode integrated terminal window.
我尝试删除 HKLM \ Console
注册表项并重新启动(几次).我删除了其中可能包含设置的快捷方式.我尝试在此计算机上以其他用户身份运行(不起作用)
I've tried deleting the HKLM\Console
registry entry and restarting (several times). I deleted the short-cut that may have settings in it. I've tried running as another user on this machine (does not work)
我欢迎任何提示.
推荐答案
自该更新以来,您必须在Windows上启用虚拟终端处理.
You have to enable virtual terminal processing on Windows since that update.
我通常添加一个文件 init_windows.go
,该文件在Windows中进行设置,但也与其他操作系统兼容:
I usually add a file init_windows.go
which sets this in windows but is also compatible with other OS:
// +build windows
package main
import (
"os"
"golang.org/x/sys/windows"
)
func init() {
stdout := windows.Handle(os.Stdout.Fd())
var originalMode uint32
windows.GetConsoleMode(stdout, &originalMode)
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
}
摘自: https://github.com/sirupsen/logrus/issues/172#issuecomment-353724264
这篇关于Windows 10上的Ansi颜色,有点不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!