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