ANSI color in C and ncurses

I know that I can make attron and attroff with the selected color, however I would like to know if this can be done using ANSI color escape codes in 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; } 

Communication with -lncurses

regular printf("\033[22;31mHello, World!\n"); works printf("\033[22;31mHello, World!\n"); in non-ncurses program.

+4
source share
4 answers

I think you probably got lost in a dangerous territory. Curses will almost certainly track the position of characters based on the output characters, and since it provides its own color processing, it will probably also not detect ANSI escape sequences.

This may work (have you tried it?), But it can also fill the window controls in general.


And, since you stated in a comment that this did not work, I think the answer will be no :-)

If you after a possible way to allow ANSI escape sequences in your lines, then one of the methods (kludge, although it exists) is to intercept the line and change it. Have a helper function like myPrintW() that takes a string and breaks it, something like (pseudocode):

 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 will basically break the string into regular character sequences and color changing sequences, and you process them separately. Converting sequences to the required attron/off calls will require a lookup table. Not beautiful, but sometimes pragmatism is best.

0
source

Yes. It all depends on what software or firmware listens for the program output. There is no MSDOS for V3.3, this will not work if the ansi.sys device driver is not loaded.

Modern window windows have ANSI x3.64 semantics, so these escape sequences will often work. But don't expect too much: very wide and extra tall characters are notoriously poorly supported.

0
source

It is not too safe to integrate ANSI on ncurses . You want to use the attron/off calls and possibly split the string into %s and %d . For> 2 conversions you need to implement your own printw

0
source

All Articles