/
colorize.ts
34 lines (28 loc) · 908 Bytes
/
colorize.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
import { inspect } from 'node:util'
import { hasColor } from './has-color'
type ColMap = Record<string, (t: string) => string>
const useColor = typeof process !== 'undefined' &&
process.stdout.isTTY &&
hasColor()
const cols = (color: string, text: string): string => {
const codes = inspect.colors[color]
return `\x1b[${codes?.[0]}m${text}\x1b[${codes?.[1]}m`
}
/**
* Basic set of fns to add colors to console output in Node.
*
* Options:
*
* bold, italic, underline, inverse,
* white, grey, black, blue, cyan, green, magenta, red, yellow
* @example
* console.log(colorize.bold(colorize.blue('foo'))) // => '\'\\u001b[34mfoo\\u001b[39m\''
*/
const _colorize = (): ColMap => {
const val: ColMap = {}
Object.keys(inspect.colors).forEach((color: string): void => {
val[color] = (text) => useColor ? cols(color, text) : text
})
return val
}
export const colorize = _colorize()