windows 如何让 win32 控制台识别 ANSI/VT100 转义序列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16755142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make win32 console recognize ANSI/VT100 escape sequences?
提问by Witiko
I'm building a lightweight version of the ncurses library. So far, it works pretty well with VT100-compatible terminals, but win32 console fails to recognise the \033
code as the beginning of an escape sequence:
我正在构建 ncurses 库的轻量级版本。到目前为止,它在兼容 VT100 的终端上运行良好,但 win32 控制台无法将\033
代码识别为转义序列的开头:
# include <stdio.h>
# include "term.h"
int main(void) {
puts(BOLD COLOR(FG, RED) "Bold text" NOT_BOLD " is cool!" CLEAR);
return 0;
}
What needs to be done on the C code level, in order that the ANSI.SYS driver is loaded and the ANSI/VT100 escape sequences recognized?
为了加载 ANSI.SYS 驱动程序并识别 ANSI/VT100 转义序列,需要在 C 代码级别做什么?
回答by Franco Rondini
[UPDATE] For latest Windows 10 please read useful contribution by @brainslugs83, just below in the comments to this answer.
[更新] 对于最新的 Windows 10,请阅读@brainslugs83 的有用贡献,就在此答案的评论下方。
While for versions before Windows 10 Anniversary Update
:
而对于之前的版本Windows 10 Anniversary Update
:
ANSI.SYS has a restriction that it can run only in the context of the MS-DOS sub-system under Windows 95-Vista.
ANSI.SYS 有一个限制,它只能在 Windows 95-Vista 下的 MS-DOS 子系统的上下文中运行。
Microsoft KB101875 explains how to enable ANSI.SYS in a command window, but it does not apply to Windows NT. According to the article: we all love colors, modern versions of Windows do not have this nice ANSI support.
Microsoft KB101875 解释了如何在命令窗口中启用 ANSI.SYS,但它不适用于 Windows NT。根据文章:我们都喜欢颜色,现代版本的 Windows 没有这么好的 ANSI 支持。
Instead, Microsoft created a lot of functions, but this is far from your need to operate ANSI/VT100 escape sequence.
取而代之的是,微软创造了很多函数,但这远非你操作ANSI/VT100转义序列的需要。
For a more detailed explanation, see the Wikipedia article:
有关更详细的解释,请参阅维基百科文章:
ANSI.SYS also works in NT-derived systems for 16-bit legacy programs executing under the NTVDM.
The Win32 console does not natively support ANSI escape sequences at all. Software such as Ansiconcan however act as a wrapper around the standard Win32 console and add support for ANSI escape sequences.
ANSI.SYS 也适用于 NT 派生系统,用于在 NTVDM 下执行的 16 位遗留程序。
Win32 控制台本身根本不支持 ANSI 转义序列。然而,诸如Ansicon 之类的软件可以充当标准 Win32 控制台的包装器,并添加对 ANSI 转义序列的支持。
So I think ANSICONby Jason Hood is your solution. It is written in C, supports 32-bit and 64-bit versions of Windows, and the source is available.
所以我认为 Jason Hood 的ANSICON是您的解决方案。它是用C编写的,支持 32 位和 64 位版本的 Windows,源代码可用。
Also I found some other similar question or post which ultimately have been answered to use ANSICON:
我还发现了一些其他类似的问题或帖子,这些问题或帖子最终已被回答使用 ANSICON:
回答by rolve
Starting from Windows 10 TH2 (v1511), conhost.exe
and cmd.exe
support ANSI and VT100 Escape Sequences out of the box (although they have to be enabled).
从 Windows 10 TH2 (v1511) 开始,conhost.exe
并cmd.exe
支持开箱即用的 ANSI 和 VT100 转义序列(尽管必须启用它们)。
See my answer over at superuserfor more details.
有关更多详细信息,请参阅我在超级用户处的回答。
回答by Star Brilliant
Starting from Windows 10, you can use ENABLE_VIRTUAL_TERMINAL_PROCESSING
to enable ANSI escape sequences:
从 Windows 10 开始,您可以使用ENABLE_VIRTUAL_TERMINAL_PROCESSING
来启用 ANSI 转义序列:
https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx
回答by Guestreader
For Python 2.7 the following script works for me fine with Windows 10 (v1607)
对于 Python 2.7,以下脚本适用于 Windows 10 (v1607)
import os
print '3[35m'+'color-test'+'3[39m'+" test end"
os.system('') #enable VT100 Escape Sequence for WINDOWS 10 Ver. 1607
print '3[35m'+'color-test'+'3[39m'+" test end"
Result should be:
结果应该是:
[35mcolor-test[39m test end
color-test test end
回答by Joakim
If ANSICON is not acceptable since it requires you to install something on the system, a more lightweight solution that parses and translates the ANSI codes into the relevant Win32 API console functionssuch as SetConsoleTextAttribute.
如果 ANSICON 是不可接受的,因为它要求您在系统上安装某些东西,那么更轻量级的解决方案可以解析 ANSI 代码并将其转换为相关的Win32 API 控制台函数,例如SetConsoleTextAttribute。
回答by Daniel De León
Base on @BrainSlugs83 you can activate on the current Windows 10 version via register, with this command line:
基于@BrainSlugs83,您可以使用以下命令行通过注册在当前的 Windows 10 版本上激活:
REG ADD HKCU\CONSOLE /f /v VirtualTerminalLevel /t REG_DWORD /d 1
回答by rho
For coloring the cmd you need Windows.h
and use SetConsoleTextAttribute()
more details can be found in http://msdn.microsoft.com/en-us/library/windows/desktop/ms686047%28v=vs.85%29.aspx
要为您需要的 cmd 着色Windows.h
并使用SetConsoleTextAttribute()
更多详细信息,请访问http://msdn.microsoft.com/en-us/library/windows/desktop/ms686047%28v=vs.85%29.aspx
回答by bitlixi
In lastest win10, it can be done by SetConsoleMode(originMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
. See https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example
在最新的win10中,可以通过SetConsoleMode(originMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
. 请参阅https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example