-2

Is there any way to check whether the 'current' console supports 256 colours in Python under Linux?

I specifically dont want to use curses.

Charles
  • 50,943
  • 13
  • 104
  • 142
funk
  • 1
  • 1
  • 7
  • 2
    Why don't you want to use `curses`? – Katriel Dec 13 '11 at 20:22
  • dont want a whole bunch of stuff in my module, just want what is absolutely needed. – funk Dec 13 '11 at 20:25
  • Do you need this to be multi-platform, or Linux only? – jsbueno Dec 13 '11 at 20:26
  • 2
    Well, curses is standard, and it's usually curses that brings along the `terminfo` database about terminal capabilities. Why use something else that probably either still uses curses under the hood, or depends on curses for the data files? – Has QUIT--Anony-Mousse Dec 13 '11 at 20:36
  • the question is about *not* using curses :) not curses under the hood or over the hood. NO CURSES! (sorry, no curses everyone) (thanks for the downvote anonymouse, pretty rubbish reason to downvote just because i dont use the library *you* want - when the question specifically states i dont want it.) – funk Dec 13 '11 at 20:41
  • 2
    @Anonymouse is right. As the terminfo / termcap database on most linux systems is supplied with ncurses, to use something else instead involves compiling your own data on every terminal that you want to support. As you only need on piece of information you could just maintain a dict: `terminal_colors = { 'vt52': 2, 'vt100': 2, 'vt320': 2 }` – CB Bailey Dec 13 '11 at 20:41
  • i will probably only support one or two so thats not a problem *if* thats the case with detecting 256 colour support. my question is about *detecting* whether the running console supports 256 colours, seeing as consoles can be built without 256 colour support it would seem useless keeping a dict of values about them as they could be wrong. – funk Dec 13 '11 at 20:44
  • 5
    -1 This is like asking `I have a nail which I need to put into a block of wood. But I don't want to use a hammer because I only own the tools that I absolutely need. NO HAMMERS!` – Katriel Dec 13 '11 at 20:50
  • 4
    (If you had a good reason not to use `curses` that would be another matter and we could help you work around that. But "I don't wanna" isn't a good reason.) – Katriel Dec 13 '11 at 20:52
  • 1
    @beginpython: there is no way to query most consoles for what they support. The way it works is you configure what type of console is attached to which serial port and the OS usually tells the application what this setting is via the TERM environment variable and the application either knows or looks up in a database what the supported features are. I'm not sure what you mean by built without 256 colour support, either they have the feature or not and you just have to hope that consoles with different features are identified with different names otherwise you are out of luck in any case. – CB Bailey Dec 13 '11 at 20:52
  • -10,000 - that is a backward analogy. seriously, im sorry to infringe on your religion like, but all im after is finding out if the console supports 256 colours, doesnt seem like i need an entire library for that. – funk Dec 13 '11 at 20:53
  • @beginpython: This isn't a 'religious' issue. Importing an extra library -- especially given that it comes in the stdlib anyway -- is trivial. You do it all the time: `sys`, `itertools`, `collections`, whatever. There's no penalty for importing another library. Being minimal in your coding is good; reinventing the wheel because you don't want to use the library that has it is taking minimality way to extremes. – Katriel Dec 13 '11 at 20:55
  • @Charles - thanks for your help. xterm has a compilation option to build with/out 256 support, i assume this may be the case for other terms n all. as i understand it tput interfaces onto curses anyway - but when i `tput colors` on eg gnome-terminal i get 8 until i go TERM=xterm-256colors or somesuch (as a side note) – funk Dec 13 '11 at 20:55
  • @katirelalex, look matey mate bloke, i just dont want to use it - it says it in the question, please respect that. thanks. – funk Dec 13 '11 at 20:56
  • @Charles - is that by importing termios ? scuse my ignorance. – funk Dec 13 '11 at 20:59
  • @beginpython: I'm sorry I don't understand your question. – CB Bailey Dec 13 '11 at 21:02
  • ahh forget it - ill go back to google. thanks for being the only one that seriously gave any info instead of getting priest like on me. – funk Dec 13 '11 at 21:16
  • 1
    `ioctl` and `TIOCGWINSZ` ought to be giving the size of the current "window" in columns and lines, not the number of colors supported. There's no way to query a terminal directly for most of its cababilities, you have to "know" and the terminfo (or the older termcap) database is the usual way to "know". – CB Bailey Dec 13 '11 at 21:33
  • 1
    If you want a serious answer, you need to explain *why* you don't want to use curses. – jterrace Dec 13 '11 at 21:34
  • nope. that doesnt contain any useful information pertaining to this question. this is not a matter of discussion whether its possible, its a simple matter of *how* it is possible. after all curses must be doing it without curses right :) – funk Dec 13 '11 at 21:35
  • 2
    It's possible, but you have to either have to query curses database (isn't this using curses?) or replicate what curses does including an equivalent to its database and isn't this effectively using curses too? – CB Bailey Dec 13 '11 at 21:37
  • @jterrace why is answer precluded by an explanation of why ? just simply because i dont want to? i want to do as much myself as possible, sort of a beginner project type thing. – funk Dec 13 '11 at 21:38
  • @Charles i dunno charles actually. termios... hmmm ill update the question. – funk Dec 13 '11 at 21:38
  • termios is all about lines speeds, break signals 7-bit/8-bit support. It never gets as high level as things like control codes, clear screen, bold, color, etc. – CB Bailey Dec 13 '11 at 21:40
  • @charles - that example was just to show that querying the console without curses was possible. although im sure youre right in this case. there *is always* a way round... oh look youre in lingfield ! i used to live in forest row not long ago... – funk Dec 13 '11 at 21:42
  • A way round what? If you link up vt320 to a serial port, it is what it is. There is no interface to ask it what it supports, you have to read the programmers manual. In the bad old days people wrote computer programs to support the consoles they had. Having a database of capabilities (e.g. termcap) meant that you could abstract your program from depending on one particular model of terminal and, basically, you have to do something equivalent. – CB Bailey Dec 13 '11 at 21:45
  • well, if the terminal could be built without 256 colour support, and curses detects this, it would maybe suggest curses has some way of detecting it ? ergo - its possible to do. im guessing here obv. – funk Dec 13 '11 at 21:48
  • 1
    Curses doesn't detect this, it merely parrots back the capability of the terminal that you've configured it to think that you are using. – CB Bailey Dec 13 '11 at 21:50
  • ah. hmmm. well if curses doesnt detect it either why are people telling me to use it !???!? its completely useless to my goal if it doesnt detect it for sure. – funk Dec 13 '11 at 21:57
  • Please refrain from vandalizing your own question. – Charles Dec 13 '11 at 22:38
  • This question answers how to do it in a bash script. https://stackoverflow.com/questions/2465425/how-do-i-determine-if-a-terminal-is-color-capable using subprocess check_output you can do bash work in python as well. Hope that helps – Samie Bencherif May 22 '19 at 18:34

2 Answers2

12

As discussed in the comments, the correct solution is to use curses.

>>> import curses
>>> curses.setupterm()
>>> curses.tigetnum("colors")
... 8
Katriel
  • 120,462
  • 19
  • 136
  • 170
4

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.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • what about non-portable ;) - appreciate your help charles. thankyou. i still feel there is a way to do this somehow.... i will post back when/if i give up... – funk Dec 13 '11 at 22:26
  • @TedSchmuck: I really don't understand where your trying to get to. Presumably, it's theoretically possible to restrict your self to supporting consoles that have an interface that you could query for color support but I've never heard of such an interface so it's probably less restrictive to just assume that you have 256 color support. Of course, your next problem would immediately be to determine what escape sequences you need to manipulate colors. – CB Bailey Dec 13 '11 at 22:34
  • 1
    oh ive done that bit already. colours movement and animations all done, just trying to figure out how to implement it... i 'code as i go' - which means in laymans terms i dont have a clue. – funk Dec 13 '11 at 22:36
  • Just because two consoles support 256 colours doesn't mean that they are going to use the same escape codes. If you're not going to _assume_ 256 colour support, then it doesn't make sense to assume that one particular set of escape codes works for all 256 colour consoles. – CB Bailey Dec 13 '11 at 22:43