source: node_modules/supports-color/index.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1'use strict';
2const os = require('os');
3const tty = require('tty');
4const hasFlag = require('has-flag');
5
6const {env} = process;
7
8let forceColor;
9if (hasFlag('no-color') ||
10 hasFlag('no-colors') ||
11 hasFlag('color=false') ||
12 hasFlag('color=never')) {
13 forceColor = 0;
14} else if (hasFlag('color') ||
15 hasFlag('colors') ||
16 hasFlag('color=true') ||
17 hasFlag('color=always')) {
18 forceColor = 1;
19}
20
21if ('FORCE_COLOR' in env) {
22 if (env.FORCE_COLOR === 'true') {
23 forceColor = 1;
24 } else if (env.FORCE_COLOR === 'false') {
25 forceColor = 0;
26 } else {
27 forceColor = env.FORCE_COLOR.length === 0 ? 1 : Math.min(parseInt(env.FORCE_COLOR, 10), 3);
28 }
29}
30
31function translateLevel(level) {
32 if (level === 0) {
33 return false;
34 }
35
36 return {
37 level,
38 hasBasic: true,
39 has256: level >= 2,
40 has16m: level >= 3
41 };
42}
43
44function supportsColor(haveStream, streamIsTTY) {
45 if (forceColor === 0) {
46 return 0;
47 }
48
49 if (hasFlag('color=16m') ||
50 hasFlag('color=full') ||
51 hasFlag('color=truecolor')) {
52 return 3;
53 }
54
55 if (hasFlag('color=256')) {
56 return 2;
57 }
58
59 if (haveStream && !streamIsTTY && forceColor === undefined) {
60 return 0;
61 }
62
63 const min = forceColor || 0;
64
65 if (env.TERM === 'dumb') {
66 return min;
67 }
68
69 if (process.platform === 'win32') {
70 // Windows 10 build 10586 is the first Windows release that supports 256 colors.
71 // Windows 10 build 14931 is the first release that supports 16m/TrueColor.
72 const osRelease = os.release().split('.');
73 if (
74 Number(osRelease[0]) >= 10 &&
75 Number(osRelease[2]) >= 10586
76 ) {
77 return Number(osRelease[2]) >= 14931 ? 3 : 2;
78 }
79
80 return 1;
81 }
82
83 if ('CI' in env) {
84 if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI', 'GITHUB_ACTIONS', 'BUILDKITE'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
85 return 1;
86 }
87
88 return min;
89 }
90
91 if ('TEAMCITY_VERSION' in env) {
92 return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
93 }
94
95 if (env.COLORTERM === 'truecolor') {
96 return 3;
97 }
98
99 if ('TERM_PROGRAM' in env) {
100 const version = parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
101
102 switch (env.TERM_PROGRAM) {
103 case 'iTerm.app':
104 return version >= 3 ? 3 : 2;
105 case 'Apple_Terminal':
106 return 2;
107 // No default
108 }
109 }
110
111 if (/-256(color)?$/i.test(env.TERM)) {
112 return 2;
113 }
114
115 if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
116 return 1;
117 }
118
119 if ('COLORTERM' in env) {
120 return 1;
121 }
122
123 return min;
124}
125
126function getSupportLevel(stream) {
127 const level = supportsColor(stream, stream && stream.isTTY);
128 return translateLevel(level);
129}
130
131module.exports = {
132 supportsColor: getSupportLevel,
133 stdout: translateLevel(supportsColor(true, tty.isatty(1))),
134 stderr: translateLevel(supportsColor(true, tty.isatty(2)))
135};
Note: See TracBrowser for help on using the repository browser.