关于u001b转义字符
关于\u001b转义字符
在nodejs中有这样⼀段代码可以使得控制台输出显⽰颜⾊,process.stderr.write('\u001b[31m error \u001b[0m :)
下⾯是摘⾃的⼀段内容:
The original specification only had 8 colors, and just gave them names. The SGR parameters 30-37 selected the foreground color, while 40-47 selected the background. Quite a few terminals implemented "bold" (SGR code 1) as a brighter color rather than a different font, thus providing 8 additional foreground colors. Usually you could not get these as background colors, though sometimes inverse video (SGR code
7) would allow that. Examples: to get black letters on white background use ESC[30;47m, to get red use
ESC[31m, to get bright red use ESC[31;1m. To reset colors to their defaults, use ESC[39;49m (not supported on some terminals), or reset all attributes with ESC[0m.
SGR code
其中有⼏个关键词:ESC,SGR code, CSI codes
E SC
C SI cod e
most of the sequences are more than two characters and start with the characters ESC and [ (left bracket/0x5B).
This sequence is called CSI for Control Sequence Introducer (or Control Sequence Initiator)
SG R cod e
\u001b 在不同字符集下的编码表现:
适⽤场景(不同字符集)编码
HTML Entity (decimal)�
HTML Entity (hex)�
javascript\033(注意有别于8进制的utf-16)b总001
C/C++/Java source code\u001B
Python source code u"\u001B"(u开头表⽰这是⼀个unicode string)
UTF-8 (hex)0x1B (1b)
UTF-8 (binary)00011011
UTF-8 (octal)033
UTF-16 (hex)0x001B (001b)
UTF-16 (decimal)27
UTF-32 (hex)0x0000001B (1b)
UTF-32 (decimal)27
结论:
process.stderr.write('\u001b[31m error \u001b[0m)
这等价于
process.stderr.write('\033[31m error \033[0m) (注意033前⾯的** \ **是必须加的,这在javascript中才会被识别为⼋进制的33,即c++中的\u001B)
1. ANSI转义字符之颜⾊
2. 玩转 命令⾏之python版本
发布评论