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 CLICOLOR
and CLICOLOR_FORCE
(which are currently
already used for this exact reason on some UNIX systems). When set, the following rules
should apply:
CLICOLOR != 0
- ANSI colors are supported and should be used when the program isn’t piped.
CLICOLOR == 0
- Don’t output ANSI color escape codes.
CLICOLOR_FORCE != 0
- ANSI colors should be enabled no matter what.
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:
import os, sys
def has_colors():
if ((os.getenv("CLICOLOR", "1") != "0" and sys.stdout.isatty()) or
os.getenv("CLICOLOR_FORCE", "0") != "0"):
return True
else:
return False
Supported Colors
To get an idea what colors you can use, run the following Python script:
RESET = "\x1b[0m"
print("To reset attributes: \\x1b[0m\n")
for i in range(0, 8):
print("\x1b[1;3{0}m\\x1b[1;3{0}m{1} \x1b[0;3{0}m\\x1b[0;3{0}m{1} "
"\x1b[1;4{0};3{0}m\\x1b[1;3{0};4{0}m{1}".format(i, RESET))
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
:
- clang compiler
- GCC compiler
- IntelliJ
- Jenkins ANSI color plugin
- Rust compiler
- supports-color package
- phpunit
- GitLab CI
The following bug reports have been resolved: