╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1236 grid.deinit(); 1237 self.grid = null; 1238 } 1239 self.child.deinit(allocator); 1240 allocator.destroy(self.child); 1241 } 1242 1243 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1244 self.clearGrid(); 1245 self.getFocus().clear(); 1246 1247 // build the child without forcing it to fill min_size; it sizes 1248 // itself to its content within the available max. 1249 try self.child.build(allocator, .{ 1250 .min_size = .{ .width = null, .height = null }, 1251 .max_size = constraint.max_size, 1252 }, root_focus); 1253 1254 const child_grid = self.child.getGrid() orelse return; 1255 1256 // prefer max when given; otherwise grow to max(min, child) so that 1257 // when only a min is set (e.g. the wasm path passes viewport rows 1258 // as min_height with no max), we can still vertically center while 1259 // letting taller content extend past the min. 1260 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; 1261 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; 1262 if (width == 0 or height == 0) return; 1263 1264 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1265 (width -| child_grid.size.width) / 2 1266 else 1267 0; 1268 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1269 (height -| child_grid.size.height) / 2 1270 else 1271 0; 1272 1273 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1274 errdefer grid.deinit(); 1275 try grid.drawGrid(child_grid, offset_x, offset_y); 1276 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1277 self.getFocus().child_id = self.child.getFocus().id; 1278 1279 self.grid = grid; 1280 } 1281 1282 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1283 try self.child.input(allocator, key, root_focus); 1284 } 1285 1286 pub fn clearGrid(self: *Center) void { 1287 if (self.grid) |*grid| { 1288 grid.deinit(); 1289 self.grid = null; 1290 } 1291 self.child.clearGrid(); 1292 } 1293 1294 pub fn getGrid(self: Center) ?Grid { 1295 return self.grid; 1296 } 1297 1298 pub fn getFocus(self: *Center) *Focus { 1299 return self.focus; 1300 } 1301 }; 1302 1303 // renders truecolor ANSI art 1304 pub const AnsiArt = struct { 1305 focus: *Focus, 1306 grid: ?Grid, 1307 content: []const u8, 1308 1309 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1310 return .{ 1311 .focus = try Focus.create(allocator, .container), 1312 .grid = null, 1313 .content = content, 1314 }; 1315 } 1316 1317 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1318 self.focus.destroy(allocator); 1319 if (self.grid) |*grid| { 1320 grid.deinit(); 1321 self.grid = null; 1322 } 1323 } 1324 1325 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1326 _ = root_focus; 1327 self.clearGrid(); 1328 1329 // parse into rows of cells, then copy into a rectangular grid 1330 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1331 defer { 1332 for (rows.items) |*row| row.deinit(allocator); 1333 rows.deinit(allocator); 1334 } 1335 var row: std.ArrayList(Grid.Cell) = .empty; 1336 errdefer row.deinit(allocator); 1337 1338 var style: Grid.Style = .{}; 1339 var width: usize = 0; 1340 const content = self.content; 1341 var i: usize = 0; 1342 while (i < content.len) { 1343 const byte = content[i]; 1344 if (byte == '\n') { 1345 width = @max(width, row.items.len); 1346 try rows.append(allocator, row); 1347 row = .empty; 1348 i += 1; 1349 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1350 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1351 var j = i + 2; 1352 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1353 if (j >= content.len) { 1354 i = content.len; // malformed trailing escape; stop 1355 } else { 1356 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1357 i = j + 1; 1358 } 1359 } else { 1360 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1361 const end = @min(content.len, i + len); 1362 const rune = content[i..end]; 1363 const transparent = end - i == 1 and rune[0] == ' ' and 1364 style.fg == null and style.bg == null and !style.inverted; 1365 try row.append(allocator, .{ 1366 .rune = if (transparent) null else rune, 1367 .style = style, 1368 }); 1369 i = end; 1370 } 1371 } 1372 // a trailing row with content but no closing newline 1373 if (row.items.len > 0) { 1374 width = @max(width, row.items.len); 1375 try rows.append(allocator, row); 1376 } else { 1377 row.deinit(allocator); 1378 } 1379 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1380 1381 const height = rows.items.len; 1382 if (width == 0 or height == 0) return; 1383 1384 const clamped_w = @min(width, constraint.max_size.width orelse width); 1385 const clamped_h = @min(height, constraint.max_size.height orelse height); 1386 1387 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1388 errdefer grid.deinit(); 1389 for (rows.items[0..clamped_h], 0..) |r, y| { 1390 const n = @min(r.items.len, clamped_w); 1391 for (r.items[0..n], 0..) |cell, x| { 1392 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1393 } 1394 } 1395 self.grid = grid; 1396 } 1397 1398 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1399 _ = self; 1400 _ = allocator; 1401 _ = key; 1402 _ = root_focus; 1403 } 1404 1405 pub fn clearGrid(self: *AnsiArt) void { 1406 if (self.grid) |*grid| { 1407 grid.deinit(); 1408 self.grid = null; 1409 } 1410 } 1411 1412 pub fn getGrid(self: AnsiArt) ?Grid { 1413 return self.grid; 1414 } 1415 1416 pub fn getFocus(self: *AnsiArt) *Focus { 1417 return self.focus; 1418 } 1419 1420 fn applySgr(style: *Grid.Style, params: []const u8) void { 1421 var nums: [16]u32 = undefined; 1422 var n: usize = 0; 1423 var it = std.mem.splitScalar(u8, params, ';'); 1424 while (it.next()) |tok| { 1425 if (n >= nums.len) break; 1426 // an empty parameter (e.g. bare "\x1b[m") means 0 1427 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1428 n += 1; 1429 } 1430 if (n == 0) { 1431 style.* = .{}; 1432 return; 1433 } 1434 var i: usize = 0; 1435 while (i < n) : (i += 1) { 1436 switch (nums[i]) { 1437 0 => style.* = .{}, 1438 7 => style.inverted = true, 1439 27 => style.inverted = false, 1440 39 => style.fg = null, 1441 49 => style.bg = null, 1442 38, 48 => { 1443 // truecolor form: 38;2;r;g;b — anything else is ignored 1444 if (i + 4 < n and nums[i + 1] == 2) { 1445 const c = Grid.Color{ 1446 .r = @truncate(nums[i + 2]), 1447 .g = @truncate(nums[i + 3]), 1448 .b = @truncate(nums[i + 4]), 1449 }; 1450 if (nums[i] == 38) style.fg = c else style.bg = c; 1451 i += 4; 1452 } 1453 }, 1454 else => {}, 1455 } 1456 } 1457 } 1458 }; 1459 1460 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1461 pub const AnsiBackground = struct { 1462 grid: ?Grid, 1463 child: *Widget, 1464 art: AnsiArt, 1465 session: *Session, 1466 1467 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1468 var cw = child_widget; 1469 const child = allocator.create(Widget) catch |e| { 1470 cw.deinit(allocator); 1471 return e; 1472 }; 1473 child.* = cw; 1474 errdefer { 1475 child.deinit(allocator); 1476 allocator.destroy(child); 1477 } 1478 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1479 } 1480 1481 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1482 if (self.grid) |*grid| grid.deinit(); 1483 self.art.deinit(allocator); 1484 self.child.deinit(allocator); 1485 allocator.destroy(self.child); 1486 } 1487 1488 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1489 self.clearGrid(); 1490 1491 // make the wrapped page fill the whole available area 1492 try self.child.build(allocator, .{ 1493 .min_size = .{ 1494 .width = constraint.max_size.width orelse constraint.min_size.width, 1495 .height = constraint.max_size.height orelse constraint.min_size.height, 1496 }, 1497 .max_size = constraint.max_size, 1498 }, root_focus); 1499 1500 if (self.session.data.enable_ansi) { 1501 if (self.child.getGrid()) |fg| { 1502 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1503 } 1504 } 1505 } 1506 1507 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1508 try self.child.input(allocator, key, root_focus); 1509 } 1510 1511 pub fn clearGrid(self: *AnsiBackground) void { 1512 if (self.grid) |*grid| { 1513 grid.deinit(); 1514 self.grid = null; 1515 } 1516 self.child.clearGrid(); 1517 self.art.clearGrid(); 1518 } 1519 1520 pub fn getGrid(self: AnsiBackground) ?Grid { 1521 return self.grid orelse self.child.getGrid(); 1522 } 1523 1524 pub fn getFocus(self: *AnsiBackground) *Focus { 1525 return self.child.getFocus(); 1526 } 1527 1528 // which top corner the art hugs. 1529 const ArtAnchor = enum { top_left, top_right }; 1530 1531 // a foreground cell counts as blank if it's empty or a bare space without styling 1532 fn cellIsBlank(cell: Grid.Cell) bool { 1533 if (cell.rune) |rune| { 1534 return std.mem.eql(u8, rune, " ") and 1535 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1536 } 1537 return true; 1538 } 1539 1540 // a single representative color for an art cell 1541 fn artCellColor(style: Grid.Style) ?Grid.Color { 1542 const f = style.fg orelse return style.bg; 1543 const b = style.bg orelse return f; 1544 return .{ 1545 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1546 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1547 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1548 }; 1549 } 1550 1551 // behind a visible glyph the art color is dimmed to this fraction of its 1552 // brightness so light terminal text stays legible on top of it 1553 const glyph_bg_brightness = 45; // percent 1554 1555 // a glyph sitting over the art also gets its foreground pinned to this color 1556 // (unless it set its own), so the text stays legible 1557 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1558 1559 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1560 const c = color orelse return null; 1561 return .{ 1562 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1563 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1564 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1565 }; 1566 } 1567 1568 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1569 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1570 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1571 // snap such channels clear of that range so the backdrop doesn't blink or 1572 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1573 // actually render truecolor. 1574 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1575 const c = color orelse return null; 1576 const snap = struct { 1577 fn f(v: u8) u8 { 1578 return if (v >= 1 and v <= 9) 10 else v; 1579 } 1580 }.f; 1581 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1582 } 1583 1584 // composites ANSI art behind a foreground grid 1585 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1586 art.clearGrid(); 1587 try art.build(allocator, .{ 1588 .min_size = .{ .width = null, .height = null }, 1589 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1590 }, root_focus); 1591 1592 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1593 errdefer out.deinit(); 1594 1595 if (art.getGrid()) |art_grid| { 1596 const anchor_x = switch (anchor) { 1597 .top_left => 0, 1598 .top_right => foreground.size.width -| art_grid.size.width, 1599 }; 1600 for (0..art_grid.size.height) |y| { 1601 for (0..art_grid.size.width) |x| { 1602 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1603 if (src.rune == null) continue; 1604 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1605 const dst = &out.cells.items[idx]; 1606 if (cellIsBlank(dst.*)) { 1607 dst.* = src; 1608 dst.style.fg = sgrSafe(dst.style.fg); 1609 dst.style.bg = sgrSafe(dst.style.bg); 1610 } else if (dst.style.bg == null) { 1611 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1612 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1613 } 1614 } 1615 } 1616 } 1617 return out; 1618 } 1619 }; 1620