npm.io
5.0.0 • Published 1 month ago

@visulima/is-ansi-color-supported

Licence
MIT
Version
5.0.0
Deps
0
Size
75 kB
Vulns
0
Weekly
0
Stars
58
is-ansi-color-supported

Detect whether a terminal or browser supports ansi colors.


typescript-image mit licence npm downloads Chat PRs Welcome


Daniel Bannert's open source work is supported by the community on GitHub Sponsors


Install

npm install @visulima/is-ansi-color-supported
yarn add @visulima/is-ansi-color-supported
pnpm add @visulima/is-ansi-color-supported

Usage

import { isStdoutColorSupported, isStderrColorSupported } from "@visulima/is-ansi-color-supported";

/**
 * Levels:
 * - `0` - All colors disabled.
 * - `1` - Basic 16 colors support.
 * - `2` - ANSI 256 colors support.
 * - `3` - Truecolor 16 million colors support.
 */
console.log(isStdoutColorSupported()); // 3

console.log(isStderrColorSupported()); // 3
Custom detection (createIsColorSupported)

For library authors who need more control, createIsColorSupported lets you pick the stream, disable CLI flag sniffing, or force the TTY state:

import { createIsColorSupported } from "@visulima/is-ansi-color-supported";

// Detect stdout color support without inspecting `process.argv`
// (useful when your CLI defines its own `--color` semantics):
const level = createIsColorSupported("stdout", { sniffFlags: false });

// Probe stderr while forcing the TTY state (e.g. when writing to a log file):
const stderrLevel = createIsColorSupported("stderr", { isTTY: false });

The browser and edge-light builds also export createIsColorSupported for API parity, but ignore the stream/options arguments (those runtimes have no streams, CLI args, tty, or os).

Color support

This package automatically detects the supported color space:

  • TrueColor
  • ANSI 256 colors
  • ANSI 16 colors
  • black & white (no color)

There is no standard way to detect which color space is supported. The most common way to detect color support is to check the TERM and COLORTERM environment variables. CI systems can be detected by checking for the existence of the CI and other specifically environment variables. Combine that with the knowledge about which operating system the program is running on, and we have a decent enough way to detect colors.

Terminal ANSI 16
colors
ANSI 256
colors
True
Color
env.
TERM
env.
COLORTERM
Specifically ENV variables
Azure CI dumb TF_BUILD
AGENT_NAME
GitHub CI dumb CI
GITHUB_ACTIONS
GitTea CI dumb CI
GITEA_ACTIONS
GitLab CI dumb CI
GITLAB_CI
Travis CI dumb TRAVIS
PM2
not isTTY
[^1] [^1] [^1] dumb PM2_HOME
pm_id
JetBrains TeamCity
>=2020.1.1
TEAMCITY_VERSION
JetBrains IDEA xterm-256color TERMINAL_EMULATOR='JetBrains-JediTerm'
VS Code xterm-256color truecolor
Windows
Terminal
[^2]
Windows
PowerShell
[^2]
macOS Terminal xterm-256color
iTerm xterm-256color truecolor
Terminal emulator Kitty xterm-kitty

[^1]: Colors supported depends on actual terminal.

[^2]: The Windows terminal supports true color since Windows 10 revision 14931 (2016-09-21).

See also:

Environment variables

To force disable or enable colored output use environment variables NO_COLOR and FORCE_COLOR.

The NO_COLOR variable should be presents with any not empty value. The value is not important, e.g., NO_COLOR=1 NO_COLOR=true disable colors. See standard description by NO_COLOR.

The FORCE_COLOR variable should be presents with one of values:
FORCE_COLOR=0 (or FORCE_COLOR=false) force disable colors
FORCE_COLOR=1, 2, 3 (or FORCE_COLOR=true) force enable colors at the given level

When FORCE_COLOR requests color (any value other than 0/false) it takes precedence over the NO_COLOR environment variable, so FORCE_COLOR=true NO_COLOR=1 and FORCE_COLOR=1 NO_COLOR=1 both enable colors. The --no-color CLI flag still wins over everything, since command-line arguments have the highest priority.

CLI arguments

Use arguments --no-color or --color=false to disable colors and --color to enable ones.

For example, an executable script colors.js:

#!/usr/bin/env node
import { isStdoutColorSupported } from "@visulima/is-ansi-color-supported";

console.log(isStdoutColorSupported());

Execute the script in a terminal:

$ ./colors.js                        # colored output in terminal
$ ./colors.js --no-color             # non colored output in terminal
$ ./colors.js --color=false          # non colored output in terminal

$ ./colors.js > log.txt              # output in file without ANSI codes
$ ./colors.js --color > log.txt      # output in file with ANSI codes
$ ./colors.js --color=true > log.txt # output in file with ANSI codes

Warning

The command line arguments have a higher priority than environment variable.

Note

A 256-color TERM (e.g. TERM=xterm-256color) reports level 2 even when the output is piped to a file and not a TTY. This is intentional (ansis-style behaviour). If you need supports-color's stricter TTY-first gating, use createIsColorSupported("stdout", { isTTY: false }).

Info

For situations where using --color is not possible, use the environment variable FORCE_COLOR=1 (level 1), FORCE_COLOR=2 (level 2), or FORCE_COLOR=3 (level 3) to forcefully enable color, or FORCE_COLOR=0 to forcefully disable. The use of FORCE_COLOR overrides all other color support checks.

Explicit 256/Truecolor mode can be enabled using the --color=256 and --color=16m flags, respectively.

Supported Node.js Versions

Libraries in this ecosystem make the best effort to track Node.js’ release schedule. Here’s a post on why we think this is important.

Contributing

If you would like to help take a look at the list of issues and check our Contributing guild.

Note: please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Credits

Made with at Anolilab

This is an open source project and will always remain free to use. If you think it's cool, please star it . Anolilab is a Development and AI Studio. Contact us at hello@anolilab.com if you need any help with these technologies or just want to say hi!

License

The visulima is-ansi-color-supported is open-sourced software licensed under the MIT

Keywords