How to use ANSI escape codes inside mvwprintw in n

2019-02-17 23:09发布

问题:

Is there a way to use ANSI escape codes inside mvwprintw?

mvwprintw(window, 0, 0,"%c[%dmCOLORED_TEXT!\n", 0x1B, 32);//doesn't work

even though:

printf("%c[%dmCOLORED_TEXT\n", 0x1B, 32); //works

This would be for cases where using wattron/wattroff is not convenient; for example, when redirecting output from stdout of a process that outputs such escape codes.

回答1:

No. The only way to make that work would be to parse the string yourself, turning escape codes back into the appropriate curses commands, to issue along with your output.

One thing you should realize is that those codes, although widely implemented, are not universal. One of the major purposes of curses is to translate its standard commands into series of terminal-specific control codes. So, passing through codes that may or may not correspond to the current terminal type doesn't really fit the curses model. Even more fundamentally, the codes would change the terminal state in a way that curses wouldn't be able to keep track of, so that the contents of its window structures no longer matched what was on screen.