forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.ts
148 lines (116 loc) · 3.01 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
/**
* A module to print ANSI terminal colors. Inspired by chalk, kleur, and colors
* on npm.
*
* ```
* import { bgBlue, red, bold } from "https://deno.land/std/fmt/colors.ts";
* console.log(bgBlue(red(bold("Hello world!"))));
* ```
*
* This module supports `NO_COLOR` environmental variable disabling any coloring
* if `NO_COLOR` is set.
*/
const { noColor } = Deno;
interface Code {
open: string;
close: string;
regexp: RegExp;
}
let enabled = !noColor;
export function setEnabled(value: boolean): void {
if (noColor) {
return;
}
enabled = value;
}
export function getEnabled(): boolean {
return enabled;
}
function code(open: number, close: number): Code {
return {
open: `\x1b[${open}m`,
close: `\x1b[${close}m`,
regexp: new RegExp(`\\x1b\\[${close}m`, "g")
};
}
function run(str: string, code: Code): string {
return enabled
? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
: str;
}
export function reset(str: string): string {
return run(str, code(0, 0));
}
export function bold(str: string): string {
return run(str, code(1, 22));
}
export function dim(str: string): string {
return run(str, code(2, 22));
}
export function italic(str: string): string {
return run(str, code(3, 23));
}
export function underline(str: string): string {
return run(str, code(4, 24));
}
export function inverse(str: string): string {
return run(str, code(7, 27));
}
export function hidden(str: string): string {
return run(str, code(8, 28));
}
export function strikethrough(str: string): string {
return run(str, code(9, 29));
}
export function black(str: string): string {
return run(str, code(30, 39));
}
export function red(str: string): string {
return run(str, code(31, 39));
}
export function green(str: string): string {
return run(str, code(32, 39));
}
export function yellow(str: string): string {
return run(str, code(33, 39));
}
export function blue(str: string): string {
return run(str, code(34, 39));
}
export function magenta(str: string): string {
return run(str, code(35, 39));
}
export function cyan(str: string): string {
return run(str, code(36, 39));
}
export function white(str: string): string {
return run(str, code(37, 39));
}
export function gray(str: string): string {
return run(str, code(90, 39));
}
export function bgBlack(str: string): string {
return run(str, code(40, 49));
}
export function bgRed(str: string): string {
return run(str, code(41, 49));
}
export function bgGreen(str: string): string {
return run(str, code(42, 49));
}
export function bgYellow(str: string): string {
return run(str, code(43, 49));
}
export function bgBlue(str: string): string {
return run(str, code(44, 49));
}
export function bgMagenta(str: string): string {
return run(str, code(45, 49));
}
export function bgCyan(str: string): string {
return run(str, code(46, 49));
}
export function bgWhite(str: string): string {
return run(str, code(47, 49));
}