@@ -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 }