如何将彩色字符打印到支持彩色字符的Linux终端?
如何判断终端是否支持颜色码?
如何将彩色字符打印到支持彩色字符的Linux终端?
如何判断终端是否支持颜色码?
当前回答
这是一个老主题,但我编写了一个类,其中包含嵌套子类和由简单C宏定义的颜色静态成员。
我在dreamincode.net中通过用户no2pencil从这篇文章中获得了颜色函数。
我这样做是为了能够像这样使用std::cout流中的静态常量:
cout << zkr::cc::fore::red << "This is red text. "
<< zkr::cc::console << "And changing to console default colors, fg, bg."
<< endl;
类和测试程序的源代码可以在这里下载。
cc::console将重置为控制台默认颜色和属性,cc::underline将为文本下划线,这在putty上工作,我已经测试了测试程序。
颜色:
black
blue
red
magenta
green
cyan
yellow
white
lightblack
lightblue
lightred
lightmagenta
lightgreen
lightcyan
lightyellow
lightwhite
它可以用于cc静态类的前静态和后静态子类。
编辑2017
为了更实用,我在这里添加了类代码。
颜色代码宏:
#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"
main color函数定义屏幕的颜色或属性:
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);
}
其他回答
您需要输出ANSI颜色代码。注意,并非所有终端都支持此功能;如果不支持颜色序列,就会显示垃圾。
例子:
cout << "\033[1;31mbold red text\033[0m\n";
这里,\033是ESC字符,ASCII 27。它后面跟着[,然后是0个或多个用;隔开的数字,最后是字母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
有关其他不太受广泛支持的代码,请参见维基百科上的表格。
要确定您的终端是否支持颜色序列,请读取TERM环境变量的值。它应该指定所使用的特定终端类型(例如vt100、gnome-terminal、xterm、screen……)。然后在terminfo数据库中查找;检查颜色能力。
你可以使用转义序列,如果你的终端支持的话。例如:
echo \[\033[32m\]Hello, \[\033[36m\]colourful \[\033[33mworld!\033[0m\]
我知道这个问题很老了,但我把这个答案贴出来给未来的读者。我用c++写了一个用于彩色输出的库。这使用操纵者,使工作容易支持跨平台,但没有在这里测试是如何使用这个,
#include "srilakshmikanthanp/ANSI.hpp"
using namespace srilakshmikanthanp;
3位和4位颜色:
// background
std::cout << ansi::BGyellow;
// foreground
std::cout << ansi::FGblue;
// output
std::cout << "Blue on yellow";
// reset
std::cout << ansi::reset;
8位颜色:
// background
std::cout << ansi::BGcolor(157);
// foreground
std::cout << ansi::FGcolor(100);
// outpt
std::cout << "8 bit color";
// reset
std::cout << ansi::reset;
24位颜色:
// background
std::cout << ansi::BGcolor(0, 255, 0);
// foreground
std::cout << ansi::FGcolor(0, 0, 255);
// output
std::cout << "24 bit color";
// reset
std::cout << ansi::reset;
字符串:
使用ansi::str可以很容易地将这个操作符转换为字符串
std::string BGyellow = ansi::str(ansi::BGyellow);
std::string FGblue = ansi::str(ansi::FGblue);
std::string reset = ansi::str(ansi::reset);
std::cout << BGyelow;
// foreground
std::cout << FGblue;
// output
std::cout << "Blue on Yellow";
// reset
std::cout << reset;
你可以在github上找到更多的信息:)
最好的方法是使用ncurses库——尽管如果你只是想输出一个简单的彩色字符串,这可能是一个大锤来敲坚果
在OSX shell上,这为我工作(包括“红色文本”前面的2个空格):
$ printf "\e[033;31m red text\n"
$ echo "$(tput setaf 1) red text"