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