-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathansi-html.js
More file actions
300 lines (276 loc) · 6.66 KB
/
ansi-html.js
File metadata and controls
300 lines (276 loc) · 6.66 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* eslint-disable */
// @ts-nocheck
/**
* The following code is modified based on mahdyar/ansi-html-community
* Added support for 24-bit RGB colors.
*/
'use strict';
module.exports = ansiHTML;
// Reference to https://github.com/sindresorhus/ansi-regex
var _regANSI =
/(?:(?:\u001b\[)|\u009b)(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\u001b[A-M]/;
var _defColors = {
reset: ['fff', '000'], // [FOREGROUND_COLOR, BACKGROUND_COLOR]
black: '000',
red: 'ff0000',
green: '209805',
yellow: 'e8bf03',
blue: '0000ff',
magenta: 'ff00ff',
cyan: '00ffee',
lightgrey: 'f0f0f0',
darkgrey: '888',
};
var _styles = {
30: 'black',
31: 'red',
32: 'green',
33: 'yellow',
34: 'blue',
35: 'magenta',
36: 'cyan',
37: 'lightgrey',
};
var _colorMode = {
2: 'rgb',
};
var _openTags = {
1: 'font-weight:bold', // bold
2: 'opacity:0.5', // dim
3: '<i>', // italic
4: '<u>', // underscore
8: 'display:none', // hidden
9: '<del>', // delete
38: function (match) {
// color
var mode = _colorMode[match[0]];
if (mode === 'rgb') {
var r, g, b;
r = match[1];
g = match[2];
b = match[3];
match.advance(4);
return 'color: rgb(' + r + ',' + g + ',' + b + ')';
}
},
48: function (match) {
// background color
var mode = _colorMode[match[0]];
if (mode === 'rgb') {
var r, g, b;
r = match[1];
g = match[2];
b = match[3];
match.advance(4);
return 'background-color: rgb(' + r + ',' + g + ',' + b + ')';
}
},
};
var _openTagToCloseTag = {
3: '23',
4: '24',
9: '29',
};
var _closeTags = {
0: function (ansiCodes) {
if (!ansiCodes) return '</span>';
if (!ansiCodes.length) return '';
var code,
ret = '';
while ((code = ansiCodes.pop())) {
var closeTag = _openTagToCloseTag[code];
if (closeTag) {
ret += _closeTags[closeTag];
continue;
}
ret += '</span>';
}
return ret;
},
23: '</i>', // reset italic
24: '</u>', // reset underscore
29: '</del>', // reset delete
};
[21, 22, 27, 28, 39, 49].forEach(function (n) {
_closeTags[n] = '</span>';
});
/**
* Normalize ';<seq>' | '<seq>' -> '<seq>'
* @param {string | null} seq
* @returns {null | string}
*/
function normalizeSeq(seq) {
if (seq === null || seq === undefined) return null;
if (seq.startsWith(';')) {
return seq.slice(1);
}
return seq;
}
/**
* Converts text with ANSI color codes to HTML markup.
* @param {String} text
* @returns {*}
*/
function ansiHTML(text) {
// Returns the text if the string has no ANSI escape code.
if (!_regANSI.test(text)) {
return text;
}
// Cache opened sequence.
var ansiCodes = [];
// Replace with markup.
var ret = text.replace(
/\033\[(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?m/g,
function (m) {
var match = m.match(/(;?\d+)/g).map(normalizeSeq);
Object.defineProperty(match, 'advance', {
value: function (count) {
this.splice(0, count);
},
});
var seq,
rep = '';
while ((seq = match[0])) {
match.advance(1);
rep += applySeq(seq);
}
return rep;
function applySeq(seq) {
var other = _openTags[seq];
if (
other &&
(other = typeof other === 'function' ? other(match) : other)
) {
// If reset signal is encountered, we have to reset everything.
var ret = '';
if (seq === '0') {
ret += _closeTags[seq](ansiCodes);
}
// If current sequence has been opened, close it.
if (!!~ansiCodes.indexOf(seq)) {
// eslint-disable-line no-extra-boolean-cast
ansiCodes.pop();
return '</span>';
}
// Open tag.
ansiCodes.push(seq);
return (
ret + (other[0] === '<' ? other : '<span style="' + other + ';">')
);
}
var ct = _closeTags[seq];
if (typeof ct === 'function') {
return ct(ansiCodes);
} else if (ct) {
// Pop sequence
ansiCodes.pop();
return ct;
}
return '';
}
},
);
// Make sure tags are closed.
var l = ansiCodes.length;
l > 0 && (ret += Array(l + 1).join('</span>'));
return ret;
}
/**
* Customize colors.
* @param {Object} colors reference to _defColors
*/
ansiHTML.setColors = function (colors) {
if (typeof colors !== 'object') {
throw new Error('`colors` parameter must be an Object.');
}
var _finalColors = {};
for (var key in _defColors) {
var hex = colors.hasOwnProperty(key) ? colors[key] : null;
if (!hex) {
_finalColors[key] = _defColors[key];
continue;
}
if ('reset' === key) {
if (typeof hex === 'string') {
hex = [hex];
}
if (
!Array.isArray(hex) ||
hex.length === 0 ||
hex.some(function (h) {
return typeof h !== 'string';
})
) {
throw new Error(
'The value of `' +
key +
'` property must be an Array and each item could only be a hex string, e.g.: FF0000',
);
}
var defHexColor = _defColors[key];
if (!hex[0]) {
hex[0] = defHexColor[0];
}
if (hex.length === 1 || !hex[1]) {
hex = [hex[0]];
hex.push(defHexColor[1]);
}
hex = hex.slice(0, 2);
} else if (typeof hex !== 'string') {
throw new Error(
'The value of `' +
key +
'` property must be a hex string, e.g.: FF0000',
);
}
_finalColors[key] = hex;
}
_setTags(_finalColors);
};
/**
* Reset colors.
*/
ansiHTML.reset = function () {
_setTags(_defColors);
};
/**
* Expose tags, including open and close.
* @type {Object}
*/
ansiHTML.tags = {};
if (Object.defineProperty) {
Object.defineProperty(ansiHTML.tags, 'open', {
get: function () {
return _openTags;
},
});
Object.defineProperty(ansiHTML.tags, 'close', {
get: function () {
return _closeTags;
},
});
} else {
ansiHTML.tags.open = _openTags;
ansiHTML.tags.close = _closeTags;
}
function _setTags(colors) {
// reset all
_openTags['0'] =
'font-weight:normal;opacity:1;color:#' +
colors.reset[0] +
';background:#' +
colors.reset[1];
// inverse
_openTags['7'] =
'color:#' + colors.reset[1] + ';background:#' + colors.reset[0];
// dark grey
_openTags['90'] = 'color:#' + colors.darkgrey;
for (var code in _styles) {
var color = _styles[code];
var oriColor = colors[color] || '000';
_openTags[code] = 'color:#' + oriColor;
code = parseInt(code);
_openTags[(code + 10).toString()] = 'background:#' + oriColor;
}
}
ansiHTML.reset();