7

我知道我可以使用我选择的颜色来执行attronand attroff,但是,我想知道是否可以使用 ncurses 中的 ANSI 颜色转义码来执行此操作:

#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 程序中的常规程序有效。

4

4 回答 4

3

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

如果您正在寻求一种可能的方法来允许字符串中的 ANSI 转义序列,那么一种方法(尽管它是 kludge)将是截取字符串并修改它。有一个辅助函数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-sequences,并且您将分别处理它们。

attron/off为了将序列转换为所需的调用,它将需要一个查找表(如果您需要处理带有任意参数的 ANSI 序列,则需要智能) 。

于 2010-12-07T05:34:48.737 回答
1

是的。这完全取决于侦听程序输出的软件或固件类型。对于 V3.3 MSDOS,不,除非加载设备驱动程序 ansi.sys,否则它将无法工作。

现代终端窗口往往具有ANSI x3.64语义,因此这些转义序列通常会起作用。但不要期望太多:众所周知,对超宽和超高字符的支持很差。

于 2010-12-07T05:37:27.073 回答
1

将 ANSI 集成到ncurses. 您想使用attron/off调用并可能将字符串拆分为%sand %d。对于 > 2 次转换,您需要实现自己的printw

于 2010-12-07T16:16:11.903 回答
1

2008 年邮件列表线程讨论此问题:https ://lists.gnu.org/archive/html/bug-ncurses/2008-11/msg00026.html

提出的可能性是:

于 2015-10-19T08:56:36.863 回答