ANSI 颜色代码在 C 中随机弄乱了我的输出

问题描述 投票:0回答:0

我正在使用 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 代码乱七八糟,我们怎么可能修复它?

c colors printf ansi-escape
© www.soinside.com 2019 - 2024. All rights reserved.