如何使Win32控制台识别ANSI/VT100转义序列? [英] How to make win32 console recognize ANSI/VT100 escape sequences?

查看:155
本文介绍了如何使Win32控制台识别ANSI/VT100转义序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建ncurses库的轻量级版本.到目前为止,它在与VT100兼容的终端上都可以很好地工作,但是win32控制台无法将\033代码识别为转义序列的开始:

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:

# include <stdio.h>
# include "term.h"

int main(void) {
  puts(BOLD COLOR(FG, RED) "Bold text" NOT_BOLD " is cool!" CLEAR);
  return 0;
}

要加载ANSI.SYS驱动程序并识别ANSI/VT100转义序列,需要在C代码级别上做什么?

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?

推荐答案

[更新]对于最新的Windows 10,请阅读@ brainslugs83的有用内容,该注释正好在以下答案中.

[UPDATE] For latest Windows 10 please read useful contribution by @brainslugs83, just below in the comments to this answer.

Windows 10 Anniversary Update之前的版本:

ANSI.SYS有一个限制,它只能在Windows 95-Vista下的MS-DOS子系统的上下文中运行.

ANSI.SYS has a restriction that it can run only in the context of the MS-DOS sub-system under Windows 95-Vista.

Microsoft KB101875说明了如何在命令窗口中启用ANSI.SYS,但不适用于Windows NT .根据文章:我们都喜欢颜色, Windows的现代版本没有很好的ANSI支持.

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创建了许多功能,但这远非您操作ANSI/VT100转义序列的需要.

Instead, Microsoft created a lot of functions, but this is far from your need to operate ANSI/VT100 escape sequence.

有关详细说明,请参见维基百科文章:

For a more detailed explanation, see the Wikipedia article:

ANSI.SYS还可以在NT派生系统中运行在NTVDM下执行的16位旧程序.

ANSI.SYS also works in NT-derived systems for 16-bit legacy programs executing under the NTVDM.

Win32控制台本身根本不支持ANSI转义序列.但是,诸如 Ansicon 之类的软件可以充当标准Win32控制台的包装,并增加对ANSI转义序列的支持.

The Win32 console does not natively support ANSI escape sequences at all. Software such as Ansicon can however act as a wrapper around the standard Win32 console and add support for ANSI escape sequences.

因此,我认为Jason Hood的 ANSICON 是您的解决方案.它是用 C 编写的,支持32位和64位版本的Windows,以及源可用.

So I think ANSICON by Jason Hood is your solution. It is written in C, supports 32-bit and 64-bit versions of Windows, and the source is available.

我还发现了其他一些类似的问题或帖子,这些问题或帖子最终被回答为使用ANSICON:

Also I found some other similar question or post which ultimately have been answered to use ANSICON:

  • How to load ANSI escape codes or get coloured file listing in WinXP cmd shell?
  • how to use ansi.sys in windows 7
  • How can I get cmd.exe to display ANSI color escape sequences?
  • ansi color in windows shells
  • enable ansi colors in windows command prompt

这篇关于如何使Win32控制台识别ANSI/VT100转义序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆