Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make win32 console recognize ANSI/VT100 escape sequences?

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?

like image 938
Witiko Avatar asked May 26 '13 00:05

Witiko


People also ask

Does Windows terminal support ANSI escape sequences?

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.

Does PowerShell support ANSI?

PowerShell has many features that support the use of ANSI escape sequences to control the rendering of output in the terminal application that is hosting PowerShell. PowerShell 7.2 added a new automatic variable, $PSStyle , and changes to the PowerShell engine to support the output of ANSI-decorated text.

What is ANSI console?

An early character-based display terminal that executed standard ANSI commands to control the cursor, clear the screen and set colors. The commands were preceded with an escape character (ANSI escape codes), and although widely used in the 1980s, ANSI commands still exist in various communications programs.


1 Answers

[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:

  • 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
like image 131
Franco Rondini Avatar answered Sep 18 '22 14:09

Franco Rondini