worenga
worenga

Reputation: 5856

How to check whether std::cout support colors?

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?

Upvotes: 4

Views: 3389

Answers (2)

Thomas Dickey
Thomas Dickey

Reputation: 54505

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:

Upvotes: 4

Cheiron
Cheiron

Reputation: 3736

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.

Upvotes: 1

Related Questions