╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1417 return .{ 1418 .focus = try Focus.create(allocator, .container), 1419 .grid = null, 1420 .session = session, 1421 .arena = std.heap.ArenaAllocator.init(allocator), 1422 }; 1423 } 1424 1425 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1426 self.focus.destroy(allocator); 1427 if (self.grid) |*grid| grid.deinit(); 1428 self.arena.deinit(); 1429 } 1430 1431 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1432 _ = root_focus; 1433 self.clearGrid(); 1434 const width = constraint.max_size.width orelse return; 1435 if (width == 0) return; 1436 1437 _ = self.arena.reset(.retain_capacity); 1438 const aa = self.arena.allocator(); 1439 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1440 const text = if (self.session.web_port) |port| 1441 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1442 else 1443 path; 1444 1445 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1446 errdefer grid.deinit(); 1447 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1448 var i: usize = 0; 1449 while (utf8.nextCodepointSlice()) |ch| { 1450 if (i == width) break; 1451 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1452 i += 1; 1453 } 1454 self.grid = grid; 1455 } 1456 1457 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1458 _ = self; 1459 _ = allocator; 1460 _ = key; 1461 _ = root_focus; 1462 } 1463 1464 pub fn clearGrid(self: *Footer) void { 1465 if (self.grid) |*grid| { 1466 grid.deinit(); 1467 self.grid = null; 1468 } 1469 } 1470 1471 pub fn getGrid(self: Footer) ?Grid { 1472 return self.grid; 1473 } 1474 1475 pub fn getFocus(self: *Footer) *Focus { 1476 return self.focus; 1477 } 1478 }; 1479 1480 // a single-child wrapper that builds the child at its natural size and 1481 // positions its grid in the middle of the area granted by the parent 1482 pub const Center = struct { 1483 focus: *Focus, 1484 grid: ?Grid, 1485 child: *Widget, 1486 direction: Direction, 1487 1488 pub const Direction = enum { both, horiz, vert }; 1489 1490 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1491 const child = try allocator.create(Widget); 1492 errdefer allocator.destroy(child); 1493 child.* = child_widget; 1494 return .{ 1495 .focus = try Focus.create(allocator, .container), 1496 .grid = null, 1497 .child = child, 1498 .direction = direction, 1499 }; 1500 } 1501 1502 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1503 self.focus.destroy(allocator); 1504 if (self.grid) |*grid| { 1505 grid.deinit(); 1506 self.grid = null; 1507 } 1508 self.child.deinit(allocator); 1509 allocator.destroy(self.child); 1510 } 1511 1512 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1513 self.clearGrid(); 1514 self.getFocus().clear(); 1515 1516 // build the child without forcing it to fill min_size; it sizes 1517 // itself to its content within the available max. 1518 try self.child.build(allocator, .{ 1519 .min_size = .{ .width = null, .height = null }, 1520 .max_size = constraint.max_size, 1521 }, root_focus); 1522 1523 const child_grid = self.child.getGrid() orelse return; 1524 1525 // prefer max when given; otherwise grow to max(min, child) so that 1526 // when only a min is set (e.g. the wasm path passes viewport rows 1527 // as min_height with no max), we can still vertically center while 1528 // letting taller content extend past the min. 1529 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; 1530 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; 1531 if (width == 0 or height == 0) return; 1532 1533 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1534 (width -| child_grid.size.width) / 2 1535 else 1536 0; 1537 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1538 (height -| child_grid.size.height) / 2 1539 else 1540 0; 1541 1542 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1543 errdefer grid.deinit(); 1544 try grid.drawGrid(child_grid, offset_x, offset_y); 1545 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1546 self.getFocus().child_id = self.child.getFocus().id; 1547 1548 self.grid = grid; 1549 } 1550 1551 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1552 try self.child.input(allocator, key, root_focus); 1553 } 1554 1555 pub fn clearGrid(self: *Center) void { 1556 if (self.grid) |*grid| { 1557 grid.deinit(); 1558 self.grid = null; 1559 } 1560 self.child.clearGrid(); 1561 } 1562 1563 pub fn getGrid(self: Center) ?Grid { 1564 return self.grid; 1565 } 1566 1567 pub fn getFocus(self: *Center) *Focus { 1568 return self.focus; 1569 } 1570 }; 1571 1572 // renders truecolor ANSI art 1573 pub const AnsiArt = struct { 1574 focus: *Focus, 1575 grid: ?Grid, 1576 content: []const u8, 1577 1578 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1579 return .{ 1580 .focus = try Focus.create(allocator, .container), 1581 .grid = null, 1582 .content = content, 1583 }; 1584 } 1585 1586 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1587 self.focus.destroy(allocator); 1588 if (self.grid) |*grid| { 1589 grid.deinit(); 1590 self.grid = null; 1591 } 1592 } 1593 1594 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1595 _ = root_focus; 1596 self.clearGrid(); 1597 1598 // parse into rows of cells, then copy into a rectangular grid 1599 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1600 defer { 1601 for (rows.items) |*row| row.deinit(allocator); 1602 rows.deinit(allocator); 1603 } 1604 var row: std.ArrayList(Grid.Cell) = .empty; 1605 errdefer row.deinit(allocator); 1606 1607 var style: Grid.Style = .{}; 1608 var width: usize = 0; 1609 const content = self.content; 1610 var i: usize = 0; 1611 while (i < content.len) { 1612 const byte = content[i]; 1613 if (byte == '\n') { 1614 width = @max(width, row.items.len); 1615 try rows.append(allocator, row); 1616 row = .empty; 1617 i += 1; 1618 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1619 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1620 var j = i + 2; 1621 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1622 if (j >= content.len) { 1623 i = content.len; // malformed trailing escape; stop 1624 } else { 1625 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1626 i = j + 1; 1627 } 1628 } else { 1629 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1630 const end = @min(content.len, i + len); 1631 const rune = content[i..end]; 1632 const transparent = end - i == 1 and rune[0] == ' ' and 1633 style.fg == null and style.bg == null and !style.inverted; 1634 try row.append(allocator, .{ 1635 .rune = if (transparent) null else rune, 1636 .style = style, 1637 }); 1638 i = end; 1639 } 1640 } 1641 // a trailing row with content but no closing newline 1642 if (row.items.len > 0) { 1643 width = @max(width, row.items.len); 1644 try rows.append(allocator, row); 1645 } else { 1646 row.deinit(allocator); 1647 } 1648 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1649 1650 const height = rows.items.len; 1651 if (width == 0 or height == 0) return; 1652 1653 const clamped_w = @min(width, constraint.max_size.width orelse width); 1654 const clamped_h = @min(height, constraint.max_size.height orelse height); 1655 1656 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1657 errdefer grid.deinit(); 1658 for (rows.items[0..clamped_h], 0..) |r, y| { 1659 const n = @min(r.items.len, clamped_w); 1660 for (r.items[0..n], 0..) |cell, x| { 1661 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1662 } 1663 } 1664 self.grid = grid; 1665 } 1666 1667 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1668 _ = self; 1669 _ = allocator; 1670 _ = key; 1671 _ = root_focus; 1672 } 1673 1674 pub fn clearGrid(self: *AnsiArt) void { 1675 if (self.grid) |*grid| { 1676 grid.deinit(); 1677 self.grid = null; 1678 } 1679 } 1680 1681 pub fn getGrid(self: AnsiArt) ?Grid { 1682 return self.grid; 1683 } 1684 1685 pub fn getFocus(self: *AnsiArt) *Focus { 1686 return self.focus; 1687 } 1688 1689 fn applySgr(style: *Grid.Style, params: []const u8) void { 1690 var nums: [16]u32 = undefined; 1691 var n: usize = 0; 1692 var it = std.mem.splitScalar(u8, params, ';'); 1693 while (it.next()) |tok| { 1694 if (n >= nums.len) break; 1695 // an empty parameter (e.g. bare "\x1b[m") means 0 1696 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1697 n += 1; 1698 } 1699 if (n == 0) { 1700 style.* = .{}; 1701 return; 1702 } 1703 var i: usize = 0; 1704 while (i < n) : (i += 1) { 1705 switch (nums[i]) { 1706 0 => style.* = .{}, 1707 7 => style.inverted = true, 1708 27 => style.inverted = false, 1709 39 => style.fg = null, 1710 49 => style.bg = null, 1711 38, 48 => { 1712 // truecolor form: 38;2;r;g;b — anything else is ignored 1713 if (i + 4 < n and nums[i + 1] == 2) { 1714 const c = Grid.Color{ 1715 .r = @truncate(nums[i + 2]), 1716 .g = @truncate(nums[i + 3]), 1717 .b = @truncate(nums[i + 4]), 1718 }; 1719 if (nums[i] == 38) style.fg = c else style.bg = c; 1720 i += 4; 1721 } 1722 }, 1723 else => {}, 1724 } 1725 } 1726 } 1727 }; 1728 1729 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1730 pub const AnsiBackground = struct { 1731 grid: ?Grid, 1732 child: *Widget, 1733 art: AnsiArt, 1734 session: *Session, 1735 1736 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1737 var cw = child_widget; 1738 const child = allocator.create(Widget) catch |e| { 1739 cw.deinit(allocator); 1740 return e; 1741 }; 1742 child.* = cw; 1743 errdefer { 1744 child.deinit(allocator); 1745 allocator.destroy(child); 1746 } 1747 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1748 } 1749 1750 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1751 if (self.grid) |*grid| grid.deinit(); 1752 self.art.deinit(allocator); 1753 self.child.deinit(allocator); 1754 allocator.destroy(self.child); 1755 } 1756 1757 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1758 self.clearGrid(); 1759 1760 // make the wrapped page fill the whole available area 1761 try self.child.build(allocator, .{ 1762 .min_size = .{ 1763 .width = constraint.max_size.width orelse constraint.min_size.width, 1764 .height = constraint.max_size.height orelse constraint.min_size.height, 1765 }, 1766 .max_size = constraint.max_size, 1767 }, root_focus); 1768 1769 if (self.session.data.enable_ansi) { 1770 if (self.child.getGrid()) |fg| { 1771 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1772 } 1773 } 1774 } 1775 1776 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1777 try self.child.input(allocator, key, root_focus); 1778 } 1779 1780 pub fn clearGrid(self: *AnsiBackground) void { 1781 if (self.grid) |*grid| { 1782 grid.deinit(); 1783 self.grid = null; 1784 } 1785 self.child.clearGrid(); 1786 self.art.clearGrid(); 1787 } 1788 1789 pub fn getGrid(self: AnsiBackground) ?Grid { 1790 return self.grid orelse self.child.getGrid(); 1791 } 1792 1793 pub fn getFocus(self: *AnsiBackground) *Focus { 1794 return self.child.getFocus(); 1795 } 1796 1797 // which top corner the art hugs. 1798 const ArtAnchor = enum { top_left, top_right }; 1799 1800 // a foreground cell counts as blank if it's empty or a bare space without styling 1801 fn cellIsBlank(cell: Grid.Cell) bool { 1802 if (cell.rune) |rune| { 1803 return std.mem.eql(u8, rune, " ") and 1804 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1805 } 1806 return true; 1807 } 1808 1809 // the art is dimmed to this fraction of its brightness so foreground text 1810 // stays legible over it 1811 const art_brightness = 45; // percent 1812 1813 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1814 const c = color orelse return null; 1815 return .{ 1816 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1817 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1818 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1819 }; 1820 } 1821 1822 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1823 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1824 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1825 // snap such channels clear of that range so the backdrop doesn't blink or 1826 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1827 // actually render truecolor. 1828 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1829 const c = color orelse return null; 1830 const snap = struct { 1831 fn f(v: u8) u8 { 1832 return if (v >= 1 and v <= 9) 10 else v; 1833 } 1834 }.f; 1835 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1836 } 1837 1838 // composites ANSI art behind a foreground grid 1839 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1840 art.clearGrid(); 1841 try art.build(allocator, .{ 1842 .min_size = .{ .width = null, .height = null }, 1843 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1844 }, root_focus); 1845 1846 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1847 errdefer out.deinit(); 1848 1849 if (art.getGrid()) |art_grid| { 1850 const anchor_x = switch (anchor) { 1851 .top_left => 0, 1852 .top_right => foreground.size.width -| art_grid.size.width, 1853 }; 1854 for (0..art_grid.size.height) |y| { 1855 for (0..art_grid.size.width) |x| { 1856 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1857 if (src.rune == null) continue; 1858 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1859 const dst = &out.cells.items[idx]; 1860 // composite the dimmed art behind every cell so it shows 1861 // through the whole UI: a blank cell becomes the art, while a 1862 // cell with a glyph keeps the glyph but takes the dimmed art as 1863 // its background (replacing whatever background it had, which 1864 // would otherwise obscure the art). 1865 if (cellIsBlank(dst.*)) { 1866 dst.* = src; 1867 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1868 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1869 } else { 1870 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1871 } 1872 } 1873 } 1874 } 1875 return out; 1876 } 1877 }; 1878