-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.ts
47 lines (34 loc) · 1.4 KB
/
color.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
46
47
interface IANSIEscapeCode {
styleNumber: number,
}
export interface IANSIEscapeCodes extends Array<IANSIEscapeCode> { }
interface IANSIStyle {
bold?:boolean,
italic?: boolean,
underline?: boolean,
strikeThrough?: boolean
}
const colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']
export function makeStyle(
color: 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white',
bright:boolean,
background: boolean,
style: IANSIStyle ): IANSIEscapeCodes{
const escapeCodes:IANSIEscapeCodes = []
escapeCodes.push({
styleNumber: colors.indexOf(color) + (bright? 60 : 0) + (background? 10: 0) + 30
})
if(style.bold) escapeCodes.push({styleNumber: 1})
if(style.italic) escapeCodes.push({styleNumber: 3})
if(style.underline) escapeCodes.push({styleNumber: 4})
if(style.strikeThrough) escapeCodes.push({styleNumber: 9})
return escapeCodes
}
export var useColor: boolean = true
export function color(value: string, styles: IANSIEscapeCodes, resetCode:number = 0):string{
if(!useColor) return value
const escapeSequence = "\u001b[%sm";
const escapedStyles = styles.map( s => escapeSequence.replace("%s", s.styleNumber.toString())).join('')
const reset = escapeSequence.replace("%s", resetCode.toString())
return escapedStyles + value + reset
}