-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddLogStyle.js
73 lines (69 loc) · 1.21 KB
/
addLogStyle.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
/**
* @param {string} text
* @param {string} key - 'b i red :blue'
*/
function addLogStyle(text, key){
key = key == null ? '' : key;
var ANSIStyles = {
reset: 0,
bold: 1,
dim: 2,
italic: 3,
under: 4,
reverse: 7,
hidden: 8,
through: 9,
black: 30,
red: 31,
green: 32,
yellow: 33,
blue: 34,
magenta: 35,
cyan: 36,
white: 37,
grey: 90,
};
var keymap = {
b: 'bold',
i: 'italic',
u: 'under',
r: 'reverse',
0: 'reset',
};
key.split(' ').forEach(function(key){
if(['', ','].indexOf(key) === -1){
key = keymap[key] || key;
text = parseStyle(text, key);
}
});
return text;
function parseStyle(text, key){
var code, bgMode;
if(key.indexOf(':') > -1){
key = key.split(':');
if(key[0] !== 'fg') bgMode = 1;
key = key[1];
}
if(key in ANSIStyles){
code = ANSIStyles[key];
if(bgMode) code += 10;
text = '\u001b[' + code + 'm' + text + '\u001b[' + getCloseCode(code) + 'm';
}
return text;
}
function getCloseCode(code){
return 0;
if(code === 0){
return 0;
}else if(code < 3){
return 22;
}else if(code < 30){
return code + 20;
}else if(code < 38 || code === 90){
return 39;
}else if(code < 48){
return 49;
}
return 0;
}
}