Skip to content

Commit ceaeee0

Browse files
committed
tty: add color support for more terminals
This adds support for a couple more terminals. PR-URL: #19730 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 1b71522 commit ceaeee0

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

lib/internal/tty.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,41 @@ const COLORS_16 = 4;
3131
const COLORS_256 = 8;
3232
const COLORS_16m = 24;
3333

34+
// Some entries were taken from `dircolors`
35+
// (https://linux.die.net/man/1/dircolors). The corresponding terminals might
36+
// support more than 16 colors, but this was not tested for.
37+
//
38+
// Copyright (C) 1996-2016 Free Software Foundation, Inc. Copying and
39+
// distribution of this file, with or without modification, are permitted
40+
// provided the copyright notice and this notice are preserved.
41+
const TERM_ENVS = [
42+
'Eterm',
43+
'cons25',
44+
'console',
45+
'cygwin',
46+
'dtterm',
47+
'gnome',
48+
'hurd',
49+
'jfbterm',
50+
'konsole',
51+
'kterm',
52+
'mlterm',
53+
'putty',
54+
'st',
55+
'terminator'
56+
];
57+
58+
const TERM_ENVS_REG_EXP = [
59+
/ansi/,
60+
/color/,
61+
/linux/,
62+
/^con[0-9]*x[0-9]/,
63+
/^rxvt/,
64+
/^screen/,
65+
/^xterm/,
66+
/^vt100/
67+
];
68+
3469
// The `getColorDepth` API got inspired by multiple sources such as
3570
// https://github.com/chalk/supports-color,
3671
// https://github.com/isaacs/color-support.
@@ -89,8 +124,19 @@ function getColorDepth(env = process.env) {
89124
if (env.TERM) {
90125
if (/^xterm-256/.test(env.TERM))
91126
return COLORS_256;
92-
if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(env.TERM))
93-
return COLORS_16;
127+
128+
const termEnv = env.TERM.toLowerCase();
129+
130+
for (const term of TERM_ENVS) {
131+
if (termEnv === term) {
132+
return COLORS_16;
133+
}
134+
}
135+
for (const term of TERM_ENVS_REG_EXP) {
136+
if (term.test(termEnv)) {
137+
return COLORS_16;
138+
}
139+
}
94140
}
95141

96142
if (env.COLORTERM)

0 commit comments

Comments
 (0)