C 和 ncurses 中的 ANSI 颜色
我知道我可以使用我选择的颜色执行 attron
和 attroff
,但是,我想知道是否可以使用 ANSI 颜色转义代码来执行此操作ncurses:
#include <stdio.h>
#include <ncurses.h>
int main()
{
initscr();
char *s2 = NULL;
const char *s1 = "World";
int n = 10;
// What would be a good way to colour %d?
// seems it is not safe to us the ANSI color escape in here...
s2 = malloc (snprintf (NULL, 0, "Hello %s \033[22;31m%d", s1, n) + 2);
sprintf (s2, "Hello %s \033[22;31m%d", s1, n);
printw("%s", s2);
refresh();
getch();
endwin();
return 0;
}
与 -lncurses
链接常规 printf("\033[22;31mHello, World!\n");
有效。
在非 ncurses 程序中
I know I can do the attron
and attroff
with the color I choose to, however, I would like to know if it's possible to do it with the ANSI colour escape codes within ncurses:
#include <stdio.h>
#include <ncurses.h>
int main()
{
initscr();
char *s2 = NULL;
const char *s1 = "World";
int n = 10;
// What would be a good way to colour %d?
// seems it is not safe to us the ANSI color escape in here...
s2 = malloc (snprintf (NULL, 0, "Hello %s \033[22;31m%d", s1, n) + 2);
sprintf (s2, "Hello %s \033[22;31m%d", s1, n);
printw("%s", s2);
refresh();
getch();
endwin();
return 0;
}
Linking with -lncurses
a regular printf("\033[22;31mHello, World!\n");
in a non-ncurses program works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你可能误入了危险的领域。 Curses 几乎肯定会根据输出字符跟踪字符位置,并且由于它提供自己的颜色处理,因此它可能也不会检测 ANSI 转义序列。
如果您正在寻求一种允许字符串中使用 ANSI 转义序列的可能方法,那么一种方法(尽管是拼凑)就是截取字符串并修改它。有一个像
myPrintW()
这样的辅助函数,它接受一个字符串并将其分解,类似于(伪代码):这基本上会将字符串分解为正常的字符序列和 ansi 序列,然后你' d 分别处理每个。
它需要一个查找表(如果您需要处理具有任意参数的 ANSI 序列,则需要一个查找表),以便将序列转换为所需的 attron/off 调用。
I think you're probably straying into dangerous territory there. Curses will almost certainly track character positions based on output characters and, since it provides its own colour handling, it probably won't detect ANSI escape sequences as well.
If you're after a possible way to allow the ANSI escape sequences in your strings, then one way (kludge though it is) would be to intercept the string and modify it. Have a helper function like
myPrintW()
which takes a string and breaks it down, something like (pseudo-code):This would basically break down the string into normal character sequences and ansi-sequences and you'd process each separately.
It would require a lookup table (or smarts if you need to handle ANSI sequences with arbitrary parameters) in order to translate the sequences into the desired
attron/off
calls.是的。这完全取决于哪种软件或固件正在监听程序的输出。对于V3.3 MSDOS,不,除非加载设备驱动程序ansi.sys,否则它不会工作。
现代终端窗口往往具有 ANSI x3.64 语义,因此这些转义序列通常会起作用。但不要期望太多:众所周知,超宽和超高字符的支持很差。
Yes. It all depends on what kind of software or firmware is listening to the program's output. For V3.3 MSDOS, no, it won't work unless the device driver ansi.sys is loaded.
Modern terminal windows tend to have ANSI x3.64 semantics, so those escape sequences will often work. But don't expect too much: extra wide and extra high characters are notoriously poorly supported.
在
ncurses
上集成 ANSI 不太安全。您想要使用attron/off
调用,并且可能将字符串拆分为%s
和%d
。对于> 2个转换,需要自己实现printw
It won't be too safe integrating ANSI on
ncurses
. You want to useattron/off
calls and perhaps split the string into%s
and%d
. For > 2 conversions, you need to implement your ownprintw
2008 年邮件列表线程对此进行了讨论:https://lists。 gnu.org/archive/html/bug-ncurses/2008-11/msg00026.html
提出的可能性是:
为 ANSI 转义码创建一个解析器(源代码中的 WONTFIX)。 Mutt 和 Screen 实现了这个: https://lists.gnu .org/archive/html/bug-ncurses/2008-11/msg00028.html
创建 terminfo 条目:https://lists.gnu.org/archive/html/bug-ncurses/2008-11/msg00029.html
2008 mailing list thread discussing this: https://lists.gnu.org/archive/html/bug-ncurses/2008-11/msg00026.html
The possibilities raised were to:
create a parser for ANSI escape codes (WONTFIX in source). Mutt and Screen implement this: https://lists.gnu.org/archive/html/bug-ncurses/2008-11/msg00028.html
create a terminfo entry: https://lists.gnu.org/archive/html/bug-ncurses/2008-11/msg00029.html