Is there any way to check whether the 'current' console supports 256 colours in Python under Linux?
I specifically dont want to use curses.
Is there any way to check whether the 'current' console supports 256 colours in Python under Linux?
I specifically dont want to use curses.
As discussed in the comments, the correct solution is to use curses
.
>>> import curses
>>> curses.setupterm()
>>> curses.tigetnum("colors")
... 8
There is no portable way to detect whether a console or terminal supports 256 colors because there is no common supported interface through which to ask this question. This is exactly the same as how there is no way to query what characters a terminal will send when, say, the user presses function or arrow keys. There are standards and conventions, but fundementally both sides need to rely on the other side using the same interface.
The way curses works is that it has a large and configurable database of terminals and it looks up the features of a particular terminal in this database to know its capabilities. curses doesn't detect the type of terminal connected, it usually gets this from the TERM
environment variable which is usually setup by something that knows (or is told) what the actual terminal connected is, e.g. getty, or a ssh or telnet server negotiates it from the client side.