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