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