xonecas.io
79113
Ma

mini-coding-agent / commit 252852a

fix: fomatting and diffs fixes

xonecas2026-10-26 252852a7a196872dd2c7ff3b6b6d1ce9caa31eb7 parent cf44c76

5 files changed

src/diff.zig+4−4
@@ -122,10 +122,10 @@ fn myers(a: std.mem.Allocator, la: []const Line, lb: []const Line) ![]Op {
122122 }
123123
124124 fn addRange(w: *std.Io.Writer, start: usize, count: usize) !void {
125- if (count == 0) {
126- try w.print("{d},0", .{start - 1});
127- } else {
128- try w.print("{d},{d}", .{ start, count });
125+ switch (count) {
126+ 0 => try w.print("{d},0", .{start - 1}),
127+ 1 => try w.print("{d}", .{start}),
128+ else => try w.print("{d},{d}", .{ start, count }),
129129 }
130130 }
131131
src/tui/diff_view.zig+6−4
@@ -37,15 +37,17 @@ pub fn diffLines(a: std.mem.Allocator, lines: []const []const u8, max_diff_lines
3737 if (std.mem.startsWith(u8, line, "diff ")) in_hunk = false;
3838 const kind = highlight.diffKind(line, in_hunk);
3939 if (kind == .hunk) in_hunk = true;
40+ // File content is untrusted: its own escapes never reach the terminal.
41+ const text = render.stripAnsi(a, line);
4042 switch (kind) {
41- .hunk => out.append(a, .{ .text = line, .style = diffHunk() }) catch {},
42- .note => out.append(a, .{ .text = line, .style = diffNote() }) catch {},
43- .ctx => out.append(a, .{ .text = line, .style = diffNote() }) catch {},
43+ .hunk => out.append(a, .{ .text = text, .style = diffHunk() }) catch {},
44+ .note => out.append(a, .{ .text = text, .style = diffNote() }) catch {},
45+ .ctx => out.append(a, .{ .text = text, .style = diffNote() }) catch {},
4446 .add, .del => {
4547 const is_add = kind == .add;
4648 if (is_add) added += 1 else removed += 1;
4749 const fg = if (is_add) styles.roles.diff_add else styles.roles.diff_delete;
48- out.append(a, .{ .text = line, .style = .{ .fg = fg } }) catch {};
50+ out.append(a, .{ .text = text, .style = .{ .fg = fg } }) catch {};
4951 },
5052 }
5153 }
src/tui/format.zig+41−30
@@ -189,40 +189,42 @@ fn splitCells(a: std.mem.Allocator, line: []const u8) []const []const u8 {
189189 return cells.items;
190190 }
191191
192-/// Cell text with its inline markers removed: what the styling pass will actually paint.
193-fn visible(a: std.mem.Allocator, cell: []const u8) []const u8 {
194- var out: std.ArrayList(u8) = .empty;
195- var i: usize = 0;
196- while (i < cell.len) {
197- const c = cell[i];
198- if (c == '\\' and i + 1 < cell.len) {
199- out.appendSlice(a, cell[i .. i + 2]) catch {};
200- i += 2;
192+/// Index of the `]` closing the `[` at `open`, skipping escaped bytes.
193+pub fn closeBracket(text: []const u8, open: usize) ?usize {
194+ var i = open + 1;
195+ while (i < text.len) : (i += 1) {
196+ if (text[i] == '\\') {
197+ i += 1;
201198 continue;
202199 }
203- if (c == '`' or c == '*' or c == '_') {
200+ if (text[i] == ']') return i;
201+ }
202+ return null;
203+}
204+
205+/// Index of the `)` closing the `(` at `open`, so destinations may hold parentheses.
206+pub fn linkEnd(text: []const u8, open: usize) ?usize {
207+ var depth: usize = 0;
208+ var i = open;
209+ while (i < text.len) : (i += 1) {
210+ if (text[i] == '\\') {
204211 i += 1;
205212 continue;
206213 }
207- if (c == '[') {
208- if (std.mem.indexOfScalarPos(u8, cell, i, ']')) |close| {
209- if (close + 1 < cell.len and cell[close + 1] == '(') {
210- if (std.mem.indexOfScalarPos(u8, cell, close + 1, ')')) |end| {
211- out.appendSlice(a, cell[i + 1 .. close]) catch {};
212- i = end + 1;
213- continue;
214- }
215- }
216- }
214+ if (text[i] != '(' and text[i] != ')') continue;
215+ if (text[i] == ')') {
216+ depth -= 1;
217+ if (depth == 0) return i;
218+ } else {
219+ depth += 1;
217220 }
218- out.append(a, c) catch {};
219- i += 1;
220221 }
221- return out.items;
222+ return null;
222223 }
223224
224-fn cellWidth(a: std.mem.Allocator, cell: []const u8) usize {
225- return render.displayWidth(visible(a, cell));
225+/// The styling pass only adds color, so a cell's own bytes are what gets painted.
226+fn cellWidth(cell: []const u8) usize {
227+ return render.displayWidth(cell);
226228 }
227229
228230 /// Delimiter row (`|---|:--:|`) as per-column alignments, or null when the line is not one.
@@ -257,7 +259,8 @@ fn rowCells(line: []const u8) bool {
257259 return trimmed_line.len > 0 and std.mem.indexOfScalar(u8, trimmed_line, '|') != null;
258260 }
259261
260-fn emitRow(a: std.mem.Allocator, block: *std.ArrayList(u8), cells: []const []const u8, named: usize, widths: []const usize, aligns: []const Align, delimiter: bool) void {
262+fn emitRow(a: std.mem.Allocator, block: *std.ArrayList(u8), indent: []const u8, cells: []const []const u8, named: usize, widths: []const usize, aligns: []const Align, delimiter: bool) void {
263+ block.appendSlice(a, indent) catch {};
261264 block.append(a, '|') catch {};
262265 for (0..named) |k| {
263266 const cell = if (k < cells.len) cells[k] else "";
@@ -268,7 +271,7 @@ fn emitRow(a: std.mem.Allocator, block: *std.ArrayList(u8), cells: []const []con
268271 block.appendNTimes(a, '-', widths[k] - 2) catch {};
269272 block.append(a, if (alignment == .right or alignment == .center) ':' else '-') catch {};
270273 } else {
271- const pad = widths[k] - cellWidth(a, cell);
274+ const pad = widths[k] - cellWidth(cell);
272275 const before: usize = switch (alignment) {
273276 .right => pad,
274277 .center => pad / 2,
@@ -282,7 +285,15 @@ fn emitRow(a: std.mem.Allocator, block: *std.ArrayList(u8), cells: []const []con
282285 }
283286 }
284287
288+/// Leading whitespace, kept so a table nested in a list stays in its item.
289+fn leadingText(line: []const u8) []const u8 {
290+ var i: usize = 0;
291+ while (i < line.len and (line[i] == ' ' or line[i] == '\t')) i += 1;
292+ return line[0..i];
293+}
294+
285295 fn formatTable(a: std.mem.Allocator, header: []const u8, aligns: []const Align, body: []const []const u8, trailing: bool) ?[]const u8 {
296+ const indent = leadingText(header);
286297 const named = aligns.len;
287298 const rows = a.alloc([]const []const u8, body.len + 1) catch return null;
288299 rows[0] = splitCells(a, header);
@@ -296,16 +307,16 @@ fn formatTable(a: std.mem.Allocator, header: []const u8, aligns: []const Align,
296307 const widths = a.alloc(usize, named) catch return null;
297308 @memset(widths, 3);
298309 for (rows) |cells| {
299- for (cells, 0..) |cell, k| widths[k] = @max(widths[k], cellWidth(a, cell));
310+ for (cells, 0..) |cell, k| widths[k] = @max(widths[k], cellWidth(cell));
300311 }
301312
302313 var block: std.ArrayList(u8) = .empty;
303314 for (rows, 0..) |cells, r| {
304315 if (r > 0) block.append(a, '\n') catch {};
305- emitRow(a, &block, cells, named, widths, aligns, false);
316+ emitRow(a, &block, indent, cells, named, widths, aligns, false);
306317 if (r == 0) {
307318 block.append(a, '\n') catch {};
308- emitRow(a, &block, &.{}, named, widths, aligns, true);
319+ emitRow(a, &block, indent, &.{}, named, widths, aligns, true);
309320 }
310321 }
311322 if (trailing) block.append(a, '\n') catch {};
src/tui/highlight.zig+61−23
@@ -1,5 +1,6 @@
11 const std = @import("std");
22 const styles = @import("styles.zig");
3+const format = @import("format.zig");
34
45 const Span = struct {
56 start: u32,
@@ -82,18 +83,49 @@ fn runLength(line: []const u8, at: usize, c: u8) usize {
8283 return n;
8384 }
8485
85-/// Closing run for `marker` repeated `count` times, ignoring empty or space-padded content.
86+/// Just past the code span opened at `i`, or null when its backticks never close.
87+fn codeSpanEnd(line: []const u8, at: usize, limit: usize) ?usize {
88+ // Backslashes are literal inside a code span, so only the run length closes it.
89+ const n = runLength(line, at, '`');
90+ var i = at + n;
91+ while (i < limit) : (i += 1) {
92+ if (line[i] == '`' and runLength(line, i, '`') >= n) return i + n;
93+ }
94+ return null;
95+}
96+
97+/// Closing run for `marker` repeated `count` times, ignoring empty or space-padded content
98+/// and anything escaped or inside a code span.
8699 fn closeRun(line: []const u8, open_at: usize, marker: u8, count: usize, limit: usize) ?usize {
87100 var i = open_at + count;
88101 while (i + count <= limit) : (i += 1) {
102+ if (line[i] == '\\') {
103+ i += 1;
104+ continue;
105+ }
106+ if (line[i] == '`') {
107+ i = (codeSpanEnd(line, i, limit) orelse i + runLength(line, i, '`')) - 1;
108+ continue;
109+ }
89110 if (line[i] != marker) continue;
90111 if (line[i - 1] == ' ' or line[i - 1] == '\t') continue;
91- if (runLength(line, i, marker) < count) continue;
112+ const run = runLength(line, i, marker);
113+ if (run < count) continue;
114+ // `_` only closes where a word ends, so `foo_bar_baz` stays literal.
115+ if (marker == '_' and i + run < limit and isAlnum(line[i + run])) continue;
92116 return i;
93117 }
94118 return null;
95119 }
96120
121+fn emphasis(count: usize) styles.Style {
122+ return switch (count) {
123+ 1 => .{ .italic = true },
124+ 2 => .{ .bold = true },
125+ else => .{ .bold = true, .italic = true },
126+ };
127+}
128+
97129 fn addSpan(a: std.mem.Allocator, spans: *std.ArrayList(Span), base: u32, start: usize, end: usize, style: styles.Style) void {
98130 if (end <= start) return;
99131 spans.append(a, .{ .start = base + @as(u32, @intCast(start)), .end = base + @as(u32, @intCast(end)), .style = style }) catch {};
@@ -104,23 +136,23 @@ fn scanInline(a: std.mem.Allocator, line: []const u8, base: u32, start: usize, l
104136 var i = start;
105137 while (i < limit) {
106138 const c = line[i];
139+ if (c == '\\' and i + 1 < limit) {
140+ i += 2;
141+ continue;
142+ }
107143 if (c == '`') {
108144 const n = runLength(line, i, '`');
109- var j = i + n;
110- while (j + n <= limit) : (j += 1) {
111- if (line[j] != '`') continue;
112- if (runLength(line, j, '`') < n) continue;
113- addSpan(a, spans, base, i, j + n, styles.roles.inline_code);
114- i = j + n;
115- break;
116- } else {
145+ const end = codeSpanEnd(line, i, limit) orelse {
117146 i += n;
118- }
147+ continue;
148+ };
149+ addSpan(a, spans, base, i, end, styles.roles.inline_code);
150+ i = end;
119151 continue;
120152 }
121153 if (c == '*' or c == '_') {
122154 const n = runLength(line, i, c);
123- const count: usize = @min(n, 2);
155+ const count: usize = @min(n, 3);
124156 const content = i + count;
125157 // An opener needs content and, for `_`, must not sit inside a word.
126158 if (content >= limit or line[content] == ' ' or line[content] == '\t' or
@@ -130,8 +162,8 @@ fn scanInline(a: std.mem.Allocator, line: []const u8, base: u32, start: usize, l
130162 continue;
131163 }
132164 if (closeRun(line, i, c, count, limit)) |close| {
133- const style: styles.Style = if (count == 2) .{ .bold = true } else .{ .italic = true };
134- addSpan(a, spans, base, i, close + count, style);
165+ addSpan(a, spans, base, i, close + count, emphasis(count));
166+ scanInline(a, line, base, content, close, spans);
135167 i = close + count;
136168 } else {
137169 i += n;
@@ -139,9 +171,9 @@ fn scanInline(a: std.mem.Allocator, line: []const u8, base: u32, start: usize, l
139171 continue;
140172 }
141173 if (c == '[') {
142- if (std.mem.indexOfScalarPos(u8, line[0..limit], i, ']')) |close| {
174+ if (format.closeBracket(line[0..limit], i)) |close| {
143175 if (close + 1 < limit and line[close + 1] == '(') {
144- if (std.mem.indexOfScalarPos(u8, line[0..limit], close + 1, ')')) |end| {
176+ if (format.linkEnd(line[0..limit], close + 1)) |end| {
145177 addSpan(a, spans, base, i, close + 1, styles.roles.link);
146178 addSpan(a, spans, base, close + 1, end + 1, styles.roles.uri);
147179 i = end + 1;
@@ -202,7 +234,7 @@ fn blockMarker(line: []const u8) ?usize {
202234 if (at >= line.len) return null;
203235 const c = line[at];
204236 if (c == '>' or c == '-' or c == '*' or c == '+') {
205- if (at + 1 < line.len and (line[at + 1] == ' ' or line[at + 1] == '\t')) return at + 1;
237+ if (at + 1 >= line.len or line[at + 1] == ' ' or line[at + 1] == '\t') return at + 1;
206238 return null;
207239 }
208240 var n = at;
@@ -218,15 +250,21 @@ fn markdownLine(a: std.mem.Allocator, line: []const u8, base: u32, spans: *std.A
218250 addSpan(a, spans, base, 0, limit, muted());
219251 return;
220252 }
221- if (headingLevel(line)) |level| {
253+ var start: usize = 0;
254+ while (blockMarker(line[start..])) |after| {
255+ addSpan(a, spans, base, start, start + after, muted());
256+ start += after;
257+ }
258+ // Headings and rules keep their style when quoted or listed.
259+ const rest = line[start..];
260+ if (headingLevel(rest)) |level| {
222261 const idx = @min(level, styles.roles.headings.len) - 1;
223- addSpan(a, spans, base, 0, limit, styles.roles.headings[idx]);
262+ addSpan(a, spans, base, start, limit, styles.roles.headings[idx]);
224263 return;
225264 }
226- var start: usize = 0;
227- if (blockMarker(line)) |after| {
228- addSpan(a, spans, base, 0, after, muted());
229- start = after;
265+ if (isFenceMarker(rest) or isRule(rest)) {
266+ addSpan(a, spans, base, start, limit, muted());
267+ return;
230268 }
231269 scanInline(a, line, base, start, limit, spans);
232270 }
src/tui/stream.zig+3−2
@@ -120,9 +120,10 @@ pub const MarkdownStream = struct {
120120
121121 pub fn flush(self: *MarkdownStream, out_a: std.mem.Allocator) []BodyLine {
122122 var out: std.ArrayList(BodyLine) = .empty;
123- if (self.fence != null) {
123+ if (self.fence) |*f| {
124+ // The unterminated last line still belongs to the fence body.
125+ if (self.rest.items.len > 0) f.lines.append(self.a, self.rest.items) catch {};
124126 self.releaseFence(out_a, null, &out);
125- if (self.rest.items.len > 0) out.append(out_a, .{ .text = highlight.highlightMarkdown(out_a, self.rest.items) }) catch {};
126127 } else {
127128 self.releaseProse(out_a, self.norm.line(self.a, self.rest.items), &out);
128129 }