ANSI C和ncurses中的颜色

ANSI C和ncurses中的颜色,c,colors,ncurses,C,Colors,Ncurses,我知道我可以使用我选择的颜色来执行attron和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 c

我知道我可以使用我选择的颜色来执行
attron
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;
}
#包括
#包括
int main()
{
initscr();
char*s2=NULL;
const char*s1=“世界”;
int n=10;
//给%d上色的好方法是什么?
//看来在这里逃跑对我们来说不安全。。。
s2=malloc(snprintf(NULL,0,“Hello%s\033[22;310m%d”,s1,n)+2);
sprintf(s2,“你好%s\033[22;3100万%d”,s1,n);
printw(“%s”,s2);
刷新();
getch();
endwin();
返回0;
}
链接到
-lncurses


常规的
printf(“\033[22;31mHello,World!\n”)
在非ncurses程序中工作。

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

它可能会起作用(你试过吗?),但它也可能会把窗口管理搞得一团糟


而且,既然你在评论中说它不起作用,那么我猜答案应该是“不”:-)

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


这将基本上将字符串分解为普通字符序列和颜色变化序列,然后分别处理。这需要一个查找表,以便将序列转换为所需的
attron/off
调用。这并不漂亮,但有时实用主义是最好的。

是的。这一切都取决于哪种软命令软件或固件正在侦听程序的输出。对于V3.3 MSDOS,不,除非加载设备驱动程序ansi.sys,否则它将无法工作


现代终端窗口往往具有语义,因此这些转义序列通常可以工作。但不要期望太多:超宽和超高字符的支持非常差。

集成ANSI on
ncurses
。您希望使用
attron/off
调用,并可能将字符串拆分为
>%s
%d
。对于>2次转换,您需要实现自己的
printw
2008邮件列表线程,讨论以下内容:

提出的可能性是:

  • 为ANSI转义码(源代码中的WONTFIX)创建解析器。Mutt和Screen实现以下功能:

  • 创建terminfo条目:


是的,我尝试过,但没有成功。我想知道,做上述工作的好方法是什么(我更新了示例代码)我喜欢这种方法,但是,我想知道查找表是否必要?如果你能找到将ANSI序列转换为颜色对的公式,那会更好。但是,因为我不知道确切的颜色对是什么(a,b)将在一般实现上执行,使用查找可能更安全。
def myPrintW(s):
    while s not end of string:
        s2 = position of color-change-sequence in s
        if s2 == NULL exit while
        printw characters from s (inclusive) to s2 (exclusive)
        decode color-change-sequence at s2 and issue relevant attron/off
        s = s2 + length of color-change-sequence
    endwhile
enddef