从一个实例开始
在终端打印绿色的 “Hello, World!” 可以通过下面的C实现:
#include <stdio.h>
int main(int argc, const char * argv[])
{
printf("\x1B[32mHello, World! \x1B[0m\n");
printf("\033[32mHello, World! \033[0m\n");
printf("\e[32mHello, World! \e[0m\n");
return 0;
}
等同于用在 shell环境 用下面 echo 命令
echo -e "\033[32mHello, World! \033[0m"
echo -e "\x1B[32mHello, World! \x1B[0m"
echo -e "\e[32mHello, World! \e[0m"
注释:
- 在C中所有的ASCII码都可以用“\”加数字(一般是8进制或者16进制)来表示。而C中定义了一些字母前加"\"来表示常见的那些不能显示的ASCII字符,如\0,\t,\n等,就称为转义字符,因为后面的字符,都不是它本来的ASCII字符意思了。
- \x1B 和 \033 分别是 27 的十六进制和八进制表示,也可以使用 \e 或者 \E 来表示。
- ASCII 中的第27号字符 ESC(Escape)也就是转义字符。
- “\033[32m” 中 “\033[” 表示一段 CSI 数据的开始,“m” 表示一段 CSI 数据的控制指令也是结束标志,“32” 是 CSI 的数据的内容,“m” 表示这段 CSI 数据是 SGR 格式,32则表示 SGR 的32号指令,也就是绿色。
CSI sequences
The ESC [ is followed by any number (including none) of “parameter bytes” in the range 0x30–0x3F (ASCII 0–9:;<=>?), then by any number of “intermediate bytes” in the range 0x20–0x2F (ASCII space and !"#$%&’()*+,-./), then finally by a single “final byte” in the range 0x40–0x7E (ASCII @A–Z[]^_`a–z{|}~).[18]:5.4
All common sequences just use the parameters as a series of semicolon-separated numbers such as 1;2;3. Missing numbers are treated as 0 (1;;3 acts like the middle number is 0, and no parameters at all in ESC[m acts like a 0 reset code). Some sequences (such as CUU) treat 0 as 1 in order to make missing parameters useful.[18]:F.4.2 Bytes other than digits and semicolon seem to not be used.[citation needed]
A subset of arrangements was declared “private” so that terminal manufacturers could insert their own sequences without conflicting with the standard. Sequences containing the parameter bytes <=>? or the final bytes 0x70–0x7E (p–z{|}~) are private.
The behavior of the terminal is undefined in the case where a CSI sequence contains any character outside of the range 0x20–0x7E. These illegal characters are either C0 control characters (the range 0–0x1F), DEL (0x7F), or bytes with the high bit set. Possible responses are to ignore the byte, to process it immediately, and furthermore whether to continue with the CSI sequence, to abort it immediately, or to ignore the rest of it.[citation needed]
SGR (Select Graphic Rendition) parameters
SGR sets display attributes. Several attributes can be set in the same sequence, separated by semicolons.[21] Each display attribute remains in effect until a following occurrence of SGR resets it.[1] If no codes are given, CSI m is treated as CSI 0 m (reset / normal).
In ECMA-48 SGR is called “Select Graphic Rendition”.[1] In Linux manual pages the term “Set Graphics Rendition” is used.
Code | Effect | Note |
---|---|---|
0 | Reset / Normal | all attributes off |
1 | Bold or increased intensity | |
2 | Faint (decreased intensity) | |
3 | Italic | Not widely supported. Sometimes treated as inverse. |
4 | Underline | |
5 | Slow Blink | less than 150 per minute |
6 | Rapid Blink | MS-DOS ANSI.SYS; 150+ per minute; not widely supported |
7 | reverse video | swap foreground and background colors |
8 | Conceal | Not widely supported. |
9 | Crossed-out | Characters legible, but marked for deletion. |
10 | Primary(default) font | |
11–19 | Alternative font | Select alternative font {\displaystyle n-10} {\displaystyle n-10} |
20 | Fraktur | Rarely supported |
21 | Doubly underline or Bold off | Double-underline per ECMA-48.[22] See discussion |
22 | Normal color or intensity | Neither bold nor faint |
23 | Not italic, not Fraktur | |
24 | Underline off | Not singly or doubly underlined |
25 | Blink off | |
27 | Inverse off | |
28 | Reveal conceal off | |
29 | Not crossed out | |
30–37 | Set foreground color | See color table below |
38 | Set foreground color | Next arguments are 5;n or 2;r;g;b, see below |
39 | Default foreground color | implementation defined (according to standard) |
40–47 | Set background color | See color table below |
48 | Set background color | Next arguments are 5;n or 2;r;g;b, see below |
49 | Default background color | implementation defined (according to standard) |
51 | Framed | |
52 | Encircled | |
53 | Overlined | |
54 | Not framed or encircled | |
55 | Not overlined | |
60 | ideogram underline or right side line | Rarely supported |
61 | ideogram double underline or double line on the right side | |
62 | ideogram overline or left side line | |
63 | ideogram double overline or double line on the left side | |
64 | ideogram stress marking | |
65 | ideogram attributes off | reset the effects of all of 60–64 |
90–97 | Set bright foreground color | aixterm (not in standard) |
100–107 | Set bright background color | aixterm (not in standard) |