Skip to content

Commit d980405

Browse files
authored
fix: better color support detection (#6556)
1 parent cd1e6aa commit d980405

File tree

12 files changed

+698
-306
lines changed

12 files changed

+698
-306
lines changed

DEPENDENCIES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ graph LR;
577577
npm-->sigstore;
578578
npm-->spawk;
579579
npm-->ssri;
580+
npm-->supports-color;
580581
npm-->tap;
581582
npm-->tar;
582583
npm-->text-table;

lib/commands/doctor.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ class Doctor extends BaseCommand {
371371

372372
output (row) {
373373
const t = new Table({
374-
chars: { top: '',
374+
chars: {
375+
top: '',
375376
'top-mid': '',
376377
'top-left': '',
377378
'top-right': '',
@@ -385,8 +386,17 @@ class Doctor extends BaseCommand {
385386
'mid-mid': '',
386387
right: '',
387388
'right-mid': '',
388-
middle: ' ' },
389-
style: { 'padding-left': 0, 'padding-right': 0 },
389+
middle: ' ',
390+
},
391+
style: {
392+
'padding-left': 0,
393+
'padding-right': 0,
394+
// setting border here is not necessary visually since we've already
395+
// zeroed out all the chars above, but without it cli-table3 will wrap
396+
// some of the separator spaces with ansi codes which show up in
397+
// snapshots.
398+
border: 0,
399+
},
390400
colWidths: [this.#checkWidth, 6],
391401
})
392402
t.push(row)

lib/npm.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,19 @@ class Npm {
194194

195195
await this.time('npm:load:configload', () => this.config.load())
196196

197-
const { Chalk, supportsColor, supportsColorStderr } = await import('chalk')
197+
// get createSupportsColor from chalk directly if this lands
198+
// https://github.com/chalk/chalk/pull/600
199+
const [{ Chalk }, { createSupportsColor }] = await Promise.all([
200+
import('chalk'),
201+
import('supports-color'),
202+
])
198203
this.#noColorChalk = new Chalk({ level: 0 })
199-
this.#chalk = this.color ? new Chalk({ level: supportsColor.level })
200-
: this.#noColorChalk
201-
this.#logChalk = this.logColor ? new Chalk({ level: supportsColorStderr.level })
202-
: this.#noColorChalk
204+
// we get the chalk level based on a null stream meaning chalk will only use
205+
// what it knows about the environment to get color support since we already
206+
// determined in our definitions that we want to show colors.
207+
const level = Math.max(createSupportsColor(null).level, 1)
208+
this.#chalk = this.color ? new Chalk({ level }) : this.#noColorChalk
209+
this.#logChalk = this.logColor ? new Chalk({ level }) : this.#noColorChalk
203210

204211
// mkdir this separately since the logs dir can be set to
205212
// a different location. if this fails, then we don't have

node_modules/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@
283283
!/string-width
284284
!/strip-ansi-cjs
285285
!/strip-ansi
286+
!/supports-color
286287
!/tar
287288
!/tar/node_modules/
288289
/tar/node_modules/*
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-env browser */
2+
3+
const level = (() => {
4+
if (navigator.userAgentData) {
5+
const brand = navigator.userAgentData.brands.find(({brand}) => brand === 'Chromium');
6+
if (brand?.version > 93) {
7+
return 3;
8+
}
9+
}
10+
11+
if (/\b(Chrome|Chromium)\//.test(navigator.userAgent)) {
12+
return 1;
13+
}
14+
15+
return 0;
16+
})();
17+
18+
const colorSupport = level !== 0 && {
19+
level,
20+
hasBasic: true,
21+
has256: level >= 2,
22+
has16m: level >= 3,
23+
};
24+
25+
const supportsColor = {
26+
stdout: colorSupport,
27+
stderr: colorSupport,
28+
};
29+
30+
export default supportsColor;

node_modules/supports-color/index.js

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import process from 'node:process';
2+
import os from 'node:os';
3+
import tty from 'node:tty';
4+
5+
// From: https://github.com/sindresorhus/has-flag/blob/main/index.js
6+
/// function hasFlag(flag, argv = globalThis.Deno?.args ?? process.argv) {
7+
function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process.argv) {
8+
const prefix = flag.startsWith('-') ? '' : (flag.length === 1 ? '-' : '--');
9+
const position = argv.indexOf(prefix + flag);
10+
const terminatorPosition = argv.indexOf('--');
11+
return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
12+
}
13+
14+
const {env} = process;
15+
16+
let flagForceColor;
17+
if (
18+
hasFlag('no-color')
19+
|| hasFlag('no-colors')
20+
|| hasFlag('color=false')
21+
|| hasFlag('color=never')
22+
) {
23+
flagForceColor = 0;
24+
} else if (
25+
hasFlag('color')
26+
|| hasFlag('colors')
27+
|| hasFlag('color=true')
28+
|| hasFlag('color=always')
29+
) {
30+
flagForceColor = 1;
31+
}
32+
33+
function envForceColor() {
34+
if ('FORCE_COLOR' in env) {
35+
if (env.FORCE_COLOR === 'true') {
36+
return 1;
37+
}
38+
39+
if (env.FORCE_COLOR === 'false') {
40+
return 0;
41+
}
42+
43+
return env.FORCE_COLOR.length === 0 ? 1 : Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
44+
}
45+
}
46+
47+
function translateLevel(level) {
48+
if (level === 0) {
49+
return false;
50+
}
51+
52+
return {
53+
level,
54+
hasBasic: true,
55+
has256: level >= 2,
56+
has16m: level >= 3,
57+
};
58+
}
59+
60+
function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
61+
const noFlagForceColor = envForceColor();
62+
if (noFlagForceColor !== undefined) {
63+
flagForceColor = noFlagForceColor;
64+
}
65+
66+
const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
67+
68+
if (forceColor === 0) {
69+
return 0;
70+
}
71+
72+
if (sniffFlags) {
73+
if (hasFlag('color=16m')
74+
|| hasFlag('color=full')
75+
|| hasFlag('color=truecolor')) {
76+
return 3;
77+
}
78+
79+
if (hasFlag('color=256')) {
80+
return 2;
81+
}
82+
}
83+
84+
// Check for Azure DevOps pipelines.
85+
// Has to be above the `!streamIsTTY` check.
86+
if ('TF_BUILD' in env && 'AGENT_NAME' in env) {
87+
return 1;
88+
}
89+
90+
if (haveStream && !streamIsTTY && forceColor === undefined) {
91+
return 0;
92+
}
93+
94+
const min = forceColor || 0;
95+
96+
if (env.TERM === 'dumb') {
97+
return min;
98+
}
99+
100+
if (process.platform === 'win32') {
101+
// Windows 10 build 10586 is the first Windows release that supports 256 colors.
102+
// Windows 10 build 14931 is the first release that supports 16m/TrueColor.
103+
const osRelease = os.release().split('.');
104+
if (
105+
Number(osRelease[0]) >= 10
106+
&& Number(osRelease[2]) >= 10_586
107+
) {
108+
return Number(osRelease[2]) >= 14_931 ? 3 : 2;
109+
}
110+
111+
return 1;
112+
}
113+
114+
if ('CI' in env) {
115+
if ('GITHUB_ACTIONS' in env) {
116+
return 3;
117+
}
118+
119+
if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI', 'BUILDKITE', 'DRONE'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
120+
return 1;
121+
}
122+
123+
return min;
124+
}
125+
126+
if ('TEAMCITY_VERSION' in env) {
127+
return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
128+
}
129+
130+
if (env.COLORTERM === 'truecolor') {
131+
return 3;
132+
}
133+
134+
if (env.TERM === 'xterm-kitty') {
135+
return 3;
136+
}
137+
138+
if ('TERM_PROGRAM' in env) {
139+
const version = Number.parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
140+
141+
switch (env.TERM_PROGRAM) {
142+
case 'iTerm.app': {
143+
return version >= 3 ? 3 : 2;
144+
}
145+
146+
case 'Apple_Terminal': {
147+
return 2;
148+
}
149+
// No default
150+
}
151+
}
152+
153+
if (/-256(color)?$/i.test(env.TERM)) {
154+
return 2;
155+
}
156+
157+
if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
158+
return 1;
159+
}
160+
161+
if ('COLORTERM' in env) {
162+
return 1;
163+
}
164+
165+
return min;
166+
}
167+
168+
export function createSupportsColor(stream, options = {}) {
169+
const level = _supportsColor(stream, {
170+
streamIsTTY: stream && stream.isTTY,
171+
...options,
172+
});
173+
174+
return translateLevel(level);
175+
}
176+
177+
const supportsColor = {
178+
stdout: createSupportsColor({isTTY: tty.isatty(1)}),
179+
stderr: createSupportsColor({isTTY: tty.isatty(2)}),
180+
};
181+
182+
export default supportsColor;

node_modules/supports-color/license

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "supports-color",
3+
"version": "9.3.1",
4+
"description": "Detect whether a terminal supports color",
5+
"license": "MIT",
6+
"repository": "chalk/supports-color",
7+
"funding": "https://github.com/chalk/supports-color?sponsor=1",
8+
"author": {
9+
"name": "Sindre Sorhus",
10+
"email": "[email protected]",
11+
"url": "https://sindresorhus.com"
12+
},
13+
"type": "module",
14+
"exports": {
15+
"node": "./index.js",
16+
"default": "./browser.js"
17+
},
18+
"engines": {
19+
"node": ">=12"
20+
},
21+
"scripts": {
22+
"//test": "xo && ava && tsd",
23+
"test": "xo && tsd"
24+
},
25+
"files": [
26+
"index.js",
27+
"index.d.ts",
28+
"browser.js",
29+
"browser.d.ts"
30+
],
31+
"keywords": [
32+
"color",
33+
"colour",
34+
"colors",
35+
"terminal",
36+
"console",
37+
"cli",
38+
"ansi",
39+
"styles",
40+
"tty",
41+
"rgb",
42+
"256",
43+
"shell",
44+
"xterm",
45+
"command-line",
46+
"support",
47+
"supports",
48+
"capability",
49+
"detect",
50+
"truecolor",
51+
"16m"
52+
],
53+
"devDependencies": {
54+
"@types/node": "^16.11.7",
55+
"ava": "^3.15.0",
56+
"import-fresh": "^3.3.0",
57+
"tsd": "^0.18.0",
58+
"typescript": "^4.4.3",
59+
"xo": "^0.49.0"
60+
}
61+
}

0 commit comments

Comments
 (0)