Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b6355ef

Browse filesBrowse files
committed
tty: improve color detection
1) Using `process.env.TERM = 'dumb'` should never return any colors. 2) `process.env.TERM = 'terminator'` supports 24 bit colors. 3) Add support for `process.env.TERM = 'rxvt-unicode-24bit'` 4) `Hyper` does not support true colors anymore. It should fall back to the xterm settings in regular cases. 5) `process.env.COLORTERM = 'truecolor'` should return 24 bit colors. PR-URL: #26264 Refs: #26261 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 91b6145 commit b6355ef
Copy full SHA for b6355ef

File tree

Expand file treeCollapse file tree

2 files changed

+35
-26
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-26
lines changed
Open diff view settings
Collapse file

‎lib/internal/tty.js‎

Copy file name to clipboardExpand all lines: lib/internal/tty.js
+26-23Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,25 @@ const COLORS_16m = 24;
3636
// Copyright (C) 1996-2016 Free Software Foundation, Inc. Copying and
3737
// distribution of this file, with or without modification, are permitted
3838
// provided the copyright notice and this notice are preserved.
39-
const TERM_ENVS = [
40-
'eterm',
41-
'cons25',
42-
'console',
43-
'cygwin',
44-
'dtterm',
45-
'gnome',
46-
'hurd',
47-
'jfbterm',
48-
'konsole',
49-
'kterm',
50-
'mlterm',
51-
'putty',
52-
'st',
53-
'terminator'
54-
];
39+
const TERM_ENVS = {
40+
'eterm': COLORS_16,
41+
'cons25': COLORS_16,
42+
'console': COLORS_16,
43+
'cygwin': COLORS_16,
44+
'dtterm': COLORS_16,
45+
'gnome': COLORS_16,
46+
'hurd': COLORS_16,
47+
'jfbterm': COLORS_16,
48+
'konsole': COLORS_16,
49+
'kterm': COLORS_16,
50+
'mlterm': COLORS_16,
51+
'putty': COLORS_16,
52+
'st': COLORS_16,
53+
// https://github.com/da-x/rxvt-unicode/tree/v9.22-with-24bit-color
54+
'rxvt-unicode-24bit': COLORS_16m,
55+
// https://gist.github.com/XVilka/8346728#gistcomment-2823421
56+
'terminator': COLORS_16m
57+
};
5558

5659
const TERM_ENVS_REG_EXP = [
5760
/ansi/,
@@ -68,7 +71,7 @@ const TERM_ENVS_REG_EXP = [
6871
// https://github.com/chalk/supports-color,
6972
// https://github.com/isaacs/color-support.
7073
function getColorDepth(env = process.env) {
71-
if (env.NODE_DISABLE_COLORS || env.TERM === 'dumb' && !env.COLORTERM) {
74+
if (env.NODE_DISABLE_COLORS || env.TERM === 'dumb') {
7275
return COLORS_2;
7376
}
7477

@@ -117,7 +120,6 @@ function getColorDepth(env = process.env) {
117120
}
118121
return COLORS_16m;
119122
case 'HyperTerm':
120-
case 'Hyper':
121123
case 'MacTerm':
122124
return COLORS_16m;
123125
case 'Apple_Terminal':
@@ -130,10 +132,8 @@ function getColorDepth(env = process.env) {
130132

131133
const termEnv = env.TERM.toLowerCase();
132134

133-
for (const term of TERM_ENVS) {
134-
if (termEnv === term) {
135-
return COLORS_16;
136-
}
135+
if (TERM_ENVS[termEnv]) {
136+
return TERM_ENVS[termEnv];
137137
}
138138
for (const term of TERM_ENVS_REG_EXP) {
139139
if (term.test(termEnv)) {
@@ -142,8 +142,11 @@ function getColorDepth(env = process.env) {
142142
}
143143
}
144144

145-
if (env.COLORTERM)
145+
if (env.COLORTERM) {
146+
if (env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit')
147+
return COLORS_16m;
146148
return COLORS_16;
149+
}
147150

148151
return COLORS_2;
149152
}
Collapse file

‎test/pseudo-tty/test-tty-get-color-depth.js‎

Copy file name to clipboardExpand all lines: test/pseudo-tty/test-tty-get-color-depth.js
+9-3Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const common = require('../common');
44
const assert = require('assert').strict;
55
const { WriteStream } = require('tty');
6+
const { inspect } = require('util');
67

78
const fd = common.getTTYfd();
89
const writeStream = new WriteStream(fd);
@@ -16,6 +17,8 @@ const writeStream = new WriteStream(fd);
1617
// Check different environment variables.
1718
[
1819
[{ COLORTERM: '1' }, 4],
20+
[{ COLORTERM: 'truecolor' }, 24],
21+
[{ COLORTERM: '24bit' }, 24],
1922
[{ TMUX: '1' }, 8],
2023
[{ CI: '1' }, 1],
2124
[{ CI: '1', TRAVIS: '1' }, 8],
@@ -29,7 +32,7 @@ const writeStream = new WriteStream(fd);
2932
[{ TERM_PROGRAM: 'iTerm.app', TERM_PROGRAM_VERSION: '3.0' }, 24],
3033
[{ TERM_PROGRAM: 'iTerm.app', TERM_PROGRAM_VERSION: '2.0' }, 8],
3134
[{ TERM_PROGRAM: 'HyperTerm' }, 24],
32-
[{ TERM_PROGRAM: 'Hyper' }, 24],
35+
[{ TERM_PROGRAM: 'Hyper' }, 1],
3336
[{ TERM_PROGRAM: 'MacTerm' }, 24],
3437
[{ TERM_PROGRAM: 'Apple_Terminal' }, 8],
3538
[{ TERM: 'xterm-256' }, 8],
@@ -40,13 +43,16 @@ const writeStream = new WriteStream(fd);
4043
[{ TERM: 'fail' }, 1],
4144
[{ NODE_DISABLE_COLORS: '1' }, 1],
4245
[{ TERM: 'dumb' }, 1],
43-
[{ TERM: 'dumb', COLORTERM: '1' }, 4],
46+
[{ TERM: 'dumb', COLORTERM: '1' }, 1],
47+
[{ TERM: 'terminator' }, 24],
48+
[{ TERM: 'console' }, 4]
4449
].forEach(([env, depth], i) => {
4550
const actual = writeStream.getColorDepth(env);
4651
assert.strictEqual(
4752
actual,
4853
depth,
49-
`i: ${i}, expected: ${depth}, actual: ${actual}, env: ${env}`
54+
`i: ${i}, expected: ${depth}, ` +
55+
`actual: ${actual}, env: ${inspect(env)}`
5056
);
5157
});
5258

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.