Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether std::cout support colors?

Tags:

c++

terminal

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?

like image 539
worenga Avatar asked Oct 12 '25 14:10

worenga


1 Answers

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:

  • conio.h is missing from Windows
  • conio.h file missing error [closed]
  • Borland-style CONIO implementation for MinGW/Dev-C++
  • Linux c++ implementation of conio.h
like image 53
Thomas Dickey Avatar answered Oct 14 '25 07:10

Thomas Dickey