diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-03-07 03:48:02 +0300 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2019-03-19 18:41:22 +0300 |
commit | 273398a3d09601b978ace4e761f569baa94f0fd0 (patch) | |
tree | 4cfcc2043824bf9306f266f84be1c04ff917d12a /lib/internal/tty.js | |
parent | 1f1914c9d454875670a48ac943e98d78671b20c2 (diff) |
tty: add NO_COLOR and FORCE_COLOR support
This adds support to enforce a specific color depth by checking the
`FORCE_COLOR` environment variable similar to `chalk`.
On top of that we also add support for the `NO_COLOR` environment
variable as suggested by https://no-color.org/.
PR-URL: https://github.com/nodejs/node/pull/26485
Refs: https://github.com/nodejs/node/pull/26248
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Diffstat (limited to 'lib/internal/tty.js')
-rw-r--r-- | lib/internal/tty.js | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/lib/internal/tty.js b/lib/internal/tty.js index 486dbbeaa50..9c34d3fdb7f 100644 --- a/lib/internal/tty.js +++ b/lib/internal/tty.js @@ -72,11 +72,49 @@ const TERM_ENVS_REG_EXP = [ /^vt100/ ]; +function warnOnDeactivatedColors(env) { + let name; + if (env.NODE_DISABLE_COLORS !== undefined) + name = 'NODE_DISABLE_COLORS'; + if (env.NO_COLOR !== undefined) + name = 'NO_COLOR'; + + if (name !== undefined) { + process.emitWarning( + `The '${name}' env is ignored due to the 'FORCE_COLOR' env being set.`, + 'Warning' + ); + } +} + // The `getColorDepth` API got inspired by multiple sources such as // https://github.com/chalk/supports-color, // https://github.com/isaacs/color-support. function getColorDepth(env = process.env) { - if (env.NODE_DISABLE_COLORS || env.TERM === 'dumb') { + // Use level 0-3 to support the same levels as `chalk` does. This is done for + // consistency throughout the ecosystem. + if (env.FORCE_COLOR !== undefined) { + switch (env.FORCE_COLOR) { + case '': + case '1': + case 'true': + warnOnDeactivatedColors(env); + return COLORS_16; + case '2': + warnOnDeactivatedColors(env); + return COLORS_256; + case '3': + warnOnDeactivatedColors(env); + return COLORS_16m; + default: + return COLORS_2; + } + } + + if (env.NODE_DISABLE_COLORS !== undefined || + // See https://no-color.org/ + env.NO_COLOR !== undefined || + env.TERM === 'dumb') { return COLORS_2; } |