2 const os = require('os');
3 const tty = require('tty');
4 const hasFlag = require('has-flag');
9 if (hasFlag('no-color') ||
10 hasFlag('no-colors') ||
11 hasFlag('color=false') ||
12 hasFlag('color=never')) {
14 } else if (hasFlag('color') ||
16 hasFlag('color=true') ||
17 hasFlag('color=always')) {
21 if ('FORCE_COLOR' in env) {
22 if (env.FORCE_COLOR === 'true') {
24 } else if (env.FORCE_COLOR === 'false') {
27 forceColor = env.FORCE_COLOR.length === 0 ? 1 : Math.min(parseInt(env.FORCE_COLOR, 10), 3);
31 function translateLevel(level) {
44 function supportsColor(haveStream, streamIsTTY) {
45 if (forceColor === 0) {
49 if (hasFlag('color=16m') ||
50 hasFlag('color=full') ||
51 hasFlag('color=truecolor')) {
55 if (hasFlag('color=256')) {
59 if (haveStream && !streamIsTTY && forceColor === undefined) {
63 const min = forceColor || 0;
65 if (env.TERM === 'dumb') {
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('.');
74 Number(osRelease[0]) >= 10 &&
75 Number(osRelease[2]) >= 10586
77 return Number(osRelease[2]) >= 14931 ? 3 : 2;
84 if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI', 'GITHUB_ACTIONS', 'BUILDKITE'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
91 if ('TEAMCITY_VERSION' in env) {
92 return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
95 if (env.COLORTERM === 'truecolor') {
99 if ('TERM_PROGRAM' in env) {
100 const version = parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
102 switch (env.TERM_PROGRAM) {
104 return version >= 3 ? 3 : 2;
105 case 'Apple_Terminal':
111 if (/-256(color)?$/i.test(env.TERM)) {
115 if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
119 if ('COLORTERM' in env) {
126 function getSupportLevel(stream) {
127 const level = supportsColor(stream, stream && stream.isTTY);
128 return translateLevel(level);
132 supportsColor: getSupportLevel,
133 stdout: translateLevel(supportsColor(true, tty.isatty(1))),
134 stderr: translateLevel(supportsColor(true, tty.isatty(2)))