栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在BASH下运行的程序的颜色输出

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在BASH下运行的程序的颜色输出

大多数终端都遵循ASCII颜色序列。它们的工作方式是输出ESC,然后输出,[然后是分号分隔的颜色值列表,然后是m。这些是常见的值:

Special0  Reset all attributes1  Bright2  Dim4  Underscore   5  Blink7  Reverse8  HiddenForeground colors30  Black31  Red32  Green33  Yellow34  Blue35  Magenta36  Cyan37  WhiteBackground colors40  Black41  Red42  Green43  Yellow44  Blue45  Magenta46  Cyan47  White

因此,输出

"33[31;47m"
应使终端正面(文本)颜色为红色,背景颜色为白色。

您可以将其很好地包装为C ++形式:

enum Color {    NONE = 0,    BLACK, RED, GREEN,    YELLOW, BLUE, MAGENTA,    CYAN, WHITE}std::string set_color(Color foreground = 0, Color background = 0) {    char num_s[3];    std::string s = "33[";    if (!foreground && ! background) s += "0"; // reset colors if no params    if (foreground) {        itoa(29 + foreground, num_s, 10);        s += num_s;        if (background) s += ";";    }    if (background) {        itoa(39 + background, num_s, 10);        s += num_s;    }    return s + "m";}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/368332.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号