ANSI colors in C and ncurses

拜拜、爱过 提交于 2019-12-18 06:13:57

问题


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.


回答1:


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.

It may work (did you try it?) but it may also stuff up the window management altogether.


And, since you stated in a comment that it didn't work then I guess the answer would be "no" :-)

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):
    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

This would basically break down the string into normal character sequences and color-change-sequences and you'd process each separately. It would require a lookup table in order to translate the sequences into the desired attron/off calls. Not pretty, but sometimes pragmatism is best.




回答2:


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.




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/4373690/ansi-colors-in-c-and-ncurses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!