C++ 中的彩色输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9158150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:23:56  来源:igfitidea点击:

Colored output in C++

c++xcodemacosiostream

提问by Shoe

Is there a way to print colored output using iostreamand Xcode? I'd like to be able to, for example, print Hello World!with Hellored, Worldblue and !yellow. How can I do that?

有没有办法使用iostreamXcode打印彩色输出?例如,我希望能够Hello World!使用Hello红色、World蓝色和!黄色进行打印。我怎样才能做到这一点?

回答by shuttle87

You need the terminal color codes. For linux it's the following (your system might be different, look it up):

您需要终端颜色代码。对于 linux,它如下(你的系统可能不同,查一下):

//the following are UBUNTU/LINUX, and MacOS ONLY terminal color codes.
#define RESET   "3[0m"
#define BLACK   "3[30m"      /* Black */
#define RED     "3[31m"      /* Red */
#define GREEN   "3[32m"      /* Green */
#define YELLOW  "3[33m"      /* Yellow */
#define BLUE    "3[34m"      /* Blue */
#define MAGENTA "3[35m"      /* Magenta */
#define CYAN    "3[36m"      /* Cyan */
#define WHITE   "3[37m"      /* White */
#define BOLDBLACK   "3[1m3[30m"      /* Bold Black */
#define BOLDRED     "3[1m3[31m"      /* Bold Red */
#define BOLDGREEN   "3[1m3[32m"      /* Bold Green */
#define BOLDYELLOW  "3[1m3[33m"      /* Bold Yellow */
#define BOLDBLUE    "3[1m3[34m"      /* Bold Blue */
#define BOLDMAGENTA "3[1m3[35m"      /* Bold Magenta */
#define BOLDCYAN    "3[1m3[36m"      /* Bold Cyan */
#define BOLDWHITE   "3[1m3[37m"      /* Bold White */

This allows you to do the following:

这允许您执行以下操作:

std::cout << RED << "hello world" << RESET << std::endl;

Note: If you don't use RESET the color will remain changed until the next time you use a color code.

注意:如果您不使用 RESET,颜色将保持更改,直到您下次使用颜色代码。

回答by user760453

In a more c++ way for an ANSI capable terminal, it is possible to write your own ansi stream manipulators like std::endl but for handling ansi escape code.

对于支持 ANSI 的终端,以一种更 C++ 的方式,可以编写自己的 ansi 流操纵器,如 std::endl,但用于处理 ansi 转义码。

Code for doing so can look like this for basic raw implementation:

对于基本的原始实现,这样做的代码可能如下所示:

namespace ansi {
  template < class CharT, class Traits >
  constexpr
  std::basic_ostream< CharT, Traits > & reset( std::basic_ostream< CharT, Traits > &os )
  {
     return os << "3[0m";
  }

  template < class CharT, class Traits >
  constexpr
  std::basic_ostream< CharT, Traits > & foreground_black( std::basic_ostream< CharT, Traits > &os )
  {
     return os << "3[30m";
  }

  template < class CharT, class Traits >
  constexpr
  std::basic_ostream< CharT, Traits > & foreground_red( std::basic_ostream< CharT, Traits > &os )
  {
     return os << "3[31m";
  }
  ...
 } // ansi

And it can be used in a code like this:

它可以用在这样的代码中:

std::cout << ansi::foreground_red << "in red" << ansi::reset << std::endl;