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_COLORset- Don’t output ANSI color escape codes, see no-color.org
CLICOLOR_FORCEset, butNO_COLORunset- ANSI colors should be enabled no matter what
CLICOLORset,NO_COLORandCLICOLOR_FORCEunset- 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
CLICOLORis 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:
import os, sys
def has_colors():
if os.environ.get('NO_COLOR'):
return False
elif os.environ.get('CLICOLOR_FORCE'):
return True
return sys.stdout.isatty()Python code example with CLICOLOR for auto-detection:
import os, sys
def has_colors():
if os.environ.get('NO_COLOR'):
return False
elif os.environ.get('CLICOLOR_FORCE'):
return True
elif os.environ.get('CLICOLOR'):
return sys.stdout.isatty()
else
return FalseAlso consider letting the user override these with command-line options like --color and --color=WHEN where WHEN would be one of:
--coloror--color=alwaysor--color=on: likeCLICOLOR_FORCE--color=neveror--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:
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_FORCE:
- clang compiler
- GCC compiler
- IntelliJ
- Jenkins ANSI color plugin
- Rust compiler
- supports-color package
- phpunit
- GitLab CI
The following bug reports have been resolved: