如何创建彩色文本?

发布于 2025-01-02 23:50:15 字数 307 浏览 1 评论 0原文

在 C++ 中,以下代码在控制台中运行时将以彩色打印文本:

cout << "\e[32;40mGreenForegroundAndBlackBackgroundText" << endl;

在 DI 中出现错误:

string s = "\e[32;40mGreenForegroundAndBlackBackgroundText";  // undefined escape sequence \e

有什么方法可以使其在 D 中工作吗?

In C++ the following code when run in the console will print the text in colour:

cout << "\e[32;40mGreenForegroundAndBlackBackgroundText" << endl;

In D I get an error:

string s = "\e[32;40mGreenForegroundAndBlackBackgroundText";  // undefined escape sequence \e

Is there any way to get this working in D?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲性难收 2025-01-09 23:50:15

转义字符的 C++ 常量字符串转义 \e 是 C 的非标准 GCC 扩展,用于 字符转义(可能也被 Clang 采用)。

您只需将其八进制编码设置为 \033\x1b

但要小心 \e[32;40m 不是标准 C 或 C++,它是与 tty-s 相关的 ANSI 终端转义序列。

The C++ constant string escape \e for the escape character is a non-standard GCC extension of C for character escapes (also adopted by Clang, probably).

You just need to put the octal encoding of it perhaps as \033 or \x1b

Be careful however that \e[32;40m is not standard C or C++, it is the ANSI terminal escape sequence related to tty-s.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文