如何使用 ISO 标准转义序列制作粗体文本?

发布于 2025-01-18 21:16:20 字数 795 浏览 1 评论 0原文

我创建了一个命名空间,用于存储 ANSI 值以用于日志记录。

namespace ANSI {
    static const char *BLACK_BG = "\x1B[48;2;0;0;0m";
    static const char *RED = "\x1B[38;2;255;0;0m";
    static const char *GREEN = "\x1B[38;2;0;255;0m";
    static const char *GREY = "\x1B[38;2;70;70;70m";
    static const char *EXIT = "\x1B[0m";
    static std::ostream &BOLD(std::ostream& log) { return log << "\e[1m"; }
}

// Example usage:
// std::cout << ANSI::BOLD << ANSI::GREEN << "This is bold green text" << ANSI::EXIT << "\n";

但是当我尝试使用 -pedantic 标志进行编译时,它告诉我 \e[1m 不是 ISO 标准转义序列。如果我删除该标志,它不会显示错误,但我想同时保留它。

PS 如果我上面的代码做错了什么,请告诉我。

编辑: 粗体转义序列 \033[1m 适用于我的代码。

I've created a namespace which would store ANSI values for logging purposes.

namespace ANSI {
    static const char *BLACK_BG = "\x1B[48;2;0;0;0m";
    static const char *RED = "\x1B[38;2;255;0;0m";
    static const char *GREEN = "\x1B[38;2;0;255;0m";
    static const char *GREY = "\x1B[38;2;70;70;70m";
    static const char *EXIT = "\x1B[0m";
    static std::ostream &BOLD(std::ostream& log) { return log << "\e[1m"; }
}

// Example usage:
// std::cout << ANSI::BOLD << ANSI::GREEN << "This is bold green text" << ANSI::EXIT << "\n";

But when I try to compile using the -pedantic flag, it tells me that \e[1m is not an ISO standard escape sequence. It doesn't show the error if I remove the flag but I want to keep it at the same time.

P.S. If I'm doing something wrong with the code above, please let me know.

EDIT:
The bold escape sequence \033[1m works for my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文