美文网首页
关于\u001b转义字符

关于\u001b转义字符

作者: 清水芦苇 | 来源:发表于2017-04-29 22:01 被阅读3422次

关于\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

ESC

Unicode Character 'ESCAPE' (U+001B) 原来U+001B在unicode编码中就是被定义为转义字符 请戳这里:ESC
【备注:Unicode code points appear as U+<codepoint>】

CSI code

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)

SGR code

|Code| Name| Effect|
|--|--|
|CSI n m|Select Graphic Rendition|Sets SGR parameters, including text color. After CSI can be zero or more parameters separated with; . With no parameters, CSI m is treated as CSI 0 m (reset / normal), which is typical of most of the ANSI escape sequences.|

\u001b 在不同字符集下的编码表现:

适用场景(不同字符集) 编码
HTML Entity (decimal)
HTML Entity (hex)
javascript \033(注意有别于8进制的utf-16)
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)
前面的\u001b[31m用于设定SGR颜色,后面的\u001b[0m相当于一个封闭标签作为前面SGR颜色的作用范围的结束点标记。
这等价于
process.stderr.write('\033[31m error \033[0m) (注意033前面的** \ **是必须加的,这在javascript中才会被识别为八进制的33,即c++中的\u001B

维基百科

  1. ANSI转义字符之颜色
  2. 玩转 命令行之python版本

相关文章

  • 关于\u001b转义字符

    关于\u001b转义字符 在nodejs中有这样一段代码可以使得控制台输出显示颜色,process.stderr....

  • 正则

    元字符 转义 字符说明\转义字符 转义字符将普通字符转义为特殊字符,将特殊字符转义为普通字符: 字符说明\b匹配字...

  • Python 基础学习

    1、关于转义: Python允许用r''表示''内部的字符串默认不转义。 2、关于换行: Python允许用'''...

  • 基础语法-字符串

    转义字符 转义字符\可以转义很多字符,比如\n表示换行,\t表示制表符,字符\本身也要转义,所以\表示的字符就是\...

  • 字符串-转义字符-编码-乱码-格式化

    1. 转义字符 转义字符\可以转义很多字符,比如\n表示换行,\t表示制表符,字符\本身也要转义,所以\表示的字符...

  • 小组分享--HTML转义字符&自定义标签

    emmmm 我们今天来讲讲html转义字符和自定义标签: 转义字符 先来说说html转义字符,转义字符大家应该都用...

  • Python语法小记忆

    1、转义字符 Python 中的字符串还支持转义字符。所谓转义字符是指使用反斜杠“\”对一些特殊字符进行转义。 转...

  • Python中的字符串处理

    Python转义字符 在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符。如下表: 转义字符描述 \...

  • Python 基础2

    Python 转义字符 在需要在字符中使用特殊字符时,python 用反斜杠\转义字符。如下表: 转义字符描述 \...

  • 第三十三节: JavaScript 正则表达式

    1. 理解转义 转义即转换意义,改变意义 转义符号 \转义字符 \字符( 把原有的字符转换意义) 一些...

网友评论

      本文标题:关于\u001b转义字符

      本文链接:https://www.haomeiwen.com/subject/krcttxtx.html