Standard for ANSI Colors in Terminals
The current situation to get colored output from most console commands is a
mess.
Some check for the TERM
environment variable, or look for ANSICON
on
Windows, or need to be passed a parameter like --color
, --ansi
or
-fdiagnostics-color
.
That’s why this page tries to create some sort of “standard” which programs should comply to no matter on which platform they are run.
The idea is to have the environment variables NO_COLOR
, CLICOLOR_FORCE
, and CLICOLOR
(which are currently already used for this exact reason on some UNIX systems).
When set, the following rules should apply:
NO_COLOR
set- Don’t output ANSI color escape codes, see no-color.org
CLICOLOR_FORCE
set, butNO_COLOR
unset- ANSI colors should be enabled no matter what
CLICOLOR
set,NO_COLOR
andCLICOLOR_FORCE
unset- ANSI colors are supported and should be used when the program is writing to a terminal
- none of the above environment variables are set
- ANSI colors are not explicitly requested
- Older programs newly gaining colors may prefer to disable them by default
- New programs may prefer to operate as if
CLICOLOR
is set
If you have ideas or comments please create a new issue on GitHub or edit this page.
How to Implement
If you want to check in your program if ANSI colors are supported, it should look like this Python code example:
Python code example with CLICOLOR
for auto-detection:
Also consider letting the user override these with command-line options like --color
and --color=WHEN
where WHEN
would be one of:
--color
or--color=always
or--color=on
: likeCLICOLOR_FORCE
--color=never
or--color=no
: likeNO_COLOR
, a good default for software newly gaining colors--color=auto
: likeCLICOLOR
, a good default for newer software
Supported Colors
To get an idea what colors you can use, run the following Python script:
Which should print:
To reset attributes: \x1b[0m \x1b[1;30m \x1b[0;30m \x1b[1;30;40m \x1b[1;31m \x1b[0;31m \x1b[1;31;41m \x1b[1;32m \x1b[0;32m \x1b[1;32;42m \x1b[1;33m \x1b[0;33m \x1b[1;33;43m \x1b[1;34m \x1b[0;34m \x1b[1;34;44m \x1b[1;35m \x1b[0;35m \x1b[1;35;45m \x1b[1;36m \x1b[0;36m \x1b[1;36;46m \x1b[1;37m \x1b[0;37m \x1b[1;37;47m
Also see the ISO 6429 (ANSI) color sequences.
Windows
Windows 10 supports ANSI colors since v1511.
For earlier versions of Windows installing ANSICON will
enable ANSI colors in cmd.exe
.
Bug Reports
This is a list of bug reports on the progress of supporting CLICOLOR_FORCE
:
- clang compiler
- GCC compiler
- IntelliJ
- Jenkins ANSI color plugin
- Rust compiler
- supports-color package
- phpunit
- GitLab CI
- cppcheck
The following bug reports have been resolved: