我正在使用 ANSI 代码为终端内的文本着色,它工作得很好,除了在循环中,
printf
随机显示 ANSI 代码本身:
#define RED "\x1B[31m"
#define BLU "\x1B[34m"
#define RST "\x1B[0m"
for
{
printf(RED "red text\n" BLU "blue text" RST);
}
我随机得到:
red text // text displayed in red
[34m blue text // still in red
或:
red text //text displayed in red
blue text // text displayed in blue
我试着用两个不同的
printf
分开颜色声明和文本,我尝试使用puts
但是没用。我不使用 ncurse
库,因为我认为它不适合这种情况。顺便说一句,如果有帮助的话,我正在 MACOS 10.12 上使用 CLion。我找到了一些解决方案,例如 Charles Campbell 的 ANSI escape code 但不适用于 C.
为什么 ANSI 代码乱七八糟,我们怎么可能修复它?