xonecas.io
64875
Mc

mini-coder / commit 9890b42

feat: colored diffs

xonecas2026-09-23 9890b424f94fd068fa10cb5acd7856eb1fd7d397 parent 7f3096e

3 files changed

src/tui/styles.ts+20−0
@@ -2,3 +2,23 @@
22 export function dim(text: string): string {
33 return `\x1b[2m${text}\x1b[22m`;
44 }
5+
6+/** Red foreground (SGR 31), restored to the default foreground (SGR 39). */
7+export function red(text: string): string {
8+ return `\x1b[31m${text}\x1b[39m`;
9+}
10+
11+/** Green foreground (SGR 32), restored to the default foreground (SGR 39). */
12+export function green(text: string): string {
13+ return `\x1b[32m${text}\x1b[39m`;
14+}
15+
16+/** Yellow foreground (SGR 33), restored to the default foreground (SGR 39). */
17+export function yellow(text: string): string {
18+ return `\x1b[33m${text}\x1b[39m`;
19+}
20+
21+/** Cyan foreground (SGR 36), restored to the default foreground (SGR 39). */
22+export function cyan(text: string): string {
23+ return `\x1b[36m${text}\x1b[39m`;
24+}
src/tui/tui.ts+33−5
@@ -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 [
src/tui/usage.ts+5−5
@@ -1,5 +1,5 @@
11 import type { Api, AssistantMessage, ImageContent, Message, Model, TextContent, Tool } from "@earendil-works/pi-ai";
2-import { dim } from "./styles.ts";
2+import { dim, red, yellow } from "./styles.ts";
33
44 /** ~1 token per 4 characters, the heuristic pi-ai uses. */
55 function estimateText(text: string): number {
@@ -63,13 +63,13 @@ export function formatTokens(n: number): string {
6363 }
6464
6565 /**
66- * The context row: dim, SGR 33 as the window nears, SGR 31 once the next
67- * request could not carry a maximum-size reply.
66+ * The context row: dim, yellow as the window nears, red once the next request
67+ * could not carry a maximum-size reply.
6868 */
6969 export function contextUsageLine(used: number, model: Model<Api>): string {
7070 const percent = (used / model.contextWindow) * 100;
7171 const sizes = `${formatTokens(used)}/${formatTokens(model.contextWindow)}`;
72- if (used + model.maxTokens > model.contextWindow) return `\x1b[31mctx full · ${sizes}\x1b[39m`;
72+ if (used + model.maxTokens > model.contextWindow) return red(`ctx full · ${sizes}`);
7373 const text = `ctx ${sizes} · ${Math.round(percent)}%`;
74- return percent >= 85 ? `\x1b[33m${text}\x1b[39m` : dim(text);
74+ return percent >= 85 ? yellow(text) : dim(text);
7575 }