/
ansiparse.js
150 lines (141 loc) · 3.51 KB
/
ansiparse.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
ansiparse = function (str) {
//
// I'm terrible at writing parsers.
//
var matchingControl = null,
matchingData = null,
matchingText = null,
ansiState = [],
result = [],
state = {};
//
// General workflow for this thing is:
// \033\[33mText
// | | |
// | | matchingText
// | matchingData
// matchingControl
//
// In further steps we hope it's all going to be fine. It usually is.
//
for (var i = 0; i < str.length; i++) {
if (matchingControl != null) {
if (matchingControl == '\033' && str[i] == '\[') {
//
// We've matched full control code. Lets start matching formating data.
//
matchingControl = null;
matchingData = '';
}
else {
//
// We failed to match anything - most likely a bad control code. We
// go back to matching regular strings.
//
matchingControl = null;
}
continue;
}
else if (matchingData != null) {
if (str[i] == ';') {
//
// `;` separates many formatting codes, for example: `\033[33;43m`
// means that both `33` and `43` should be applied.
//
// TODO: this can be simplified by modifying state here.
//
ansiState.push(matchingData);
matchingData = '';
}
else if (str[i] == 'm') {
//
// `m` finished whole formatting code. We can proceed to matching
// formatted text.
//
ansiState.push(matchingData);
matchingData = null;
matchingText = '';
//
// Convert matched formatting data into user-friendly state object.
//
// TODO: DRY.
//
ansiState.forEach(function (ansiCode) {
if ((30 <= ansiCode) && (ansiCode <= 37)) {
state.foreground = ansiparse.foregroundColors[ansiCode];
}
else if ((40 <= ansiCode) && (ansiCode <= 47)) {
state.background = ansiparse.backgroundColors[ansiCode];
}
else if (ansiCode == 39) {
delete state.foreground;
}
else if (ansiCode == 49) {
delete state.background;
}
else if (ansiCode == 1) {
state.bold = true;
}
else if (ansiCode == 3) {
state.italic = true;
}
else if (ansiCode == 4) {
state.underline = true;
}
else if (ansiCode == 22) {
state.bold = false;
}
else if (ansiCode == 23) {
state.italic = false;
}
else if (ansiCode == 24) {
state.underline = false;
}
});
ansiState = [];
}
else {
matchingData += str[i];
}
continue;
}
if (str[i] == '\033') {
matchingControl = str[i];
//
// "emit" matched text with correct state
//
if (matchingText) {
state.text = matchingText;
result.push(state);
state = {};
}
}
else {
matchingText += str[i];
}
}
return result;
}
ansiparse.foregroundColors = {
'30': 'black',
'31': 'red',
'32': 'green',
'33': 'yellow',
'34': 'blue',
'35': 'magenta',
'36': 'cyan',
'37': 'white'
};
ansiparse.backgroundColors = {
'40': 'black',
'41': 'red',
'42': 'green',
'43': 'yellow',
'44': 'blue',
'45': 'magenta',
'46': 'cyan',
'47': 'white'
};
if (typeof module == "object" && typeof window == "undefined") {
module.exports = ansiparse;
}