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\033[1J\033[J\033[2K\033[1K\033[KMoving 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\033[H\033[xA\033[xB\033[xD\033[xC\033[s\033[u\0337\0338Use 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.
| Attachment | Size |
|---|---|
| ColorTerminal.zip | 3.2 KB |