╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1667 pub fn clearGrid(self: *Spacer) void { 1668 if (self.grid) |*grid| { 1669 grid.deinit(); 1670 self.grid = null; 1671 } 1672 } 1673 1674 pub fn getGrid(self: Spacer) ?Grid { 1675 return self.grid; 1676 } 1677 1678 pub fn getFocus(self: *Spacer) *Focus { 1679 return self.focus; 1680 } 1681 }; 1682 1683 // a one-row, non-focusable status bar showing the current page's url 1684 pub const Footer = struct { 1685 focus: *Focus, 1686 grid: ?Grid, 1687 session: *Session, 1688 arena: std.heap.ArenaAllocator, 1689 1690 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1691 return .{ 1692 .focus = try Focus.create(allocator, .container), 1693 .grid = null, 1694 .session = session, 1695 .arena = std.heap.ArenaAllocator.init(allocator), 1696 }; 1697 } 1698 1699 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1700 self.focus.destroy(allocator); 1701 if (self.grid) |*grid| grid.deinit(); 1702 self.arena.deinit(); 1703 } 1704 1705 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1706 _ = root_focus; 1707 self.clearGrid(); 1708 const width = constraint.max_size.width orelse return; 1709 if (width == 0) return; 1710 1711 _ = self.arena.reset(.retain_capacity); 1712 const aa = self.arena.allocator(); 1713 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1714 const text = if (self.session.web_port) |port| 1715 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1716 else 1717 path; 1718 1719 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1720 errdefer grid.deinit(); 1721 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1722 var i: usize = 0; 1723 while (utf8.nextCodepointSlice()) |ch| { 1724 if (i == width) break; 1725 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1726 i += 1; 1727 } 1728 self.grid = grid; 1729 } 1730 1731 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1732 _ = self; 1733 _ = allocator; 1734 _ = key; 1735 _ = root_focus; 1736 } 1737 1738 pub fn clearGrid(self: *Footer) void { 1739 if (self.grid) |*grid| { 1740 grid.deinit(); 1741 self.grid = null; 1742 } 1743 } 1744 1745 pub fn getGrid(self: Footer) ?Grid { 1746 return self.grid; 1747 } 1748 1749 pub fn getFocus(self: *Footer) *Focus { 1750 return self.focus; 1751 } 1752 }; 1753 1754 // a single-child wrapper that builds the child at its natural size and 1755 // positions its grid in the middle of the area granted by the parent 1756 pub const Center = struct { 1757 focus: *Focus, 1758 grid: ?Grid, 1759 child: *Widget, 1760 1761 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1762 const child = try allocator.create(Widget); 1763 errdefer allocator.destroy(child); 1764 child.* = child_widget; 1765 return .{ 1766 .focus = try Focus.create(allocator, .container), 1767 .grid = null, 1768 .child = child, 1769 }; 1770 } 1771 1772 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1773 self.focus.destroy(allocator); 1774 if (self.grid) |*grid| { 1775 grid.deinit(); 1776 self.grid = null; 1777 } 1778 self.child.deinit(allocator); 1779 allocator.destroy(self.child); 1780 } 1781 1782 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1783 self.clearGrid(); 1784 self.getFocus().clear(); 1785 1786 // build the child without forcing it to fill min_size; it sizes 1787 // itself to its content within the available max. 1788 try self.child.build(allocator, .{ 1789 .min_size = .{ .width = null, .height = null }, 1790 .max_size = constraint.max_size, 1791 }, root_focus); 1792 1793 const child_grid = self.child.getGrid() orelse return; 1794 1795 // prefer max when given; otherwise grow to max(min, child) so that 1796 // when only a min is set (e.g. the wasm path passes viewport rows 1797 // as min_height with no max), we can still vertically center while 1798 // letting taller content extend past the min. 1799 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; 1800 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; 1801 if (width == 0 or height == 0) return; 1802 1803 const offset_x: usize = (width -| child_grid.size.width) / 2; 1804 const offset_y: usize = (height -| child_grid.size.height) / 2; 1805 1806 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1807 errdefer grid.deinit(); 1808 try grid.drawGrid(child_grid, offset_x, offset_y); 1809 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1810 self.getFocus().child_id = self.child.getFocus().id; 1811 1812 self.grid = grid; 1813 } 1814 1815 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1816 try self.child.input(allocator, key, root_focus); 1817 } 1818 1819 pub fn clearGrid(self: *Center) void { 1820 if (self.grid) |*grid| { 1821 grid.deinit(); 1822 self.grid = null; 1823 } 1824 self.child.clearGrid(); 1825 } 1826 1827 pub fn getGrid(self: Center) ?Grid { 1828 return self.grid; 1829 } 1830 1831 pub fn getFocus(self: *Center) *Focus { 1832 return self.focus; 1833 } 1834 }; 1835 1836 // renders truecolor ANSI art 1837 pub const AnsiArt = struct { 1838 focus: *Focus, 1839 grid: ?Grid, 1840 content: []const u8, 1841 1842 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1843 return .{ 1844 .focus = try Focus.create(allocator, .container), 1845 .grid = null, 1846 .content = content, 1847 }; 1848 } 1849 1850 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1851 self.focus.destroy(allocator); 1852 if (self.grid) |*grid| { 1853 grid.deinit(); 1854 self.grid = null; 1855 } 1856 } 1857 1858 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1859 _ = root_focus; 1860 self.clearGrid(); 1861 1862 // parse into rows of cells, then copy into a rectangular grid 1863 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1864 defer { 1865 for (rows.items) |*row| row.deinit(allocator); 1866 rows.deinit(allocator); 1867 } 1868 var row: std.ArrayList(Grid.Cell) = .empty; 1869 errdefer row.deinit(allocator); 1870 1871 var style: Grid.Style = .{}; 1872 var width: usize = 0; 1873 const content = self.content; 1874 var i: usize = 0; 1875 while (i < content.len) { 1876 const byte = content[i]; 1877 if (byte == '\n') { 1878 width = @max(width, row.items.len); 1879 try rows.append(allocator, row); 1880 row = .empty; 1881 i += 1; 1882 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1883 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1884 var j = i + 2; 1885 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1886 if (j >= content.len) { 1887 i = content.len; // malformed trailing escape; stop 1888 } else { 1889 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1890 i = j + 1; 1891 } 1892 } else { 1893 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1894 const end = @min(content.len, i + len); 1895 const rune = content[i..end]; 1896 const transparent = end - i == 1 and rune[0] == ' ' and 1897 style.fg == null and style.bg == null and !style.inverted; 1898 try row.append(allocator, .{ 1899 .rune = if (transparent) null else rune, 1900 .style = style, 1901 }); 1902 i = end; 1903 } 1904 } 1905 // a trailing row with content but no closing newline 1906 if (row.items.len > 0) { 1907 width = @max(width, row.items.len); 1908 try rows.append(allocator, row); 1909 } else { 1910 row.deinit(allocator); 1911 } 1912 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1913 1914 const height = rows.items.len; 1915 if (width == 0 or height == 0) return; 1916 1917 const clamped_w = @min(width, constraint.max_size.width orelse width); 1918 const clamped_h = @min(height, constraint.max_size.height orelse height); 1919 1920 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1921 errdefer grid.deinit(); 1922 for (rows.items[0..clamped_h], 0..) |r, y| { 1923 const n = @min(r.items.len, clamped_w); 1924 for (r.items[0..n], 0..) |cell, x| { 1925 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1926 } 1927 } 1928 self.grid = grid; 1929 } 1930 1931 pub fn clearGrid(self: *AnsiArt) void { 1932 if (self.grid) |*grid| { 1933 grid.deinit(); 1934 self.grid = null; 1935 } 1936 } 1937 1938 pub fn getGrid(self: AnsiArt) ?Grid { 1939 return self.grid; 1940 } 1941 1942 pub fn getFocus(self: *AnsiArt) *Focus { 1943 return self.focus; 1944 } 1945 1946 fn applySgr(style: *Grid.Style, params: []const u8) void { 1947 var nums: [16]u32 = undefined; 1948 var n: usize = 0; 1949 var it = std.mem.splitScalar(u8, params, ';'); 1950 while (it.next()) |tok| { 1951 if (n >= nums.len) break; 1952 // an empty parameter (e.g. bare "\x1b[m") means 0 1953 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1954 n += 1; 1955 } 1956 if (n == 0) { 1957 style.* = .{}; 1958 return; 1959 } 1960 var i: usize = 0; 1961 while (i < n) : (i += 1) { 1962 switch (nums[i]) { 1963 0 => style.* = .{}, 1964 7 => style.inverted = true, 1965 27 => style.inverted = false, 1966 39 => style.fg = null, 1967 49 => style.bg = null, 1968 38, 48 => { 1969 // truecolor form: 38;2;r;g;b — anything else is ignored 1970 if (i + 4 < n and nums[i + 1] == 2) { 1971 const c = Grid.Color{ 1972 .r = @truncate(nums[i + 2]), 1973 .g = @truncate(nums[i + 3]), 1974 .b = @truncate(nums[i + 4]), 1975 }; 1976 if (nums[i] == 38) style.fg = c else style.bg = c; 1977 i += 4; 1978 } 1979 }, 1980 else => {}, 1981 } 1982 } 1983 } 1984 }; 1985 1986 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1987 pub const AnsiBackground = struct { 1988 grid: ?Grid, 1989 child: *Widget, 1990 art: AnsiArt, 1991 session: *Session, 1992 1993 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1994 var cw = child_widget; 1995 const child = allocator.create(Widget) catch |e| { 1996 cw.deinit(allocator); 1997 return e; 1998 }; 1999 child.* = cw; 2000 errdefer { 2001 child.deinit(allocator); 2002 allocator.destroy(child); 2003 } 2004 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2005 } 2006 2007 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2008 if (self.grid) |*grid| grid.deinit(); 2009 self.art.deinit(allocator); 2010 self.child.deinit(allocator); 2011 allocator.destroy(self.child); 2012 } 2013 2014 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2015 self.clearGrid(); 2016 2017 // make the wrapped page fill the whole available area 2018 try self.child.build(allocator, .{ 2019 .min_size = .{ 2020 .width = constraint.max_size.width orelse constraint.min_size.width, 2021 .height = constraint.max_size.height orelse constraint.min_size.height, 2022 }, 2023 .max_size = constraint.max_size, 2024 }, root_focus); 2025 2026 if (self.session.data.enable_ansi) { 2027 if (self.child.getGrid()) |fg| { 2028 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2029 } 2030 } 2031 } 2032 2033 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2034 try self.child.input(allocator, key, root_focus); 2035 } 2036 2037 pub fn clearGrid(self: *AnsiBackground) void { 2038 if (self.grid) |*grid| { 2039 grid.deinit(); 2040 self.grid = null; 2041 } 2042 self.child.clearGrid(); 2043 self.art.clearGrid(); 2044 } 2045 2046 pub fn getGrid(self: AnsiBackground) ?Grid { 2047 return self.grid orelse self.child.getGrid(); 2048 } 2049 2050 pub fn getFocus(self: *AnsiBackground) *Focus { 2051 return self.child.getFocus(); 2052 } 2053 2054 // which top corner the art hugs. 2055 const ArtAnchor = enum { top_left, top_right }; 2056 2057 // a foreground cell counts as blank if it's empty or a bare space without styling 2058 fn cellIsBlank(cell: Grid.Cell) bool { 2059 if (cell.rune) |rune| { 2060 return std.mem.eql(u8, rune, " ") and 2061 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2062 } 2063 return true; 2064 } 2065 2066 // the art is dimmed to this fraction of its brightness so foreground text 2067 // stays legible over it 2068 const art_brightness = 45; // percent 2069 2070 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2071 const c = color orelse return null; 2072 return .{ 2073 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2074 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2075 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2076 }; 2077 } 2078 2079 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2080 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2081 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2082 // snap such channels clear of that range so the backdrop doesn't blink or 2083 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2084 // actually render truecolor. 2085 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2086 const c = color orelse return null; 2087 const snap = struct { 2088 fn f(v: u8) u8 { 2089 return if (v >= 1 and v <= 9) 10 else v; 2090 } 2091 }.f; 2092 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2093 } 2094 2095 // composites ANSI art behind a foreground grid 2096 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2097 art.clearGrid(); 2098 try art.build(allocator, .{ 2099 .min_size = .{ .width = null, .height = null }, 2100 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2101 }, root_focus); 2102 2103 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2104 errdefer out.deinit(); 2105 2106 if (art.getGrid()) |art_grid| { 2107 const anchor_x = switch (anchor) { 2108 .top_left => 0, 2109 .top_right => foreground.size.width -| art_grid.size.width, 2110 }; 2111 for (0..art_grid.size.height) |y| { 2112 for (0..art_grid.size.width) |x| { 2113 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2114 if (src.rune == null) continue; 2115 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2116 const dst = &out.cells.items[idx]; 2117 // composite the dimmed art behind every cell so it shows 2118 // through the whole UI: a blank cell becomes the art, while a 2119 // cell with a glyph keeps the glyph but takes the dimmed art as 2120 // its background (replacing whatever background it had, which 2121 // would otherwise obscure the art). 2122 if (cellIsBlank(dst.*)) { 2123 dst.* = src; 2124 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2125 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2126 } else { 2127 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2128 } 2129 } 2130 } 2131 } 2132 return out; 2133 } 2134 }; 2135