dcaa45412304d464fe37e9ceab64ba68ac1a092a
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / inquirer / node_modules / supports-color / index.js
1 'use strict';
2 const os = require('os');
3 const tty = require('tty');
4 const hasFlag = require('has-flag');
5
6 const {env} = process;
7
8 let forceColor;
9 if (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
21 if ('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
31 function 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
44 function 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'].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 ('GITHUB_ACTIONS' in env) {
96                 return 1;
97         }
98
99         if (env.COLORTERM === 'truecolor') {
100                 return 3;
101         }
102
103         if ('TERM_PROGRAM' in env) {
104                 const version = parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
105
106                 switch (env.TERM_PROGRAM) {
107                         case 'iTerm.app':
108                                 return version >= 3 ? 3 : 2;
109                         case 'Apple_Terminal':
110                                 return 2;
111                         // No default
112                 }
113         }
114
115         if (/-256(color)?$/i.test(env.TERM)) {
116                 return 2;
117         }
118
119         if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
120                 return 1;
121         }
122
123         if ('COLORTERM' in env) {
124                 return 1;
125         }
126
127         return min;
128 }
129
130 function getSupportLevel(stream) {
131         const level = supportsColor(stream, stream && stream.isTTY);
132         return translateLevel(level);
133 }
134
135 module.exports = {
136         supportsColor: getSupportLevel,
137         stdout: translateLevel(supportsColor(true, tty.isatty(1))),
138         stderr: translateLevel(supportsColor(true, tty.isatty(2)))
139 };