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