@@ -15,7 +15,7 @@ import type { Session } from "../session.ts";
1515 import type { ToolName } from "../config.ts";
1616 import { Terminal, expandTabs, wrapLine, type Key } from "./term.ts";
1717 import { Editor } from "./editor.ts";
18-import { dim } from "./styles.ts";
18+import { cyan, dim, green, red } from "./styles.ts";
1919 import { contextUsageLine, estimateContextTokens } from "./usage.ts";
2020
2121 export interface TuiOptions {
@@ -65,9 +65,26 @@ function callSummary(name: string, args: JsonObject): string {
6565 return JSON.stringify(args);
6666 }
6767
68+/** One display line: unwrapped text plus the style its wrapped rows inherit. */
69+interface BodyLine {
70+ text: string;
71+ style?: (text: string) => string;
72+}
73+
74+/** Unified-diff color for one `edit` body line; file headers are stripped. */
75+function diffStyle(line: string): ((text: string) => string) | undefined {
76+ if (line.startsWith("@@")) return cyan;
77+ if (line.startsWith("+")) return green;
78+ if (line.startsWith("-")) return red;
79+ if (line.startsWith("\\ No newline")) return dim;
80+ if (line.startsWith(" ")) return dim;
81+ return undefined;
82+}
83+
6884 /** Display rewrite of a tool result, by tool name. */
69-function resultLines(name: string, text: string, isError: boolean): string[] {
85+function resultLines(name: string, text: string, isError: boolean): BodyLine[] {
7086 const lines = text.trimEnd().split("\n");
87+ let diff = false;
7188 if (name === "bash") {
7289 const exit = EXIT_LINE.exec(lines[lines.length - 1]);
7390 if (exit !== null) {
@@ -78,12 +95,23 @@ function resultLines(name: string, text: string, isError: boolean): string[] {
7895 lines.shift();
7996 // The call line already names the path; drop the repeated diff file header.
8097 while (lines.length > 0 && EDIT_HEADER.test(lines[0])) lines.shift();
98+ diff = true;
8199 }
82- return lines;
100+ return lines.map((line) => ({ text: line, style: diff ? diffStyle(line) : undefined }));
83101 }
84102
85-function bodyRows(lines: string[], width: number): string[] {
86- const rows = lines.flatMap((line) => wrapLine(expandTabs(line), width));
103+/**
104+ * Wraps plain text, then styles each row: `wrapLine` measures escapes as
105+ * printable cells, so it must never see them. A continuation row carries no
106+ * marker and inherits the style of its source line; the elision marker and
107+ * empty rows stay unstyled.
108+ */
109+function bodyRows(lines: BodyLine[], width: number): string[] {
110+ const rows = lines.flatMap((line) => {
111+ const style = line.style;
112+ const wrapped = wrapLine(expandTabs(line.text), width);
113+ return style === undefined ? wrapped : wrapped.map((row) => (row === "" ? row : style(row)));
114+ });
87115 if (rows.length > MAX_BODY_ROWS) {
88116 const hidden = rows.length - ELIDED_HEAD - ELIDED_TAIL;
89117 return [