╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1190 try self.child.input(allocator, key, root_focus); 1191 } 1192 1193 pub fn clearGrid(self: *Center) void { 1194 if (self.grid) |*grid| { 1195 grid.deinit(); 1196 self.grid = null; 1197 } 1198 self.child.clearGrid(); 1199 } 1200 1201 pub fn getGrid(self: Center) ?Grid { 1202 return self.grid; 1203 } 1204 1205 pub fn getFocus(self: *Center) *Focus { 1206 return self.focus; 1207 } 1208 }; 1209 1210 // renders truecolor ANSI art 1211 pub const AnsiArt = struct { 1212 focus: *Focus, 1213 grid: ?Grid, 1214 content: []const u8, 1215 1216 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1217 return .{ 1218 .focus = try Focus.create(allocator, .container), 1219 .grid = null, 1220 .content = content, 1221 }; 1222 } 1223 1224 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1225 self.focus.destroy(allocator); 1226 if (self.grid) |*grid| { 1227 grid.deinit(); 1228 self.grid = null; 1229 } 1230 } 1231 1232 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1233 _ = root_focus; 1234 self.clearGrid(); 1235 1236 // parse into rows of cells, then copy into a rectangular grid 1237 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1238 defer { 1239 for (rows.items) |*row| row.deinit(allocator); 1240 rows.deinit(allocator); 1241 } 1242 var row: std.ArrayList(Grid.Cell) = .empty; 1243 errdefer row.deinit(allocator); 1244 1245 var style: Grid.Style = .{}; 1246 var width: usize = 0; 1247 const content = self.content; 1248 var i: usize = 0; 1249 while (i < content.len) { 1250 const byte = content[i]; 1251 if (byte == '\n') { 1252 width = @max(width, row.items.len); 1253 try rows.append(allocator, row); 1254 row = .empty; 1255 i += 1; 1256 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1257 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1258 var j = i + 2; 1259 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1260 if (j >= content.len) { 1261 i = content.len; // malformed trailing escape; stop 1262 } else { 1263 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1264 i = j + 1; 1265 } 1266 } else { 1267 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1268 const end = @min(content.len, i + len); 1269 const rune = content[i..end]; 1270 const transparent = end - i == 1 and rune[0] == ' ' and 1271 style.fg == null and style.bg == null and !style.inverted; 1272 try row.append(allocator, .{ 1273 .rune = if (transparent) null else rune, 1274 .style = style, 1275 }); 1276 i = end; 1277 } 1278 } 1279 // a trailing row with content but no closing newline 1280 if (row.items.len > 0) { 1281 width = @max(width, row.items.len); 1282 try rows.append(allocator, row); 1283 } else { 1284 row.deinit(allocator); 1285 } 1286 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1287 1288 const height = rows.items.len; 1289 if (width == 0 or height == 0) return; 1290 1291 const clamped_w = @min(width, constraint.max_size.width orelse width); 1292 const clamped_h = @min(height, constraint.max_size.height orelse height); 1293 1294 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1295 errdefer grid.deinit(); 1296 for (rows.items[0..clamped_h], 0..) |r, y| { 1297 const n = @min(r.items.len, clamped_w); 1298 for (r.items[0..n], 0..) |cell, x| { 1299 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1300 } 1301 } 1302 self.grid = grid; 1303 } 1304 1305 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1306 _ = self; 1307 _ = allocator; 1308 _ = key; 1309 _ = root_focus; 1310 } 1311 1312 pub fn clearGrid(self: *AnsiArt) void { 1313 if (self.grid) |*grid| { 1314 grid.deinit(); 1315 self.grid = null; 1316 } 1317 } 1318 1319 pub fn getGrid(self: AnsiArt) ?Grid { 1320 return self.grid; 1321 } 1322 1323 pub fn getFocus(self: *AnsiArt) *Focus { 1324 return self.focus; 1325 } 1326 1327 fn applySgr(style: *Grid.Style, params: []const u8) void { 1328 var nums: [16]u32 = undefined; 1329 var n: usize = 0; 1330 var it = std.mem.splitScalar(u8, params, ';'); 1331 while (it.next()) |tok| { 1332 if (n >= nums.len) break; 1333 // an empty parameter (e.g. bare "\x1b[m") means 0 1334 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1335 n += 1; 1336 } 1337 if (n == 0) { 1338 style.* = .{}; 1339 return; 1340 } 1341 var i: usize = 0; 1342 while (i < n) : (i += 1) { 1343 switch (nums[i]) { 1344 0 => style.* = .{}, 1345 7 => style.inverted = true, 1346 27 => style.inverted = false, 1347 39 => style.fg = null, 1348 49 => style.bg = null, 1349 38, 48 => { 1350 // truecolor form: 38;2;r;g;b — anything else is ignored 1351 if (i + 4 < n and nums[i + 1] == 2) { 1352 const c = Grid.Color{ 1353 .r = @truncate(nums[i + 2]), 1354 .g = @truncate(nums[i + 3]), 1355 .b = @truncate(nums[i + 4]), 1356 }; 1357 if (nums[i] == 38) style.fg = c else style.bg = c; 1358 i += 4; 1359 } 1360 }, 1361 else => {}, 1362 } 1363 } 1364 } 1365 }; 1366 1367 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1368 pub const AnsiBackground = struct { 1369 grid: ?Grid, 1370 child: *Widget, 1371 art: AnsiArt, 1372 session: *Session, 1373 1374 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1375 var cw = child_widget; 1376 const child = allocator.create(Widget) catch |e| { 1377 cw.deinit(allocator); 1378 return e; 1379 }; 1380 child.* = cw; 1381 errdefer { 1382 child.deinit(allocator); 1383 allocator.destroy(child); 1384 } 1385 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1386 } 1387 1388 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1389 if (self.grid) |*grid| grid.deinit(); 1390 self.art.deinit(allocator); 1391 self.child.deinit(allocator); 1392 allocator.destroy(self.child); 1393 } 1394 1395 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1396 self.clearGrid(); 1397 1398 // make the wrapped page fill the whole available area 1399 try self.child.build(allocator, .{ 1400 .min_size = .{ 1401 .width = constraint.max_size.width orelse constraint.min_size.width, 1402 .height = constraint.max_size.height orelse constraint.min_size.height, 1403 }, 1404 .max_size = constraint.max_size, 1405 }, root_focus); 1406 1407 if (self.session.data.enable_ansi) { 1408 if (self.child.getGrid()) |fg| { 1409 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1410 } 1411 } 1412 } 1413 1414 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1415 try self.child.input(allocator, key, root_focus); 1416 } 1417 1418 pub fn clearGrid(self: *AnsiBackground) void { 1419 if (self.grid) |*grid| { 1420 grid.deinit(); 1421 self.grid = null; 1422 } 1423 self.child.clearGrid(); 1424 self.art.clearGrid(); 1425 } 1426 1427 pub fn getGrid(self: AnsiBackground) ?Grid { 1428 return self.grid orelse self.child.getGrid(); 1429 } 1430 1431 pub fn getFocus(self: *AnsiBackground) *Focus { 1432 return self.child.getFocus(); 1433 } 1434 1435 // which top corner the art hugs. 1436 const ArtAnchor = enum { top_left, top_right }; 1437 1438 // a foreground cell counts as blank if it's empty or a bare space without styling 1439 fn cellIsBlank(cell: Grid.Cell) bool { 1440 if (cell.rune) |rune| { 1441 return std.mem.eql(u8, rune, " ") and 1442 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1443 } 1444 return true; 1445 } 1446 1447 // a single representative color for an art cell 1448 fn artCellColor(style: Grid.Style) ?Grid.Color { 1449 const f = style.fg orelse return style.bg; 1450 const b = style.bg orelse return f; 1451 return .{ 1452 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1453 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1454 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1455 }; 1456 } 1457 1458 // behind a visible glyph the art color is dimmed to this fraction of its 1459 // brightness so light terminal text stays legible on top of it 1460 const glyph_bg_brightness = 45; // percent 1461 1462 // a glyph sitting over the art also gets its foreground pinned to this color 1463 // (unless it set its own), so the text stays legible 1464 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1465 1466 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1467 const c = color orelse return null; 1468 return .{ 1469 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1470 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1471 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1472 }; 1473 } 1474 1475 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1476 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1477 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1478 // snap such channels clear of that range so the backdrop doesn't blink or 1479 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1480 // actually render truecolor. 1481 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1482 const c = color orelse return null; 1483 const snap = struct { 1484 fn f(v: u8) u8 { 1485 return if (v >= 1 and v <= 9) 10 else v; 1486 } 1487 }.f; 1488 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1489 } 1490 1491 // composites ANSI art behind a foreground grid 1492 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1493 art.clearGrid(); 1494 try art.build(allocator, .{ 1495 .min_size = .{ .width = null, .height = null }, 1496 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1497 }, root_focus); 1498 1499 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1500 errdefer out.deinit(); 1501 1502 if (art.getGrid()) |art_grid| { 1503 const anchor_x = switch (anchor) { 1504 .top_left => 0, 1505 .top_right => foreground.size.width -| art_grid.size.width, 1506 }; 1507 for (0..art_grid.size.height) |y| { 1508 for (0..art_grid.size.width) |x| { 1509 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1510 if (src.rune == null) continue; 1511 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1512 const dst = &out.cells.items[idx]; 1513 if (cellIsBlank(dst.*)) { 1514 dst.* = src; 1515 dst.style.fg = sgrSafe(dst.style.fg); 1516 dst.style.bg = sgrSafe(dst.style.bg); 1517 } else if (dst.style.bg == null) { 1518 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1519 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1520 } 1521 } 1522 } 1523 } 1524 return out; 1525 } 1526 }; 1527