Skip to content

Commit ce27ea6

Browse files
committed
tty: improve color terminal color detection
This adds a couple new entries or increases the support depending on newer data. I checked ncurses, tput, supports-color, and termstandard on github. Most updates are from supports-color.
1 parent f89baf2 commit ce27ea6

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

lib/internal/tty.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
const {
2626
ArrayPrototypeSome,
27+
ObjectPrototypeHasOwnProperty: hasOwn,
2728
RegExpPrototypeExec,
29+
SafeMap,
2830
StringPrototypeSplit,
2931
StringPrototypeToLowerCase,
3032
} = primordials;
@@ -64,17 +66,31 @@ const TERM_ENVS = {
6466
'rxvt-unicode-24bit': COLORS_16m,
6567
// https://bugs.launchpad.net/terminator/+bug/1030562
6668
'terminator': COLORS_16m,
69+
'xterm-kitty': COLORS_16m,
6770
};
6871

72+
const CI_ENVS_MAP = new SafeMap(Object.entries({
73+
APPVEYOR: COLORS_256,
74+
BUILDKITE: COLORS_256,
75+
CIRCLECI: COLORS_16m,
76+
DRONE: COLORS_256,
77+
GITEA_ACTIONS: COLORS_16m,
78+
GITHUB_ACTIONS: COLORS_16m,
79+
GITLAB_CI: COLORS_256,
80+
TRAVIS: COLORS_256,
81+
}));
82+
6983
const TERM_ENVS_REG_EXP = [
7084
/ansi/,
7185
/color/,
7286
/linux/,
87+
/direct/,
7388
/^con[0-9]*x[0-9]/,
7489
/^rxvt/,
7590
/^screen/,
7691
/^xterm/,
7792
/^vt100/,
93+
/^vt220/,
7894
];
7995

8096
let warned = false;
@@ -155,19 +171,21 @@ function getColorDepth(env = process.env) {
155171
}
156172

157173
if (env.TMUX) {
158-
return COLORS_256;
174+
return COLORS_16m;
159175
}
160176

161-
if (env.CI) {
162-
if ([
163-
'APPVEYOR',
164-
'BUILDKITE',
165-
'CIRCLECI',
166-
'DRONE',
167-
'GITHUB_ACTIONS',
168-
'GITLAB_CI',
169-
'TRAVIS',
170-
].some((sign) => sign in env) || env.CI_NAME === 'codeship') {
177+
// Azure DevOps
178+
if (hasOwn(env, 'TF_BUILD') && hasOwn(env, 'AGENT_NAME')) {
179+
return COLORS_16;
180+
}
181+
182+
if (hasOwn(env, 'CI')) {
183+
for (const [envName, colors] of CI_ENVS_MAP) {
184+
if (hasOwn(env, envName)) {
185+
return colors;
186+
}
187+
}
188+
if (env.CI_NAME === 'codeship') {
171189
return COLORS_256;
172190
}
173191
return COLORS_2;
@@ -198,6 +216,10 @@ function getColorDepth(env = process.env) {
198216
}
199217

200218
if (env.TERM) {
219+
if (RegExpPrototypeExec(/truecolor/, env.TERM) !== null) {
220+
return COLORS_16m;
221+
}
222+
201223
if (RegExpPrototypeExec(/^xterm-256/, env.TERM) !== null) {
202224
return COLORS_256;
203225
}

test/pseudo-tty/test-tty-color-support.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ const writeStream = new WriteStream(fd);
3737
[{ COLORTERM: '1' }, 4],
3838
[{ COLORTERM: 'truecolor' }, 24],
3939
[{ COLORTERM: '24bit' }, 24],
40-
[{ TMUX: '1' }, 8],
40+
[{ TMUX: '1' }, 24],
4141
[{ CI: '1' }, 1],
42-
[{ CI: '1', TRAVIS: '1' }, 8],
43-
[{ CI: '1', CIRCLECI: '1' }, 8],
44-
[{ CI: '1', APPVEYOR: '1' }, 8],
45-
[{ CI: '1', GITLAB_CI: '1' }, 8],
42+
[{ CI: '', APPVEYOR: '1' }, 8],
43+
[{ CI: '1', BUILDKITE: '' }, 8],
4644
[{ CI: '1', CI_NAME: 'codeship' }, 8],
45+
[{ CI: '1', CIRCLECI: '1' }, 24],
46+
[{ CI: '1', DRONE: '' }, 8],
47+
[{ CI: '1', GITEA_ACTIONS: '' }, 24],
48+
[{ CI: '1', GITHUB_ACTIONS: '' }, 24],
49+
[{ CI: '1', GITLAB_CI: '1' }, 8],
50+
[{ CI: '1', TRAVIS: '1' }, 8],
51+
[{ CI: '', TRAVIS: '' }, 8],
4752
[{ TEAMCITY_VERSION: '1.0.0' }, 1],
4853
[{ TEAMCITY_VERSION: '9.11.0' }, 4],
4954
[{ TERM_PROGRAM: 'iTerm.app' }, 8],
@@ -53,17 +58,22 @@ const writeStream = new WriteStream(fd);
5358
[{ TERM_PROGRAM: 'Hyper' }, 1],
5459
[{ TERM_PROGRAM: 'MacTerm' }, 24],
5560
[{ TERM_PROGRAM: 'Apple_Terminal' }, 8],
56-
[{ TERM: 'xterm-256' }, 8],
5761
[{ TERM: 'ansi' }, 4],
5862
[{ TERM: 'ANSI' }, 4],
5963
[{ TERM: 'color' }, 4],
60-
[{ TERM: 'linux' }, 4],
61-
[{ TERM: 'fail' }, 1],
6264
[{ TERM: 'color', NODE_DISABLE_COLORS: '1' }, 1],
65+
[{ TERM: 'console' }, 4],
66+
[{ TERM: 'direct' }, 4],
6367
[{ TERM: 'dumb' }, 1],
6468
[{ TERM: 'dumb', COLORTERM: '1' }, 1],
69+
[{ TERM: 'fail' }, 1],
70+
[{ TERM: 'linux' }, 4],
6571
[{ TERM: 'terminator' }, 24],
66-
[{ TERM: 'console' }, 4],
72+
[{ TERM: 'vt100' }, 4],
73+
[{ TERM: 'vt220' }, 4],
74+
[{ TERM: 'xterm-256' }, 8],
75+
[{ TERM: 'xterm-kitty' }, 24],
76+
[{ TERM: 'xterm-truecolor' }, 24],
6777
[{ COLORTERM: '24bit', FORCE_COLOR: '' }, 4],
6878
[{ NO_COLOR: '1', FORCE_COLOR: '2' }, 8],
6979
[{ NODE_DISABLE_COLORS: '1', FORCE_COLOR: '3' }, 24],
@@ -72,6 +82,7 @@ const writeStream = new WriteStream(fd);
7282
[{ TMUX: '1', FORCE_COLOR: 0 }, 1],
7383
[{ NO_COLOR: 'true', FORCE_COLOR: 0, COLORTERM: 'truecolor' }, 1],
7484
[{ TERM: 'xterm-256color', COLORTERM: 'truecolor' }, 24],
85+
[{ TF_BUILD: '', AGENT_NAME: '' }, 4],
7586
].forEach(([env, depth], i) => {
7687
const actual = writeStream.getColorDepth(env);
7788
assert.strictEqual(

0 commit comments

Comments
 (0)