I'm looking for a way to reliably test if the output (stdout, stderr) terminal is ANSI (or any) color capable.
Because most terminals today, even on Windows, does support at least ANSI colors, all the answers I have found, are either outdated or wrong. (I already know how easy it is to print color test patterns and check for Unicode/UTF-8 capability.)
I have seen partial tries and variations of this:
It should be noted that this is quite easy to do for a *nix based environment, as they almost always have the TERM
variable set to an xterm
-type. But was hoping to also find some Windows environment variable for this.
Q: How can I have my python3 code to check if the output is using a color capable terminal/console?
The answer need to be cross-platform compatible, being able to check this on windows, for example.
UPDATE:
Apparently this is not an easy problem! It seem to depend on many factors, like:
- What is your Windows OS version
- What is your PowersHell version
- What is your "terminal/console" capabilities? (poweshell, mintty, cmd are all very diiferent)
- What are the fonts used in those "consoles"?
- What character sets are you using? (ASCII, UTF-8/16, etc.)