NO_COLOR Standard - Respecting User Color Preferences
Implement the NO_COLOR standard in your CLI tools to respect user preferences for uncolored output. Best practices for conditional ANSI color usage across languages.
Detailed Explanation
The NO_COLOR Standard
The NO_COLOR environment variable is a widely adopted convention that signals CLI tools to suppress colored output. By checking for this variable, your tools respect user preferences and accessibility needs.
The Standard
From no-color.org:
Command-line software which outputs text with ANSI color added should check for the presence of a
NO_COLORenvironment variable that, when present (regardless of its value), prevents ANSI color. Checking forNO_COLOR=(empty string) is sufficient.
Implementation in Different Languages
Bash:
if [ -n "$NO_COLOR" ]; then
RED=""
GREEN=""
RESET=""
else
RED="\033[31m"
GREEN="\033[32m"
RESET="\033[0m"
fi
echo -e "${RED}Error message${RESET}"
Python:
import os, sys
USE_COLOR = (
sys.stdout.isatty()
and "NO_COLOR" not in os.environ
and os.environ.get("TERM") != "dumb"
)
def colored(text, code):
if USE_COLOR:
return f"\033[{code}m{text}\033[0m"
return text
Node.js:
const USE_COLOR = process.stdout.isTTY
&& !("NO_COLOR" in process.env)
&& process.env.TERM !== "dumb";
function colored(text, code) {
return USE_COLOR
? `\x1b[${code}m${text}\x1b[0m`
: text;
}
Go:
import "os"
func useColor() bool {
_, noColor := os.LookupEnv("NO_COLOR")
return !noColor && os.Getenv("TERM") != "dumb"
}
When to Disable Colors
Colors should be disabled when:
NO_COLORenvironment variable is setTERMis set todumb- Output is not a terminal (piped or redirected)
- The
--no-colorCLI flag is passed
FORCE_COLOR
Some tools also support FORCE_COLOR to enable colors even when output is piped:
# Force colors in piped output
FORCE_COLOR=1 my-tool | less -R
Accessibility Considerations
Some users disable colors because they use screen readers, have color vision deficiency, or use high-contrast terminal themes where ANSI colors reduce readability. Respecting NO_COLOR makes your tools more inclusive.
Use Case
Every CLI tool that outputs colored text should implement the NO_COLOR standard. It is essential for CI/CD environments where log files should not contain escape sequences, automated testing where output is parsed, accessibility compliance, and Unix philosophy compliance where output may be piped to other commands. Major tools like git, cargo, and npm all respect this standard.