C 和 ncurses 中的 ANSI 颜色

发布于 2024-10-06 08:47:34 字数 708 浏览 6 评论 0原文

我知道我可以使用我选择的颜色执行 attronattroff ,但是,我想知道是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故人的歌 2024-10-13 08:47:34

我认为你可能误入了危险的领域。 Curses 几乎肯定会根据输出字符跟踪字符位置,并且由于它提供自己的颜色处理,因此它可能也不会检测 ANSI 转义序列。

如果您正在寻求一种允许字符串中使用 ANSI 转义序列的可能方法,那么一种方法(尽管是拼凑)就是截取字符串并修改它。有一个像 myPrintW() 这样的辅助函数,它接受一个字符串并将其分解,类似于(伪代码):

def myPrintW(s as copy):
    while s not empty:
        p = position of first ansi-sequence in s
        if p == NULL exit while
        printw first p characters of s
        remove the first p characters from s

        decode ansi-sequence at start of s
        issue relevant attron/off for that ansi-sequence
        remove ansi-sequence from start of s

    endwhile
    output s though it may be empty
enddef

这基本上会将字符串分解为正常的字符序列和 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):

def myPrintW(s as copy):
    while s not empty:
        p = position of first ansi-sequence in s
        if p == NULL exit while
        printw first p characters of s
        remove the first p characters from s

        decode ansi-sequence at start of s
        issue relevant attron/off for that ansi-sequence
        remove ansi-sequence from start of s

    endwhile
    output s though it may be empty
enddef

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.

萌能量女王 2024-10-13 08:47:34

是的。这完全取决于哪种软件或固件正在监听程序的输出。对于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.

痴者 2024-10-13 08:47:34

ncurses 上集成 ANSI 不太安全。您想要使用 attron/off 调用,并且可能将字符串拆分为 %s%d。对于> 2个转换,需要自己实现printw

It won't be too safe integrating ANSI on ncurses. You want to use attron/off calls and perhaps split the string into %s and %d. For > 2 conversions, you need to implement your own printw

酒绊 2024-10-13 08:47:34

2008 年邮件列表线程对此进行了讨论:https://lists。 gnu.org/archive/html/bug-ncurses/2008-11/msg00026.html

提出的可能性是:

2008 mailing list thread discussing this: https://lists.gnu.org/archive/html/bug-ncurses/2008-11/msg00026.html

The possibilities raised were to:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文