╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1991 } 1992 } 1993 1994 pub fn getGrid(self: Spacer) ?Grid { 1995 return self.grid; 1996 } 1997 1998 pub fn getFocus(self: *Spacer) *Focus { 1999 return self.focus; 2000 } 2001 }; 2002 2003 // a one-row, non-focusable status bar showing the current page's url 2004 pub const Footer = struct { 2005 focus: *Focus, 2006 grid: ?Grid, 2007 session: *Session, 2008 arena: std.heap.ArenaAllocator, 2009 2010 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2011 return .{ 2012 .focus = try Focus.create(allocator, .container), 2013 .grid = null, 2014 .session = session, 2015 .arena = std.heap.ArenaAllocator.init(allocator), 2016 }; 2017 } 2018 2019 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2020 self.focus.destroy(allocator); 2021 if (self.grid) |*grid| grid.deinit(); 2022 self.arena.deinit(); 2023 } 2024 2025 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2026 _ = root_focus; 2027 self.clearGrid(); 2028 const width = constraint.max_size.width orelse return; 2029 if (width == 0) return; 2030 2031 _ = self.arena.reset(.retain_capacity); 2032 const aa = self.arena.allocator(); 2033 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2034 const text = if (self.session.web_port) |port| 2035 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 2036 else 2037 path; 2038 2039 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2040 errdefer grid.deinit(); 2041 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2042 var i: usize = 0; 2043 while (utf8.nextCodepointSlice()) |ch| { 2044 if (i == width) break; 2045 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2046 i += 1; 2047 } 2048 self.grid = grid; 2049 } 2050 2051 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2052 _ = self; 2053 _ = allocator; 2054 _ = key; 2055 _ = root_focus; 2056 } 2057 2058 pub fn clearGrid(self: *Footer) void { 2059 if (self.grid) |*grid| { 2060 grid.deinit(); 2061 self.grid = null; 2062 } 2063 } 2064 2065 pub fn getGrid(self: Footer) ?Grid { 2066 return self.grid; 2067 } 2068 2069 pub fn getFocus(self: *Footer) *Focus { 2070 return self.focus; 2071 } 2072 }; 2073 2074 // a single-child wrapper that builds the child at its natural size and 2075 // positions its grid in the middle of the area granted by the parent 2076 pub const Center = struct { 2077 focus: *Focus, 2078 grid: ?Grid, 2079 child: *Widget, 2080 2081 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2082 const child = try allocator.create(Widget); 2083 errdefer allocator.destroy(child); 2084 child.* = child_widget; 2085 return .{ 2086 .focus = try Focus.create(allocator, .container), 2087 .grid = null, 2088 .child = child, 2089 }; 2090 } 2091 2092 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2093 self.focus.destroy(allocator); 2094 if (self.grid) |*grid| { 2095 grid.deinit(); 2096 self.grid = null; 2097 } 2098 self.child.deinit(allocator); 2099 allocator.destroy(self.child); 2100 } 2101 2102 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2103 self.clearGrid(); 2104 self.getFocus().clear(); 2105 2106 // build the child without forcing it to fill min_size; it sizes 2107 // itself to its content within the available max. 2108 try self.child.build(allocator, .{ 2109 .min_size = .{ .width = null, .height = null }, 2110 .max_size = constraint.max_size, 2111 }, root_focus); 2112 2113 const child_grid = self.child.getGrid() orelse return; 2114 2115 // prefer max when given; otherwise grow to max(min, child) so that 2116 // when only a min is set (e.g. the wasm path passes viewport rows 2117 // as min_height with no max), we can still vertically center while 2118 // letting taller content extend past the min. 2119 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; 2120 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; 2121 if (width == 0 or height == 0) return; 2122 2123 const offset_x: usize = (width -| child_grid.size.width) / 2; 2124 const offset_y: usize = (height -| child_grid.size.height) / 2; 2125 2126 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2127 errdefer grid.deinit(); 2128 try grid.drawGrid(child_grid, offset_x, offset_y); 2129 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2130 self.getFocus().child_id = self.child.getFocus().id; 2131 2132 self.grid = grid; 2133 } 2134 2135 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2136 try self.child.input(allocator, key, root_focus); 2137 } 2138 2139 pub fn clearGrid(self: *Center) void { 2140 if (self.grid) |*grid| { 2141 grid.deinit(); 2142 self.grid = null; 2143 } 2144 self.child.clearGrid(); 2145 } 2146 2147 pub fn getGrid(self: Center) ?Grid { 2148 return self.grid; 2149 } 2150 2151 pub fn getFocus(self: *Center) *Focus { 2152 return self.focus; 2153 } 2154 }; 2155 2156 // renders truecolor ANSI art 2157 pub const AnsiArt = struct { 2158 focus: *Focus, 2159 grid: ?Grid, 2160 content: []const u8, 2161 2162 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2163 return .{ 2164 .focus = try Focus.create(allocator, .container), 2165 .grid = null, 2166 .content = content, 2167 }; 2168 } 2169 2170 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2171 self.focus.destroy(allocator); 2172 if (self.grid) |*grid| { 2173 grid.deinit(); 2174 self.grid = null; 2175 } 2176 } 2177 2178 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2179 _ = root_focus; 2180 self.clearGrid(); 2181 2182 // parse into rows of cells, then copy into a rectangular grid 2183 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2184 defer { 2185 for (rows.items) |*row| row.deinit(allocator); 2186 rows.deinit(allocator); 2187 } 2188 var row: std.ArrayList(Grid.Cell) = .empty; 2189 errdefer row.deinit(allocator); 2190 2191 var style: Grid.Style = .{}; 2192 var width: usize = 0; 2193 const content = self.content; 2194 var i: usize = 0; 2195 while (i < content.len) { 2196 const byte = content[i]; 2197 if (byte == '\n') { 2198 width = @max(width, row.items.len); 2199 try rows.append(allocator, row); 2200 row = .empty; 2201 i += 1; 2202 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2203 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2204 var j = i + 2; 2205 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2206 if (j >= content.len) { 2207 i = content.len; // malformed trailing escape; stop 2208 } else { 2209 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2210 i = j + 1; 2211 } 2212 } else { 2213 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2214 const end = @min(content.len, i + len); 2215 const rune = content[i..end]; 2216 const transparent = end - i == 1 and rune[0] == ' ' and 2217 style.fg == null and style.bg == null and !style.inverted; 2218 try row.append(allocator, .{ 2219 .rune = if (transparent) null else rune, 2220 .style = style, 2221 }); 2222 i = end; 2223 } 2224 } 2225 // a trailing row with content but no closing newline 2226 if (row.items.len > 0) { 2227 width = @max(width, row.items.len); 2228 try rows.append(allocator, row); 2229 } else { 2230 row.deinit(allocator); 2231 } 2232 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2233 2234 const height = rows.items.len; 2235 if (width == 0 or height == 0) return; 2236 2237 const clamped_w = @min(width, constraint.max_size.width orelse width); 2238 const clamped_h = @min(height, constraint.max_size.height orelse height); 2239 2240 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2241 errdefer grid.deinit(); 2242 for (rows.items[0..clamped_h], 0..) |r, y| { 2243 const n = @min(r.items.len, clamped_w); 2244 for (r.items[0..n], 0..) |cell, x| { 2245 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2246 } 2247 } 2248 self.grid = grid; 2249 } 2250 2251 pub fn clearGrid(self: *AnsiArt) void { 2252 if (self.grid) |*grid| { 2253 grid.deinit(); 2254 self.grid = null; 2255 } 2256 } 2257 2258 pub fn getGrid(self: AnsiArt) ?Grid { 2259 return self.grid; 2260 } 2261 2262 pub fn getFocus(self: *AnsiArt) *Focus { 2263 return self.focus; 2264 } 2265 2266 fn applySgr(style: *Grid.Style, params: []const u8) void { 2267 var nums: [16]u32 = undefined; 2268 var n: usize = 0; 2269 var it = std.mem.splitScalar(u8, params, ';'); 2270 while (it.next()) |tok| { 2271 if (n >= nums.len) break; 2272 // an empty parameter (e.g. bare "\x1b[m") means 0 2273 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2274 n += 1; 2275 } 2276 if (n == 0) { 2277 style.* = .{}; 2278 return; 2279 } 2280 var i: usize = 0; 2281 while (i < n) : (i += 1) { 2282 switch (nums[i]) { 2283 0 => style.* = .{}, 2284 7 => style.inverted = true, 2285 27 => style.inverted = false, 2286 39 => style.fg = null, 2287 49 => style.bg = null, 2288 38, 48 => { 2289 // truecolor form: 38;2;r;g;b — anything else is ignored 2290 if (i + 4 < n and nums[i + 1] == 2) { 2291 const c = Grid.Color{ 2292 .r = @truncate(nums[i + 2]), 2293 .g = @truncate(nums[i + 3]), 2294 .b = @truncate(nums[i + 4]), 2295 }; 2296 if (nums[i] == 38) style.fg = c else style.bg = c; 2297 i += 4; 2298 } 2299 }, 2300 else => {}, 2301 } 2302 } 2303 } 2304 }; 2305 2306 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2307 pub const AnsiBackground = struct { 2308 grid: ?Grid, 2309 child: *Widget, 2310 art: AnsiArt, 2311 session: *Session, 2312 2313 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2314 var cw = child_widget; 2315 const child = allocator.create(Widget) catch |e| { 2316 cw.deinit(allocator); 2317 return e; 2318 }; 2319 child.* = cw; 2320 errdefer { 2321 child.deinit(allocator); 2322 allocator.destroy(child); 2323 } 2324 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2325 } 2326 2327 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2328 if (self.grid) |*grid| grid.deinit(); 2329 self.art.deinit(allocator); 2330 self.child.deinit(allocator); 2331 allocator.destroy(self.child); 2332 } 2333 2334 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2335 self.clearGrid(); 2336 2337 // make the wrapped page fill the whole available area 2338 try self.child.build(allocator, .{ 2339 .min_size = .{ 2340 .width = constraint.max_size.width orelse constraint.min_size.width, 2341 .height = constraint.max_size.height orelse constraint.min_size.height, 2342 }, 2343 .max_size = constraint.max_size, 2344 }, root_focus); 2345 2346 if (self.session.data.enable_ansi) { 2347 if (self.child.getGrid()) |fg| { 2348 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2349 } 2350 } 2351 } 2352 2353 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2354 try self.child.input(allocator, key, root_focus); 2355 } 2356 2357 pub fn clearGrid(self: *AnsiBackground) void { 2358 if (self.grid) |*grid| { 2359 grid.deinit(); 2360 self.grid = null; 2361 } 2362 self.child.clearGrid(); 2363 self.art.clearGrid(); 2364 } 2365 2366 pub fn getGrid(self: AnsiBackground) ?Grid { 2367 return self.grid orelse self.child.getGrid(); 2368 } 2369 2370 pub fn getFocus(self: *AnsiBackground) *Focus { 2371 return self.child.getFocus(); 2372 } 2373 2374 // which top corner the art hugs. 2375 const ArtAnchor = enum { top_left, top_right }; 2376 2377 // a foreground cell counts as blank if it's empty or a bare space without styling 2378 fn cellIsBlank(cell: Grid.Cell) bool { 2379 if (cell.rune) |rune| { 2380 return std.mem.eql(u8, rune, " ") and 2381 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2382 } 2383 return true; 2384 } 2385 2386 // the art is dimmed to this fraction of its brightness so foreground text 2387 // stays legible over it 2388 const art_brightness = 45; // percent 2389 2390 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2391 const c = color orelse return null; 2392 return .{ 2393 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2394 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2395 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2396 }; 2397 } 2398 2399 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2400 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2401 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2402 // snap such channels clear of that range so the backdrop doesn't blink or 2403 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2404 // actually render truecolor. 2405 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2406 const c = color orelse return null; 2407 const snap = struct { 2408 fn f(v: u8) u8 { 2409 return if (v >= 1 and v <= 9) 10 else v; 2410 } 2411 }.f; 2412 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2413 } 2414 2415 // composites ANSI art behind a foreground grid 2416 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2417 art.clearGrid(); 2418 try art.build(allocator, .{ 2419 .min_size = .{ .width = null, .height = null }, 2420 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2421 }, root_focus); 2422 2423 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2424 errdefer out.deinit(); 2425 2426 if (art.getGrid()) |art_grid| { 2427 const anchor_x = switch (anchor) { 2428 .top_left => 0, 2429 .top_right => foreground.size.width -| art_grid.size.width, 2430 }; 2431 for (0..art_grid.size.height) |y| { 2432 for (0..art_grid.size.width) |x| { 2433 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2434 if (src.rune == null) continue; 2435 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2436 const dst = &out.cells.items[idx]; 2437 // composite the dimmed art behind every cell so it shows 2438 // through the whole UI: a blank cell becomes the art, while a 2439 // cell with a glyph keeps the glyph but takes the dimmed art as 2440 // its background (replacing whatever background it had, which 2441 // would otherwise obscure the art). 2442 if (cellIsBlank(dst.*)) { 2443 dst.* = src; 2444 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2445 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2446 } else { 2447 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2448 } 2449 } 2450 } 2451 } 2452 return out; 2453 } 2454 }; 2455