Skip to content

Commit c5b3e75

Browse files
committed
Prefer using an HTML table with user-select: none in GHA logs
1 parent 5e15238 commit c5b3e75

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/gha_logs.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ body {{
219219
}}
220220
.timestamp {{
221221
color: #848484;
222+
user-select: none;
222223
text-decoration: none;
223224
}}
224225
.timestamp:hover {{
@@ -270,14 +271,19 @@ body {{
270271
var html = ansi_up.ansi_to_html(logs);
271272
272273
// 2. Remove UTF-8 useless BOM
273-
if (html.charCodeAt(0) === 0xFEFF) {{
274-
html = html.substr(1);
275-
}}
276-
277-
// 3. Add a self-referencial anchor to all timestamps at the start of the lines
278-
const dateRegex = /^(\d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}\.\d+Z)/gm;
279-
html = html.replace(dateRegex, (ts) =>
280-
`<a id="${{ts}}" href="#${{ts}}" class="timestamp">${{ts}}</a>`
274+
html = html.replace(/^\uFEFF/gm, "");
275+
276+
// 3. Transform each log lines that doesn't start with a timestamp into a row where everything is in the second column
277+
const untsRegex = /^(?!\d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}\.\d+Z)(.*)$/gm;
278+
html = html.replace(untsRegex, (match, log) =>
279+
`<tr><td></td><td>${{log}}</td></tr>`
280+
);
281+
282+
// 3.b Transform each log lines that start with a timestamp in a row with two columns and make the timestamp be a
283+
// self-referencial anchor.
284+
const tsRegex = /^(\d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}\.\d+Z) (.*)$/gm;
285+
html = html.replace(tsRegex, (match, ts, log) =>
286+
`<tr><td><a id="${{ts}}" href="#${{ts}}" class="timestamp">${{ts}}</a></td><td>${{log}}</td></tr>`
281287
);
282288
283289
// 4. Add a anchor around every "##[error]" string
@@ -324,8 +330,8 @@ body {{
324330
return `${{boundary_start}}<a href="https://github.com/{owner}/{repo}/blob/{sha}/${{path}}${{pos}}" class="path-marker">${{inner}}</a>${{boundary_end}}`;
325331
}});
326332
327-
// 6. Add the html to the DOM
328-
document.body.innerHTML = html;
333+
// 6. Add the html to the table
334+
document.getElementById("logs").innerHTML = html;
329335
330336
// 7. If no anchor is given, scroll to the last error
331337
if (location.hash === "" && errorCounter >= 0) {{
@@ -337,11 +343,9 @@ body {{
337343
}});
338344
}}
339345
340-
// 8. Add a copy handler that force plain/text copy and removes the timestamps
341-
// from the copied selection.
342-
const dateRegexWithSpace = /^(\d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}\.\d+Z )/gm;
346+
// 8. Add a copy handler that force plain/text copy
343347
document.addEventListener("copy", function(e) {{
344-
var text = window.getSelection().toString().replace(dateRegexWithSpace, '');
348+
var text = window.getSelection().toString();
345349
e.clipboardData.setData('text/plain', text);
346350
e.preventDefault();
347351
}});
@@ -353,6 +357,14 @@ body {{
353357
</script>
354358
</head>
355359
<body>
360+
<table style="table-layout: fixed; width: 100%">
361+
<colgroup>
362+
<col style="width: 29ch">
363+
<col style="width: 100%">
364+
</colgroup>
365+
<tbody id="logs">
366+
</tbody>
367+
</table>
356368
</body>
357369
</html>"###,
358370
);

0 commit comments

Comments
 (0)