fmtlib支持控制台的样式:
主要的API
template<typename S, typename ...Args>
void fmt::print(const text_style &ts, const S &format_str, const Args&... args)
text_style fmt::fg(detail::color_type foreground) noexcept
text_style fmt::bg(detail::color_type background) noexcept
template<typename T>
auto fmt::styled(const T &value, text_style ts) -> detail::styled_arg<remove_cvref_t<T>>
返回一个参数,该参数将使用ANSI转义序列进行格式化,用于格式化函数
测试代码:
int main(int argc, char** argv)
{
fmt::print("Elapsed time: {0:.2f} seconds\n",
fmt::styled(1.23, fmt::fg(fmt::color::green) | fmt::bg(fmt::color::blue)));
auto msg = fmt::format(fmt::fg(fmt::color::green) | fmt::bg(fmt::color::blue), "a");
cout << msg << endl;
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", "世界");
return 0;
}
运行结果: