╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2030 if (width == 0) return; 2031 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2032 } 2033 2034 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2035 _ = self; 2036 _ = allocator; 2037 _ = key; 2038 _ = root_focus; 2039 } 2040 2041 pub fn clearGrid(self: *Spacer) void { 2042 if (self.grid) |*grid| { 2043 grid.deinit(); 2044 self.grid = null; 2045 } 2046 } 2047 2048 pub fn getGrid(self: Spacer) ?Grid { 2049 return self.grid; 2050 } 2051 2052 pub fn getFocus(self: *Spacer) *Focus { 2053 return self.focus; 2054 } 2055 }; 2056 2057 // a one-row, non-focusable status bar showing the current page's url 2058 pub const Footer = struct { 2059 focus: *Focus, 2060 grid: ?Grid, 2061 session: *Session, 2062 arena: std.heap.ArenaAllocator, 2063 2064 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2065 return .{ 2066 .focus = try Focus.create(allocator, .container), 2067 .grid = null, 2068 .session = session, 2069 .arena = std.heap.ArenaAllocator.init(allocator), 2070 }; 2071 } 2072 2073 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2074 self.focus.destroy(allocator); 2075 if (self.grid) |*grid| grid.deinit(); 2076 self.arena.deinit(); 2077 } 2078 2079 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2080 _ = root_focus; 2081 self.clearGrid(); 2082 const width = constraint.max_size.width orelse return; 2083 if (width == 0) return; 2084 2085 _ = self.arena.reset(.retain_capacity); 2086 const aa = self.arena.allocator(); 2087 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2088 const text = if (self.session.web_port) |port| 2089 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 2090 else 2091 path; 2092 2093 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2094 errdefer grid.deinit(); 2095 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2096 var i: usize = 0; 2097 while (utf8.nextCodepointSlice()) |ch| { 2098 if (i == width) break; 2099 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2100 i += 1; 2101 } 2102 self.grid = grid; 2103 } 2104 2105 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2106 _ = self; 2107 _ = allocator; 2108 _ = key; 2109 _ = root_focus; 2110 } 2111 2112 pub fn clearGrid(self: *Footer) void { 2113 if (self.grid) |*grid| { 2114 grid.deinit(); 2115 self.grid = null; 2116 } 2117 } 2118 2119 pub fn getGrid(self: Footer) ?Grid { 2120 return self.grid; 2121 } 2122 2123 pub fn getFocus(self: *Footer) *Focus { 2124 return self.focus; 2125 } 2126 }; 2127 2128 // a single-child wrapper that builds the child at its natural size and 2129 // positions its grid in the middle of the area granted by the parent 2130 // a bordered label, indented past the content it precedes 2131 pub const SectionLabel = struct { 2132 box: wgt.Box(Widget), 2133 2134 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2135 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2136 errdefer box.deinit(allocator); 2137 2138 { 2139 var gap = try wgt.Text(Widget).init(allocator, " "); 2140 errdefer gap.deinit(allocator); 2141 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2142 } 2143 2144 { 2145 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2146 errdefer tb.deinit(allocator); 2147 tb.getFocus().focusable = true; 2148 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2149 } 2150 2151 box.getFocus().child_id = box.children.keys()[1]; 2152 return .{ .box = box }; 2153 } 2154 2155 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2156 self.box.deinit(allocator); 2157 } 2158 2159 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2160 try self.box.build(allocator, constraint, root_focus); 2161 } 2162 2163 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2164 _ = self; 2165 _ = allocator; 2166 _ = key; 2167 _ = root_focus; 2168 } 2169 2170 pub fn clearGrid(self: *SectionLabel) void { 2171 self.box.clearGrid(); 2172 } 2173 2174 pub fn getGrid(self: SectionLabel) ?Grid { 2175 return self.box.getGrid(); 2176 } 2177 2178 pub fn getFocus(self: *SectionLabel) *Focus { 2179 return self.box.getFocus(); 2180 } 2181 }; 2182 2183 // a submit button, indented past its form's content. the web overlay POSTs 2184 // the button's form; terminal hosts detect presses through `buttonId`. 2185 pub const SubmitButton = struct { 2186 box: wgt.Box(Widget), 2187 2188 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2189 return initLabeled(allocator, "submit"); 2190 } 2191 2192 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2193 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2194 errdefer box.deinit(allocator); 2195 2196 { 2197 var gap = try wgt.Text(Widget).init(allocator, " "); 2198 errdefer gap.deinit(allocator); 2199 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2200 } 2201 2202 { 2203 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2204 errdefer button.deinit(allocator); 2205 button.getFocus().focusable = true; 2206 // the renderer distinguishes plain clickables from buttons that 2207 // should POST to a server route by this kind. 2208 button.getFocus().kind = .{ .custom = "submit" }; 2209 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2210 } 2211 2212 box.getFocus().child_id = box.children.keys()[1]; 2213 return .{ .box = box }; 2214 } 2215 2216 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2217 self.box.deinit(allocator); 2218 } 2219 2220 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2221 try self.box.build(allocator, constraint, root_focus); 2222 } 2223 2224 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2225 _ = self; 2226 _ = allocator; 2227 _ = key; 2228 _ = root_focus; 2229 } 2230 2231 pub fn clearGrid(self: *SubmitButton) void { 2232 self.box.clearGrid(); 2233 } 2234 2235 pub fn getGrid(self: SubmitButton) ?Grid { 2236 return self.box.getGrid(); 2237 } 2238 2239 pub fn getFocus(self: *SubmitButton) *Focus { 2240 return self.box.getFocus(); 2241 } 2242 2243 // the button's focus id, for click hit-testing 2244 pub fn buttonId(self: *const SubmitButton) usize { 2245 return self.box.children.keys()[1]; 2246 } 2247 }; 2248 2249 pub const Center = struct { 2250 focus: *Focus, 2251 grid: ?Grid, 2252 child: *Widget, 2253 2254 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2255 const child = try allocator.create(Widget); 2256 errdefer allocator.destroy(child); 2257 child.* = child_widget; 2258 return .{ 2259 .focus = try Focus.create(allocator, .container), 2260 .grid = null, 2261 .child = child, 2262 }; 2263 } 2264 2265 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2266 self.focus.destroy(allocator); 2267 if (self.grid) |*grid| { 2268 grid.deinit(); 2269 self.grid = null; 2270 } 2271 self.child.deinit(allocator); 2272 allocator.destroy(self.child); 2273 } 2274 2275 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2276 self.clearGrid(); 2277 self.getFocus().clear(); 2278 2279 // build the child without forcing it to fill min_size; it sizes 2280 // itself to its content within the available max. 2281 try self.child.build(allocator, .{ 2282 .min_size = .{ .width = null, .height = null }, 2283 .max_size = constraint.max_size, 2284 }, root_focus); 2285 2286 const child_grid = self.child.getGrid() orelse return; 2287 2288 // prefer max when given; otherwise grow to max(min, child) so that 2289 // when only a min is set (e.g. the wasm path passes viewport rows 2290 // as min_height with no max), we can still vertically center while 2291 // letting taller content extend past the min. 2292 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; 2293 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; 2294 if (width == 0 or height == 0) return; 2295 2296 const offset_x: usize = (width -| child_grid.size.width) / 2; 2297 const offset_y: usize = (height -| child_grid.size.height) / 2; 2298 2299 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2300 errdefer grid.deinit(); 2301 try grid.drawGrid(child_grid, offset_x, offset_y); 2302 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2303 self.getFocus().child_id = self.child.getFocus().id; 2304 2305 self.grid = grid; 2306 } 2307 2308 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2309 try self.child.input(allocator, key, root_focus); 2310 } 2311 2312 pub fn clearGrid(self: *Center) void { 2313 if (self.grid) |*grid| { 2314 grid.deinit(); 2315 self.grid = null; 2316 } 2317 self.child.clearGrid(); 2318 } 2319 2320 pub fn getGrid(self: Center) ?Grid { 2321 return self.grid; 2322 } 2323 2324 pub fn getFocus(self: *Center) *Focus { 2325 return self.focus; 2326 } 2327 }; 2328 2329 // renders truecolor ANSI art 2330 pub const AnsiArt = struct { 2331 focus: *Focus, 2332 grid: ?Grid, 2333 content: []const u8, 2334 2335 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2336 return .{ 2337 .focus = try Focus.create(allocator, .container), 2338 .grid = null, 2339 .content = content, 2340 }; 2341 } 2342 2343 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2344 self.focus.destroy(allocator); 2345 if (self.grid) |*grid| { 2346 grid.deinit(); 2347 self.grid = null; 2348 } 2349 } 2350 2351 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2352 _ = root_focus; 2353 self.clearGrid(); 2354 2355 // parse into rows of cells, then copy into a rectangular grid 2356 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2357 defer { 2358 for (rows.items) |*row| row.deinit(allocator); 2359 rows.deinit(allocator); 2360 } 2361 var row: std.ArrayList(Grid.Cell) = .empty; 2362 errdefer row.deinit(allocator); 2363 2364 var style: Grid.Style = .{}; 2365 var width: usize = 0; 2366 const content = self.content; 2367 var i: usize = 0; 2368 while (i < content.len) { 2369 const byte = content[i]; 2370 if (byte == '\n') { 2371 width = @max(width, row.items.len); 2372 try rows.append(allocator, row); 2373 row = .empty; 2374 i += 1; 2375 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2376 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2377 var j = i + 2; 2378 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2379 if (j >= content.len) { 2380 i = content.len; // malformed trailing escape; stop 2381 } else { 2382 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2383 i = j + 1; 2384 } 2385 } else { 2386 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2387 const end = @min(content.len, i + len); 2388 const rune = content[i..end]; 2389 const transparent = end - i == 1 and rune[0] == ' ' and 2390 style.fg == null and style.bg == null and !style.inverted; 2391 try row.append(allocator, .{ 2392 .rune = if (transparent) null else rune, 2393 .style = style, 2394 }); 2395 i = end; 2396 } 2397 } 2398 // a trailing row with content but no closing newline 2399 if (row.items.len > 0) { 2400 width = @max(width, row.items.len); 2401 try rows.append(allocator, row); 2402 } else { 2403 row.deinit(allocator); 2404 } 2405 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2406 2407 const height = rows.items.len; 2408 if (width == 0 or height == 0) return; 2409 2410 const clamped_w = @min(width, constraint.max_size.width orelse width); 2411 const clamped_h = @min(height, constraint.max_size.height orelse height); 2412 2413 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2414 errdefer grid.deinit(); 2415 for (rows.items[0..clamped_h], 0..) |r, y| { 2416 const n = @min(r.items.len, clamped_w); 2417 for (r.items[0..n], 0..) |cell, x| { 2418 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2419 } 2420 } 2421 self.grid = grid; 2422 } 2423 2424 pub fn clearGrid(self: *AnsiArt) void { 2425 if (self.grid) |*grid| { 2426 grid.deinit(); 2427 self.grid = null; 2428 } 2429 } 2430 2431 pub fn getGrid(self: AnsiArt) ?Grid { 2432 return self.grid; 2433 } 2434 2435 pub fn getFocus(self: *AnsiArt) *Focus { 2436 return self.focus; 2437 } 2438 2439 fn applySgr(style: *Grid.Style, params: []const u8) void { 2440 var nums: [16]u32 = undefined; 2441 var n: usize = 0; 2442 var it = std.mem.splitScalar(u8, params, ';'); 2443 while (it.next()) |tok| { 2444 if (n >= nums.len) break; 2445 // an empty parameter (e.g. bare "\x1b[m") means 0 2446 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2447 n += 1; 2448 } 2449 if (n == 0) { 2450 style.* = .{}; 2451 return; 2452 } 2453 var i: usize = 0; 2454 while (i < n) : (i += 1) { 2455 switch (nums[i]) { 2456 0 => style.* = .{}, 2457 7 => style.inverted = true, 2458 27 => style.inverted = false, 2459 39 => style.fg = null, 2460 49 => style.bg = null, 2461 38, 48 => { 2462 // truecolor form: 38;2;r;g;b — anything else is ignored 2463 if (i + 4 < n and nums[i + 1] == 2) { 2464 const c = Grid.Color{ 2465 .r = @truncate(nums[i + 2]), 2466 .g = @truncate(nums[i + 3]), 2467 .b = @truncate(nums[i + 4]), 2468 }; 2469 if (nums[i] == 38) style.fg = c else style.bg = c; 2470 i += 4; 2471 } 2472 }, 2473 else => {}, 2474 } 2475 } 2476 } 2477 }; 2478 2479 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2480 pub const AnsiBackground = struct { 2481 grid: ?Grid, 2482 child: *Widget, 2483 art: AnsiArt, 2484 session: *Session, 2485 2486 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2487 var cw = child_widget; 2488 const child = allocator.create(Widget) catch |e| { 2489 cw.deinit(allocator); 2490 return e; 2491 }; 2492 child.* = cw; 2493 errdefer { 2494 child.deinit(allocator); 2495 allocator.destroy(child); 2496 } 2497 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2498 } 2499 2500 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2501 if (self.grid) |*grid| grid.deinit(); 2502 self.art.deinit(allocator); 2503 self.child.deinit(allocator); 2504 allocator.destroy(self.child); 2505 } 2506 2507 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2508 self.clearGrid(); 2509 2510 // make the wrapped page fill the whole available area 2511 try self.child.build(allocator, .{ 2512 .min_size = .{ 2513 .width = constraint.max_size.width orelse constraint.min_size.width, 2514 .height = constraint.max_size.height orelse constraint.min_size.height, 2515 }, 2516 .max_size = constraint.max_size, 2517 }, root_focus); 2518 2519 if (self.session.data.enable_ansi) { 2520 if (self.child.getGrid()) |fg| { 2521 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2522 } 2523 } 2524 } 2525 2526 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2527 try self.child.input(allocator, key, root_focus); 2528 } 2529 2530 pub fn clearGrid(self: *AnsiBackground) void { 2531 if (self.grid) |*grid| { 2532 grid.deinit(); 2533 self.grid = null; 2534 } 2535 self.child.clearGrid(); 2536 self.art.clearGrid(); 2537 } 2538 2539 pub fn getGrid(self: AnsiBackground) ?Grid { 2540 return self.grid orelse self.child.getGrid(); 2541 } 2542 2543 pub fn getFocus(self: *AnsiBackground) *Focus { 2544 return self.child.getFocus(); 2545 } 2546 2547 // which top corner the art hugs. 2548 const ArtAnchor = enum { top_left, top_right }; 2549 2550 // a foreground cell counts as blank if it's empty or a bare space without styling 2551 fn cellIsBlank(cell: Grid.Cell) bool { 2552 if (cell.rune) |rune| { 2553 return std.mem.eql(u8, rune, " ") and 2554 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2555 } 2556 return true; 2557 } 2558 2559 // the art is dimmed to this fraction of its brightness so foreground text 2560 // stays legible over it 2561 const art_brightness = 45; // percent 2562 2563 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2564 const c = color orelse return null; 2565 return .{ 2566 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2567 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2568 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2569 }; 2570 } 2571 2572 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2573 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2574 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2575 // snap such channels clear of that range so the backdrop doesn't blink or 2576 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2577 // actually render truecolor. 2578 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2579 const c = color orelse return null; 2580 const snap = struct { 2581 fn f(v: u8) u8 { 2582 return if (v >= 1 and v <= 9) 10 else v; 2583 } 2584 }.f; 2585 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2586 } 2587 2588 // composites ANSI art behind a foreground grid 2589 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2590 art.clearGrid(); 2591 try art.build(allocator, .{ 2592 .min_size = .{ .width = null, .height = null }, 2593 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2594 }, root_focus); 2595 2596 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2597 errdefer out.deinit(); 2598 2599 if (art.getGrid()) |art_grid| { 2600 const anchor_x = switch (anchor) { 2601 .top_left => 0, 2602 .top_right => foreground.size.width -| art_grid.size.width, 2603 }; 2604 for (0..art_grid.size.height) |y| { 2605 for (0..art_grid.size.width) |x| { 2606 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2607 if (src.rune == null) continue; 2608 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2609 const dst = &out.cells.items[idx]; 2610 // composite the dimmed art behind every cell so it shows 2611 // through the whole UI: a blank cell becomes the art, while a 2612 // cell with a glyph keeps the glyph but takes the dimmed art as 2613 // its background (replacing whatever background it had, which 2614 // would otherwise obscure the art). 2615 if (cellIsBlank(dst.*)) { 2616 dst.* = src; 2617 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2618 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2619 } else { 2620 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2621 dst.style.bg = background_maybe; 2622 // change the color of text overlaying ANSI art to ensure 2623 // it is readable. don't do this in the web UI, since we 2624 // have direct control of the theme there (also, inner 2625 // scroll panes allow text to move without a re-render 2626 // by the TUI, which would break this). also don't do this 2627 // if the text is explicitly styled. 2628 if (is_terminal and dst.style.fg == null) { 2629 if (background_maybe) |background| { 2630 // use near-black and near-white, which is easier on the eyes 2631 // while retaining high contrast 2632 const luminance = (@as(u32, background.r) * 299 + 2633 @as(u32, background.g) * 587 + 2634 @as(u32, background.b) * 114) / 1000; 2635 dst.style.fg = if (luminance >= 128) 2636 .{ .r = 16, .g = 16, .b = 16 } 2637 else 2638 .{ .r = 240, .g = 240, .b = 240 }; 2639 } 2640 } 2641 } 2642 } 2643 } 2644 } 2645 return out; 2646 } 2647 }; 2648