UNPKG

5.76 kBJavaScriptView Raw
1'use strict'
2
3const isObject = (val) =>
4 val !== null && typeof val === 'object' && !Array.isArray(val)
5
6// this is a modified version of https://github.com/chalk/ansi-regex (MIT License)
7/* eslint-disable-next-line no-control-regex */
8const ANSI_REGEX = /[\u001b\u009b][[\]#;?()]*(?:(?:(?:[^\W_]*;?[^\W_]*)\u0007)|(?:(?:[0-9]{1,4}(;[0-9]{0,4})*)?[~0-9=<>cf-nqrtyA-PRZ]))/g
9
10const create = () => {
11 const colors = { enabled: true, visible: true, styles: {}, keys: {} }
12
13 if ('FORCE_COLOR' in process.env) {
14 colors.enabled = process.env.FORCE_COLOR !== '0'
15 }
16
17 const ansi = (style) => {
18 let open = (style.open = `\u001b[${style.codes[0]}m`)
19 let close = (style.close = `\u001b[${style.codes[1]}m`)
20 let regex = (style.regex = new RegExp(`\\u001b\\[${style.codes[1]}m`, 'g'))
21 style.wrap = (input, newline) => {
22 if (input.includes(close)) input = input.replace(regex, close + open)
23 let output = open + input + close
24 // see https://github.com/chalk/chalk/pull/92, thanks to the
25 // chalk contributors for this fix. However, we've confirmed that
26 // this issue is also present in Windows terminals
27 return newline ? output.replace(/\r*\n/g, `${close}$&${open}`) : output
28 }
29 return style
30 }
31
32 const wrap = (style, input, newline) => {
33 return typeof style === 'function'
34 ? style(input)
35 : style.wrap(input, newline)
36 }
37
38 const style = (input, stack) => {
39 if (input === '' || input == null) return ''
40 if (colors.enabled === false) return input
41 if (colors.visible === false) return ''
42 let str = '' + input
43 let nl = str.includes('\n')
44 let n = stack.length
45 if (n > 0 && stack.includes('unstyle')) {
46 stack = [...new Set(['unstyle', ...stack])].reverse()
47 }
48 while (n-- > 0) str = wrap(colors.styles[stack[n]], str, nl)
49 return str
50 }
51
52 const define = (name, codes, type) => {
53 colors.styles[name] = ansi({ name, codes })
54 let keys = colors.keys[type] || (colors.keys[type] = [])
55 keys.push(name)
56
57 Reflect.defineProperty(colors, name, {
58 configurable: true,
59 enumerable: true,
60 set(value) {
61 colors.alias(name, value)
62 },
63 get() {
64 let color = (input) => style(input, color.stack)
65 Reflect.setPrototypeOf(color, colors)
66 color.stack = this.stack ? this.stack.concat(name) : [name]
67 return color
68 },
69 })
70 }
71
72 define('reset', [0, 0], 'modifier')
73 define('bold', [1, 22], 'modifier')
74 define('dim', [2, 22], 'modifier')
75 define('italic', [3, 23], 'modifier')
76 define('underline', [4, 24], 'modifier')
77 define('inverse', [7, 27], 'modifier')
78 define('hidden', [8, 28], 'modifier')
79 define('strikethrough', [9, 29], 'modifier')
80
81 define('black', [30, 39], 'color')
82 define('red', [31, 39], 'color')
83 define('green', [32, 39], 'color')
84 define('yellow', [33, 39], 'color')
85 define('blue', [34, 39], 'color')
86 define('magenta', [35, 39], 'color')
87 define('cyan', [36, 39], 'color')
88 define('white', [37, 39], 'color')
89 define('gray', [90, 39], 'color')
90 define('grey', [90, 39], 'color')
91
92 define('bgBlack', [40, 49], 'bg')
93 define('bgRed', [41, 49], 'bg')
94 define('bgGreen', [42, 49], 'bg')
95 define('bgYellow', [43, 49], 'bg')
96 define('bgBlue', [44, 49], 'bg')
97 define('bgMagenta', [45, 49], 'bg')
98 define('bgCyan', [46, 49], 'bg')
99 define('bgWhite', [47, 49], 'bg')
100
101 define('blackBright', [90, 39], 'bright')
102 define('redBright', [91, 39], 'bright')
103 define('greenBright', [92, 39], 'bright')
104 define('yellowBright', [93, 39], 'bright')
105 define('blueBright', [94, 39], 'bright')
106 define('magentaBright', [95, 39], 'bright')
107 define('cyanBright', [96, 39], 'bright')
108 define('whiteBright', [97, 39], 'bright')
109
110 define('bgBlackBright', [100, 49], 'bgBright')
111 define('bgRedBright', [101, 49], 'bgBright')
112 define('bgGreenBright', [102, 49], 'bgBright')
113 define('bgYellowBright', [103, 49], 'bgBright')
114 define('bgBlueBright', [104, 49], 'bgBright')
115 define('bgMagentaBright', [105, 49], 'bgBright')
116 define('bgCyanBright', [106, 49], 'bgBright')
117 define('bgWhiteBright', [107, 49], 'bgBright')
118
119 colors.ansiRegex = ANSI_REGEX
120 colors.hasColor = colors.hasAnsi = (str) => {
121 colors.ansiRegex.lastIndex = 0
122 return typeof str === 'string' && str !== '' && colors.ansiRegex.test(str)
123 }
124
125 colors.alias = (name, color) => {
126 let fn = typeof color === 'string' ? colors[color] : color
127
128 if (typeof fn !== 'function') {
129 throw new TypeError(
130 'Expected alias to be the name of an existing color (string) or a function',
131 )
132 }
133
134 if (!fn.stack) {
135 Reflect.defineProperty(fn, 'name', { value: name })
136 colors.styles[name] = fn
137 fn.stack = [name]
138 }
139
140 Reflect.defineProperty(colors, name, {
141 configurable: true,
142 enumerable: true,
143 set(value) {
144 colors.alias(name, value)
145 },
146 get() {
147 let color = (input) => style(input, color.stack)
148 Reflect.setPrototypeOf(color, colors)
149 color.stack = this.stack ? this.stack.concat(fn.stack) : fn.stack
150 return color
151 },
152 })
153 }
154
155 colors.theme = (custom) => {
156 if (!isObject(custom)) throw new TypeError('Expected theme to be an object')
157 for (let name of Object.keys(custom)) {
158 colors.alias(name, custom[name])
159 }
160 return colors
161 }
162
163 colors.alias('unstyle', (str) => {
164 if (typeof str === 'string' && str !== '') {
165 colors.ansiRegex.lastIndex = 0
166 return str.replace(colors.ansiRegex, '')
167 }
168 return ''
169 })
170
171 colors.alias('noop', (str) => str)
172 colors.none = colors.clear = colors.noop
173
174 colors.stripColor = colors.unstyle
175 colors.define = define
176 return colors
177}
178
179module.exports = create()
180module.exports.create = create