有没有办法使用
iostream
和 Xcode 打印彩色输出?例如,我希望能够使用 Hello World!
红色、Hello
蓝色和 World
黄色打印 !
。我怎样才能做到这一点?
您需要终端颜色代码。对于 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 */
这允许您执行以下操作:
std::cout << RED << "hello world" << RESET << std::endl;
注意:如果您不使用 RESET,颜色将保持更改状态,直到您下次使用颜色代码。
对于支持 ANSI 的终端,以更 C++ 的方式,可以编写自己的 ansi 流操纵器,例如 std::endl,但用于处理 ansi 转义码。
对于基本的原始实现,这样做的代码可能如下所示:
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
它可以在这样的代码中使用:
std::cout << ansi::foreground_red << "in red" << ansi::reset << std::endl;
使用 {fmt} 库,它正在慢慢被吸收到 C++ 标准中,从
<format>
标头中的 C++20 开始。 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", "世界");
}
这个方法怎么样?
enum class ColorText {
RESET = 0,
BRIGHT_BLACK= 90,
BRIGHT_RED= 91,
BRIGHT_GREEN= 92,
BRIGHT_YELLOW= 93,
BRIGHT_BLUE= 94,
BRIGHT_MAGENTA= 95,
BRIGHT_CYAN= 96,
BRIGHT_WHITE= 97
};
enum class ColorBack {
RESET = 0,
DARK_BLACK = 40,
DARK_RED = 41,
DARK_GREEN = 42,
DARK_YELLOW = 43,
DARK_BLUE = 44,
DARK_MAGENTA = 45,
DARK_CYAN = 46,
DARK_WHITE = 47,
};
std::ostream& operator<<(std::ostream& os, ColorText color) {
return os << "\033[" << static_cast<int>(color) << "m";
}
std::ostream& operator<<(std::ostream& os, ColorBack color) {
return os << "\033[" << static_cast<int>(color) << "m";
}
int main(){
std::cout << ColorText::BRIGHT_GREEN << "Hello in green!" << ColorText::RESET << '\n';
std::cout << "Back to normal!\n\n";
std::cout << ColorText::DARK_BLACK << ColorBack::BRIGHT_GREEN << "Hello in green background!" << ColorBack::RESET << '\n';
std::cout << "Back to normal!\n\n";
return 0;
}