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