C++中的彩色输出

Sho*_*hoe 15 c++ macos xcode iostream

有没有办法使用iostream和Xcode 打印彩色输出?我想是可以的,例如,印Hello World!Hello红色,World蓝色和!黄色.我怎样才能做到这一点?

shu*_*e87 42

您需要终端颜色代码.对于linux,它是以下(您的系统可能不同,查找它):

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

这允许您执行以下操作:

std::cout << RED << "hello world" << RESET << std::endl;
Run Code Online (Sandbox Code Playgroud)

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

  • 它可以在终端中工作,但不能在Xcode控制台窗口中工作 (11认同)
  • @Paul R 你知道如何检测控制台是否支持 ANSI 转义码,以便 Xcode 控制台不打印它们吗? (2认同)

use*_*453 9

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.

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 << "\033[0m";
  }

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

  template < class CharT, class Traits >
  constexpr
  std::basic_ostream< CharT, Traits > & foreground_red( std::basic_ostream< CharT, Traits > &os )
  {
     return os << "\033[31m";
  }
  ...
 } // ansi
Run Code Online (Sandbox Code Playgroud)

And it can be used in a code like this:

std::cout << ansi::foreground_red << "in red" << ansi::reset << std::endl;
Run Code Online (Sandbox Code Playgroud)

  • 非常优雅的解决方案,荣誉! (2认同)

Pau*_*zak 6

使用{fmt} library,它正在慢慢被 C++ 标准吸收,从头文件中的 C++20 开始<format>。文本颜色和样式还不是标准的,AFAIK,但你可以从 github 的版本中获取它们,你可以在那里找到这个例子:

#include <fmt/color.h>

int main() {
  fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold,
             "Hello, {}!\n", "world");
  fmt::print(fg(fmt::color::floral_white) | bg(fmt::color::slate_gray) |
             fmt::emphasis::underline, "Hello, {}!\n", "???");
  fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic,
             "Hello, {}!\n", "??");
}
Run Code Online (Sandbox Code Playgroud)