╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 clearGrid(self: *AnsiArt) void { 1662 if (self.grid) |*grid| { 1663 grid.deinit(); 1664 self.grid = null; 1665 } 1666 } 1667 1668 pub fn getGrid(self: AnsiArt) ?Grid { 1669 return self.grid; 1670 } 1671 1672 pub fn getFocus(self: *AnsiArt) *Focus { 1673 return self.focus; 1674 } 1675 1676 fn applySgr(style: *Grid.Style, params: []const u8) void { 1677 var nums: [16]u32 = undefined; 1678 var n: usize = 0; 1679 var it = std.mem.splitScalar(u8, params, ';'); 1680 while (it.next()) |tok| { 1681 if (n >= nums.len) break; 1682 // an empty parameter (e.g. bare "\x1b[m") means 0 1683 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1684 n += 1; 1685 } 1686 if (n == 0) { 1687 style.* = .{}; 1688 return; 1689 } 1690 var i: usize = 0; 1691 while (i < n) : (i += 1) { 1692 switch (nums[i]) { 1693 0 => style.* = .{}, 1694 7 => style.inverted = true, 1695 27 => style.inverted = false, 1696 39 => style.fg = null, 1697 49 => style.bg = null, 1698 38, 48 => { 1699 // truecolor form: 38;2;r;g;b — anything else is ignored 1700 if (i + 4 < n and nums[i + 1] == 2) { 1701 const c = Grid.Color{ 1702 .r = @truncate(nums[i + 2]), 1703 .g = @truncate(nums[i + 3]), 1704 .b = @truncate(nums[i + 4]), 1705 }; 1706 if (nums[i] == 38) style.fg = c else style.bg = c; 1707 i += 4; 1708 } 1709 }, 1710 else => {}, 1711 } 1712 } 1713 } 1714 }; 1715 1716 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1717 pub const AnsiBackground = struct { 1718 grid: ?Grid, 1719 child: *Widget, 1720 art: AnsiArt, 1721 session: *Session, 1722 1723 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1724 var cw = child_widget; 1725 const child = allocator.create(Widget) catch |e| { 1726 cw.deinit(allocator); 1727 return e; 1728 }; 1729 child.* = cw; 1730 errdefer { 1731 child.deinit(allocator); 1732 allocator.destroy(child); 1733 } 1734 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1735 } 1736 1737 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1738 if (self.grid) |*grid| grid.deinit(); 1739 self.art.deinit(allocator); 1740 self.child.deinit(allocator); 1741 allocator.destroy(self.child); 1742 } 1743 1744 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1745 self.clearGrid(); 1746 1747 // make the wrapped page fill the whole available area 1748 try self.child.build(allocator, .{ 1749 .min_size = .{ 1750 .width = constraint.max_size.width orelse constraint.min_size.width, 1751 .height = constraint.max_size.height orelse constraint.min_size.height, 1752 }, 1753 .max_size = constraint.max_size, 1754 }, root_focus); 1755 1756 if (self.session.data.enable_ansi) { 1757 if (self.child.getGrid()) |fg| { 1758 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1759 } 1760 } 1761 } 1762 1763 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1764 try self.child.input(allocator, key, root_focus); 1765 } 1766 1767 pub fn clearGrid(self: *AnsiBackground) void { 1768 if (self.grid) |*grid| { 1769 grid.deinit(); 1770 self.grid = null; 1771 } 1772 self.child.clearGrid(); 1773 self.art.clearGrid(); 1774 } 1775 1776 pub fn getGrid(self: AnsiBackground) ?Grid { 1777 return self.grid orelse self.child.getGrid(); 1778 } 1779 1780 pub fn getFocus(self: *AnsiBackground) *Focus { 1781 return self.child.getFocus(); 1782 } 1783 1784 // which top corner the art hugs. 1785 const ArtAnchor = enum { top_left, top_right }; 1786 1787 // a foreground cell counts as blank if it's empty or a bare space without styling 1788 fn cellIsBlank(cell: Grid.Cell) bool { 1789 if (cell.rune) |rune| { 1790 return std.mem.eql(u8, rune, " ") and 1791 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1792 } 1793 return true; 1794 } 1795 1796 // the art is dimmed to this fraction of its brightness so foreground text 1797 // stays legible over it 1798 const art_brightness = 45; // percent 1799 1800 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1801 const c = color orelse return null; 1802 return .{ 1803 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1804 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1805 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1806 }; 1807 } 1808 1809 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1810 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1811 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1812 // snap such channels clear of that range so the backdrop doesn't blink or 1813 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1814 // actually render truecolor. 1815 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1816 const c = color orelse return null; 1817 const snap = struct { 1818 fn f(v: u8) u8 { 1819 return if (v >= 1 and v <= 9) 10 else v; 1820 } 1821 }.f; 1822 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1823 } 1824 1825 // composites ANSI art behind a foreground grid 1826 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1827 art.clearGrid(); 1828 try art.build(allocator, .{ 1829 .min_size = .{ .width = null, .height = null }, 1830 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1831 }, root_focus); 1832 1833 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1834 errdefer out.deinit(); 1835 1836 if (art.getGrid()) |art_grid| { 1837 const anchor_x = switch (anchor) { 1838 .top_left => 0, 1839 .top_right => foreground.size.width -| art_grid.size.width, 1840 }; 1841 for (0..art_grid.size.height) |y| { 1842 for (0..art_grid.size.width) |x| { 1843 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1844 if (src.rune == null) continue; 1845 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1846 const dst = &out.cells.items[idx]; 1847 // composite the dimmed art behind every cell so it shows 1848 // through the whole UI: a blank cell becomes the art, while a 1849 // cell with a glyph keeps the glyph but takes the dimmed art as 1850 // its background (replacing whatever background it had, which 1851 // would otherwise obscure the art). 1852 if (cellIsBlank(dst.*)) { 1853 dst.* = src; 1854 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1855 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1856 } else { 1857 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1858 } 1859 } 1860 } 1861 } 1862 return out; 1863 } 1864 }; 1865