Windows – How to make win32 console recognize ANSI/VT100 escape sequences

ansi-escapeconsole-applicationncursesterminalwindows

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;
}

Screenshot

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?

Best Answer

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

While for versions before 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.

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.

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 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 Ansicon can however act as a wrapper around the standard Win32 console and add support for ANSI escape sequences.

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.

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