╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1515 self.clearGrid(); 1516 self.focus.clear(); 1517 1518 if (self.text_boxes.items.len == 0) return; 1519 const max_width = constraint.max_size.width orelse 80; 1520 if (max_width == 0) return; 1521 1522 // lay the items out left to right, wrapping to a new row when the next 1523 // one wouldn't fit. 1524 self.rects.clearRetainingCapacity(); 1525 var x: usize = 0; 1526 var y: usize = 0; 1527 var row_height: usize = 0; 1528 var total_width: usize = 0; 1529 for (self.text_boxes.items) |*tb| { 1530 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1531 try tb.build(allocator, .{ 1532 .min_size = .{ .width = null, .height = null }, 1533 .max_size = .{ .width = max_width, .height = null }, 1534 }, root_focus); 1535 const tb_grid_maybe = tb.getGrid(); 1536 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1537 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1538 if (x > 0 and x + w > max_width) { 1539 x = 0; 1540 y += row_height; 1541 row_height = 0; 1542 } 1543 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 1544 if (h > row_height) row_height = h; 1545 x += w; 1546 if (x > total_width) total_width = x; 1547 } 1548 1549 const total_height = y + row_height; 1550 if (total_width == 0 or total_height == 0) return; 1551 1552 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1553 errdefer grid.deinit(); 1554 1555 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1556 const tb_grid = tb.getGrid() orelse continue; 1557 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1558 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1559 } 1560 1561 self.grid = grid; 1562 } 1563 1564 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1565 _ = self; 1566 _ = allocator; 1567 _ = key; 1568 _ = root_focus; 1569 } 1570 1571 pub fn clearGrid(self: *TagFlow) void { 1572 if (self.grid) |*grid| { 1573 grid.deinit(); 1574 self.grid = null; 1575 } 1576 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1577 } 1578 1579 pub fn getGrid(self: TagFlow) ?Grid { 1580 return self.grid; 1581 } 1582 1583 pub fn getFocus(self: *TagFlow) *Focus { 1584 return self.focus; 1585 } 1586 1587 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1588 for (self.text_boxes.items, 0..) |tb, i| { 1589 if (tb.box.focus.id == focus_id) return i; 1590 } 1591 return null; 1592 } 1593 1594 // the first item of the row below (when `downward`) or above, or null at 1595 // the flow's edge. items are laid out in order, so y is nondecreasing. 1596 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1597 const rects = self.rects.items; 1598 if (index >= rects.len) return null; 1599 const cur_y = rects[index].y; 1600 if (downward) { 1601 for (rects[index + 1 ..], index + 1..) |r, i| { 1602 if (r.y > cur_y) return i; 1603 } 1604 return null; 1605 } 1606 // walk the earlier rows; the last one visited is the row above. 1607 var prev_first: ?usize = null; 1608 var i: usize = 0; 1609 while (i < rects.len and rects[i].y < cur_y) { 1610 prev_first = i; 1611 const y = rects[i].y; 1612 while (i < rects.len and rects[i].y == y) i += 1; 1613 } 1614 return prev_first; 1615 } 1616 }; 1617 1618 // an invisible widget that fills the horizontal space granted by its parent. 1619 // used inside Box(horiz) with a min_size so the box reserves space for the 1620 // children that follow, pushing them to the right. 1621 pub const Spacer = struct { 1622 focus: *Focus, 1623 grid: ?Grid, 1624 1625 pub fn init(allocator: std.mem.Allocator) !Spacer { 1626 return .{ 1627 .focus = try Focus.create(allocator, .container), 1628 .grid = null, 1629 }; 1630 } 1631 1632 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1633 self.focus.destroy(allocator); 1634 if (self.grid) |*grid| { 1635 grid.deinit(); 1636 self.grid = null; 1637 } 1638 } 1639 1640 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1641 _ = root_focus; 1642 self.clearGrid(); 1643 const width = constraint.max_size.width orelse return; 1644 if (width == 0) return; 1645 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1646 } 1647 1648 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1649 _ = self; 1650 _ = allocator; 1651 _ = key; 1652 _ = root_focus; 1653 } 1654 1655 pub fn clearGrid(self: *Spacer) void { 1656 if (self.grid) |*grid| { 1657 grid.deinit(); 1658 self.grid = null; 1659 } 1660 } 1661 1662 pub fn getGrid(self: Spacer) ?Grid { 1663 return self.grid; 1664 } 1665 1666 pub fn getFocus(self: *Spacer) *Focus { 1667 return self.focus; 1668 } 1669 }; 1670 1671 // a one-row, non-focusable status bar showing the current page's url 1672 pub const Footer = struct { 1673 focus: *Focus, 1674 grid: ?Grid, 1675 session: *Session, 1676 arena: std.heap.ArenaAllocator, 1677 1678 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1679 return .{ 1680 .focus = try Focus.create(allocator, .container), 1681 .grid = null, 1682 .session = session, 1683 .arena = std.heap.ArenaAllocator.init(allocator), 1684 }; 1685 } 1686 1687 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1688 self.focus.destroy(allocator); 1689 if (self.grid) |*grid| grid.deinit(); 1690 self.arena.deinit(); 1691 } 1692 1693 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1694 _ = root_focus; 1695 self.clearGrid(); 1696 const width = constraint.max_size.width orelse return; 1697 if (width == 0) return; 1698 1699 _ = self.arena.reset(.retain_capacity); 1700 const aa = self.arena.allocator(); 1701 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1702 const text = if (self.session.web_port) |port| 1703 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1704 else 1705 path; 1706 1707 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1708 errdefer grid.deinit(); 1709 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1710 var i: usize = 0; 1711 while (utf8.nextCodepointSlice()) |ch| { 1712 if (i == width) break; 1713 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1714 i += 1; 1715 } 1716 self.grid = grid; 1717 } 1718 1719 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1720 _ = self; 1721 _ = allocator; 1722 _ = key; 1723 _ = root_focus; 1724 } 1725 1726 pub fn clearGrid(self: *Footer) void { 1727 if (self.grid) |*grid| { 1728 grid.deinit(); 1729 self.grid = null; 1730 } 1731 } 1732 1733 pub fn getGrid(self: Footer) ?Grid { 1734 return self.grid; 1735 } 1736 1737 pub fn getFocus(self: *Footer) *Focus { 1738 return self.focus; 1739 } 1740 }; 1741 1742 // a single-child wrapper that builds the child at its natural size and 1743 // positions its grid in the middle of the area granted by the parent 1744 pub const Center = struct { 1745 focus: *Focus, 1746 grid: ?Grid, 1747 child: *Widget, 1748 1749 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1750 const child = try allocator.create(Widget); 1751 errdefer allocator.destroy(child); 1752 child.* = child_widget; 1753 return .{ 1754 .focus = try Focus.create(allocator, .container), 1755 .grid = null, 1756 .child = child, 1757 }; 1758 } 1759 1760 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1761 self.focus.destroy(allocator); 1762 if (self.grid) |*grid| { 1763 grid.deinit(); 1764 self.grid = null; 1765 } 1766 self.child.deinit(allocator); 1767 allocator.destroy(self.child); 1768 } 1769 1770 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1771 self.clearGrid(); 1772 self.getFocus().clear(); 1773 1774 // build the child without forcing it to fill min_size; it sizes 1775 // itself to its content within the available max. 1776 try self.child.build(allocator, .{ 1777 .min_size = .{ .width = null, .height = null }, 1778 .max_size = constraint.max_size, 1779 }, root_focus); 1780 1781 const child_grid = self.child.getGrid() orelse return; 1782 1783 // prefer max when given; otherwise grow to max(min, child) so that 1784 // when only a min is set (e.g. the wasm path passes viewport rows 1785 // as min_height with no max), we can still vertically center while 1786 // letting taller content extend past the min. 1787 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; 1788 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; 1789 if (width == 0 or height == 0) return; 1790 1791 const offset_x: usize = (width -| child_grid.size.width) / 2; 1792 const offset_y: usize = (height -| child_grid.size.height) / 2; 1793 1794 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1795 errdefer grid.deinit(); 1796 try grid.drawGrid(child_grid, offset_x, offset_y); 1797 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1798 self.getFocus().child_id = self.child.getFocus().id; 1799 1800 self.grid = grid; 1801 } 1802 1803 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1804 try self.child.input(allocator, key, root_focus); 1805 } 1806 1807 pub fn clearGrid(self: *Center) void { 1808 if (self.grid) |*grid| { 1809 grid.deinit(); 1810 self.grid = null; 1811 } 1812 self.child.clearGrid(); 1813 } 1814 1815 pub fn getGrid(self: Center) ?Grid { 1816 return self.grid; 1817 } 1818 1819 pub fn getFocus(self: *Center) *Focus { 1820 return self.focus; 1821 } 1822 }; 1823 1824 // renders truecolor ANSI art 1825 pub const AnsiArt = struct { 1826 focus: *Focus, 1827 grid: ?Grid, 1828 content: []const u8, 1829 1830 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1831 return .{ 1832 .focus = try Focus.create(allocator, .container), 1833 .grid = null, 1834 .content = content, 1835 }; 1836 } 1837 1838 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1839 self.focus.destroy(allocator); 1840 if (self.grid) |*grid| { 1841 grid.deinit(); 1842 self.grid = null; 1843 } 1844 } 1845 1846 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1847 _ = root_focus; 1848 self.clearGrid(); 1849 1850 // parse into rows of cells, then copy into a rectangular grid 1851 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1852 defer { 1853 for (rows.items) |*row| row.deinit(allocator); 1854 rows.deinit(allocator); 1855 } 1856 var row: std.ArrayList(Grid.Cell) = .empty; 1857 errdefer row.deinit(allocator); 1858 1859 var style: Grid.Style = .{}; 1860 var width: usize = 0; 1861 const content = self.content; 1862 var i: usize = 0; 1863 while (i < content.len) { 1864 const byte = content[i]; 1865 if (byte == '\n') { 1866 width = @max(width, row.items.len); 1867 try rows.append(allocator, row); 1868 row = .empty; 1869 i += 1; 1870 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1871 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1872 var j = i + 2; 1873 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1874 if (j >= content.len) { 1875 i = content.len; // malformed trailing escape; stop 1876 } else { 1877 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1878 i = j + 1; 1879 } 1880 } else { 1881 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1882 const end = @min(content.len, i + len); 1883 const rune = content[i..end]; 1884 const transparent = end - i == 1 and rune[0] == ' ' and 1885 style.fg == null and style.bg == null and !style.inverted; 1886 try row.append(allocator, .{ 1887 .rune = if (transparent) null else rune, 1888 .style = style, 1889 }); 1890 i = end; 1891 } 1892 } 1893 // a trailing row with content but no closing newline 1894 if (row.items.len > 0) { 1895 width = @max(width, row.items.len); 1896 try rows.append(allocator, row); 1897 } else { 1898 row.deinit(allocator); 1899 } 1900 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1901 1902 const height = rows.items.len; 1903 if (width == 0 or height == 0) return; 1904 1905 const clamped_w = @min(width, constraint.max_size.width orelse width); 1906 const clamped_h = @min(height, constraint.max_size.height orelse height); 1907 1908 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1909 errdefer grid.deinit(); 1910 for (rows.items[0..clamped_h], 0..) |r, y| { 1911 const n = @min(r.items.len, clamped_w); 1912 for (r.items[0..n], 0..) |cell, x| { 1913 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1914 } 1915 } 1916 self.grid = grid; 1917 } 1918 1919 pub fn clearGrid(self: *AnsiArt) void { 1920 if (self.grid) |*grid| { 1921 grid.deinit(); 1922 self.grid = null; 1923 } 1924 } 1925 1926 pub fn getGrid(self: AnsiArt) ?Grid { 1927 return self.grid; 1928 } 1929 1930 pub fn getFocus(self: *AnsiArt) *Focus { 1931 return self.focus; 1932 } 1933 1934 fn applySgr(style: *Grid.Style, params: []const u8) void { 1935 var nums: [16]u32 = undefined; 1936 var n: usize = 0; 1937 var it = std.mem.splitScalar(u8, params, ';'); 1938 while (it.next()) |tok| { 1939 if (n >= nums.len) break; 1940 // an empty parameter (e.g. bare "\x1b[m") means 0 1941 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1942 n += 1; 1943 } 1944 if (n == 0) { 1945 style.* = .{}; 1946 return; 1947 } 1948 var i: usize = 0; 1949 while (i < n) : (i += 1) { 1950 switch (nums[i]) { 1951 0 => style.* = .{}, 1952 7 => style.inverted = true, 1953 27 => style.inverted = false, 1954 39 => style.fg = null, 1955 49 => style.bg = null, 1956 38, 48 => { 1957 // truecolor form: 38;2;r;g;b — anything else is ignored 1958 if (i + 4 < n and nums[i + 1] == 2) { 1959 const c = Grid.Color{ 1960 .r = @truncate(nums[i + 2]), 1961 .g = @truncate(nums[i + 3]), 1962 .b = @truncate(nums[i + 4]), 1963 }; 1964 if (nums[i] == 38) style.fg = c else style.bg = c; 1965 i += 4; 1966 } 1967 }, 1968 else => {}, 1969 } 1970 } 1971 } 1972 }; 1973 1974 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1975 pub const AnsiBackground = struct { 1976 grid: ?Grid, 1977 child: *Widget, 1978 art: AnsiArt, 1979 session: *Session, 1980 1981 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1982 var cw = child_widget; 1983 const child = allocator.create(Widget) catch |e| { 1984 cw.deinit(allocator); 1985 return e; 1986 }; 1987 child.* = cw; 1988 errdefer { 1989 child.deinit(allocator); 1990 allocator.destroy(child); 1991 } 1992 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1993 } 1994 1995 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1996 if (self.grid) |*grid| grid.deinit(); 1997 self.art.deinit(allocator); 1998 self.child.deinit(allocator); 1999 allocator.destroy(self.child); 2000 } 2001 2002 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2003 self.clearGrid(); 2004 2005 // make the wrapped page fill the whole available area 2006 try self.child.build(allocator, .{ 2007 .min_size = .{ 2008 .width = constraint.max_size.width orelse constraint.min_size.width, 2009 .height = constraint.max_size.height orelse constraint.min_size.height, 2010 }, 2011 .max_size = constraint.max_size, 2012 }, root_focus); 2013 2014 if (self.session.data.enable_ansi) { 2015 if (self.child.getGrid()) |fg| { 2016 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2017 } 2018 } 2019 } 2020 2021 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2022 try self.child.input(allocator, key, root_focus); 2023 } 2024 2025 pub fn clearGrid(self: *AnsiBackground) void { 2026 if (self.grid) |*grid| { 2027 grid.deinit(); 2028 self.grid = null; 2029 } 2030 self.child.clearGrid(); 2031 self.art.clearGrid(); 2032 } 2033 2034 pub fn getGrid(self: AnsiBackground) ?Grid { 2035 return self.grid orelse self.child.getGrid(); 2036 } 2037 2038 pub fn getFocus(self: *AnsiBackground) *Focus { 2039 return self.child.getFocus(); 2040 } 2041 2042 // which top corner the art hugs. 2043 const ArtAnchor = enum { top_left, top_right }; 2044 2045 // a foreground cell counts as blank if it's empty or a bare space without styling 2046 fn cellIsBlank(cell: Grid.Cell) bool { 2047 if (cell.rune) |rune| { 2048 return std.mem.eql(u8, rune, " ") and 2049 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2050 } 2051 return true; 2052 } 2053 2054 // the art is dimmed to this fraction of its brightness so foreground text 2055 // stays legible over it 2056 const art_brightness = 45; // percent 2057 2058 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2059 const c = color orelse return null; 2060 return .{ 2061 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2062 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2063 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2064 }; 2065 } 2066 2067 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2068 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2069 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2070 // snap such channels clear of that range so the backdrop doesn't blink or 2071 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2072 // actually render truecolor. 2073 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2074 const c = color orelse return null; 2075 const snap = struct { 2076 fn f(v: u8) u8 { 2077 return if (v >= 1 and v <= 9) 10 else v; 2078 } 2079 }.f; 2080 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2081 } 2082 2083 // composites ANSI art behind a foreground grid 2084 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2085 art.clearGrid(); 2086 try art.build(allocator, .{ 2087 .min_size = .{ .width = null, .height = null }, 2088 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2089 }, root_focus); 2090 2091 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2092 errdefer out.deinit(); 2093 2094 if (art.getGrid()) |art_grid| { 2095 const anchor_x = switch (anchor) { 2096 .top_left => 0, 2097 .top_right => foreground.size.width -| art_grid.size.width, 2098 }; 2099 for (0..art_grid.size.height) |y| { 2100 for (0..art_grid.size.width) |x| { 2101 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2102 if (src.rune == null) continue; 2103 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2104 const dst = &out.cells.items[idx]; 2105 // composite the dimmed art behind every cell so it shows 2106 // through the whole UI: a blank cell becomes the art, while a 2107 // cell with a glyph keeps the glyph but takes the dimmed art as 2108 // its background (replacing whatever background it had, which 2109 // would otherwise obscure the art). 2110 if (cellIsBlank(dst.*)) { 2111 dst.* = src; 2112 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2113 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2114 } else { 2115 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2116 } 2117 } 2118 } 2119 } 2120 return out; 2121 } 2122 }; 2123