Skip to content

Commit 8499742

Browse files
authored
Merge pull request #2200 from Urgau/gha-logs-fix-perf
Prefer using an HTML table with `user-select: none` in GHA logs
2 parents b6ec119 + 79d316e commit 8499742

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/gha_logs.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -211,30 +211,20 @@ pub async fn gha_logs(
211211
<title>{job_name} - {owner}/{repo}@{short_sha}</title>
212212
{icon_status}
213213
<style>
214-
:root {{
215-
--timestamp-length: 28ch;
216-
}}
217214
body {{
218215
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
219216
background: #0C0C0C;
220217
color: #CCC;
221218
white-space: pre;
222-
padding-left: calc(var(--timestamp-length) + 1ch);
223-
margin: 0;
224219
}}
225220
.timestamp {{
226221
color: #848484;
222+
user-select: none;
227223
text-decoration: none;
228-
position: absolute;
229-
left: 0;
230-
width: var(--timestamp-length);
231224
}}
232225
.timestamp:hover {{
233226
text-decoration: underline;
234227
}}
235-
.timestamp::before {{
236-
content: attr(data-timestamp);
237-
}}
238228
.error-marker {{
239229
scroll-margin-bottom: 15vh;
240230
color: #e5534b;
@@ -281,14 +271,19 @@ body {{
281271
var html = ansi_up.ansi_to_html(logs);
282272
283273
// 2. Remove UTF-8 useless BOM
284-
if (html.charCodeAt(0) === 0xFEFF) {{
285-
html = html.substr(1);
286-
}}
287-
288-
// 3. Add a self-referencial anchor to all timestamps at the start of the lines
289-
const dateRegex = /^(\d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}\.\d+Z) /gm;
290-
html = html.replace(dateRegex, (match, ts) =>
291-
`<a id="${{ts}}" href="#${{ts}}" class="timestamp" data-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>`
292287
);
293288
294289
// 4. Add a anchor around every "##[error]" string
@@ -335,8 +330,8 @@ body {{
335330
return `${{boundary_start}}<a href="https://github.com/{owner}/{repo}/blob/{sha}/${{path}}${{pos}}" class="path-marker">${{inner}}</a>${{boundary_end}}`;
336331
}});
337332
338-
// 6. Add the html to the DOM
339-
document.body.innerHTML = html;
333+
// 6. Add the html to the table
334+
document.getElementById("logs").innerHTML = html;
340335
341336
// 7. If no anchor is given, scroll to the last error
342337
if (location.hash === "" && errorCounter >= 0) {{
@@ -348,9 +343,11 @@ body {{
348343
}});
349344
}}
350345
351-
// 8. Add a copy handler that force plain/text copy
346+
// 8. Add a copy handler that force plain/text copy and removes the timestamps
347+
// from the copied selection.
348+
const dateRegexWithSpace = /^(\d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}\.\d+Z )/gm;
352349
document.addEventListener("copy", function(e) {{
353-
var text = window.getSelection().toString();
350+
var text = window.getSelection().toString().replace(dateRegexWithSpace, '');
354351
e.clipboardData.setData('text/plain', text);
355352
e.preventDefault();
356353
}});
@@ -362,6 +359,14 @@ body {{
362359
</script>
363360
</head>
364361
<body>
362+
<table style="table-layout: fixed; width: 100%">
363+
<colgroup>
364+
<col style="width: 29ch">
365+
<col style="width: 100%">
366+
</colgroup>
367+
<tbody id="logs">
368+
</tbody>
369+
</table>
365370
</body>
366371
</html>"###,
367372
);

0 commit comments

Comments
 (0)