colorsupport
Detect how much color a terminal stream can show (none, 16, 256 or truecolor) and say
why. It follows the NO_COLOR, FORCE_COLOR, CLICOLOR and CLICOLOR_FORCE
conventions, reads TERM, COLORTERM, terminfo, CI services and Windows consoles, and
takes every input as an argument so tests are deterministic. No dependencies, fully typed.
Install
pip install colorsupport
Usage
import sys
from colorsupport import ColorLevel, level
match level(sys.stdout):
case ColorLevel.TRUECOLOR:
line = "\x1b[38;2;255;120;0mhello\x1b[0m"
case ColorLevel.ANSI256:
line = "\x1b[38;5;208mhello\x1b[0m"
case ColorLevel.BASIC:
line = "\x1b[33mhello\x1b[0m"
case ColorLevel.NONE:
line = "hello"
detect returns the level together with the reason, which is what a user asking "why no
colors?" needs to see. Every input can be passed in; anything left out is read from the
running process when the function is called.
from colorsupport import ColorLevel, detect
class Terminal:
def isatty(self) -> bool:
return True
found = detect(Terminal(), env={"TERM": "xterm-256color"}, platform="linux")
print(found)
# 256: TERM=xterm-256color
print(found.level >= ColorLevel.BASIC, found.level.colors)
# True 256
print(detect(Terminal(), env={"NO_COLOR": "1", "COLORTERM": "truecolor"}))
# none: NO_COLOR is set
print(detect(Terminal(), env={"FORCE_COLOR": "true", "COLORTERM": "truecolor"}))
# truecolor: FORCE_COLOR='true' turns color on; COLORTERM=truecolor
print(detect(Terminal(), env={}, platform="win32", vt=True, windows_build=19045))
# truecolor: Windows console with VT processing, build 19045
A program with its own --color=auto|always|never flag passes it as when. The
NO_COLOR page asks for exactly this: a flag given for one run beats the variable.
import io
from colorsupport import detect, supports_color
piped = io.StringIO()
print(supports_color(piped, env={}))
# False
print(detect(piped, env={"NO_COLOR": "1"}, when="always", platform="linux"))
# 16: the caller turned color on (when='always'); the terminal does not say how many colors
print(detect(piped, env={"FORCE_COLOR": "3"}, when="never"))
# none: the caller turned color off (when='never')
How the level is decided
Highest priority first. NO_COLOR, FORCE_COLOR, CLICOLOR_FORCE and CLICOLOR set to an
empty string count as unset, as their pages ask; the other variables (CI, TF_BUILD,
WT_SESSION, COLORTERM, the CI service names) count when present, as in supports-color.
when="never"gives none;when="always"turns color on and skips steps 2 to 5.NO_COLORgives none.FORCE_COLOR:0orfalsegives none;1,2and3give exactly 16, 256 and truecolor (larger numbers mean truecolor), even on a pipe; any other value turns color on and lets the terminal decide the depth. Surrounding spaces are ignored andfalseis read in any case.CLICOLOR_FORCEturns color on and lets the terminal decide the depth.CLICOLOR=0gives none.- Azure Pipelines (
TF_BUILDandAGENT_NAME) gives 16, even on a pipe. - A stream that is not a terminal gives none, unless color was turned on above.
TERM=dumbgives none.- On Windows, a console stream decides: ConEmu with
ConEmuANSI=ONgives 256,ANSICONgives 16, a console without virtual terminal processing gives none, Windows Terminal (WT_SESSION) gives truecolor, otherwise build 14931 and later give truecolor and 10586 and later 256. A stream that is not a console goes on to step 10. (Under mintty a native Windows Python sees pipes, so it stops at step 7 unless color is forced.) - CI: GitHub Actions, Gitea Actions and CircleCI give truecolor; Travis, AppVeyor,
GitLab, Buildkite, Drone and Codeship give 16; any other
CIgives none. TeamCity 9.1 and later gives 16. COLORTERM=truecoloror24bit,TERMof kitty, Ghostty or WezTerm give truecolor; iTerm2 3+ gives truecolor and older iTerm2 and Apple Terminal give 256.TERMending in-256or-256colorgives 256; thescreen,xterm,vt100,vt220,rxvt,ansi,cygwinandlinuxfamilies or a name withcolorgive 16. The terminfo entry forTERM(itscolorsnumber) can raise this level.- Any other
COLORTERMgives 16. Otherwise none.
When color was turned on in steps 1, 3 or 4, the result is never below 16.
Where the sources disagree
- The FORCE_COLOR page lets
FORCE_COLORwin overNO_COLOR; CPython and the CLICOLOR page letNO_COLORwin.NO_COLORwins here, andwhen="always"is the way past it. - The FORCE_COLOR page and CPython read any non-empty
FORCE_COLOR,0included, as "on".supports-colorreads0andfalseas "off", and so does this library. - The CLICOLOR page asks for
FORCE_COLORas an alias ofCLICOLOR_FORCE, color on when either is set. HereFORCE_COLORis read first, soFORCE_COLOR=0turns color off even withCLICOLOR_FORCEset. supports-colorignores aFORCE_COLORthat is neither a number nortrue/false(and2orFALSEwith it); the FORCE_COLOR page asks for any non-empty value to turn color on, which is followed, and spaces and case are forgiven.supports-colorreads the Windows build without looking at the console mode, and does not know ConEmu or Windows Terminal. Here the console mode is read (never changed); callenable_vtto turn virtual terminal processing on first.supports-colorreads--colorflags from the command line. A library should not parse its host's arguments, so that is left towhen.
Windows
import sys
from colorsupport import enable_vt, level
enable_vt(sys.stdout) # True when stdout is a console that now reads escape sequences
enable_vt sets ENABLE_VIRTUAL_TERMINAL_PROCESSING on the console behind the stream.
It returns False and does nothing for pipes, files, other terminals and other systems.
terminfo
terminfo_colors(term, env) reads the colors number of a compiled terminfo entry
straight from the database, in the order ncurses searches it (TERMINFO, ~/.terminfo,
TERMINFO_DIRS, then the usual system folders, which are skipped on Windows), in both the
legacy and the 32-bit format. Only regular files are read, and names that could leave the
database folder are refused. It does not use curses, whose setupterm can only run once
per process.
Command line
$ colorsupport
truecolor: COLORTERM=truecolor
$ colorsupport --json
{"level": "truecolor", "colors": 16777216, "reason": "COLORTERM=truecolor"}
$ colorsupport --check 256 && echo "256 colors or more"
$ NO_COLOR=1 python -m colorsupport --stderr
none: NO_COLOR is set
--stderr looks at stderr instead of stdout, --when passes a --color choice, and
--check LEVEL prints nothing and exits 0 when the level is at least LEVEL (none,
16, 256 or truecolor), 1 otherwise.
API
| Name | What it does |
|---|---|
detect(stream=None, *, env, when, platform, windows_build, vt, terminfo) |
The Detection for stream |
level(...) |
The ColorLevel only, same arguments |
supports_color(...) |
Whether any color can be shown, same arguments |
ColorLevel |
NONE, BASIC, ANSI256, TRUECOLOR; .colors, .label, ColorLevel.parse |
Detection |
Frozen level and reason; str() gives "256: TERM=xterm-256color" |
enable_vt(stream=None) |
Turn on escape sequence processing for a Windows console |
terminfo_colors(term, env=None) |
The colors number of a terminfo entry, or None |
Why
Terminal programs keep rewriting the same check and getting the conventions subtly
wrong: an empty NO_COLOR turning color off, FORCE_COLOR=0 turning it on, a legacy
Windows console printing raw escape codes. JavaScript has supports-color; Python's own
check (_colorize, 3.13+) is private and answers only yes or no. This library gives the
depth and the reason, follows the published conventions and writes down where they differ.
Sources
The rules come from no-color.org, force-color.org, the CLICOLOR page, the
supports-color package, CPython's _colorize, anstyle-query, termstandard/colors,
the Microsoft console documentation, ConEmu's documentation and ncurses term(5) and
terminfo(5).
tests/upstream/MANIFEST.json pins each one; python scripts/fetch_upstream.py fetches
them so the tests can check the rules against them.
License
MIT. See LICENSE.
Links
- Website: https://lianes8studio.com
- GitHub: https://github.com/JaviLianes8
- LinkedIn: https://www.linkedin.com/in/jlianes/
Metadata
Release files for colorsupport 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| colorsupport-0.1.0.tar.gz | 31.9 kB | Without provenance | Details for colorsupport-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| colorsupport-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for colorsupport-0.1.0-py3-none-any.whl |
Total release size: 49.1 kB
Release files / colorsupport-0.1.0.tar.gz
| Download URL | colorsupport-0.1.0.tar.gz |
|---|---|
| Size | 31.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e5cffe8abac83b284149bb94988a2684782368a4dca555a0274cf9d763d292df
|
|
BLAKE2b-256 checksum How to use checksums |
936251e35058d7c550906d430b7b69e949fd4ebdb89fde65a8150bab559ac116
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / colorsupport-0.1.0-py3-none-any.whl
| Download URL | colorsupport-0.1.0-py3-none-any.whl |
|---|---|
| Size | 17.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9c0a1f71544cde45f7a181695f36a209009fa918a3e4ea99f5766e6c150c7d37
|
|
BLAKE2b-256 checksum How to use checksums |
d67a43226673773f7fc8a7b380c1329bd27ee5acb71bf6b31113efac7dfbdba8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Security for colorsupport 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.