xonecas.io
64875
Mc

mini-coder / commit d235e08

refactor(cli): keep tool diff output compact and predictable

xonecas2026-03-14 d235e08fe4f28d72924cdb98a35bc256d975980f parent 3e4ee5c

2 files changed

src/cli/tool-render-result.test.ts+27−0
@@ -106,6 +106,33 @@ describe("renderToolResult", () => {
106106 expect(plain).toContain("+console.log('ok')");
107107 });
108108
109+ test("truncates large file edit diffs to keep output compact", () => {
110+ terminal.stdoutWrite = (chunk: string) => {
111+ stdout += chunk;
112+ };
113+
114+ const diffLines = [
115+ "@@ -0,0 +30 @@",
116+ ...Array.from({ length: 30 }, (_, i) => `+line ${i + 1}`),
117+ ];
118+ renderToolResult(
119+ "create",
120+ {
121+ path: "src/huge.ts",
122+ created: true,
123+ diff: diffLines.join("\n"),
124+ },
125+ false,
126+ );
127+
128+ const plain = stripAnsi(stdout);
129+ expect(plain).toContain("created src/huge.ts");
130+ expect(plain).toContain("+line 1");
131+ expect(plain).toContain("+line 23");
132+ expect(plain).toContain("… +7 more diff lines");
133+ expect(plain).not.toContain("+line 30");
134+ });
135+
109136 test("renders one-line error when tool result fails", () => {
110137 terminal.stdoutWrite = (chunk: string) => {
111138 stdout += chunk;
src/cli/tool-result-shared.ts+23−12
@@ -1,20 +1,31 @@
11 import * as c from "yoctocolors";
22 import { writeln } from "./output.ts";
33
4+const MAX_DIFF_PREVIEW_LINES = 24;
5+
6+function colorizeDiffLine(line: string): string {
7+ if (line.startsWith("+++") || line.startsWith("---")) return c.dim(line);
8+ if (line.startsWith("+")) return c.green(line);
9+ if (line.startsWith("-")) return c.red(line);
10+ if (line.startsWith("@@")) return c.cyan(line);
11+ return c.dim(line);
12+}
13+
414 export function renderDiff(diff: string): void {
515 if (!diff || diff === "(no changes)") return;
6- for (const line of diff.split("\n")) {
7- if (line.startsWith("+++") || line.startsWith("---")) {
8- writeln(` ${c.dim(line)}`);
9- } else if (line.startsWith("+")) {
10- writeln(` ${c.green(line)}`);
11- } else if (line.startsWith("-")) {
12- writeln(` ${c.red(line)}`);
13- } else if (line.startsWith("@@")) {
14- writeln(` ${c.cyan(line)}`);
15- } else {
16- writeln(` ${c.dim(line)}`);
17- }
16+
17+ const normalized = diff.replace(/[\r\n]+$/, "");
18+ if (!normalized) return;
19+
20+ const lines = normalized.split("\n");
21+ const shown = lines.slice(0, MAX_DIFF_PREVIEW_LINES);
22+ for (const line of shown) {
23+ writeln(` ${colorizeDiffLine(line)}`);
24+ }
25+ if (lines.length > shown.length) {
26+ writeln(
27+ ` ${c.dim(`… +${lines.length - shown.length} more diff lines`)}`,
28+ );
1829 }
1930 }