Skip to content

Commit aa9c920

Browse files
authored
Improve action log display with control chars (#23820)
Close #23680 Some CLI programs use "\r" and control chars to print new content in current line. So, the strings in one line are actually from `\rReading...1%\rReading...5%\rReading...100%` This PR tries to make the output better.
1 parent 9a30b2e commit aa9c920

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@playwright/test": "1.31.2",
5656
"@rollup/pluginutils": "5.0.2",
5757
"@stoplight/spectral-cli": "6.6.0",
58+
"@vitejs/plugin-vue": "4.1.0",
5859
"eslint": "8.36.0",
5960
"eslint-plugin-import": "2.27.5",
6061
"eslint-plugin-jquery": "1.5.1",

vitest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {defineConfig} from 'vitest/dist/config.js';
22
import {readFile} from 'node:fs/promises';
33
import {dataToEsm} from '@rollup/pluginutils';
44
import {extname} from 'node:path';
5+
import vue from '@vitejs/plugin-vue';
56

67
function stringPlugin() {
78
return {
@@ -28,5 +29,6 @@ export default defineConfig({
2829
},
2930
plugins: [
3031
stringPlugin(),
32+
vue(),
3133
],
3234
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {expect, test} from 'vitest';
2+
3+
import {ansiLogToHTML} from './RepoActionView.vue';
4+
import AnsiToHTML from 'ansi-to-html';
5+
6+
test('processConsoleLine', () => {
7+
expect(ansiLogToHTML('abc')).toEqual('abc');
8+
expect(ansiLogToHTML('abc\n')).toEqual('abc');
9+
expect(ansiLogToHTML('abc\r\n')).toEqual('abc');
10+
expect(ansiLogToHTML('\r')).toEqual('');
11+
expect(ansiLogToHTML('\rx\rabc')).toEqual('x\nabc');
12+
expect(ansiLogToHTML('\rabc\rx\r')).toEqual('abc\nx');
13+
14+
expect(ansiLogToHTML('\x1b[30mblack\x1b[37mwhite')).toEqual('<span style="color:#000">black<span style="color:#AAA">white</span></span>');
15+
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[Kb\x1b[2Jc')).toEqual('a\nb\nc');
30+
});

web_src/js/components/RepoActionView.vue

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ import AnsiToHTML from 'ansi-to-html';
7777
7878
const {csrfToken} = window.config;
7979
80+
const ansiLogRender = new AnsiToHTML({escapeXML: true});
81+
8082
const sfc = {
8183
name: 'RepoActionView',
8284
components: {
@@ -91,8 +93,6 @@ const sfc = {
9193
9294
data() {
9395
return {
94-
ansiToHTML: new AnsiToHTML({escapeXML: true}),
95-
9696
// internal state
9797
loading: false,
9898
intervalID: null,
@@ -214,7 +214,7 @@ const sfc = {
214214
215215
const logMessage = document.createElement('div');
216216
logMessage.className = 'log-msg';
217-
logMessage.innerHTML = this.ansiToHTML.toHtml(line.message);
217+
logMessage.innerHTML = ansiLogToHTML(line.message);
218218
div.appendChild(logMessage);
219219
220220
return div;
@@ -307,6 +307,48 @@ 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 no-op.
313+
const ansiRegexpNewLine = /\x1b\[\d?[JK]/g; // Erase display/line, treat them as a Carriage 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+
323+
export function ansiLogToHTML(line) {
324+
if (line.endsWith('\r\n')) {
325+
line = line.substring(0, line.length - 2);
326+
} else if (line.endsWith('\n')) {
327+
line = line.substring(0, line.length - 1);
328+
}
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+
334+
if (!line.includes('\r')) {
335+
return ansiLogRender.toHtml(line);
336+
}
337+
338+
// handle "\rReading...1%\rReading...5%\rReading...100%",
339+
// convert it into a multiple-line string: "Reading...1%\nReading...5%\nReading...100%"
340+
const lines = [];
341+
for (const part of line.split('\r')) {
342+
if (part === '') continue;
343+
const partHtml = ansiLogRender.toHtml(part);
344+
if (partHtml !== '') {
345+
lines.push(partHtml);
346+
}
347+
}
348+
// the log message element is with "white-space: break-spaces;", so use "\n" to break lines
349+
return lines.join('\n');
350+
}
351+
310352
</script>
311353
312354
<style scoped>

0 commit comments

Comments
 (0)