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