-1

I just switched back to Linux after a few years of Windows use and am wondering if ANSI Escape Sequences are okay for coloring text. I ask because the way I did it on windows isn't capable of crossing platforms because of the use of windows.h.

Someone had mentioned NCurses or something but didn't explain it to me. Would this be a good alternative? Is there any negative aspects of NCurses or ANSI esc? Sorry if this question is dumb, I'm still a newbie.

This was the code I used for colored text on windows.

int setColor(const int foreground, const int background)
{
    int Color = foreground + (background * 16);
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, Color);

    return 0;
}

Thanks, xChubz

2
  • 1
    Do you mean ANSI escape sesquences?
    – outis
    May 29, 2015 at 3:47
  • WOW! Face-palmed so hard to this! Ya, that's what I meant. Thanks for pointing that out to me haha!
    – TDubs95
    May 29, 2015 at 3:50

1 Answer 1

5

ANSI escape sequences aren't supported by all terminals; the most common case you'll come across is a remote session that doesn't support color, where the terminal emulatior will attempt to display the color escape sequences. You definitely should be using a terminal/textual user interface (TUI) library (you may also see them called "console user interface" (CUI) libraries). Ncurses (or curses, its predecessor) is one such, but isn't the only one; newt is another. There are also terminal capabilities libraries, such as termcap and terminfo (the latter has generally supplanted the former), which contain terminal control sequences. Ncurses (and other TUI libs) use terminfo.

TUI libs provide not only cross-terminal portability, but cross-platform, as some libs (including ncurses) have been ported to Windows.

The trade-off is a certain type of complexity, but you'll also be getting rid of a different (and much less manageable) complexity (dealing with different terminal capabilities).

1
  • Thanks for the quick reply, outis! Your post was a great help. I will do some research on TUI libraries today and see which library matches what I'm looking for. From the little bit I found last night, these will help me the most with my little project.
    – TDubs95
    May 29, 2015 at 9:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.