如何将彩色字符打印到支持彩色字符的Linux终端?

如何判断终端是否支持颜色码?


当前回答

你可以使用ANSI颜色代码。

使用这些函数。

enum c_color{BLACK=30,RED=31,GREEN=32,YELLOW=33,BLUE=34,MAGENTA=35,CYAN=36,WHITE=37};
enum c_decoration{NORMAL=0,BOLD=1,FAINT=2,ITALIC=3,UNDERLINE=4,RIVERCED=26,FRAMED=51};
void pr(const string str,c_color color,c_decoration decoration=c_decoration::NORMAL){
  cout<<"\033["<<decoration<<";"<<color<<"m"<<str<<"\033[0m";
}

void prl(const string str,c_color color,c_decoration decoration=c_decoration::NORMAL){
   cout<<"\033["<<decoration<<";"<<color<<"m"<<str<<"\033[0m"<<endl;
}

其他回答

你可以使用转义序列,如果你的终端支持的话。例如:

echo \[\033[32m\]Hello, \[\033[36m\]colourful \[\033[33mworld!\033[0m\]

从我的理解,一个典型的ANSI颜色代码

"\033[{FORMAT_ATTRIBUTE};{FORGROUND_COLOR};{BACKGROUND_COLOR}m{TEXT}\033[{RESET_FORMATE_ATTRIBUTE}m"

由(名称和编解码器)组成

FORMAT ATTRIBUTE { "Default", "0" }, { "Bold", "1" }, { "Dim", "2" }, { "Italics", "3"}, { "Underlined", "4" }, { "Blink", "5" }, { "Reverse", "7" }, { "Hidden", "8" } FORGROUND COLOR { "Default", "39" }, { "Black", "30" }, { "Red", "31" }, { "Green", "32" }, { "Yellow", "33" }, { "Blue", "34" }, { "Magenta", "35" }, { "Cyan", "36" }, { "Light Gray", "37" }, { "Dark Gray", "90" }, { "Light Red", "91" }, { "Light Green", "92" }, { "Light Yellow", "93" }, { "Light Blue", "94" }, { "Light Magenta", "95" }, { "Light Cyan", "96" }, { "White", "97" } BACKGROUND COLOR { "Default", "49" }, { "Black", "40" }, { "Red", "41" }, { "Green", "42" }, { "Yellow", "43" }, { "Blue", "44" }, { "Megenta", "45" }, { "Cyan", "46" }, { "Light Gray", "47" }, { "Dark Gray", "100" }, { "Light Red", "101" }, { "Light Green", "102" }, { "Light Yellow", "103" }, { "Light Blue", "104" }, { "Light Magenta", "105" }, { "Light Cyan", "106" }, { "White", "107" } TEXT RESET FORMAT ATTRIBUTE { "All", "0" }, { "Bold", "21" }, { "Dim", "22" }, { "Underlined", "24" }, { "Blink", "25" }, { "Reverse", "27" }, { "Hidden", "28" }

有了这些信息,就很容易给字符串“I am a banana!”上色,前景色为“黄色”,背景色为“绿色”,就像这样

"\033[0;33;42mI am a Banana!\033[0m"

或者用c++库着色

auto const& colorized_text = color::rize( "I am a banana!", "Yellow", "Green" );
std::cout << colorized_text << std::endl;

更多关于FORMAT ATTRIBUTE的例子

试试我的标题这里一个快速和简单的方法来着色文本:Aedi的颜色标题

Escape-Sequence-Color-Header

在Unix中使用c++为输出着色!!

文本属性选项:

ATTRIBUTES_OFF, BOLD, UNDERSCORE, BLINK, REVERSE_VIDEO, CONCEALED

颜色选择:

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE

格式:

通用格式,在$变量$中包含你想要的值

COLOR_$Foreground_Color$_$Background_Color$
COLOR_$Text_Attribute$_$Foreground_Color$_$Background_Color$
COLOR_NORMAL  // To set color to default

e.g.

COLOR_BLUE_BLACK // Leave Text Attribute Blank if no Text Attribute appied
COLOR_UNDERSCORE_YELLOW_RED
COLOR_NORMAL

用法:

只是使用流的颜色,你想在输出文本和 在输出文本后再次使用以将颜色设置为正常。

cout << COLOR_BLUE_BLACK << "TEXT" << COLOR_NORMAL << endl;
cout << COLOR_BOLD_YELLOW_CYAN << "TEXT" << COLOR_NORMAL << endl;

最好的方法是使用ncurses库——尽管如果你只是想输出一个简单的彩色字符串,这可能是一个大锤来敲坚果

你可以直接编写ANSI转义代码来控制颜色,也可以使用诸如{fmt}这样的库来提供api。

例如:

#include <fmt/color.h>

int main() { 
  fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold,
             "Hello, {}!\n", "world");
}

打印

大多数现代终端都支持ANSI转义序列,但您可以使用terminfo数据库进行检查。