-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcolor.js
129 lines (116 loc) · 3.06 KB
/
concolor.js
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
'use strict';
const COLORS = [
/* 1 */ 'black',
/* 2 */ 'red',
/* 3 */ 'green',
/* 4 */ 'yellow',
/* 5 */ 'blue',
/* 6 */ 'magenta',
/* 7 */ 'cyan',
/* 8 */ 'white',
];
const ANSI = [
/* 1 */ 'b', // bold (increased intensity)
/* 2 */ 'f', // faint (decreased intensity)
/* 3 */ 'i', // italic
/* 4 */ 'u', // underline
/* 5 */ 'l', // blink slow
/* 6 */ 'h', // blink rapid
/* 7 */ 'n', // negative
/* 8 */ 'c', // conceal
/* 9 */ 's', // strikethrough
];
const esc = (code, s) => `\x1b[${code}m${s}\x1b[0m`;
const stylize = (styles, s) => {
const list = styles.split(',');
let result = s;
for (const style of list) {
if (style.length === 1) {
const code = ANSI.indexOf(style) + 1;
result = esc(code, result);
} else {
const [foreground, background] = style.split('/');
const index = COLORS.indexOf(foreground);
if (index > -1) result = esc('3' + index.toString(), result);
if (background) {
const index = COLORS.indexOf(background);
if (index > -1) result = esc('4' + index.toString(), result);
}
}
}
return result;
};
const tag =
(styles) =>
(strings, ...values) => {
if (typeof strings === 'string') {
return stylize(styles, strings);
}
const result = [strings[0]];
let i = 1;
for (const val of values) {
const str = strings[i++];
result.push(val, str);
}
return stylize(styles, result.join(''));
};
const theme = (tags) => {
const styles = (strings, ...values) => {
const result = [strings[0]];
let i = 1;
for (const val of values) {
const str = strings[i++];
for (const name in val) {
const style = styles[name];
const value = val[name];
const res = style(value);
result.push(res);
}
result.push(str);
}
return result.join('');
};
for (const name in tags) {
styles[name] = tag(tags[name]);
}
return styles;
};
const concolor = (strings, ...values) => {
if (typeof strings === 'string') {
return tag(strings);
}
if (!Array.isArray(strings)) {
return theme(strings);
}
const result = [strings[0]];
let i = 1;
for (const val of values) {
const str = strings[i++];
if (str.startsWith('(')) {
const pos = str.indexOf(')');
const styles = str.substring(1, pos);
const value = stylize(styles, val);
const rest = str.substring(pos + 1);
result.push(value, rest);
}
}
return result.join('');
};
concolor.b = concolor('b');
concolor.i = concolor('i');
concolor.u = concolor('u');
concolor.em = concolor('b');
concolor.error = concolor('b,red');
concolor.info = concolor('b,green');
concolor.warn = concolor('b,yellow');
concolor.debug = concolor('b,blue');
concolor.success = concolor.info;
concolor.fail = concolor.error;
concolor.red = concolor('red');
concolor.green = concolor('green');
concolor.yellow = concolor('yellow');
concolor.blue = concolor('blue');
concolor.magenta = concolor('magenta');
concolor.cyan = concolor('cyan');
concolor.white = concolor('white');
module.exports = concolor;