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