xonecas.io
79113
Ma

mini-coding-agent / commit 7f575e6

feat: remove hand rolled parsed and use tree-sitter for diffs

xonecas2026-10-06 7f575e649a88770afd3f851235ffccd0eef11e3e parent 46c09fd

5 files changed

build.zig+6−0
@@ -28,6 +28,8 @@ pub fn build(b: *std.Build) void {
2828 const py = b.dependency("tree_sitter_python", .{ .target = target, .optimize = optimize });
2929 const go = b.dependency("tree_sitter_go", .{ .target = target, .optimize = optimize });
3030 const zig = b.dependency("tree_sitter_zig", .{ .target = target, .optimize = optimize });
31+ const bash = b.dependency("tree_sitter_bash", .{ .target = target, .optimize = optimize });
32+ const diff = b.dependency("tree_sitter_diff", .{ .target = target, .optimize = optimize });
3133
3234 exe.root_module.link_libc = true;
3335 addGrammar(exe, js, "src", &.{ "parser.c", "scanner.c" });
@@ -38,6 +40,8 @@ pub fn build(b: *std.Build) void {
3840 addGrammar(exe, py, "src", &.{ "parser.c", "scanner.c" });
3941 addGrammar(exe, go, "src", &.{"parser.c"});
4042 addGrammar(exe, zig, "src", &.{"parser.c"});
43+ addGrammar(exe, bash, "src", &.{ "parser.c", "scanner.c" });
44+ addGrammar(exe, diff, "src", &.{"parser.c"});
4145
4246 exe.root_module.addAnonymousImport("js_highlights", .{ .root_source_file = js.path("queries/highlights.scm") });
4347 exe.root_module.addAnonymousImport("ts_highlights", .{ .root_source_file = ts.path("queries/highlights.scm") });
@@ -47,6 +51,8 @@ pub fn build(b: *std.Build) void {
4751 exe.root_module.addAnonymousImport("py_highlights", .{ .root_source_file = py.path("queries/highlights.scm") });
4852 exe.root_module.addAnonymousImport("go_highlights", .{ .root_source_file = go.path("queries/highlights.scm") });
4953 exe.root_module.addAnonymousImport("zig_highlights", .{ .root_source_file = zig.path("queries/highlights.scm") });
54+ exe.root_module.addAnonymousImport("bash_highlights", .{ .root_source_file = bash.path("queries/highlights.scm") });
55+ exe.root_module.addAnonymousImport("diff_highlights", .{ .root_source_file = diff.path("queries/highlights.scm") });
5056
5157 const run_cmd = b.addRunArtifact(exe);
5258 run_cmd.step.dependOn(b.getInstallStep());
build.zig.zon+8−0
@@ -28,6 +28,14 @@
2828 .url = "git+https://github.com/tree-sitter/tree-sitter-go?ref=v0.25.0#1547678a9da59885853f5f5cc8a99cc203fa2e2c",
2929 .hash = "N-V-__8AAM9cIgDSWz10pu4QwvyegGvG1p3E3KBTILC3fqGh",
3030 },
31+ .tree_sitter_bash = .{
32+ .url = "git+https://github.com/tree-sitter/tree-sitter-bash?ref=v0.25.0#56b54c61fb48bce0c63e3dfa2240b5d274384763",
33+ .hash = "N-V-__8AANjenwCy_cXlIbU8nqBzGxMlEHVokLZJYrmIXtoz",
34+ },
35+ .tree_sitter_diff = .{
36+ .url = "git+https://github.com/the-mikedavis/tree-sitter-diff#46c09fdf38f4b29b3baefcb88aed6c4ea67ffc7d",
37+ .hash = "N-V-__8AAPUcFACL4N4XTVUilDAwPIkX8066VSaipW5tYXDT",
38+ },
3139 // The v1.1.2 tag predates the repo's `build.zig.zon`, and Zig resolves
3240 // a `?ref=` tag against the default branch when the tag has none, whose
3341 // manifest lacks a `fingerprint`. The commit is pinned instead.
src/tui/highlight.zig+106−37
@@ -11,6 +11,8 @@ extern fn tree_sitter_markdown_inline() callconv(.c) *const ts.Language;
1111 extern fn tree_sitter_python() callconv(.c) *const ts.Language;
1212 extern fn tree_sitter_go() callconv(.c) *const ts.Language;
1313 extern fn tree_sitter_zig() callconv(.c) *const ts.Language;
14+extern fn tree_sitter_bash() callconv(.c) *const ts.Language;
15+extern fn tree_sitter_diff() callconv(.c) *const ts.Language;
1416
1517 const JS_QUERIES = @embedFile("js_highlights");
1618 const TS_QUERIES = @embedFile("ts_highlights");
@@ -20,6 +22,8 @@ const MD_INLINE_QUERIES = @embedFile("md_inline_highlights");
2022 const PY_QUERIES = @embedFile("py_highlights");
2123 const GO_QUERIES = @embedFile("go_highlights");
2224 const ZIG_QUERIES = @embedFile("zig_highlights");
25+const BASH_QUERIES = @embedFile("bash_highlights");
26+const DIFF_QUERIES = @embedFile("diff_highlights");
2327
2428 const Syntax = struct {
2529 parser: *ts.Parser,
@@ -56,6 +60,8 @@ const Grammars = struct {
5660 python: Syntax,
5761 go: Syntax,
5862 zig: Syntax,
63+ bash: Syntax,
64+ diff: Syntax,
5965
6066 fn init(a: std.mem.Allocator) Grammars {
6167 const js = tree_sitter_javascript();
@@ -66,6 +72,8 @@ const Grammars = struct {
6672 const py = tree_sitter_python();
6773 const go_lang = tree_sitter_go();
6874 const zig_lang = tree_sitter_zig();
75+ const bash_lang = tree_sitter_bash();
76+ const diff_lang = tree_sitter_diff();
6977 return .{
7078 .javascript = syntax(a, js, &.{JS_QUERIES}) orelse unreachable,
7179 .typescript = syntax(a, ts_lang, &.{ JS_QUERIES, TS_QUERIES }) orelse unreachable,
@@ -75,6 +83,8 @@ const Grammars = struct {
7583 .python = syntax(a, py, &.{PY_QUERIES}) orelse unreachable,
7684 .go = syntax(a, go_lang, &.{GO_QUERIES}) orelse unreachable,
7785 .zig = syntax(a, zig_lang, &.{ZIG_QUERIES}) orelse unreachable,
86+ .bash = syntax(a, bash_lang, &.{BASH_QUERIES}) orelse unreachable,
87+ .diff = syntax(a, diff_lang, &.{DIFF_QUERIES}) orelse unreachable,
7888 };
7989 }
8090 };
@@ -84,7 +94,7 @@ fn getGrammars(a: std.mem.Allocator) *Grammars {
8494 return &grammars.?;
8595 }
8696
87-const Span = struct {
97+pub const Span = struct {
8898 start: u32,
8999 end: u32,
90100 style: theme.Style,
@@ -232,7 +242,7 @@ fn eventLess(_: void, x: Event, y: Event) bool {
232242 return x.order < y.order;
233243 }
234244
235-fn paint(a: std.mem.Allocator, text: []const u8, spans: []const Span) []u8 {
245+fn paint(a: std.mem.Allocator, text: []const u8, spans: []const Span, base: theme.Style) []u8 {
236246 var events: std.ArrayList(Event) = .empty;
237247 for (spans, 0..) |span, i| {
238248 if (span.end <= span.start) continue;
@@ -247,7 +257,7 @@ fn paint(a: std.mem.Allocator, text: []const u8, spans: []const Span) []u8 {
247257 var at: u32 = 0;
248258
249259 for (events.items) |event| {
250- emit(a, &out, &active, spans, text, event.at, &at, &plain);
260+ emit(a, &out, &active, spans, text, event.at, &at, &plain, base);
251261 if (event.open) {
252262 active.append(a, event.span) catch {};
253263 } else {
@@ -259,33 +269,113 @@ fn paint(a: std.mem.Allocator, text: []const u8, spans: []const Span) []u8 {
259269 }
260270 }
261271 }
262- emit(a, &out, &active, spans, text, @intCast(text.len), &at, &plain);
263- if (!plain) out.appendSlice(a, theme.SGR_PLAIN) catch {};
272+ emit(a, &out, &active, spans, text, @intCast(text.len), &at, &plain, base);
273+ if (!plain) out.appendSlice(a, theme.sgrOn(a, .{}, base)) catch {};
264274 return out.items;
265275 }
266276
267-fn emit(a: std.mem.Allocator, out: *std.ArrayList(u8), active: *std.ArrayList(usize), spans: []const Span, text: []const u8, end: u32, at: *u32, plain: *bool) void {
277+fn emit(a: std.mem.Allocator, out: *std.ArrayList(u8), active: *std.ArrayList(usize), spans: []const Span, text: []const u8, end: u32, at: *u32, plain: *bool, base: theme.Style) void {
268278 if (end <= at.*) return;
279+ const reset = theme.sgrOn(a, .{}, base);
269280 const style: ?theme.Style = if (active.items.len == 0) null else spans[active.items[active.items.len - 1]].style;
270281 if (style) |s| {
271- out.appendSlice(a, theme.sgr(a, s)) catch {};
282+ const sgr = theme.sgrOn(a, s, base);
283+ out.appendSlice(a, sgr) catch {};
272284 plain.* = false;
273- } else {
274- if (!plain.*) out.appendSlice(a, theme.SGR_PLAIN) catch {};
275- plain.* = true;
276- }
277- const chunk = text[at.*..end];
278- if (style) |s| {
285+ const chunk = text[at.*..end];
279286 for (chunk) |c| {
280287 out.append(a, c) catch {};
281- if (c == '\n') out.appendSlice(a, theme.sgr(a, s)) catch {};
288+ if (c == '\n') out.appendSlice(a, sgr) catch {};
282289 }
283290 } else {
284- out.appendSlice(a, chunk) catch {};
291+ if (!plain.*) out.appendSlice(a, reset) catch {};
292+ plain.* = true;
293+ out.appendSlice(a, text[at.*..end]) catch {};
285294 }
286295 at.* = end;
287296 }
288297
298+pub fn paintOn(a: std.mem.Allocator, text: []const u8, spans: []const Span, base: theme.Style) []u8 {
299+ return paint(a, text, spans, base);
300+}
301+
302+fn syntaxFor(g: *Grammars, lang: []const u8) ?*Syntax {
303+ if (std.mem.eql(u8, lang, "js") or std.mem.eql(u8, lang, "javascript") or std.mem.eql(u8, lang, "jsx")) return &g.javascript;
304+ if (std.mem.eql(u8, lang, "ts") or std.mem.eql(u8, lang, "typescript")) return &g.typescript;
305+ if (std.mem.eql(u8, lang, "tsx")) return &g.tsx;
306+ if (std.mem.eql(u8, lang, "py") or std.mem.eql(u8, lang, "python")) return &g.python;
307+ if (std.mem.eql(u8, lang, "go") or std.mem.eql(u8, lang, "golang")) return &g.go;
308+ if (std.mem.eql(u8, lang, "zig")) return &g.zig;
309+ if (std.mem.eql(u8, lang, "bash") or std.mem.eql(u8, lang, "sh") or std.mem.eql(u8, lang, "shell")) return &g.bash;
310+ return null;
311+}
312+
313+fn infoLang(info: []const u8) []const u8 {
314+ const trimmed = std.mem.trim(u8, info, " \t\r\n");
315+ var end: usize = 0;
316+ while (end < trimmed.len and trimmed[end] != ' ' and trimmed[end] != '\t') end += 1;
317+ return trimmed[0..end];
318+}
319+
320+pub fn spansFor(a: std.mem.Allocator, info: []const u8, text: []const u8) []const Span {
321+ const g = getGrammars(a);
322+ const lang = std.ascii.allocLowerString(a, infoLang(info)) catch infoLang(info);
323+ const s = syntaxFor(g, lang) orelse return &.{};
324+ const u = utf16(a, text) orelse return &.{};
325+ const root = parse(s.parser, u) orelse return &.{};
326+ defer root.destroy();
327+ return spansOf(a, s, root.rootNode(), u, text, 0);
328+}
329+
330+pub fn highlightOn(a: std.mem.Allocator, info: []const u8, text: []const u8, base: theme.Style) []const u8 {
331+ return paint(a, text, spansFor(a, info, text), base);
332+}
333+
334+pub const NodeSpan = struct { start: u32, end: u32, kind: []const u8 };
335+
336+const diff_line_kinds = [_][]const u8{
337+ "file_change", "binary_change", "index", "similarity", "dissimilarity",
338+ "old_file", "new_file", "location", "addition", "deletion",
339+ "change", "context", "comment", "special", "unrecognized",
340+};
341+
342+fn isDiffLineKind(kind: []const u8) bool {
343+ for (diff_line_kinds) |k| {
344+ if (std.mem.eql(u8, kind, k)) return true;
345+ }
346+ return false;
347+}
348+
349+fn collectDiffNodes(a: std.mem.Allocator, node: ts.Node, u: Utf16, out: *std.ArrayList(NodeSpan)) void {
350+ if (isDiffLineKind(node.kind())) {
351+ out.append(a, .{
352+ .start = byteOf(u, node.startByte() / 2),
353+ .end = byteOf(u, node.endByte() / 2),
354+ .kind = node.kind(),
355+ }) catch {};
356+ return;
357+ }
358+ var i: u32 = 0;
359+ while (i < node.childCount()) : (i += 1) {
360+ const child = node.child(i) orelse continue;
361+ collectDiffNodes(a, child, u, out);
362+ }
363+}
364+
365+pub fn diffSpans(a: std.mem.Allocator, text: []const u8) []NodeSpan {
366+ const g = getGrammars(a);
367+ const u = utf16(a, text) orelse return &.{};
368+ const root = parse(g.diff.parser, u) orelse return &.{};
369+ defer root.destroy();
370+ var out: std.ArrayList(NodeSpan) = .empty;
371+ collectDiffNodes(a, root.rootNode(), u, &out);
372+ return out.items;
373+}
374+
375+pub fn highlightCode(a: std.mem.Allocator, info: []const u8, text: []const u8) []const u8 {
376+ return highlightOn(a, info, text, .{});
377+}
378+
289379 pub fn highlightMarkdown(a: std.mem.Allocator, text: []const u8) []const u8 {
290380 const g = getGrammars(a);
291381 const u = utf16(a, text) orelse return text;
@@ -334,7 +424,7 @@ pub fn highlightMarkdown(a: std.mem.Allocator, text: []const u8) []const u8 {
334424 const idx = @min(level, theme.current.headings.len) - 1;
335425 spans = std.ArrayList(Span).fromOwnedSlice(recolor(a, spans.items, byteOf(u, heading.startByte() / 2), byteOf(u, heading.endByte() / 2), theme.current.headings[idx]));
336426 }
337- return paint(a, text, spans.items);
427+ return paint(a, text, spans.items, .{});
338428 }
339429
340430 const Align = enum { none, left, center, right };
@@ -478,24 +568,3 @@ fn byteOf(u: Utf16, unit: u32) u32 {
478568 fn parse(parser: *ts.Parser, u: Utf16) ?*ts.Tree {
479569 return parser.parseStringEncoding(std.mem.sliceAsBytes(u.units), null, .utf16le);
480570 }
481-
482-pub fn highlightCode(a: std.mem.Allocator, info: []const u8, text: []const u8) []const u8 {
483- const trimmed = std.mem.trim(u8, info, " \t\r\n");
484- var end: usize = 0;
485- while (end < trimmed.len and trimmed[end] != ' ' and trimmed[end] != '\t') end += 1;
486- const lang = std.ascii.allocLowerString(a, trimmed[0..end]) catch trimmed[0..end];
487- const g = getGrammars(a);
488- const u = utf16(a, text) orelse return text;
489- const syn: ?*Syntax =
490- if (std.mem.eql(u8, lang, "js") or std.mem.eql(u8, lang, "javascript") or std.mem.eql(u8, lang, "jsx")) &g.javascript
491- else if (std.mem.eql(u8, lang, "ts") or std.mem.eql(u8, lang, "typescript")) &g.typescript
492- else if (std.mem.eql(u8, lang, "tsx")) &g.tsx
493- else if (std.mem.eql(u8, lang, "py") or std.mem.eql(u8, lang, "python")) &g.python
494- else if (std.mem.eql(u8, lang, "go") or std.mem.eql(u8, lang, "golang")) &g.go
495- else if (std.mem.eql(u8, lang, "zig")) &g.zig
496- else null;
497- const s = syn orelse return text;
498- const root = parse(s.parser, u) orelse return text;
499- defer root.destroy();
500- return paint(a, text, spansOf(a, s, root.rootNode(), u, text, 0));
501-}
src/tui/theme.zig+16−0
@@ -264,3 +264,19 @@ pub fn sgr(a: std.mem.Allocator, style: Style) []const u8 {
264264 b[2],
265265 }) catch "";
266266 }
267+
268+pub fn sgrOn(a: std.mem.Allocator, style: Style, base: Style) []const u8 {
269+ const f = rgb(style.fg orelse base.fg orelse current.fg);
270+ const b = rgb(style.bg orelse base.bg orelse current.bg);
271+ return std.fmt.allocPrint(a, "\x1b[{s}{s}{s}38;2;{d};{d};{d};48;2;{d};{d};{d}m", .{
272+ if (style.bold or base.bold) "1;" else "22;",
273+ if (style.italic or base.italic) "3;" else "23;",
274+ if (style.underline or base.underline) "4;" else "24;",
275+ f[0],
276+ f[1],
277+ f[2],
278+ b[0],
279+ b[1],
280+ b[2],
281+ }) catch "";
282+}
src/tui/tui.zig+140−16
@@ -10,6 +10,7 @@ const tools_index = @import("../tools/index.zig");
1010 const common = @import("../tools/common.zig");
1111 const render = @import("render.zig");
1212 const theme = @import("theme.zig");
13+const highlight = @import("highlight.zig");
1314 const styles = @import("styles.zig");
1415 const term = @import("term.zig");
1516 const editor_mod = @import("editor.zig");
@@ -123,13 +124,122 @@ fn isEditHeader(line: []const u8) bool {
123124 std.mem.startsWith(u8, line, "+++ ") or (line.len >= 3 and line[0] == '=' and line[1] == '=' and line[2] == '=');
124125 }
125126
126-fn diffLine(line: []const u8) stream.BodyLine {
127- if (std.mem.startsWith(u8, line, "@@")) return .{ .text = line, .style = .{ .fg = theme.current.prompt } };
128- if (std.mem.startsWith(u8, line, "+")) return .{ .text = line, .style = .{ .fg = theme.current.add }, .bg = theme.current.diff_add };
129- if (std.mem.startsWith(u8, line, "-")) return .{ .text = line, .style = .{ .fg = theme.current.error_ }, .bg = theme.current.diff_delete };
130- if (std.mem.startsWith(u8, line, "\\ No newline")) return .{ .text = line, .style = .{ .fg = theme.current.comment } };
131- if (std.mem.startsWith(u8, line, " ")) return .{ .text = line, .style = .{ .fg = theme.current.comment } };
132- return .{ .text = line };
127+fn diffHunk() theme.Style {
128+ return .{ .fg = theme.current.prompt };
129+}
130+
131+const DiffKind = enum { hunk, add, del, ctx, note };
132+
133+fn diffKindOf(kind: []const u8) ?DiffKind {
134+ if (std.mem.eql(u8, kind, "location")) return .hunk;
135+ if (std.mem.eql(u8, kind, "addition")) return .add;
136+ if (std.mem.eql(u8, kind, "deletion")) return .del;
137+ if (std.mem.eql(u8, kind, "context")) return .ctx;
138+ if (std.mem.eql(u8, kind, "special") or std.mem.eql(u8, kind, "change") or
139+ std.mem.eql(u8, kind, "comment") or std.mem.eql(u8, kind, "unrecognized") or
140+ std.mem.eql(u8, kind, "old_file") or std.mem.eql(u8, kind, "new_file") or
141+ std.mem.eql(u8, kind, "index") or std.mem.eql(u8, kind, "file_change") or
142+ std.mem.eql(u8, kind, "binary_change") or std.mem.eql(u8, kind, "similarity") or
143+ std.mem.eql(u8, kind, "dissimilarity")) return .note;
144+ return null;
145+}
146+
147+fn prefixKind(line: []const u8) DiffKind {
148+ if (std.mem.startsWith(u8, line, "@@")) return .hunk;
149+ if (std.mem.startsWith(u8, line, "+")) return .add;
150+ if (std.mem.startsWith(u8, line, "-")) return .del;
151+ if (std.mem.startsWith(u8, line, " ")) return .ctx;
152+ return .note;
153+}
154+
155+fn langFor(path: []const u8) []const u8 {
156+ const ext = std.fs.path.extension(path);
157+ const named = [_]struct { ext: []const u8, lang: []const u8 }{
158+ .{ .ext = ".js", .lang = "js" },
159+ .{ .ext = ".jsx", .lang = "jsx" },
160+ .{ .ext = ".mjs", .lang = "js" },
161+ .{ .ext = ".cjs", .lang = "js" },
162+ .{ .ext = ".ts", .lang = "ts" },
163+ .{ .ext = ".tsx", .lang = "tsx" },
164+ .{ .ext = ".py", .lang = "py" },
165+ .{ .ext = ".go", .lang = "go" },
166+ .{ .ext = ".zig", .lang = "zig" },
167+ };
168+ for (named) |entry| {
169+ if (std.ascii.eqlIgnoreCase(ext, entry.ext)) return entry.lang;
170+ }
171+ return "";
172+}
173+
174+fn rebaseSpans(a: std.mem.Allocator, spans: []const highlight.Span, lo: u32, hi: u32) []const highlight.Span {
175+ var out: std.ArrayList(highlight.Span) = .empty;
176+ for (spans) |s| {
177+ const start = @max(s.start, lo);
178+ const end = @min(s.end, hi);
179+ if (end <= start) continue;
180+ out.append(a, .{ .start = start - lo, .end = end - lo, .style = s.style }) catch {};
181+ }
182+ return out.items;
183+}
184+
185+fn diffLines(a: std.mem.Allocator, path: []const u8, lines: []const []const u8) []const stream.BodyLine {
186+ var joined: std.ArrayList(u8) = .empty;
187+ var starts = a.alloc(u32, lines.len) catch return &.{};
188+ for (lines, 0..) |l, i| {
189+ if (i > 0) joined.append(a, '\n') catch {};
190+ starts[i] = @intCast(joined.items.len);
191+ joined.appendSlice(a, l) catch {};
192+ }
193+ const nodes = highlight.diffSpans(a, joined.items);
194+ std.mem.sort(highlight.NodeSpan, nodes, {}, struct {
195+ fn lt(_: void, x: highlight.NodeSpan, y: highlight.NodeSpan) bool {
196+ return x.start < y.start;
197+ }
198+ }.lt);
199+
200+ var code: std.ArrayList(u8) = .empty;
201+ var kinds = a.alloc(DiffKind, lines.len) catch return &.{};
202+ var los = a.alloc(u32, lines.len) catch return &.{};
203+ var his = a.alloc(u32, lines.len) catch return &.{};
204+ var cursor: usize = 0;
205+ for (lines, 0..) |l, i| {
206+ while (cursor < nodes.len and nodes[cursor].end <= starts[i]) cursor += 1;
207+ const parsed: ?DiffKind = if (cursor < nodes.len and nodes[cursor].start == starts[i])
208+ diffKindOf(nodes[cursor].kind)
209+ else
210+ null;
211+ const kind = parsed orelse prefixKind(l);
212+ kinds[i] = kind;
213+ los[i] = 0;
214+ his[i] = 0;
215+ if (kind != .add and kind != .del and kind != .ctx) continue;
216+ const content = l[1..];
217+ los[i] = @intCast(code.items.len);
218+ code.appendSlice(a, content) catch {};
219+ code.append(a, '\n') catch {};
220+ his[i] = @intCast(code.items.len - 1);
221+ }
222+
223+ const spans = highlight.spansFor(a, langFor(path), code.items);
224+
225+ var out: std.ArrayList(stream.BodyLine) = .empty;
226+ for (lines, 0..) |l, i| {
227+ switch (kinds[i]) {
228+ .hunk => out.append(a, .{ .text = l, .style = diffHunk() }) catch {},
229+ .note => out.append(a, .{ .text = l, .style = .{ .fg = theme.current.comment } }) catch {},
230+ .ctx => out.append(a, .{ .text = l, .style = .{ .fg = theme.current.comment } }) catch {},
231+ .add, .del => {
232+ const bg = if (kinds[i] == .add) theme.current.diff_add else theme.current.diff_delete;
233+ const content = l[1..];
234+ const painted = highlight.paintOn(a, content, rebaseSpans(a, spans, los[i], his[i]), .{ .bg = bg });
235+ out.append(a, .{
236+ .text = std.fmt.allocPrint(a, "{c}{s}", .{ l[0], painted }) catch l,
237+ .bg = bg,
238+ }) catch {};
239+ },
240+ }
241+ }
242+ return out.items;
133243 }
134244
135245 fn diffRows(a: std.mem.Allocator, diffs: []const common.FileDiff) []const stream.BodyLine {
@@ -140,11 +250,13 @@ fn diffRows(a: std.mem.Allocator, diffs: []const common.FileDiff) []const stream
140250 const trimmed = std.mem.trimEnd(u8, patch, " \t\r\n");
141251 var it = std.mem.splitScalar(u8, trimmed, '\n');
142252 var header = true;
253+ var body: std.ArrayList([]const u8) = .empty;
143254 while (it.next()) |l| {
144255 if (header and isEditHeader(l)) continue;
145256 header = false;
146- out.append(a, diffLine(l)) catch {};
257+ body.append(a, l) catch {};
147258 }
259+ out.appendSlice(a, diffLines(a, d.path, body.items)) catch {};
148260 } else if (d.note) |note| {
149261 out.append(a, .{ .text = note, .style = .{ .fg = theme.current.comment } }) catch {};
150262 }
@@ -152,12 +264,19 @@ fn diffRows(a: std.mem.Allocator, diffs: []const common.FileDiff) []const stream
152264 return out.items;
153265 }
154266
267+fn callBody(a: std.mem.Allocator, name: []const u8, summary: []const u8) stream.BodyLine {
268+ const body = if (std.mem.eql(u8, name, "bash"))
269+ highlight.highlightOn(a, "bash", summary, .{})
270+ else
271+ summary;
272+ return .{ .text = std.fmt.allocPrint(a, "{s} {s}", .{ callHead(a, name), body }) catch summary };
273+}
274+
155275 fn resultLines(a: std.mem.Allocator, name: []const u8, text: []const u8, is_error: bool) []const stream.BodyLine {
156276 const trimmed = std.mem.trimEnd(u8, text, " \t\r\n");
157277 var lines: std.ArrayList([]const u8) = .empty;
158278 var it = std.mem.splitScalar(u8, trimmed, '\n');
159279 while (it.next()) |l| lines.append(a, l) catch {};
160- var diff = false;
161280 if (std.mem.eql(u8, name, "bash")) {
162281 if (lines.items.len > 0 and std.mem.startsWith(u8, lines.items[lines.items.len - 1], "exit code: ")) {
163282 const exit = lines.items[lines.items.len - 1]["exit code: ".len..];
@@ -165,16 +284,21 @@ fn resultLines(a: std.mem.Allocator, name: []const u8, text: []const u8, is_erro
165284 if (is_error) lines.append(a, styles.red(a, std.fmt.allocPrint(a, "exit {s}", .{exit}) catch "exit")) catch {};
166285 }
167286 } else if (std.mem.eql(u8, name, "edit")) {
168- if (lines.items.len > 0 and (std.mem.startsWith(u8, lines.items[0], "edited ") or std.mem.startsWith(u8, lines.items[0], "created "))) {
287+ const verb = if (std.mem.startsWith(u8, lines.items[0], "edited "))
288+ "edited "
289+ else if (std.mem.startsWith(u8, lines.items[0], "created "))
290+ "created "
291+ else
292+ "";
293+ if (verb.len > 0) {
294+ const path = lines.items[0][verb.len..];
169295 _ = lines.orderedRemove(0);
170296 while (lines.items.len > 0 and isEditHeader(lines.items[0])) _ = lines.orderedRemove(0);
171- diff = true;
297+ return diffLines(a, path, lines.items);
172298 }
173299 }
174300 var out: std.ArrayList(stream.BodyLine) = .empty;
175- for (lines.items) |l| {
176- if (diff) out.append(a, diffLine(l)) catch {} else out.append(a, .{ .text = l }) catch {};
177- }
301+ for (lines.items) |l| out.append(a, .{ .text = l }) catch {};
178302 return out.items;
179303 }
180304
@@ -637,7 +761,7 @@ const Tui = struct {
637761 self.separator = true;
638762 if (self.pending_calls.items.len > 0) {
639763 const call = self.pending_calls.orderedRemove(0);
640- self.commitLines(&.{.{ .text = std.fmt.allocPrint(self.s, "{s} {s}", .{ callHead(self.s, call.name), call.summary }) catch call.summary }});
764+ self.commitLines(&.{callBody(self.s, call.name, call.summary)});
641765 }
642766 const width = @max(self.term.width() - BODY_PREFIX.len, 1);
643767 const lines: []const stream.BodyLine = if (!is_error and body != null)
@@ -660,7 +784,7 @@ const Tui = struct {
660784 fn flushCalls(self: *Tui) void {
661785 for (self.pending_calls.items) |call| {
662786 self.separator = true;
663- self.commitLines(&.{.{ .text = std.fmt.allocPrint(self.s, "{s} {s}", .{ callHead(self.s, call.name), call.summary }) catch call.summary }});
787+ self.commitLines(&.{callBody(self.s, call.name, call.summary)});
664788 }
665789 self.pending_calls.clearRetainingCapacity();
666790 }