ANSI control sequences

ANSI control sequences are defined in standard ANSI X3.64-1979. Escape sequences are sequences of characters beginning with the escape character. These sequences can be used to change terminal configuration, like text and background colors or the cursor position. They should work in all terminals commonly used under Linux and MacOS operating systems. They are supported in DOS as well, after loading the ANSI.SYS file. New versions of Windows operating systems don't support this standard.

ANSI escape sequences are a very good light weight alternative to heavy libraries like slang or ncurses. Escape sequences are ideal when the advanced functions (like synchronous work with keyboard) are not needed, and you only need to draw a simple TUI interface.

Using escape sequences is very simple. They only have to be printed out, along with normal text. The terminal should not print these sequences, but instead interpret them.

Text attributes

The most commonly used feature is changing the foreground and background color. However, escape sequences also support change of other text attributes. The presentation of text attributes may unfortunately differ, terminal from terminal.

Supported colors are: Black (0), Red (1), Green (2), Yellow (3), Blue (4), Magenta (5), Cyan (6) and White (7). Number 9 represents the default color.

If you want to use the color as foreground color, add 30 to the color number, if you want to use it as background color, add 40 to the color number.

Possible text attributes are: Bright (1), Dark (2), Underline (4), Blink (5), Inverse (7), Hidden (8).

The escape sequence for setting text attributes has 1-3 parameters. These parameters can be presented in any order, due to unambiguous numbering (1-8 attributes, 30-37 foreground colors, 40-47 background colors).

All text printed after \033[x;x;xm will be presented using the selected text attributes. To reset back to default use \033[0m.

All attributes will remain until reset, therefore don't forget to return the terminal to normal state.

Erasing text

\033[2J
erase whole screen
\033[1J
erase the screen from the cursor upwards
\033[J
erase the screen from the cursor downwards
\033[2K
erase the current line
\033[1K
erase the line from start to the cursor
\033[K
erase the line from the cursor to end

Moving the cursor

All following escape sequences that allow specifying the count, exist in alternative version, with no count specified. In that case, the escape sequence works as if count 1 was specified.

\033[y;xH
move cursor to column x row y
\033[H
move cursor to left top corner
\033[xA
move cursor x rows up
\033[xB
move cursor x rows down
\033[xD
move cursor x columns left
\033[xC
move cursor x columns right
\033[s
save cursor position
\033[u
restore cursor position
\0337
save cursor position and text attributes
\0338
restore cursor position and text attributes

Use in software

As already mentioned, using the escape sequences is very simple. Just print out the escape sequence together with the rest of the text using printf (or other equivalent command) and the rest will be handled by the terminal.

Don't forget to add a backup option (like command line switch) that will turn off the output of escape sequences. This is important especially when saving the output into a file. In that case, there is no possible interpretation, and escape sequences will be simply saved together with the rest of the text.

Library

To make your life easier, here is a small library for C programming language.

For C++ you can use the following implementation using stream manipulators.

AttachmentSize
ColorTerminal.zip3.2 KB
Your rating: None Average: 5 (1 vote)