fmtlib 格式化 基本用法 6—— 支持控制台颜色样式

12 篇文章 0 订阅

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;
}

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值