Public: anyone can read this; the crew can change it.

A .log file opens with its error and warning lines marked and a jump to the first error #13344

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13327-a-log-file-opens-with-its-error-and-warn → main 4 commits fork 4 threads · 4 unresolved

  • Merged. scout/13327-a-log-file-opens-with-its-error-and-warn was merged into main as 1d0373e.

Review

Sign in to review this change.
7 files changed +139 −15
docs/design/areas/render.md +1
@@ -14,6 +14,7 @@ The header shows the size (`1.2 MB · PDF`) through `Rendered.summary`. Both the
1414 `src/web/render/log.ts` draws a log that holds escape codes inside the existing `.card.code` table, so it looks like any code file. Colour comes only from fixed classes (`src/web/style/content.ts`), tokens only, both themes:
1515
1616 - `.ansi-red` `--danger`, `.ansi-green` `--success`, `.ansi-yellow` `--attn`, `.ansi-blue` `--tok-number`, `.ansi-magenta` `--merged`, `.ansi-cyan` `--tok-property`, `.ansi-gray` (black) `--tok-comment`. White is the page's own ink.
17 - `.log-error` and `.log-warn` (a row the log states a level on: a `--danger` or `--attn` wash), `.log-jump` (the line above the table with the Jump to first error link).

The design area is updated, but the feature how-to docs/features/ansi-log.md is not. Document the marked rows and the jump link there.

1718 - `.ansi-b` bold, `.ansi-u` underline.
1819
1920 Backgrounds, 256-colour above 15 and truecolour are dropped; so are cursor moves and OSC. A log with no escape code keeps the plain source view.
docs/features/ansi-log.md +1 −1
@@ -1,6 +1,6 @@
11 # Terminal logs in colour
22
3 `.log` and `.ansi` files that contain terminal escapes open on the blob page in colour (`src/web/render/log.ts`, parser in `ansi.ts`). A log with no escape byte keeps the ordinary source view.
3 `.log` and `.ansi` files that contain terminal escapes open on the blob page in colour (`src/web/render/log.ts`, parser in `ansi.ts`). A plain log (no escape byte) with an error or warning line (`ERROR x`, `[warn] x`, `level=error x`, after an optional timestamp; prose like "no errors found" does not count) now opens drawn the same way, with those rows marked (`log-error`, `log-warn`) and a "Jump to first error" link above it (`#L<n>`, or `?all=1#L<n>` when the error is past the first 1,500 lines; none past 10,000). A plain log with no such line keeps the ordinary source view.
44 Only SGR is read: foreground colour (30-37, 90-97, and 256-colour 0-15), bold, underline, and the resets 0, 22, 24, 39. Each becomes a fixed class (`ansi-red`, `ansi-b`, `ansi-u`, …) coloured by design tokens; the input never reaches a `style` attribute or a class name. Background, truecolour and 256-colour above 15 are dropped.
55 Every other escape (cursor moves, OSC titles and links, an unterminated OSC up to its newline) and every other control character, a bare CR included, is removed, never acted on. A colour is closed at each line end and reopened on the next.
66 Drawn as the code view is: first 1,500 lines, `?all=1` ("Show all lines", no JavaScript) up to 10,000, with an "N more lines" note. `?view=source`, Raw and Download show the bytes untouched.
src/web/render/log.ts +49 −6
@@ -1,7 +1,6 @@
11 /** A terminal log (.log, .ansi) with its colours, laid out as the code view is. */
22
33 import { html, raw } from "../html.ts";
4 import { rowsMarkup } from "../code_rows.ts";
54 import { btn, empty } from "../ui.ts";
65 import { ansiLines } from "./ansi.ts";
76 import type { Rendered, RenderInput } from "./renderers.ts";
@@ -10,18 +9,62 @@ import type { Rendered, RenderInput } from "./renderers.ts";
109 const FIRST_LINES = 1_500;
1110 const MAX_LINES = 10_000;
1211
13 /** Whether a body holds an escape at all; one without is shown as plain text. */
14 export const hasEscape = (body: Uint8Array): boolean => body.includes(0x1b);
12 // eslint-disable-next-line no-control-regex
13 const SGR = /\x1b\[[0-?]*[ -/]*[@-~]/g;
14
15 /** The level of each of the first `limit` lines of a body, its colours ignored. */
16 function levels(text: string, limit: number): (ReturnType<typeof logLevel>)[] {
17 const out: (ReturnType<typeof logLevel>)[] = [];
18 let at = 0;
19 while (out.length < limit && at <= text.length) {
20 let end = text.indexOf("\n", at);
21 if (end === -1) end = text.length;
22 const line = text.slice(at, end);
23 out.push(logLevel(line.includes("\x1b") ? line.replace(SGR, "") : line));
24 at = end + 1;
25 }
26 return out;
27 }
28

drawsLog decodes the whole body and scans up to 10,000 lines, then log() decodes and scans again (line 31 and 35). On a multi-MB log that is double work on the request path. Compute the levels once and pass them in, or cache by body.

29 /** One decode and one scan per body: `drawsLog` and `log()` of the same request share it. */
30 const scanned = new WeakMap<Uint8Array, { text: string; marks: ReturnType<typeof levels> }>();
31 function scan(body: Uint8Array) {
32 let hit = scanned.get(body);
33 if (hit === undefined) {
34 const text = new TextDecoder().decode(body);
35 hit = { text, marks: levels(text, MAX_LINES) };
36 scanned.set(body, hit);
37 }
38 return hit;
39 }
40
41 /** Whether a log is drawn: it holds an escape, or a line that states a level. */
42 export const drawsLog = (body: Uint8Array): boolean =>
43 body.includes(0x1b) || scan(body).marks.some((l) => l === "error" || l === "warn");
1544
1645 export function log(input: RenderInput): Rendered {
17 const { lines, total } = ansiLines(new TextDecoder().decode(input.body), input.all ? MAX_LINES : FIRST_LINES);
46 const { text, marks } = scan(input.body);
47 const { lines, total } = ansiLines(text, input.all ? MAX_LINES : FIRST_LINES);
1848 const cut = total - lines.length;
1949 const href = input.pathHref ?? "";
50 const first = marks.indexOf("error");
51 // The first error is in the page, or one `?all=1` away; past the ceiling it has no anchor.
52 const jump =
53 first === -1 ? "" : first < lines.length ? `#L${first + 1}` : `${href}?all=1#L${first + 1}`;
54 const rows = lines
55 .map((line, i) => {
56 const mark = marks[i] === "error" || marks[i] === "warn" ? ` class="log-${marks[i]}"` : "";
57 return html`<tr id="L${i + 1}"${raw(mark)}><td class="ln"><a href="#L${i + 1}">${i + 1}</a></td><td><pre>${raw(line)}</pre></td></tr>`.text;
58 })
59 .join("");
2060 return {
2161 lines: total,
22 content: html`<div class="card code ansi">
62 content: html`${first === -1
63 ? html``
64 : html`<p class="log-jump"><a id="log-first-error" href="${jump}">Jump to first error</a> <span class="muted">line ${first + 1}</span></p>`}
65 <div class="card code ansi">
2366 <table>
24 ${raw(rowsMarkup(lines.map(raw)))}
67 ${raw(rows)}
2568 </table>
2669 </div>
2770 ${cut > 0
src/web/render/renderers.ts +11 −5
@@ -32,7 +32,7 @@ import { vcf } from "./vcf.ts";
3232 import { htmlPreview } from "./html_preview.ts";
3333 import { image } from "./image.ts";
3434 import { fileKind, HEAD_BYTES, kindByName, NO_BYTES, RENDER_CAP, type FileKind } from "./kinds.ts";
35 import { log, hasEscape } from "./log.ts";
35 import { log, drawsLog } from "./log.ts";
3636 import { makefile } from "./makefile.ts";
3737 import { markdown } from "./markdown.ts";
3838 import { model3d } from "./model3d.ts";
@@ -72,13 +72,19 @@ export interface RenderInput {
7272 readonly pathHref?: string;
7373 /** The repository base and ref (as written in a URL), so a relative address resolves. */
7474 readonly repo?: { readonly base: string; readonly ref: string };
75 /** Whether a repository-relative path is a file at the ref; a renderer that links paths links only these. */
75 /**
76 * Whether a repository-relative path is a file at the ref; a renderer that links paths links only
77 * these.
78 */
7679 readonly exists?: (path: string) => boolean;
7780 /** `?all=1`: draw past the default window, up to the renderer's cap. */
7881 readonly all?: boolean;
7982 /** `?table=<name>`: unchecked text; a renderer matches it against its own list. */
8083 readonly table?: string;
81 /** Set for a writer only: where a shift of this file posts (the file edit route), and what it names. */
84 /**
85 * Set for a writer only: where a shift of this file posts (the file edit route), and what it
86 * names.
87 */
8288 readonly shift?: { readonly action: string; readonly ref: string; readonly path: string };
8389 /** `?layer=N` (1-based) on a G-code file; the renderer clamps it to the layers it has. */
8490 readonly layer?: number;
@@ -190,11 +196,11 @@ export function rendererFor(
190196 if (blob.kind === "too_large" && !MEDIA_KINDS.has(kind) && kind !== "psd") return undefined;
191197 // A kind may draw only some of its files: `config` draws JSON, YAML and
192198 // TOML as a form, and leaves the rest (INI, .env …) their source.
193 // A log with no escape in it keeps the source view, exactly as plain text.
199 // A log with no escape and no marked line keeps the source view, exactly as plain text.
194200 // A package.json that is no manifest (malformed, or nothing to list) too.
195201 const declined =
196202 (kind === "config" && !isFormConfig(path)) ||
197 (kind === "log" && blob.kind !== "too_large" && !hasEscape(blob.body)) ||
203 (kind === "log" && blob.kind !== "too_large" && !drawsLog(blob.body)) ||
198204 (kind === "package" && blob.kind !== "too_large" && readPackage(blob.body) === undefined);
199205 const render = declined ? undefined : renderers[kind];
200206 if (render === undefined) return undefined;
src/web/style/content.ts +5
@@ -496,6 +496,11 @@ img.img-box, .prose img.img-box, .tell-picture.img-box { display: block; width:
496496 .ansi-magenta { color: var(--merged); }
497497 .ansi-cyan { color: var(--tok-property); }
498498 .ansi-gray { color: var(--tok-comment); }
499 .code tr.log-error { background: color-mix(in srgb, var(--danger) 12%, transparent); }
500 .code tr.log-warn { background: color-mix(in srgb, var(--attn) 12%, transparent); }
501 .log-jump { margin: 0 0 8px; min-height: 44px; display: flex; align-items: center; gap: 8px; }
outdated

Phone/keyboard: .log-jump is a 20px-tall inline link, hard to hit with a thumb. Give the link display:inline-block, padding and a 44px min-height. Also add a visible :focus-visible state if the shared link style does not already give one.

502 .log-jump a { display: inline-block; padding: 12px 8px 12px 0; }
503 .log-jump a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
499504 .ansi-b { font-weight: 700; }
500505 .ansi-u { text-decoration: underline; }
501506
test/fuzz-renderers.test.ts +1
@@ -77,6 +77,7 @@ const SAMPLES: Record<string, { path: string; render: Renderer; files: readonly
7777 render: log,
7878 files: [
7979 "\u001b[1;32mok\u001b[0m step one\n\u001b[38;5;196mfail\u001b[0m\n\u001b]8;;https://a.test\u0007link\u001b]8;;\u0007\n",
80 "INFO start\nWARN slow\nERROR boom\nFATAL end\n",
8081 ],
8182 },
8283 ics: {
test/render-ansi.test.ts +71 −3
@@ -2,7 +2,7 @@
22
33 import { describe, expect, test } from "bun:test";
44 import { ansiLines } from "../src/web/render/ansi.ts";
5 import { rendererFor, RENDERERS } from "../src/web/render/renderers.ts";
5 import { rendererFor, RENDERERS, type RenderInput } from "../src/web/render/renderers.ts";
66 import { log } from "../src/web/render/log.ts";
77 import { buildLines } from "../src/tui/screens/workspace.ts";
88 import { theme } from "../src/tui/ui/theme.ts";
@@ -78,9 +78,19 @@ describe("the log kind", () => {
7878 });
7979 });
8080
81 const rin = (body: Uint8Array, all: boolean): RenderInput => ({
82 path: "b.log",
83 size: body.length,
84 body,
85 media: "/o/r/media/main/b.log",
86 download: "/o/r/media/main/b.log?download=1",
87 pathHref: "/o/r/blob/main/b.log",
88 all,
89 });
90
8191 describe("log()", () => {
82 const input = (n: number, all = false) =>
83 ({ body: enc(Array.from({ length: n }, (_, i) => `\x1b[31mline ${i}`).join("\n")), all, pathHref: "/o/r/blob/main/b.log" }) as never;
92 const input = (n: number, all = false): RenderInput =>
93 rin(enc(Array.from({ length: n }, (_, i) => `\x1b[31mline ${i}`).join("\n")), all);
8494
8595 test("a long log is cut with a count and a Show all lines link", () => {
8696 const r = log(input(1_600));
@@ -96,6 +106,64 @@ describe("log()", () => {
96106 });
97107 });
98108
109 describe("log() marks", () => {
110 const body = (s: string, all = false): RenderInput => rin(enc(s), all);
111 const mixed = "INFO start\nno errors found\n2026-01-02T03:04:05Z WARN slow <script>alert(1)</script>\n[error] boom\nFATAL later\n";
112
113 test("a plain log with a level line is drawn, one without stays source", () => {
114 const blob = (s: string) => ({ kind: "text", body: enc(s), size: s.length }) as unknown as Blob;
115 expect(rendererFor("b.log", blob(mixed))?.render).toBe(RENDERERS.log!);
116 expect(rendererFor("b.log", blob("INFO a\nno errors found\n"))).toBeUndefined();
117 });
118
119 test("error and warn lines are marked and the jump goes to the first error", () => {
120 const out = String(log(body(mixed)).content);
121 expect(out).toContain('<tr id="L3" class="log-warn">');
122 expect(out).toContain('<tr id="L4" class="log-error">');
123 expect(out).toContain('<tr id="L5" class="log-error">');
124 expect(out).not.toContain('id="L2" class');
125 expect(out).toContain('href="#L4"');
126 expect(out).toContain("Jump to first error");
127 });
128
129 test("a script-bearing line stays escaped", () => {
130 const out = String(log(body(mixed)).content);
131 expect(out).not.toContain("<script>");
132 expect(out).toContain("&lt;script&gt;");
133 });
134
135 test("no error, no jump; a warning alone is marked only", () => {
136 const out = String(log(body("WARN x\n")).content);
137 expect(out).toContain("log-warn");
138 expect(out).not.toContain("Jump to first error");
139 });
140
141 test("an error past the first window links to ?all=1 with a stable anchor", () => {
142 const text = Array.from({ length: 5_000 }, (_, i) => (i === 3_999 ? "ERROR deep" : "ok")).join("\n");
143 const out = String(log(body(text)).content);
144 expect(out).toContain('href="/o/r/blob/main/b.log?all=1#L4000"');
145 const all = String(log(body(text, true)).content);
146 expect(all).toContain('href="#L4000"');
147 expect(all).toContain('<tr id="L4000" class="log-error">');
148 });
149 });

Box 2 asks that a 5 MB log still renders under the cap; no test covers it. Add one with a ~5 MB body, asserting it renders and a first-error jump exists.

150
151 describe("a 5 MB log", () => {
152 test("renders, escapes, and links to the first error", () => {
153 const line = "INFO <b>tick</b> padding padding padding padding padding\n";
154 // The scan reads 10,000 lines, so the error sits inside it, past the first window.
155 const text = line.repeat(8_000) + "ERROR <script>x</script>\n" + line.repeat(Math.ceil(5_000_000 / line.length));
156 const bytes = enc(text);
157 expect(bytes.length).toBeGreaterThan(5_000_000);
158 const out = String(log(rin(bytes, false)).content);
159 expect(out).toContain("&lt;b&gt;tick");
160 expect(out).not.toContain("<script>");
161 expect(out).toContain("more lines");
162 expect(out).toContain("Jump to first error");
163 expect(out).toContain("?all=1#L");
164 });
165 });
166
99167 describe("the terminal file view", () => {
100168 test("a .log's escapes are stripped; other files are left alone", () => {
101169 expect(buildLines("\x1b[31mred\x1b[0m\x1b]0;t\x07 ok\n", "b.log", theme("dark").c).plain).toEqual(["red ok"]);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs