gpt4 book ai didi

linux - 如何创建彩色文本?

转载 作者:IT王子 更新时间:2023-10-29 01:14:26 26 4
gpt4 key购买 nike

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

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

在 D 中我得到一个错误:

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

有什么方法可以让它在 D 中工作吗?

最佳答案

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

你只需要将它的八进制编码为 \033\x1b

但是请注意 \e[32;40m 不是标准的 C 或 C++,它是与 tty 相关的 ANSI 终端转义序列-s.

关于linux - 如何创建彩色文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9148612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com