ANSI Terminal Echo

ANSI Terminal Echo

In Windows, the console API is supposed to be used to control the screen from a console application, but in UNIX/Linux, escape sequences (also called escape codes, control sequences, etc.) It is normal to control with (Photo 01). Escape sequences are originally control codes for terminal equipment, and are called ANSI escape sequences after the American National Standards Institute (ANSI) standardized them as ANSI X3.64 (1979). There is It is also called the VT escape sequence because the terminal equipment VT series of the former DEC, which complies with this standard, was widely used. ANSI X3.64 and ECMA-48 (1976 European standard) are almost the same. These standards subsequently became an international standard as ISO/IEC 6429.

The escape sequence is a combination of characters starting with the "Esc" code (0x1B) to control the cursor, etc. In addition, character colors and attributes (italic, underline, reverse, blink, high brightness, etc.) can be specified. Libraries such as ncures are used in UNIX/Linux systems for full-scale use from applications such as editors, but if you want to add a little color to characters in a script, you may write directly in the string. . The escape sequence used at this time is called SGR (Select Graphic Rendition). Even now, names given by ANSI X3.64, etc. are often used to distinguish escape sequences.

The basic format is "\e[m". On UNIX/Linux systems, when embedding an Esc code in a string, the escape character is used and written as \e. Specify a numeric character in ASCII code for . Table 01 is the number that shows the parameters. Multiple parameters can be placed between "[" and "m", separated by ";". The latest ISO/IEC 6429 defines 0 to 69, but which one to implement depends on the terminal (terminal software). In many cases, it is often around the old ANSI X3.64. The "\e[" part is written as CSI (Control Sequence Introducer) in the standard.

For example, in bash on Linux, "echo -e "\e[34mTest\e[0m"" should display characters in blue. The leading "\e[34m" specifies the text color, blue, and the trailing "\e[0m" resets the attribute. If you forget this, the color will always change.

When writing multiple attributes at once, use "echo -e "\e[3;34;43mTest\e[0m"". This makes the text italic, the text blue, and the background yellow.

ANSI Terminal Echo

PowerShell's Write-Host command has an option to display the text color, so you don't have to use escape sequences to specify the color, but if you want to display italics that can't be specified with options,

write-host "This is $([char]0x1B)[3mItalic$([char]0x1B)[0m string"

. In PowerShell 7 or later, you can use "`e", which represents the Esc code, instead of "$([char]0x1B)".

ANSI X3.64 defines parameter "1" as "bold or high brightness", but there are many terminal softwares that display in high brightness. This is because the bold font changes the font size on the screen, making it difficult to handle with terminal emulators that use monospaced fonts. This is because the character spacing must match the bold type, and the character spacing would be too large if normal characters were displayed. So if you specify this, most terminal software will display the color in a bright color. In addition, since SGR also has a low luminance "2",

echo -e "\e[34m normal\e[1m high brightness\e[2m low brightness"

will display 3 levels of blue (For Windows Terminal, of course it's terminal dependent).

Unfortunately cmd.exe doesn't have a notation for putting the Esc code into a string. Also, the standard console window (the conhost window) does not interpret escape sequences by default. Therefore, escape sequences cannot be used unless a program is created.

In Windows, it has long been assumed that screen control is performed using the Windows-specific console API, but when the console is improved in Windows 10, Linux uses escape sequences in WSL, so ANSI escape sequences corresponding to. Looking at Microsoft's documentation, it seems that from now on, they will use escape sequences. Microsoft cut ties with escape sequences with Windows NT when development began in 1989, but it seems that it will eventually return to using escape sequences. However, since Windows does not have a library like ncurses, Windows 11 will strengthen this area.

The original title was supposed to be Michael Crichton's "The Terminal Man," but it was so cliché that I decided to rethink it. That's when I remembered "Terminal Echo," which was the name of the station building that used to be at JR Kichijoji Station. The term terminal is derived from the Roman god Terminus. Because the stone associated with Terminus was placed at the border, it later took on the meaning of 'end' or 'boundary', and became a word to describe stations and airports. Terminal equipment also comes from here, and "The Terminal Man" is a title attached from the relationship with the computer. It was made into a movie with the same name in 1974, but the Japanese title was "Electronic Brain Man". There is another novel with the same name, this one is an autobiography of a man who was forced to live in an airport. Inspired by Spielberg's film Terminal. Echo means tree spirit, but it is also the name of a fairy in Greek mythology. For young people these days, terminal software and escape sequences may be a thing of the "mythical" age.