-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcolor.js
More file actions
136 lines (121 loc) · 3.3 KB
/
Copy pathconcolor.js
File metadata and controls
136 lines (121 loc) · 3.3 KB
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
'use strict';
const COLORS = [
/* 0 */ 'black',
/* 1 */ 'red',
/* 2 */ 'green',
/* 3 */ 'yellow',
/* 4 */ 'blue',
/* 5 */ 'magenta',
/* 6 */ 'cyan',
/* 7 */ 'white',
];
const COLOR_INDEX = new Map(COLORS.map((c, i) => [c, i]));
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 ANSI_INDEX = new Map(ANSI.map((a, i) => [a, i + 1]));
const esc = (codes, s) => `\x1b[${codes}m${s}\x1b[0m`;
const stylize = (styles, s) => {
const list = styles.split(',');
const codes = [];
for (const style of list) {
if (style.length === 1) {
const code = ANSI_INDEX.get(style);
if (code !== undefined) codes.push(code);
} else {
const [foreground, background] = style.split('/');
const fgIndex = COLOR_INDEX.get(foreground);
if (fgIndex !== undefined) codes.push(`3${fgIndex}`);
if (background) {
const bgIndex = COLOR_INDEX.get(background);
if (bgIndex !== undefined) codes.push(`4${bgIndex}`);
}
}
}
return codes.length > 0 ? esc(codes.join(';'), s) : s;
};
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 styleList = str.substring(1, pos);
const value = stylize(styleList, val);
const rest = str.substring(pos + 1);
result.push(value, rest);
} else {
result.push(val, str);
}
}
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.black = concolor('black');
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;