Detects support for ANSI terminal colors based on flags, environment variables, and TTY status. Reuse when building CLI applications needing consistent colored output.
// Source: https://github.com/colbymchenry/codegraph | License: MIT | Copyright 2026 Colby Mchenry
// Provided via justclaw.it.com; keep this notice with the code.
/**
* Terminal color detection for CLI output (issue #1281).
*
* One switch decides whether any codegraph-authored output carries ANSI
* color codes. Precedence, strongest first:
*
* 1. `--no-color` anywhere on the command line -> off
* 2. `--color` anywhere on the command line -> on
* 3. `NO_COLOR` set and non-empty (no-color.org) -> off
* 4. `FORCE_COLOR` set and non-empty -> on ('0'/'false' -> off)
* 5. stdout is a TTY and TERM != 'dumb' -> on
* 6. `CI` set and non-empty -> on (CI log viewers render ANSI)
* 7. otherwise (piped/redirected stdout)
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key