Skip to content

Commit 0e72f3b

Browse files
authored
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. PR-URL: #58146 Refs: #57998 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 283ed53 commit 0e72f3b

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

lib/internal/tty.js

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

2525
const {
2626
ArrayPrototypeSome,
27+
ObjectEntries,
28+
ObjectPrototypeHasOwnProperty: hasOwn,
2729
RegExpPrototypeExec,
30+
SafeMap,
2831
StringPrototypeSplit,
2932
StringPrototypeToLowerCase,
3033
} = primordials;
@@ -64,17 +67,31 @@ const TERM_ENVS = {
6467
'rxvt-unicode-24bit': COLORS_16m,
6568
// https://bugs.launchpad.net/terminator/+bug/1030562
6669
'terminator': COLORS_16m,
70+
'xterm-kitty': COLORS_16m,
6771
};
6872

73+
const CI_ENVS_MAP = new SafeMap(ObjectEntries({
74+
APPVEYOR: COLORS_256,
75+
BUILDKITE: COLORS_256,
76+
CIRCLECI: COLORS_16m,
77+
DRONE: COLORS_256,
78+
GITEA_ACTIONS: COLORS_16m,
79+
GITHUB_ACTIONS: COLORS_16m,
80+
GITLAB_CI: COLORS_256,
81+
TRAVIS: COLORS_256,
82+
}));
83+
6984
const TERM_ENVS_REG_EXP = [
7085
/ansi/,
7186
/color/,
7287
/linux/,
88+
/direct/,
7389
/^con[0-9]*x[0-9]/,
7490
/^rxvt/,
7591
/^screen/,
7692
/^xterm/,
7793
/^vt100/,
94+
/^vt220/,
7895
];
7996

8097
let warned = false;
@@ -155,19 +172,21 @@ function getColorDepth(env = process.env) {
155172
}
156173

157174
if (env.TMUX) {
158-
return COLORS_256;
175+
return COLORS_16m;
176+
}
177+
178+
// Azure DevOps
179+
if (hasOwn(env, 'TF_BUILD') && hasOwn(env, 'AGENT_NAME')) {
180+
return COLORS_16;
159181
}
160182

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') {
183+
if (hasOwn(env, 'CI')) {
184+
for (const { 0: envName, 1: colors } of CI_ENVS_MAP) {
185+
if (hasOwn(env, envName)) {
186+
return colors;
187+
}
188+
}
189+
if (env.CI_NAME === 'codeship') {
171190
return COLORS_256;
172191
}
173192
return COLORS_2;
@@ -198,6 +217,10 @@ function getColorDepth(env = process.env) {
198217
}
199218

200219
if (env.TERM) {
220+
if (RegExpPrototypeExec(/truecolor/, env.TERM) !== null) {
221+
return COLORS_16m;
222+
}
223+
201224
if (RegExpPrototypeExec(/^xterm-256/, env.TERM) !== null) {
202225
return COLORS_256;
203226
}

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)