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: improve color detection #26264

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions lib/internal/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,25 @@ const COLORS_16m = 24;
// Copyright (C) 1996-2016 Free Software Foundation, Inc. Copying and
// distribution of this file, with or without modification, are permitted
// provided the copyright notice and this notice are preserved.
const TERM_ENVS = [
'eterm',
'cons25',
'console',
'cygwin',
'dtterm',
'gnome',
'hurd',
'jfbterm',
'konsole',
'kterm',
'mlterm',
'putty',
'st',
'terminator'
];
const TERM_ENVS = {
'eterm': COLORS_16,
'cons25': COLORS_16,
'console': COLORS_16,
'cygwin': COLORS_16,
'dtterm': COLORS_16,
'gnome': COLORS_16,
'hurd': COLORS_16,
'jfbterm': COLORS_16,
'konsole': COLORS_16,
'kterm': COLORS_16,
'mlterm': COLORS_16,
'putty': COLORS_16,
'st': COLORS_16,
// https://github.com/da-x/rxvt-unicode/tree/v9.22-with-24bit-color
'rxvt-unicode-24bit': COLORS_16m,
// https://gist.github.com/XVilka/8346728#gistcomment-2823421
'terminator': COLORS_16m
};

const TERM_ENVS_REG_EXP = [
/ansi/,
Expand All @@ -68,7 +71,7 @@ const TERM_ENVS_REG_EXP = [
// 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' && !env.COLORTERM) {
if (env.NODE_DISABLE_COLORS || env.TERM === 'dumb') {
return COLORS_2;
}

Expand Down Expand Up @@ -117,7 +120,6 @@ function getColorDepth(env = process.env) {
}
return COLORS_16m;
case 'HyperTerm':
case 'Hyper':
case 'MacTerm':
return COLORS_16m;
case 'Apple_Terminal':
Expand All @@ -130,10 +132,8 @@ function getColorDepth(env = process.env) {

const termEnv = env.TERM.toLowerCase();

for (const term of TERM_ENVS) {
if (termEnv === term) {
return COLORS_16;
}
if (TERM_ENVS[termEnv]) {
return TERM_ENVS[termEnv];
}
for (const term of TERM_ENVS_REG_EXP) {
if (term.test(termEnv)) {
Expand All @@ -142,8 +142,11 @@ function getColorDepth(env = process.env) {
}
}

if (env.COLORTERM)
if (env.COLORTERM) {
if (env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit')
return COLORS_16m;
return COLORS_16;
}

return COLORS_2;
}
Expand Down
4 changes: 4 additions & 0 deletions test/pseudo-tty/test-assert-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const assert = require('assert').strict;
try {
// Activate colors even if the tty does not support colors.
process.env.COLORTERM = '1';
// Make sure TERM is not set to e.g., 'dumb' and NODE_DISABLE_COLORS is not
// active.
process.env.TERM = 'FOOBAR';
delete process.env.NODE_DISABLE_COLORS;
assert.deepStrictEqual([1, 2, 2, 2], [2, 2, 2, 2]);
} catch (err) {
const expected = 'Expected values to be strictly deep-equal:\n' +
Expand Down
12 changes: 9 additions & 3 deletions test/pseudo-tty/test-tty-get-color-depth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const common = require('../common');
const assert = require('assert').strict;
const { WriteStream } = require('tty');
const { inspect } = require('util');

const fd = common.getTTYfd();
const writeStream = new WriteStream(fd);
Expand All @@ -16,6 +17,8 @@ const writeStream = new WriteStream(fd);
// Check different environment variables.
[
[{ COLORTERM: '1' }, 4],
[{ COLORTERM: 'truecolor' }, 24],
[{ COLORTERM: '24bit' }, 24],
[{ TMUX: '1' }, 8],
[{ CI: '1' }, 1],
[{ CI: '1', TRAVIS: '1' }, 8],
Expand All @@ -29,7 +32,7 @@ const writeStream = new WriteStream(fd);
[{ TERM_PROGRAM: 'iTerm.app', TERM_PROGRAM_VERSION: '3.0' }, 24],
[{ TERM_PROGRAM: 'iTerm.app', TERM_PROGRAM_VERSION: '2.0' }, 8],
[{ TERM_PROGRAM: 'HyperTerm' }, 24],
[{ TERM_PROGRAM: 'Hyper' }, 24],
[{ TERM_PROGRAM: 'Hyper' }, 1],
[{ TERM_PROGRAM: 'MacTerm' }, 24],
[{ TERM_PROGRAM: 'Apple_Terminal' }, 8],
[{ TERM: 'xterm-256' }, 8],
Expand All @@ -40,13 +43,16 @@ const writeStream = new WriteStream(fd);
[{ TERM: 'fail' }, 1],
[{ NODE_DISABLE_COLORS: '1' }, 1],
[{ TERM: 'dumb' }, 1],
[{ TERM: 'dumb', COLORTERM: '1' }, 4],
[{ TERM: 'dumb', COLORTERM: '1' }, 1],
[{ TERM: 'terminator' }, 24],
[{ TERM: 'console' }, 4]
].forEach(([env, depth], i) => {
const actual = writeStream.getColorDepth(env);
assert.strictEqual(
actual,
depth,
`i: ${i}, expected: ${depth}, actual: ${actual}, env: ${env}`
`i: ${i}, expected: ${depth}, ` +
`actual: ${actual}, env: ${inspect(env)}`
);
});

Expand Down