如何创建彩色文本?
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
转义字符的 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.