Skip to content

Commit 2604c22

Browse files
committed
bypass AnsiToHTML bug
1 parent 9d35890 commit 2604c22

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

web_src/js/components/RepoActionView.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {expect, test} from 'vitest';
22

33
import {ansiLogToHTML} from './RepoActionView.vue';
4+
import AnsiToHTML from 'ansi-to-html';
45

56
test('processConsoleLine', () => {
67
expect(ansiLogToHTML('abc')).toEqual('abc');
@@ -12,4 +13,18 @@ test('processConsoleLine', () => {
1213

1314
expect(ansiLogToHTML('\x1b[30mblack\x1b[37mwhite')).toEqual('<span style="color:#000">black<span style="color:#AAA">white</span></span>');
1415
expect(ansiLogToHTML('<script>')).toEqual('&lt;script&gt;');
16+
17+
18+
// upstream AnsiToHTML has bugs when processing "\033[1A" and "\033[1B", we fixed these control sequences in our code
19+
// if upstream could fix these bugs, we can remove these tests and remove our patch code
20+
const ath = new AnsiToHTML({escapeXML: true});
21+
expect(ath.toHtml('\x1b[1A\x1b[2Ktest\x1b[1B\x1b[1A\x1b[2K')).toEqual('AtestBA'); // AnsiToHTML bug
22+
expect(ath.toHtml('\x1b[1A\x1b[2K\rtest\r\x1b[1B\x1b[1A\x1b[2K')).toEqual('A\rtest\rBA'); // AnsiToHTML bug
23+
24+
// test our patched behavior
25+
expect(ansiLogToHTML('\x1b[1A\x1b[2Ktest\x1b[1B\x1b[1A\x1b[2K')).toEqual('test');
26+
expect(ansiLogToHTML('\x1b[1A\x1b[2K\rtest\r\x1b[1B\x1b[1A\x1b[2K')).toEqual('test');
27+
28+
// treat "\033[0K" and "\033[0J" (Erase display/line) as "\r", then it will be covered to "\n" finally.
29+
expect(ansiLogToHTML('a\x1b[0Kb\x1b[2Jc')).toEqual('a\nb\nc');
1530
});

web_src/js/components/RepoActionView.vue

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,26 +307,46 @@ export function initRepositoryActionView() {
307307
view.mount(el);
308308
}
309309
310+
// some unhandled control sequences by AnsiToHTML
311+
// https://man7.org/linux/man-pages/man4/console_codes.4.html
312+
const ansiRegexpRemove = /\x1b\[\d[A-H]/g; // Move cursor, treat them as a no-op.
313+
const ansiRegexpNewLine = /\x1b\[\d[JK]/g; // Erase display/line, treat them as a Carrige Return
314+
315+
function ansiCleanControlSequences(line) {
316+
if (line.includes('\x1b')) {
317+
line = line.replace(ansiRegexpRemove, '');
318+
line = line.replace(ansiRegexpNewLine, '\r');
319+
}
320+
return line;
321+
}
322+
310323
export function ansiLogToHTML(line) {
311324
if (line.endsWith('\r\n')) {
312325
line = line.substring(0, line.length - 2);
313326
} else if (line.endsWith('\n')) {
314327
line = line.substring(0, line.length - 1);
315328
}
329+
330+
// usually we do not need to process control chars like "\033[", let AnsiToHTML do it
331+
// but AnsiToHTML has bugs, so we need to clean some control sequences first
332+
line = ansiCleanControlSequences(line);
333+
316334
if (!line.includes('\r')) {
317335
return ansiLogRender.toHtml(line);
318336
}
319337
320338
// handle "\rReading...1%\rReading...5%\rReading...100%",
321339
// convert it into a multiple-line string: "Reading...1%\nReading...5%\nReading...100%"
322-
// then we do not need to process control chars like "\033[".
323340
const lines = [];
324341
for (const part of line.split('\r')) {
325342
if (part === '') continue;
326-
lines.push(part);
343+
const partHtml = ansiLogRender.toHtml(part);
344+
if (partHtml !== '') {
345+
lines.push(partHtml);
346+
}
327347
}
328348
// the log message element is with "white-space: break-spaces;", so use "\n" to break lines
329-
return ansiLogRender.toHtml(lines.join('\n'));
349+
return lines.join('\n');
330350
}
331351
332352
</script>

0 commit comments

Comments
 (0)