@@ -6,6 +6,12 @@ const render = @import("render.zig");
66
77 const BodyLine = render.BodyLine;
88
9+pub const Rendered = struct {
10+ lines: []const BodyLine = &.{},
11+ added: usize = 0,
12+ removed: usize = 0,
13+};
14+
915 fn diffHunk() theme.Style {
1016 return .{ .fg = theme.current.prompt };
1117 }
@@ -59,17 +65,17 @@ fn rebaseSpans(a: std.mem.Allocator, spans: []const highlight.Span, lo: u32, hi:
5965 return out.items;
6066 }
6167
62-pub fn diffLines(a: std.mem.Allocator, path: []const u8, lines: []const []const u8, max_diff_lines: usize) []const BodyLine {
68+pub fn diffLines(a: std.mem.Allocator, path: []const u8, lines: []const []const u8, max_diff_lines: usize) Rendered {
6369 if (lines.len > max_diff_lines) {
6470 var warn: std.ArrayList(BodyLine) = .empty;
6571 warn.append(a, .{
6672 .text = "Diff too large to render efficiently. Full contents in session.jsonl",
6773 .style = .{ .fg = theme.current.comment },
6874 }) catch {};
69- return warn.items;
75+ return .{ .lines = warn.items };
7076 }
7177 var joined: std.ArrayList(u8) = .empty;
72- var starts = a.alloc(u32, lines.len) catch return &.{};
78+ var starts = a.alloc(u32, lines.len) catch return .{};
7379 for (lines, 0..) |l, i| {
7480 if (i > 0) joined.append(a, '\n') catch {};
7581 starts[i] = @intCast(joined.items.len);
@@ -83,9 +89,9 @@ pub fn diffLines(a: std.mem.Allocator, path: []const u8, lines: []const []const
8389 }.lt);
8490
8591 var code: std.ArrayList(u8) = .empty;
86- var kinds = a.alloc(DiffKind, lines.len) catch return &.{};
87- var los = a.alloc(u32, lines.len) catch return &.{};
88- var his = a.alloc(u32, lines.len) catch return &.{};
92+ var kinds = a.alloc(DiffKind, lines.len) catch return .{};
93+ var los = a.alloc(u32, lines.len) catch return .{};
94+ var his = a.alloc(u32, lines.len) catch return .{};
8995 var cursor: usize = 0;
9096 for (lines, 0..) |l, i| {
9197 while (cursor < nodes.len and nodes[cursor].end <= starts[i]) cursor += 1;
@@ -108,13 +114,17 @@ pub fn diffLines(a: std.mem.Allocator, path: []const u8, lines: []const []const
108114 const spans = highlight.spansFor(a, langFor(path), code.items);
109115
110116 var out: std.ArrayList(BodyLine) = .empty;
117+ var added: usize = 0;
118+ var removed: usize = 0;
111119 for (lines, 0..) |l, i| {
112120 switch (kinds[i]) {
113121 .hunk => out.append(a, .{ .text = l, .style = diffHunk() }) catch {},
114122 .note => out.append(a, .{ .text = l, .style = .{ .fg = theme.current.comment } }) catch {},
115123 .ctx => out.append(a, .{ .text = l, .style = .{ .fg = theme.current.comment } }) catch {},
116124 .add, .del => {
117- const bg = if (kinds[i] == .add) theme.current.diff_add else theme.current.diff_delete;
125+ const is_add = kinds[i] == .add;
126+ if (is_add) added += 1 else removed += 1;
127+ const bg = if (is_add) theme.current.diff_add else theme.current.diff_delete;
118128 const content = l[1..];
119129 const painted = highlight.paint(a, content, rebaseSpans(a, spans, los[i], his[i]), .{ .bg = bg });
120130 out.append(a, .{
@@ -124,10 +134,10 @@ pub fn diffLines(a: std.mem.Allocator, path: []const u8, lines: []const []const
124134 },
125135 }
126136 }
127- return out.items;
137+ return .{ .lines = out.items, .added = added, .removed = removed };
128138 }
129139
130-pub fn diffBody(a: std.mem.Allocator, d: tools.FileDiff, max_diff_lines: usize) []const BodyLine {
140+pub fn diffBody(a: std.mem.Allocator, d: tools.FileDiff, max_diff_lines: usize) Rendered {
131141 if (d.patch) |patch| {
132142 const trimmed = std.mem.trimEnd(u8, patch, " \t\r\n");
133143 var it = std.mem.splitScalar(u8, trimmed, '\n');
@@ -137,5 +147,5 @@ pub fn diffBody(a: std.mem.Allocator, d: tools.FileDiff, max_diff_lines: usize)
137147 }
138148 var out: std.ArrayList(BodyLine) = .empty;
139149 if (d.note) |note| out.append(a, .{ .text = note, .style = .{ .fg = theme.current.comment } }) catch {};
140- return out.items;
150+ return .{ .lines = out.items };
141151 }