╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1032 _ = allocator; 1033 _ = key; 1034 _ = root_focus; 1035 } 1036 1037 pub fn clearGrid(self: *Spacer) void { 1038 if (self.grid) |*grid| { 1039 grid.deinit(); 1040 self.grid = null; 1041 } 1042 } 1043 1044 pub fn getGrid(self: Spacer) ?Grid { 1045 return self.grid; 1046 } 1047 1048 pub fn getFocus(self: *Spacer) *Focus { 1049 return &self.focus; 1050 } 1051 }; 1052 1053 // a single-child wrapper that builds the child at its natural size and 1054 // positions its grid in the middle of the area granted by the parent 1055 pub const Center = struct { 1056 focus: Focus, 1057 grid: ?Grid, 1058 child: *Widget, 1059 direction: Direction, 1060 1061 pub const Direction = enum { both, horiz, vert }; 1062 1063 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1064 const child = try allocator.create(Widget); 1065 errdefer allocator.destroy(child); 1066 child.* = child_widget; 1067 return .{ 1068 .focus = Focus.init(.container), 1069 .grid = null, 1070 .child = child, 1071 .direction = direction, 1072 }; 1073 } 1074 1075 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1076 self.focus.deinit(allocator); 1077 if (self.grid) |*grid| { 1078 grid.deinit(); 1079 self.grid = null; 1080 } 1081 self.child.deinit(allocator); 1082 allocator.destroy(self.child); 1083 } 1084 1085 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1086 self.clearGrid(); 1087 self.getFocus().clear(); 1088 1089 // build the child without forcing it to fill min_size; it sizes 1090 // itself to its content within the available max. 1091 try self.child.build(allocator, .{ 1092 .min_size = .{ .width = null, .height = null }, 1093 .max_size = constraint.max_size, 1094 }, root_focus); 1095 1096 const child_grid = self.child.getGrid() orelse return; 1097 1098 // prefer max when given; otherwise grow to max(min, child) so that 1099 // when only a min is set (e.g. the wasm path passes viewport rows 1100 // as min_height with no max), we can still vertically center while 1101 // letting taller content extend past the min. 1102 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 1103 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 1104 if (width == 0 or height == 0) return; 1105 1106 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1107 (width -| child_grid.size.width) / 2 1108 else 1109 0; 1110 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1111 (height -| child_grid.size.height) / 2 1112 else 1113 0; 1114 1115 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1116 errdefer grid.deinit(); 1117 try grid.drawGrid(child_grid, offset_x, offset_y); 1118 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1119 self.getFocus().child_id = self.child.getFocus().id; 1120 1121 self.grid = grid; 1122 } 1123 1124 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1125 try self.child.input(allocator, key, root_focus); 1126 } 1127 1128 pub fn clearGrid(self: *Center) void { 1129 if (self.grid) |*grid| { 1130 grid.deinit(); 1131 self.grid = null; 1132 } 1133 self.child.clearGrid(); 1134 } 1135 1136 pub fn getGrid(self: Center) ?Grid { 1137 return self.grid; 1138 } 1139 1140 pub fn getFocus(self: *Center) *Focus { 1141 return &self.focus; 1142 } 1143 }; 1144 1145 // renders truecolor ANSI art 1146 pub const AnsiArt = struct { 1147 focus: Focus, 1148 grid: ?Grid, 1149 content: []const u8, 1150 1151 pub fn init(content: []const u8) AnsiArt { 1152 return .{ 1153 .focus = Focus.init(.container), 1154 .grid = null, 1155 .content = content, 1156 }; 1157 } 1158 1159 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1160 self.focus.deinit(allocator); 1161 if (self.grid) |*grid| { 1162 grid.deinit(); 1163 self.grid = null; 1164 } 1165 } 1166 1167 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1168 _ = root_focus; 1169 self.clearGrid(); 1170 1171 // parse into rows of cells, then copy into a rectangular grid 1172 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1173 defer { 1174 for (rows.items) |*row| row.deinit(allocator); 1175 rows.deinit(allocator); 1176 } 1177 var row: std.ArrayList(Grid.Cell) = .empty; 1178 errdefer row.deinit(allocator); 1179 1180 var style: Grid.Style = .{}; 1181 var width: usize = 0; 1182 const content = self.content; 1183 var i: usize = 0; 1184 while (i < content.len) { 1185 const byte = content[i]; 1186 if (byte == '\n') { 1187 width = @max(width, row.items.len); 1188 try rows.append(allocator, row); 1189 row = .empty; 1190 i += 1; 1191 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1192 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1193 var j = i + 2; 1194 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1195 if (j >= content.len) { 1196 i = content.len; // malformed trailing escape; stop 1197 } else { 1198 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1199 i = j + 1; 1200 } 1201 } else { 1202 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1203 const end = @min(content.len, i + len); 1204 const rune = content[i..end]; 1205 const transparent = end - i == 1 and rune[0] == ' ' and 1206 style.fg == null and style.bg == null and !style.inverted; 1207 try row.append(allocator, .{ 1208 .rune = if (transparent) null else rune, 1209 .style = style, 1210 }); 1211 i = end; 1212 } 1213 } 1214 // a trailing row with content but no closing newline 1215 if (row.items.len > 0) { 1216 width = @max(width, row.items.len); 1217 try rows.append(allocator, row); 1218 } else { 1219 row.deinit(allocator); 1220 } 1221 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1222 1223 const height = rows.items.len; 1224 if (width == 0 or height == 0) return; 1225 1226 const clamped_w = @min(width, constraint.max_size.width orelse width); 1227 const clamped_h = @min(height, constraint.max_size.height orelse height); 1228 1229 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1230 errdefer grid.deinit(); 1231 for (rows.items[0..clamped_h], 0..) |r, y| { 1232 const n = @min(r.items.len, clamped_w); 1233 for (r.items[0..n], 0..) |cell, x| { 1234 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1235 } 1236 } 1237 self.grid = grid; 1238 } 1239 1240 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1241 _ = self; 1242 _ = allocator; 1243 _ = key; 1244 _ = root_focus; 1245 } 1246 1247 pub fn clearGrid(self: *AnsiArt) void { 1248 if (self.grid) |*grid| { 1249 grid.deinit(); 1250 self.grid = null; 1251 } 1252 } 1253 1254 pub fn getGrid(self: AnsiArt) ?Grid { 1255 return self.grid; 1256 } 1257 1258 pub fn getFocus(self: *AnsiArt) *Focus { 1259 return &self.focus; 1260 } 1261 1262 fn applySgr(style: *Grid.Style, params: []const u8) void { 1263 var nums: [16]u32 = undefined; 1264 var n: usize = 0; 1265 var it = std.mem.splitScalar(u8, params, ';'); 1266 while (it.next()) |tok| { 1267 if (n >= nums.len) break; 1268 // an empty parameter (e.g. bare "\x1b[m") means 0 1269 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1270 n += 1; 1271 } 1272 if (n == 0) { 1273 style.* = .{}; 1274 return; 1275 } 1276 var i: usize = 0; 1277 while (i < n) : (i += 1) { 1278 switch (nums[i]) { 1279 0 => style.* = .{}, 1280 7 => style.inverted = true, 1281 27 => style.inverted = false, 1282 39 => style.fg = null, 1283 49 => style.bg = null, 1284 38, 48 => { 1285 // truecolor form: 38;2;r;g;b — anything else is ignored 1286 if (i + 4 < n and nums[i + 1] == 2) { 1287 const c = Grid.Color{ 1288 .r = @truncate(nums[i + 2]), 1289 .g = @truncate(nums[i + 3]), 1290 .b = @truncate(nums[i + 4]), 1291 }; 1292 if (nums[i] == 38) style.fg = c else style.bg = c; 1293 i += 4; 1294 } 1295 }, 1296 else => {}, 1297 } 1298 } 1299 } 1300 }; 1301 1302 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1303 pub const AnsiBackground = struct { 1304 grid: ?Grid, 1305 child: *Widget, 1306 art: AnsiArt, 1307 session: *Session, 1308 1309 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1310 var cw = child_widget; 1311 const child = allocator.create(Widget) catch |e| { 1312 cw.deinit(allocator); 1313 return e; 1314 }; 1315 child.* = cw; 1316 return .{ .grid = null, .child = child, .art = AnsiArt.init(art_content), .session = session }; 1317 } 1318 1319 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1320 if (self.grid) |*grid| grid.deinit(); 1321 self.art.deinit(allocator); 1322 self.child.deinit(allocator); 1323 allocator.destroy(self.child); 1324 } 1325 1326 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1327 self.clearGrid(); 1328 1329 // make the wrapped page fill the whole available area 1330 try self.child.build(allocator, .{ 1331 .min_size = .{ 1332 .width = constraint.max_size.width orelse constraint.min_size.width, 1333 .height = constraint.max_size.height orelse constraint.min_size.height, 1334 }, 1335 .max_size = constraint.max_size, 1336 }, root_focus); 1337 1338 if (self.session.data.enable_ansi) { 1339 if (self.child.getGrid()) |fg| { 1340 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1341 } 1342 } 1343 } 1344 1345 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1346 try self.child.input(allocator, key, root_focus); 1347 } 1348 1349 pub fn clearGrid(self: *AnsiBackground) void { 1350 if (self.grid) |*grid| { 1351 grid.deinit(); 1352 self.grid = null; 1353 } 1354 self.child.clearGrid(); 1355 self.art.clearGrid(); 1356 } 1357 1358 pub fn getGrid(self: AnsiBackground) ?Grid { 1359 return self.grid orelse self.child.getGrid(); 1360 } 1361 1362 pub fn getFocus(self: *AnsiBackground) *Focus { 1363 return self.child.getFocus(); 1364 } 1365 1366 // which top corner the art hugs. 1367 const ArtAnchor = enum { top_left, top_right }; 1368 1369 // a foreground cell counts as blank if it's empty or a bare space without styling 1370 fn cellIsBlank(cell: Grid.Cell) bool { 1371 if (cell.rune) |rune| { 1372 return std.mem.eql(u8, rune, " ") and 1373 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1374 } 1375 return true; 1376 } 1377 1378 // a single representative color for an art cell 1379 fn artCellColor(style: Grid.Style) ?Grid.Color { 1380 const f = style.fg orelse return style.bg; 1381 const b = style.bg orelse return f; 1382 return .{ 1383 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1384 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1385 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1386 }; 1387 } 1388 1389 // behind a visible glyph the art color is dimmed to this fraction of its 1390 // brightness so light terminal text stays legible on top of it 1391 const glyph_bg_brightness = 45; // percent 1392 1393 // a glyph sitting over the art also gets its foreground pinned to this color 1394 // (unless it set its own), so the text stays legible 1395 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1396 1397 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1398 const c = color orelse return null; 1399 return .{ 1400 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1401 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1402 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1403 }; 1404 } 1405 1406 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1407 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1408 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1409 // snap such channels clear of that range so the backdrop doesn't blink or 1410 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1411 // actually render truecolor. 1412 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1413 const c = color orelse return null; 1414 const snap = struct { 1415 fn f(v: u8) u8 { 1416 return if (v >= 1 and v <= 9) 10 else v; 1417 } 1418 }.f; 1419 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1420 } 1421 1422 // composites ANSI art behind a foreground grid 1423 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1424 art.clearGrid(); 1425 try art.build(allocator, .{ 1426 .min_size = .{ .width = null, .height = null }, 1427 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1428 }, root_focus); 1429 1430 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1431 errdefer out.deinit(); 1432 1433 if (art.getGrid()) |art_grid| { 1434 const anchor_x = switch (anchor) { 1435 .top_left => 0, 1436 .top_right => foreground.size.width -| art_grid.size.width, 1437 }; 1438 for (0..art_grid.size.height) |y| { 1439 for (0..art_grid.size.width) |x| { 1440 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1441 if (src.rune == null) continue; 1442 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1443 const dst = &out.cells.items[idx]; 1444 if (cellIsBlank(dst.*)) { 1445 dst.* = src; 1446 dst.style.fg = sgrSafe(dst.style.fg); 1447 dst.style.bg = sgrSafe(dst.style.bg); 1448 } else if (dst.style.bg == null) { 1449 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1450 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1451 } 1452 } 1453 } 1454 } 1455 return out; 1456 } 1457 }; 1458