Skip to content

Commit 1b71522

Browse files
committed
tty: add attribution for chalk
This adds attributions for the getColorDepth function as it got inspired by https://github.com/chalk/supports-color and more sources. 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 3d61e14 commit 1b71522

File tree

3 files changed

+107
-72
lines changed

3 files changed

+107
-72
lines changed

lib/internal/tty.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// MIT License
2+
3+
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
4+
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
12+
// The above copyright notice and this permission notice shall be included in
13+
// all copies or substantial portions of the Software.
14+
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
'use strict';
24+
25+
const { release } = require('os');
26+
27+
const OSRelease = release().split('.');
28+
29+
const COLORS_2 = 1;
30+
const COLORS_16 = 4;
31+
const COLORS_256 = 8;
32+
const COLORS_16m = 24;
33+
34+
// The `getColorDepth` API got inspired by multiple sources such as
35+
// https://github.com/chalk/supports-color,
36+
// https://github.com/isaacs/color-support.
37+
function getColorDepth(env = process.env) {
38+
if (env.NODE_DISABLE_COLORS || env.TERM === 'dumb' && !env.COLORTERM) {
39+
return COLORS_2;
40+
}
41+
42+
if (process.platform === 'win32') {
43+
// Windows 10 build 10586 is the first Windows release that supports 256
44+
// colors. Windows 10 build 14931 is the first release that supports
45+
// 16m/TrueColor.
46+
if (+OSRelease[0] >= 10) {
47+
const build = +OSRelease[2];
48+
if (build >= 14931)
49+
return COLORS_16m;
50+
if (build >= 10586)
51+
return COLORS_256;
52+
}
53+
54+
return COLORS_16;
55+
}
56+
57+
if (env.TMUX) {
58+
return COLORS_256;
59+
}
60+
61+
if (env.CI) {
62+
if ('TRAVIS' in env || 'CIRCLECI' in env || 'APPVEYOR' in env ||
63+
'GITLAB_CI' in env || env.CI_NAME === 'codeship') {
64+
return COLORS_256;
65+
}
66+
return COLORS_2;
67+
}
68+
69+
if ('TEAMCITY_VERSION' in env) {
70+
return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ?
71+
COLORS_16 : COLORS_2;
72+
}
73+
74+
switch (env.TERM_PROGRAM) {
75+
case 'iTerm.app':
76+
if (!env.TERM_PROGRAM_VERSION ||
77+
/^[0-2]\./.test(env.TERM_PROGRAM_VERSION)) {
78+
return COLORS_256;
79+
}
80+
return COLORS_16m;
81+
case 'HyperTerm':
82+
case 'Hyper':
83+
case 'MacTerm':
84+
return COLORS_16m;
85+
case 'Apple_Terminal':
86+
return COLORS_256;
87+
}
88+
89+
if (env.TERM) {
90+
if (/^xterm-256/.test(env.TERM))
91+
return COLORS_256;
92+
if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(env.TERM))
93+
return COLORS_16;
94+
}
95+
96+
if (env.COLORTERM)
97+
return COLORS_16;
98+
99+
return COLORS_2;
100+
}
101+
102+
module.exports = {
103+
getColorDepth
104+
};

lib/tty.js

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,7 @@ const { TTY, isTTY } = process.binding('tty_wrap');
2727
const errors = require('internal/errors');
2828
const { ERR_INVALID_FD } = errors.codes;
2929
const readline = require('readline');
30-
const { release } = require('os');
31-
32-
const OSRelease = release().split('.');
33-
34-
const COLORS_2 = 1;
35-
const COLORS_16 = 4;
36-
const COLORS_256 = 8;
37-
const COLORS_16m = 24;
30+
const { getColorDepth } = require('internal/tty');
3831

3932
function isatty(fd) {
4033
return Number.isInteger(fd) && fd >= 0 && isTTY(fd);
@@ -108,70 +101,7 @@ inherits(WriteStream, net.Socket);
108101

109102
WriteStream.prototype.isTTY = true;
110103

111-
WriteStream.prototype.getColorDepth = function(env = process.env) {
112-
if (env.NODE_DISABLE_COLORS || env.TERM === 'dumb' && !env.COLORTERM) {
113-
return COLORS_2;
114-
}
115-
116-
if (process.platform === 'win32') {
117-
// Windows 10 build 10586 is the first Windows release that supports 256
118-
// colors. Windows 10 build 14931 is the first release that supports
119-
// 16m/TrueColor.
120-
if (+OSRelease[0] >= 10) {
121-
const build = +OSRelease[2];
122-
if (build >= 14931)
123-
return COLORS_16m;
124-
if (build >= 10586)
125-
return COLORS_256;
126-
}
127-
128-
return COLORS_16;
129-
}
130-
131-
if (env.TMUX) {
132-
return COLORS_256;
133-
}
134-
135-
if (env.CI) {
136-
if ('TRAVIS' in env || 'CIRCLECI' in env || 'APPVEYOR' in env ||
137-
'GITLAB_CI' in env || env.CI_NAME === 'codeship') {
138-
return COLORS_256;
139-
}
140-
return COLORS_2;
141-
}
142-
143-
if ('TEAMCITY_VERSION' in env) {
144-
return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ?
145-
COLORS_16 : COLORS_2;
146-
}
147-
148-
switch (env.TERM_PROGRAM) {
149-
case 'iTerm.app':
150-
if (!env.TERM_PROGRAM_VERSION ||
151-
/^[0-2]\./.test(env.TERM_PROGRAM_VERSION)) {
152-
return COLORS_256;
153-
}
154-
return COLORS_16m;
155-
case 'HyperTerm':
156-
case 'Hyper':
157-
case 'MacTerm':
158-
return COLORS_16m;
159-
case 'Apple_Terminal':
160-
return COLORS_256;
161-
}
162-
163-
if (env.TERM) {
164-
if (/^xterm-256/.test(env.TERM))
165-
return COLORS_256;
166-
if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(env.TERM))
167-
return COLORS_16;
168-
}
169-
170-
if (env.COLORTERM)
171-
return COLORS_16;
172-
173-
return COLORS_2;
174-
};
104+
WriteStream.prototype.getColorDepth = getColorDepth;
175105

176106
WriteStream.prototype._refreshSize = function() {
177107
const oldCols = this.columns;

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
'lib/internal/timers.js',
134134
'lib/internal/tls.js',
135135
'lib/internal/trace_events_async_hooks.js',
136+
'lib/internal/tty.js',
136137
'lib/internal/url.js',
137138
'lib/internal/util.js',
138139
'lib/internal/util/comparisons.js',

0 commit comments

Comments
 (0)