Skip to content

Commit 0a5d0f8

Browse files
authoredJun 22, 2021
Add support for xterm256 background color sequences (#92)
* Currently, only 8bit/256-color foreground color sequences are supported (ESC[ 38;5;⟨n⟩ m) * This PR adds additional patterns for 8bit/256-color backgrond color sequences (ESC[ 48;5;⟨n⟩ m) sequences
1 parent e6c3a5b commit 0a5d0f8

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed
 

‎src/ansi_to_html.js

+20-10
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ function generateOutput(stack, token, data, options) {
103103
result = pushText(data, options);
104104
} else if (token === 'display') {
105105
result = handleDisplay(stack, data, options);
106-
} else if (token === 'xterm256') {
106+
} else if (token === 'xterm256Foreground') {
107107
result = pushForegroundColor(stack, options.colors[data]);
108+
} else if (token === 'xterm256Background') {
109+
result = pushBackgroundColor(stack, options.colors[data]);
108110
} else if (token === 'rgb') {
109111
result = handleRgb(stack, data);
110112
}
@@ -119,10 +121,10 @@ function generateOutput(stack, token, data, options) {
119121
*/
120122
function handleRgb(stack, data) {
121123
data = data.substring(2).slice(0, -1);
122-
const operation = +data.substr(0,2);
124+
const operation = +data.substr(0, 2);
123125

124126
const color = data.substring(5).split(';');
125-
const rgb = color.map(function(value) {
127+
const rgb = color.map(function (value) {
126128
return ('0' + Number(value).toString(16)).substr(-2);
127129
}).join('');
128130

@@ -280,7 +282,7 @@ function pushTag(stack, tag, style) {
280282
* @param {string} style
281283
* @returns {string}
282284
*/
283-
function pushStyle (stack, style) {
285+
function pushStyle(stack, style) {
284286
return pushTag(stack, 'span', style);
285287
}
286288

@@ -323,8 +325,13 @@ function tokenize(text, options, callback) {
323325
return '';
324326
}
325327

326-
function removeXterm256(m, g1) {
327-
callback('xterm256', g1);
328+
function removeXterm256Foreground(m, g1) {
329+
callback('xterm256Foreground', g1);
330+
return '';
331+
}
332+
333+
function removeXterm256Background(m, g1) {
334+
callback('xterm256Background', g1);
328335
return '';
329336
}
330337

@@ -380,11 +387,14 @@ function tokenize(text, options, callback) {
380387
sub: rgb
381388
}, {
382389
pattern: /^\x1b\[38;5;(\d+)m/,
383-
sub: removeXterm256
390+
sub: removeXterm256Foreground
391+
}, {
392+
pattern: /^\x1b\[48;5;(\d+)m/,
393+
sub: removeXterm256Background
384394
}, {
385395
pattern: /^\n/,
386396
sub: newline
387-
},{
397+
}, {
388398
pattern: /^\r+\n/,
389399
sub: newline
390400
}, {
@@ -486,7 +496,7 @@ class Filter {
486496
* @param {boolean=} options.stream Save style state across invocations of `toHtml()`.
487497
* @param {(string[] | {[code: number]: string})=} options.colors Can override specific colors or the entire ANSI palette.
488498
*/
489-
constructor (options) {
499+
constructor(options) {
490500
options = options || {};
491501

492502
if (options.colors) {
@@ -501,7 +511,7 @@ class Filter {
501511
* @param {string | string[]} input
502512
* @returns {string}
503513
*/
504-
toHtml (input) {
514+
toHtml(input) {
505515
input = typeof input === 'string' ? [input] : input;
506516
const {stack, options} = this;
507517
const buf = [];

‎test/ansi_to_html.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,20 @@ describe('ansi to html', function () {
154154
return test(text, result, done);
155155
});
156156

157-
it('renders xterm 256 sequences', function (done) {
157+
it('renders xterm 256 foreground sequences', function (done) {
158158
const text = '\x1b[38;5;196mhello';
159159
const result = '<span style="color:#ff0000">hello</span>';
160160

161161
return test(text, result, done);
162162
});
163163

164+
it('renders xterm 256 background sequences', function (done) {
165+
const text = '\x1b[48;5;196mhello';
166+
const result = '<span style="background-color:#ff0000">hello</span>';
167+
168+
return test(text, result, done);
169+
});
170+
164171
it('renders foreground rgb sequences', function (done) {
165172
const text = '\x1b[38;2;210;60;114mhello';
166173
const result = '<span style="color:#d23c72">hello</span>';

0 commit comments

Comments
 (0)
Please sign in to comment.