ESCAPE 控制字符
本文最后更新于 194 天前,其中的信息可能已经有所发展或是发生改变。

介绍

  控制字符 ASCII-27,转义表示为 \e\033,键盘键为 ESC,目前常见终端(terminal)程序都支持此转义字符。
  Windows 系统的 cmdPowerShell 并不是真正意义上的终端程序,更可以说是命令行程序。经实验,VS CODE 项目可以用 \e,VS 项目请使用 \033,即八进制的 ASCII-27。没做过多测试,如有错误请提出~

使用

  开始格式:\033[参数1;参数2;参数3m
  结束格式(恢复):\033[0m

参数效果

总览

控制码 效果
\033[0m 关闭所有属性
\033[1m 设置字体高亮度
\033[2m 低亮(减弱)显示
\033[4m 下划线
\033[5m 闪烁
\033[7m 反显
\033[8m 消隐
\033[30m~\033[39m 字体颜色
\033[40m~\033[49m 背景颜色

单值控制码

不能与其他控制码联用

控制码 效果
\033[nA 光标上移 n 行
\033[nB 光标下移 n 行
\033[nC 光标右移 n 列
\033[nD 光标左移 n 列
\033[y;xH 设置光标位置 y 行 x 列
\033[2J 清屏
\033[K 清除从光标到行尾的内容
\033[s 保存光标位置
\033[u 恢复光标位置
\033[?25l 隐藏光标
\033[?25h 显示光标

字体颜色

控制码 字体效果
\033[30m 黑色
\033[31m 红色
\033[32m 绿色
\033[33m 黄色
\033[34m 蓝色
\033[35m 紫色
\033[36m 浅蓝色
\033[37m 白色
\033[38m
\033[39m

背景颜色

控制码 背景效果
\033[40m 黑色
\033[41m 红色
\033[42m 绿色
\033[43m 黄色
\033[44m 蓝色
\033[45m 紫色
\033[46m 浅蓝色
\033[47m 白色
\033[48m
\033[49m

整合

╔══════════╦════════════════════════════════╦═════════════════════════════════════════════════════════════════════════╗
║  Code    ║             Effect             ║                                   Note                                  ║
╠══════════╬════════════════════════════════╬═════════════════════════════════════════════════════════════════════════╣
║ 0        ║  Reset / Normal                ║  all attributes off                                                     ║
║ 1        ║  Bold or increased intensity   ║                                                                         ║
║ 2        ║  Faint (decreased intensity)   ║  Not widely supported.                                                  ║
║ 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.  Not widely supported.    ║
║ 10       ║  Primary(default) font         ║                                                                         ║
║ 11–19    ║  Alternate font                ║  Select alternate font `n-10`                                           ║
║ 20       ║  Fraktur                       ║  hardly ever supported                                                  ║
║ 21       ║  Bold off or Double Underline  ║  Bold off not widely supported; double underline hardly ever supported. ║
║ 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            ║  hardly ever supported                                                  ║
║ 61       ║  ideogram double underline     ║  hardly ever supported                                                  ║
║ 62       ║  ideogram overline             ║  hardly ever supported                                                  ║
║ 63       ║  ideogram double overline      ║  hardly ever supported                                                  ║
║ 64       ║  ideogram stress marking       ║  hardly ever supported                                                  ║
║ 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)                                              ║
╚══════════╩════════════════════════════════╩═════════════════════════════════════════════════════════════════════════╝

代码及运行效果

#include <iostream>

using namespace std;

int main() {
    cout << "以下是测试文字:" << endl;
    cout << "\033[0m默认文字\033[0m" << endl;
    cout << "\033[1m高亮文字\033[0m" << endl;
    cout << "\033[2m低亮文字\033[0m" << endl;
    // cout << "\033[3m无效文字\033[0m" << endl;
    cout << "\033[4m下划线文字\033[0m" << endl;
    cout << "\033[5m闪烁文字\033[0m" << endl;
    // cout << "\033[6m无效文字\033[0m" << endl;
    cout << "\033[7m反显文字\033[0m" << endl;
    cout << "\033[8m消隐文字\033[0m" << endl;

    cout << "\n\033[31;1m字体颜色\033[0m测试文字" << endl;
    cout << "\033[30m低亮黑色文字\033[0m\t\033[30;1m高亮黑色文字\033[0m" << endl;
    cout << "\033[31m低亮红色文字\033[0m\t\033[31;1m高亮红色文字\033[0m" << endl;
    cout << "\033[32m低亮绿色文字\033[0m\t\033[32;1m高亮绿色文字\033[0m" << endl;
    cout << "\033[33m低亮黄色文字\033[0m\t\033[33;1m高亮黄色文字\033[0m" << endl;
    cout << "\033[34m低亮蓝色文字\033[0m\t\033[34;1m高亮蓝色文字\033[0m" << endl;
    cout << "\033[35m低亮紫色文字\033[0m\t\033[35;1m高亮紫色文字\033[0m" << endl;
    cout << "\033[36m低亮浅蓝文字\033[0m\t\033[36;1m高亮浅蓝文字\033[0m" << endl;
    cout << "\033[37m低亮白色文字\033[0m\t\033[37;1m高亮白色文字\033[0m" << endl;
    // cout << "\033[38m测试文字\033[0m" << endl;
    // cout << "\033[39m测试文字\033[0m" << endl;

    cout << "\n\033[31;1m背景颜色\033[0m测试文字" << endl;
    cout << "\033[40m低亮文字黑色背景\033[0m\t\033[40;1m高亮文字黑色背景\033[0m" << endl;
    cout << "\033[41m低亮文字红色背景\033[0m\t\033[41;1m高亮文字红色背景\033[0m" << endl;
    cout << "\033[42m低亮文字绿色背景\033[0m\t\033[42;1m高亮文字绿色背景\033[0m" << endl;
    cout << "\033[43m低亮文字黄色背景\033[0m\t\033[43;1m高亮文字黄色背景\033[0m" << endl;
    cout << "\033[44m低亮文字蓝色背景\033[0m\t\033[44;1m高亮文字蓝色背景\033[0m" << endl;
    cout << "\033[45m低亮文字紫色背景\033[0m\t\033[45;1m高亮文字紫色背景\033[0m" << endl;
    cout << "\033[46m低亮文字浅蓝背景\033[0m\t\033[46;1m高亮文字浅蓝背景\033[0m" << endl;
    cout << "\033[47m低亮文字白色背景\033[0m\t\033[47;1m高亮文字白色背景\033[0m" << endl;
    // cout << "\033[48m测试文字\033[0m" << endl;
    // cout << "\033[49m测试文字\033[0m" << endl;

    return 0;
}

文章作者: xzakota
文章链接: https://blog.xzakota.com/archives/165
版权声明: 本博客内所有文章除特別声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 xzakota
上一篇