-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcolors.ts
45 lines (38 loc) · 1.13 KB
/
colors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* @copyright Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
* @license {@link https://github.com/Marak/colors.js/blob/master/LICENSE MIT}
* @description modified version of https://github.com/Marak/colors.js/blob/master/lib/styles.js
*/
// usage '\u001b[Xm' // where X is the number
export const colors = (str: string, clr: Colors) => {
const c = colorCodes[clr]
const open = `\u001b[${c[0]}m`
const close = `\u001b[${c[1]}m`
return `${open}${str}${close}`
}
export const removeColors = str => {
let _str = encodeURI(str)
_str = _str.replace(/%1B%5B\d+m/gm, '')
return decodeURI(_str)
}
export type Colors = keyof typeof colorCodes
const colorCodes = {
bold: [1, 22], // no color, but still useful
black: [30, 39],
red: [31, 39],
green: [32, 39],
yellow: [33, 39],
blue: [34, 39],
magenta: [35, 39],
cyan: [36, 39],
white: [37, 39],
gray: [90, 39], // brightBlack
grey: [90, 39], // brightBlack
brightRed: [91, 39],
brightGreen: [92, 39],
brightYellow: [93, 39],
brightBlue: [94, 39],
brightMagenta: [95, 39],
brightCyan: [96, 39],
brightWhite: [97, 39]
}