3

I am aware that you can output colors using special escape sequences, if the terminal supports this. For details I refer to this question.

However, I understand that there are some terminals that do not support color codes/those escape sequences. How do I determine programatically in my C++ programm whether a terminal supports this or not? Are there any platform-independent libraries for this?

Community
  • 1
  • 1
worenga
  • 5,776
  • 2
  • 28
  • 50
  • 1
    _"Are there any platform-independent libraries for this?"_ If so, I could imagine that [`ncurses`](http://www.gnu.org/software/ncurses/) might be capable of such feature. – πάντα ῥεῖ Jun 07 '15 at 11:45

2 Answers2

4

There is no portable way to determine if a given device supports colors. There are several programming interfaces (some portable, some not) which attempt to determine this information, but rely upon being configured properly. For instance, curses (including ncurses) relies upon your setting TERM correctly to tell the library what the terminal can do. A few terminals have the capability of providing this information, but that does not help with portability.

Noting comments: PDCurses is not a "port" but a separate program. ncurses has a workable port to Windows, for what it's worth.

In any case, you are not going to find a portable program which is supported, which can do what was asked. Long ago, printing escape sequences using ansi.sys was the way to go with Windows, but it has been unsupported for more than ten years.

Likewise, long ago there was conio.h, also unsupported. If you find a working version for Windows which supports color, it probably uses the Windows console api (which is not portable, but you may find comparable conio.h implementations on other platforms). There are a few unsupported implementations of conio.h to consider. Since that niche appears to be what was requested, here are a few relevant links:

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
1

The library Ncurses seems to be able to do it: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/color.html

But this does not work for the Windows CMD, you will need something like Cygwin for that.

Cheiron
  • 3,620
  • 4
  • 32
  • 63
  • _"But this does not work for Windows."_ Can you elaborate about this please? I can't see any evidence. – πάντα ῥεῖ Jun 07 '15 at 11:47
  • Whoops, it does not work in Windows' CMD. You need something like Cygwin, Windows itself is indeed not the problem. – Cheiron Jun 07 '15 at 11:51
  • there seems to be a port PDCurses, however i am rather looking for a small fire and forget solution. ncurses does way more than i need – worenga Jun 07 '15 at 11:51