如何输出彩色文本到Linuxterminal?

如何将彩色字符打印到支持它的Linuxterminal上?

如何判断terminal是否支持颜色代码?

我为这个程序使用C ++。

您需要输出ANSI颜色代码 。 请注意,并非所有的terminal都支持 如果颜色序列不被支持,垃圾就会出现。

例:

  cout << "\033[1;31mbold red text\033[0m\n"; 

在这里, \033是ESC字符,ASCII 27。它后面是[ ,然后零个或多个数字分隔; ,最后是字母m 。 这些数字描述了从该点开始转换的颜色和格式。

前景色和背景色的代码是:

  foreground background black 30 40 red 31 41 green 32 42 yellow 33 43 blue 34 44 magenta 35 45 cyan 36 46 white 37 47 

另外,你可以使用这些:

 reset 0 (everything back to normal) bold/bright 1 (often a brighter shade of the same colour) underline 4 inverse 7 (swap foreground and background colours) bold/bright off 21 underline off 24 inverse off 27 

请参阅Wikipedia上的表格,了解其他支持程度较低的代码。


编辑:要确定您的terminal是否支持色彩序列,请阅读TERM环境variables的值。 它应该指定使用的特定terminaltypes(例如vt100gnome-terminalxtermscreen ,…)。 然后在terminfo数据库中查看 检查colors能力。

基本

我写了一个C ++类,可以用来设置输出的前景色和背景色。 这个示例程序是打印的一个例子。 This ->word<- is red. 并对其进行格式化,以使word的前景色为红色。

 #include "colormod.h" // namespace Color #include <iostream> using namespace std; int main() { Color::Modifier red(Color::FG_RED); Color::Modifier def(Color::FG_DEFAULT); cout << "This ->" << red << "word" << def << "<- is red." << endl; } 

资源

 #include <ostream> namespace Color { enum Code { FG_RED = 31, FG_GREEN = 32, FG_BLUE = 34, FG_DEFAULT = 39, BG_RED = 41, BG_GREEN = 42, BG_BLUE = 44, BG_DEFAULT = 49 }; class Modifier { Code code; public: Modifier(Code pCode) : code(pCode) {} friend std::ostream& operator<<(std::ostream& os, const Modifier& mod) { return os << "\033[" << mod.code << "m"; } }; } 

高级

您可能需要为课程添加其他function。 例如,可以添加洋红色的颜色,甚至可以添加黑体字样式。 要做到这一点CodeGo.net,只是另一个条目枚举。 这是一个很好的参考。

在你输出任何颜色之前,你需要确保你在terminal中:

 [ -t 1 ] && echo 'Yes I am in a terminal' # isatty(3) call in C 

那么你需要检查terminal的能力,如果它支持颜色

在带有terminfo系统上(基于Linux),您可以获得支持的颜色数量

 Number_Of_colors_Supported=$(tput colors) 

在带有termcap (基于BSD)的系统上,您可以获得支持的颜色数量

 Number_Of_colors_Supported=$(tput Co) 

然后让你决定:

 [ ${Number_Of_colors_Supported} -ge 8 ] && { echo 'You are fine and can print colors' } || { echo 'Terminal does not support color' } 

顺便说一句,不要像以前使用ESC字符所暗示的那样使用着色。 使用标准的呼叫terminal的能力,将分配给你正确的颜色,特定的terminal支持。

基于BSD

 fg_black="$(tput AF 0)" fg_red="$(tput AF 1)" fg_green="$(tput AF 2)" fg_yellow="$(tput AF 3)" fg_blue="$(tput AF 4)" fg_magenta="$(tput AF 5)" fg_cyan="$(tput AF 6)" fg_white="$(tput AF 7)" reset="$(tput me)" 

基于Linux的

 fg_black="$(tput setaf 0)" fg_red="$(tput setaf 1)" fg_green="$(tput setaf 2)" fg_yellow="$(tput setaf 3)" fg_blue="$(tput setaf 4)" fg_magenta="$(tput setaf 5)" fg_cyan="$(tput setaf 6)" fg_white="$(tput setaf 7)" reset="$(tput sgr0)" 

用于

 echo -e "${fg_red} Red ${fg_green} Bull ${reset}" 

正如其他人所说,你可以使用转义字符。 你可以使用我的头 ,以便更容易:

 #ifndef _COLORS_ #define _COLORS_ /* FOREGROUND */ #define RST "\x1B[0m" #define KRED "\x1B[31m" #define KGRN "\x1B[32m" #define KYEL "\x1B[33m" #define KBLU "\x1B[34m" #define KMAG "\x1B[35m" #define KCYN "\x1B[36m" #define KWHT "\x1B[37m" #define FRED(x) KRED x RST #define FGRN(x) KGRN x RST #define FYEL(x) KYEL x RST #define FBLU(x) KBLU x RST #define FMAG(x) KMAG x RST #define FCYN(x) KCYN x RST #define FWHT(x) KWHT x RST #define BOLD(x) "\x1B[1m" x RST #define UNDL(x) "\x1B[4m" x RST #endif /* _COLORS_ */ 

使用头的macros的例子可以是:

 #include <iostream> #include "colors.h" using namespace std; int main() { cout << FBLU("I'm blue.") << endl; cout << BOLD(FBLU("I'm blue-bold.")) << endl; return 0; } 

在这里输入图像描述

这是一个老话题,但是我写了一个嵌套的子类和静态成员的类,它们是由简单的Cmacros定义的颜色。

我从用户no2pencil的dreamincode.net的C编程中的Color文本中获得了color函数。

我这样做是为了能够像这样使用std :: coutstream中的静态常量:

 cout << zkr::cc::fore::red << "This is red text. " << zkr::cc::console << "And changing to console default colors, fg, bg." << endl; 

这个类和一个testing程序的源代码可以在这里下载。

cc::console将重置为控制台的默认颜色和属性, cc::underline将会强调文本,该文本在我testing过testing程序的putty上工作。

颜色:

 black blue red magenta green cyan yellow white lightblack lightblue lightred lightmagenta lightgreen lightcyan lightyellow lightwhite 

它可以用于cc静态类的fore静态子类。

编辑2017年

我只是在这里添加类代码更实际。

颜色代码macros:

 #define CC_CONSOLE_COLOR_DEFAULT "\033[0m" #define CC_FORECOLOR(C) "\033[" #C "m" #define CC_BACKCOLOR(C) "\033[" #C "m" #define CC_ATTR(A) "\033[" #A "m" 

以及为屏幕定义颜色或属性的主要颜色函数:

 char *cc::color(int attr, int fg, int bg) { static char command[13]; /* Command is the control command to the terminal */ sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40); return command; } 

ccolor.h

 #include <stdio.h> #define CC_CONSOLE_COLOR_DEFAULT "\033[0m" #define CC_FORECOLOR(C) "\033[" #C "m" #define CC_BACKCOLOR(C) "\033[" #C "m" #define CC_ATTR(A) "\033[" #A "m" namespace zkr { class cc { public: class fore { public: static const char *black; static const char *blue; static const char *red; static const char *magenta; static const char *green; static const char *cyan; static const char *yellow; static const char *white; static const char *console; static const char *lightblack; static const char *lightblue; static const char *lightred; static const char *lightmagenta; static const char *lightgreen; static const char *lightcyan; static const char *lightyellow; static const char *lightwhite; }; class back { public: static const char *black; static const char *blue; static const char *red; static const char *magenta; static const char *green; static const char *cyan; static const char *yellow; static const char *white; static const char *console; static const char *lightblack; static const char *lightblue; static const char *lightred; static const char *lightmagenta; static const char *lightgreen; static const char *lightcyan; static const char *lightyellow; static const char *lightwhite; }; static char *color(int attr, int fg, int bg); static const char *console; static const char *underline; static const char *bold; }; } 

ccolor.cpp

 #include "ccolor.h" using namespace std; namespace zkr { enum Color { Black, Red, Green, Yellow, Blue, Magenta, Cyan, White, Default = 9 }; enum Attributes { Reset, Bright, Dim, Underline, Blink, Reverse, Hidden }; char *cc::color(int attr, int fg, int bg) { static char command[13]; /* Command is the control command to the terminal */ sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40); return command; } const char *cc::console = CC_CONSOLE_COLOR_DEFAULT; const char *cc::underline = CC_ATTR(4); const char *cc::bold = CC_ATTR(1); const char *cc::fore::black = CC_FORECOLOR(30); const char *cc::fore::blue = CC_FORECOLOR(34); const char *cc::fore::red = CC_FORECOLOR(31); const char *cc::fore::magenta = CC_FORECOLOR(35); const char *cc::fore::green = CC_FORECOLOR(92); const char *cc::fore::cyan = CC_FORECOLOR(36); const char *cc::fore::yellow = CC_FORECOLOR(33); const char *cc::fore::white = CC_FORECOLOR(37); const char *cc::fore::console = CC_FORECOLOR(39); const char *cc::fore::lightblack = CC_FORECOLOR(90); const char *cc::fore::lightblue = CC_FORECOLOR(94); const char *cc::fore::lightred = CC_FORECOLOR(91); const char *cc::fore::lightmagenta = CC_FORECOLOR(95); const char *cc::fore::lightgreen = CC_FORECOLOR(92); const char *cc::fore::lightcyan = CC_FORECOLOR(96); const char *cc::fore::lightyellow = CC_FORECOLOR(93); const char *cc::fore::lightwhite = CC_FORECOLOR(97); const char *cc::back::black = CC_BACKCOLOR(40); const char *cc::back::blue = CC_BACKCOLOR(44); const char *cc::back::red = CC_BACKCOLOR(41); const char *cc::back::magenta = CC_BACKCOLOR(45); const char *cc::back::green = CC_BACKCOLOR(42); const char *cc::back::cyan = CC_BACKCOLOR(46); const char *cc::back::yellow = CC_BACKCOLOR(43); const char *cc::back::white = CC_BACKCOLOR(47); const char *cc::back::console = CC_BACKCOLOR(49); const char *cc::back::lightblack = CC_BACKCOLOR(100); const char *cc::back::lightblue = CC_BACKCOLOR(104); const char *cc::back::lightred = CC_BACKCOLOR(101); const char *cc::back::lightmagenta = CC_BACKCOLOR(105); const char *cc::back::lightgreen = CC_BACKCOLOR(102); const char *cc::back::lightcyan = CC_BACKCOLOR(106); const char *cc::back::lightyellow = CC_BACKCOLOR(103); const char *cc::back::lightwhite = CC_BACKCOLOR(107); } 

我使用下面的解决scheme,它非常简单和优雅,可以很容易地粘贴到源代码,并在Linux / Bash上工作:

 const std::string red("\033[0;31m"); const std::string green("\033[1;32m"); const std::string yellow("\033[1;33m"); const std::string cyan("\033[0;36m"); const std::string magenta("\033[0;35m"); const std::string reset("\033[0m"); std::cout << "Measured runtime: " << yellow << timer.count() << reset << std::endl; 

如果您的terminal支持,您可以使用转义序列。 例如:

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

您可以使用ANSI颜色代码。

使用这些function。

 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; } 

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

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

由(名称和编解码器)

  • FORMAT ATTRIBUTE

     { "Default", "0" }, { "Bold", "1" }, { "Dim", "2" }, { "Underlined", "3" }, { "Blink", "5" }, { "Reverse", "7" }, { "Hidden", "8" } 
  • FORGROUND颜色

     { "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" } 
  • 背景颜色

     { "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" } 
  • 文本

  • 重置格式属性

     { "All", "0" }, { "Bold", "21" }, { "Dim", "22" }, { "Underlined", "24" }, { "Blink", "25" }, { "Reverse", "27" }, { "Hidden", "28" } 

有了这些信息,就很容易上色一串“我是香蕉! 与这样的背景色“黄色”和背景色“绿色”

 "\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的更多例子在这里 在这里输入图像描述

最好的方法是使用ncurses库 – 虽然这可能是一个大锤破解螺母,如果你只是想输出一个简单的彩色string

尝试我的头在这里为一个快速和简单的方法来彩色文本: Aedi的彩色标题

逃生序彩色报头

使用C ++在Unix中输出颜色!

文本属性选项:

 ATTRIBUTES_OFF, BOLD, UNDERSCORE, BLINK, REVERSE_VIDEO, CONCEALED 

颜色选项:

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

格式:

一般格式,在$ variable $中包含你想要的值

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

例如

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

用法:

只需在输出文本之前使用您想要的颜色进行stream式处理,然后在输出文本后再次使用颜色设置为正常。

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

在OSXshell上,这适用于我(包括“红色文本”前面的2个空格):

 $ printf "\e[033;31m red text\n" $ echo "$(tput setaf 1) red text"