79113
Ma
mini-coding-agent / files
mini-coding-agent / src / tui / render.zig
8.3 KB
1const std = @import("std"); 2const styles = @import("styles.zig"); 3const wcwidth = @import("width.zig"); 4const utf8 = @import("../text.zig"); 5 6pub const nextCluster = wcwidth.nextCluster; 7 8pub fn displayWidth(text: []const u8) usize { 9 var width: usize = 0; 10 var i: usize = 0; 11 while (nextPiece(text, &i)) |piece| width += piece.width; 12 return width; 13} 14 15pub fn rowsForCells(cells: usize, width: usize) usize { 16 if (cells == 0) return 1; 17 return (cells + width - 1) / width; 18} 19 20pub fn physicalRows(line: []const u8, width: usize) usize { 21 return rowsForCells(displayWidth(line), width); 22} 23 24const Piece = struct { text: []const u8, width: usize, sgr: bool }; 25 26fn nextPiece(text: []const u8, i: *usize) ?Piece { 27 if (i.* >= text.len) return null; 28 if (isSgrAt(text, i.*)) |n| { 29 const piece = Piece{ .text = text[i.* .. i.* + n], .width = 0, .sgr = true }; 30 i.* += n; 31 return piece; 32 } 33 if (wcwidth.nextCluster(text, i.*)) |cluster| { 34 const piece = Piece{ .text = cluster.text, .width = if (cluster.text[0] == '\t') 0 else cluster.width, .sgr = false }; 35 i.* += cluster.text.len; 36 return piece; 37 } 38 const len = (utf8.decodeAt(text, i.*) orelse return null).len; 39 const piece = Piece{ .text = text[i.* .. i.* + len], .width = 1, .sgr = false }; 40 i.* += len; 41 return piece; 42} 43 44fn isSgrAt(text: []const u8, i: usize) ?usize { 45 if (i + 1 >= text.len or text[i] != 0x1b or text[i + 1] != '[') return null; 46 var j = i + 2; 47 while (j < text.len and (text[j] == ';' or (text[j] >= '0' and text[j] <= '9'))) j += 1; 48 if (j < text.len and text[j] == 'm') return j - i + 1; 49 if (j < text.len and text[j] == ':') { 50 while (j < text.len and (text[j] == ':' or text[j] == ';' or (text[j] >= '0' and text[j] <= '9'))) j += 1; 51 if (j < text.len and text[j] == 'm') return j - i + 1; 52 } 53 if (j == i + 2 and j < text.len and text[j] == 'm') return j - i + 1; 54 return null; 55} 56 57fn escapeLen(text: []const u8, i: usize) usize { 58 if (i + 1 >= text.len) return 1; 59 if (text[i + 1] == '[') { 60 var j = i + 2; 61 while (j < text.len and (text[j] < 0x40 or text[j] > 0x7e)) j += 1; 62 return (if (j < text.len) j + 1 else text.len) - i; 63 } 64 if (text[i + 1] == ']') { 65 var j = i + 2; 66 while (j < text.len) : (j += 1) { 67 if (text[j] == 0x07) return j + 1 - i; 68 if (text[j] == 0x1b and j + 1 < text.len and text[j + 1] == '\\') return j + 2 - i; 69 } 70 return text.len - i; 71 } 72 return 2; 73} 74 75pub fn sanitize(a: std.mem.Allocator, text: []const u8) []const u8 { 76 var clean = true; 77 for (text) |c| { 78 if (c >= 0x80 or c == 0x1b or c == 0x7f or (c < 0x20 and c != 0x09 and c != 0x0a)) { 79 clean = false; 80 break; 81 } 82 } 83 if (clean) return text; 84 85 var out: std.ArrayList(u8) = .empty; 86 var i: usize = 0; 87 while (i < text.len) { 88 const c = text[i]; 89 if (c == 0x1b) { 90 if (isSgrAt(text, i)) |n| { 91 out.appendSlice(a, text[i .. i + n]) catch {}; 92 i += n; 93 } else { 94 i += escapeLen(text, i); 95 } 96 continue; 97 } 98 if (c < 0x80) { 99 if ((c < 0x20 and c != 0x09 and c != 0x0a) or c == 0x7f) { 100 i += 1; 101 continue; 102 } 103 out.append(a, c) catch {}; 104 i += 1; 105 continue; 106 } 107 const d = utf8.decodeAt(text, i) orelse break; 108 if (d.cp > 0x9f) out.appendSlice(a, text[i .. i + d.len]) catch {}; 109 i += d.len; 110 } 111 return out.items; 112} 113 114pub fn stripAnsi(a: std.mem.Allocator, text: []const u8) []const u8 { 115 if (std.mem.indexOfScalar(u8, text, 0x1b) == null) return text; 116 var out: std.ArrayList(u8) = .empty; 117 var i: usize = 0; 118 while (i < text.len) { 119 if (text[i] == 0x1b) { 120 i += escapeLen(text, i); 121 continue; 122 } 123 out.append(a, text[i]) catch {}; 124 i += 1; 125 } 126 return out.items; 127} 128 129pub fn expandTabs(a: std.mem.Allocator, text: []const u8, size: usize) []const u8 { 130 if (std.mem.indexOfScalar(u8, text, '\t') == null) return text; 131 var out: std.ArrayList(u8) = .empty; 132 var column: usize = 0; 133 var i: usize = 0; 134 while (nextPiece(text, &i)) |piece| { 135 if (piece.text[0] != '\t') { 136 out.appendSlice(a, piece.text) catch {}; 137 column += piece.width; 138 continue; 139 } 140 const spaces = size - (column % size); 141 var k: usize = 0; 142 while (k < spaces) : (k += 1) out.append(a, ' ') catch {}; 143 column += spaces; 144 } 145 return out.items; 146} 147 148pub fn wrapLine(a: std.mem.Allocator, text: []const u8, width: usize) []const []const u8 { 149 var rows: std.ArrayList([]const u8) = .empty; 150 if (width == 0 or text.len == 0) { 151 rows.append(a, "") catch {}; 152 return rows.items; 153 } 154 var current: std.ArrayList(u8) = .empty; 155 var used: usize = 0; 156 var active: []const u8 = ""; 157 var i: usize = 0; 158 while (nextPiece(text, &i)) |piece| { 159 if (piece.sgr) { 160 current.appendSlice(a, piece.text) catch {}; 161 active = piece.text; 162 continue; 163 } 164 if (used + piece.width > width and used != 0) { 165 rows.append(a, current.items) catch {}; 166 current = .empty; 167 current.appendSlice(a, active) catch {}; 168 used = 0; 169 } 170 current.appendSlice(a, piece.text) catch {}; 171 used += piece.width; 172 } 173 rows.append(a, current.items) catch {}; 174 return rows.items; 175} 176 177pub fn clip(a: std.mem.Allocator, text: []const u8, width: usize) []const u8 { 178 if (width == 0) return ""; 179 if (displayWidth(text) <= width) return text; 180 var out: std.ArrayList(u8) = .empty; 181 var used: usize = 0; 182 var i: usize = 0; 183 while (nextPiece(text, &i)) |piece| { 184 if (piece.sgr) { 185 out.appendSlice(a, piece.text) catch {}; 186 continue; 187 } 188 if (used + piece.width > width - 1) break; 189 out.appendSlice(a, piece.text) catch {}; 190 used += piece.width; 191 } 192 out.appendSlice(a, "…") catch {}; 193 return out.items; 194} 195 196pub const BodyLine = struct { 197 text: []const u8, 198 style: ?styles.Style = null, 199}; 200 201const max_body_rows = 12; 202const elided_head = 4; 203const elided_tail = 4; 204 205pub fn paintRow(a: std.mem.Allocator, line: []const u8) []const u8 { 206 return std.fmt.allocPrint(a, "{s}{s}\x1b[K", .{ styles.SGR_PLAIN, line }) catch line; 207} 208 209pub fn styleLine(a: std.mem.Allocator, line: BodyLine, base: styles.Style) []const u8 { 210 const safe = sanitize(a, line.text); 211 const expanded = expandTabs(a, safe, 4); 212 if (expanded.len == 0) return ""; 213 if (line.style == null and !styles.anySet(base)) return expanded; 214 return std.fmt.allocPrint(a, "{s}{s}{s}", .{ styles.sgrOn(a, line.style orelse .{}, base), expanded, styles.SGR_PLAIN }) catch expanded; 215} 216 217pub fn plainRows(a: std.mem.Allocator, lines: []const BodyLine, base: styles.Style) []const []const u8 { 218 var out: std.ArrayList([]const u8) = .empty; 219 for (lines) |line| out.append(a, styleLine(a, line, base)) catch {}; 220 return out.items; 221} 222 223pub fn bodyRows(a: std.mem.Allocator, lines: []const BodyLine, width: usize, base: styles.Style) []const []const u8 { 224 return elide(a, plainRows(a, lines, base), width); 225} 226 227pub fn elide(a: std.mem.Allocator, rows: []const []const u8, width: usize) []const []const u8 { 228 var height: usize = 0; 229 for (rows) |r| height += physicalRows(r, width); 230 if (height <= max_body_rows or rows.len <= elided_head + elided_tail) return rows; 231 const tail_at = rows.len - elided_tail; 232 var shown: usize = 0; 233 var head: usize = 0; 234 while (head < tail_at) : (head += 1) { 235 const h = physicalRows(rows[head], width); 236 if (shown + h + elided_tail > max_body_rows - 1) break; 237 shown += h; 238 } 239 var out: std.ArrayList([]const u8) = .empty; 240 for (rows[0..head]) |r| out.append(a, r) catch {}; 241 const hidden_rows: usize = blk: { 242 var n = height; 243 for (rows[0..head]) |r| n -= physicalRows(r, width); 244 for (rows[tail_at..]) |r| n -= physicalRows(r, width); 245 break :blk n; 246 }; 247 out.append(a, styles.comment(a, std.fmt.allocPrint(a, "... {d} lines not shown ...", .{hidden_rows}) catch "")) catch {}; 248 for (rows[tail_at..]) |r| out.append(a, r) catch {}; 249 return out.items; 250}