Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tty: add NO_COLOR and FORCE_COLOR support #26485

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -144,8 +144,9 @@ position.
added: v9.9.0
-->

* `env` {Object} An object containing the environment variables to check.
**Default:** `process.env`.
* `env` {Object} An object containing the environment variables to check. This
enables simulating the usage of a specific terminal. **Default:**
`process.env`.
* Returns: {number}

Returns:
@@ -159,11 +160,18 @@ Use this to determine what colors the terminal supports. Due to the nature of
colors in terminals it is possible to either have false positives or false
negatives. It depends on process information and the environment variables that
may lie about what terminal is used.
To enforce a specific behavior without relying on `process.env` it is possible
to pass in an object with different settings.
It is possible to pass in an `env` object to simulate the usage of a specific
terminal. This can be useful to check how specific environment settings behave.

To enforce a specific color support, use one of the below environment settings.

* 2 colors: `FORCE_COLOR = 0` (Disables colors)
* 16 colors: `FORCE_COLOR = 1`
* 256 colors: `FORCE_COLOR = 2`
* 16,777,216 colors: `FORCE_COLOR = 3`

Use the `NODE_DISABLE_COLORS` environment variable to enforce this function to
always return 1.
Disabling color support is also possible by using the `NO_COLOR` and
`NODE_DISABLE_COLORS` environment variables.

### writeStream.getWindowSize()
<!-- YAML
@@ -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);

This comment has been minimized.

Copy link
@Fishrock123

Fishrock123 Mar 18, 2019

Member

Place this above the switch?

This comment has been minimized.

Copy link
@BridgeAR

BridgeAR Mar 18, 2019

Author Member

When moved above the switch, I'll have to exclude a lot of values. That's why it's inside.

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;
}

@@ -59,11 +59,18 @@ const writeStream = new WriteStream(fd);
[{ TERM: 'color' }, 4],
[{ TERM: 'linux' }, 4],
[{ TERM: 'fail' }, 1],
[{ NODE_DISABLE_COLORS: '1' }, 1],
[{ TERM: 'color', NODE_DISABLE_COLORS: '1' }, 1],
[{ TERM: 'dumb' }, 1],
[{ TERM: 'dumb', COLORTERM: '1' }, 1],
[{ TERM: 'terminator' }, 24],
[{ TERM: 'console' }, 4]
[{ TERM: 'console' }, 4],
[{ COLORTERM: '24bit', FORCE_COLOR: '' }, 4],
[{ NO_COLOR: '1', FORCE_COLOR: '2' }, 8],
[{ NODE_DISABLE_COLORS: '1', FORCE_COLOR: '3' }, 24],
[{ NO_COLOR: '1', COLORTERM: '24bit' }, 1],
[{ NO_COLOR: '', COLORTERM: '24bit' }, 1],
[{ TMUX: '1', FORCE_COLOR: 0 }, 1],
[{ NO_COLOR: 'true', FORCE_COLOR: 0, COLORTERM: 'truecolor' }, 1],
].forEach(([env, depth], i) => {
const actual = writeStream.getColorDepth(env);
assert.strictEqual(
@@ -0,0 +1,8 @@
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.