Colored output in C++

前端 未结 3 2005
后悔当初
后悔当初 2021-02-01 04:39

Is there a way to print colored output using iostream and Xcode? I\'d like to be able to, for example, print Hello World! with Hello red,

3条回答
  •  独厮守ぢ
    2021-02-01 05:22

    Use {fmt} library, which is being slowly absorbed into C++ standard, starting with C++20 in header. Text colors and styles are not in the standard yet, AFAIK, but you can get them with the version on github, where you can find this example:

    #include 
    
    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", "世界");
    }
    

提交回复
热议问题