╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2187 2188 // the button's focus id, for click hit-testing 2189 pub fn buttonId(self: *const SubmitButton) usize { 2190 return self.box.children.keys()[1]; 2191 } 2192 }; 2193 2194 pub const Center = struct { 2195 focus: *Focus, 2196 grid: ?Grid, 2197 child: *Widget, 2198 2199 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2200 const child = try allocator.create(Widget); 2201 errdefer allocator.destroy(child); 2202 child.* = child_widget; 2203 return .{ 2204 .focus = try Focus.create(allocator, .container), 2205 .grid = null, 2206 .child = child, 2207 }; 2208 } 2209 2210 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2211 self.focus.destroy(allocator); 2212 if (self.grid) |*grid| { 2213 grid.deinit(); 2214 self.grid = null; 2215 } 2216 self.child.deinit(allocator); 2217 allocator.destroy(self.child); 2218 } 2219 2220 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2221 self.clearGrid(); 2222 self.getFocus().clear(); 2223 2224 // build the child without forcing it to fill min_size; it sizes 2225 // itself to its content within the available max. 2226 try self.child.build(allocator, .{ 2227 .min_size = .{ .width = null, .height = null }, 2228 .max_size = constraint.max_size, 2229 }, root_focus); 2230 2231 const child_grid = self.child.getGrid() orelse return; 2232 2233 // prefer max when given; otherwise grow to max(min, child) so that 2234 // when only a min is set (e.g. the wasm path passes viewport rows 2235 // as min_height with no max), we can still vertically center while 2236 // letting taller content extend past the min. 2237 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; 2238 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; 2239 if (width == 0 or height == 0) return; 2240 2241 const offset_x: usize = (width -| child_grid.size.width) / 2; 2242 const offset_y: usize = (height -| child_grid.size.height) / 2; 2243 2244 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2245 errdefer grid.deinit(); 2246 try grid.drawGrid(child_grid, offset_x, offset_y); 2247 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2248 self.getFocus().child_id = self.child.getFocus().id; 2249 2250 self.grid = grid; 2251 } 2252 2253 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2254 try self.child.input(allocator, key, root_focus); 2255 } 2256 2257 pub fn clearGrid(self: *Center) void { 2258 if (self.grid) |*grid| { 2259 grid.deinit(); 2260 self.grid = null; 2261 } 2262 self.child.clearGrid(); 2263 } 2264 2265 pub fn getGrid(self: Center) ?Grid { 2266 return self.grid; 2267 } 2268 2269 pub fn getFocus(self: *Center) *Focus { 2270 return self.focus; 2271 } 2272 }; 2273 2274 // renders truecolor ANSI art 2275 pub const AnsiArt = struct { 2276 focus: *Focus, 2277 grid: ?Grid, 2278 content: []const u8, 2279 2280 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2281 return .{ 2282 .focus = try Focus.create(allocator, .container), 2283 .grid = null, 2284 .content = content, 2285 }; 2286 } 2287 2288 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2289 self.focus.destroy(allocator); 2290 if (self.grid) |*grid| { 2291 grid.deinit(); 2292 self.grid = null; 2293 } 2294 } 2295 2296 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2297 _ = root_focus; 2298 self.clearGrid(); 2299 2300 // parse into rows of cells, then copy into a rectangular grid 2301 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2302 defer { 2303 for (rows.items) |*row| row.deinit(allocator); 2304 rows.deinit(allocator); 2305 } 2306 var row: std.ArrayList(Grid.Cell) = .empty; 2307 errdefer row.deinit(allocator); 2308 2309 var style: Grid.Style = .{}; 2310 var width: usize = 0; 2311 const content = self.content; 2312 var i: usize = 0; 2313 while (i < content.len) { 2314 const byte = content[i]; 2315 if (byte == '\n') { 2316 width = @max(width, row.items.len); 2317 try rows.append(allocator, row); 2318 row = .empty; 2319 i += 1; 2320 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2321 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2322 var j = i + 2; 2323 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2324 if (j >= content.len) { 2325 i = content.len; // malformed trailing escape; stop 2326 } else { 2327 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2328 i = j + 1; 2329 } 2330 } else { 2331 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2332 const end = @min(content.len, i + len); 2333 const rune = content[i..end]; 2334 const transparent = end - i == 1 and rune[0] == ' ' and 2335 style.fg == null and style.bg == null and !style.inverted; 2336 try row.append(allocator, .{ 2337 .rune = if (transparent) null else rune, 2338 .style = style, 2339 }); 2340 i = end; 2341 } 2342 } 2343 // a trailing row with content but no closing newline 2344 if (row.items.len > 0) { 2345 width = @max(width, row.items.len); 2346 try rows.append(allocator, row); 2347 } else { 2348 row.deinit(allocator); 2349 } 2350 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2351 2352 const height = rows.items.len; 2353 if (width == 0 or height == 0) return; 2354 2355 const clamped_w = @min(width, constraint.max_size.width orelse width); 2356 const clamped_h = @min(height, constraint.max_size.height orelse height); 2357 2358 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2359 errdefer grid.deinit(); 2360 for (rows.items[0..clamped_h], 0..) |r, y| { 2361 const n = @min(r.items.len, clamped_w); 2362 for (r.items[0..n], 0..) |cell, x| { 2363 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2364 } 2365 } 2366 self.grid = grid; 2367 } 2368 2369 pub fn clearGrid(self: *AnsiArt) void { 2370 if (self.grid) |*grid| { 2371 grid.deinit(); 2372 self.grid = null; 2373 } 2374 } 2375 2376 pub fn getGrid(self: AnsiArt) ?Grid { 2377 return self.grid; 2378 } 2379 2380 pub fn getFocus(self: *AnsiArt) *Focus { 2381 return self.focus; 2382 } 2383 2384 fn applySgr(style: *Grid.Style, params: []const u8) void { 2385 var nums: [16]u32 = undefined; 2386 var n: usize = 0; 2387 var it = std.mem.splitScalar(u8, params, ';'); 2388 while (it.next()) |tok| { 2389 if (n >= nums.len) break; 2390 // an empty parameter (e.g. bare "\x1b[m") means 0 2391 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2392 n += 1; 2393 } 2394 if (n == 0) { 2395 style.* = .{}; 2396 return; 2397 } 2398 var i: usize = 0; 2399 while (i < n) : (i += 1) { 2400 switch (nums[i]) { 2401 0 => style.* = .{}, 2402 7 => style.inverted = true, 2403 27 => style.inverted = false, 2404 39 => style.fg = null, 2405 49 => style.bg = null, 2406 38, 48 => { 2407 // truecolor form: 38;2;r;g;b — anything else is ignored 2408 if (i + 4 < n and nums[i + 1] == 2) { 2409 const c = Grid.Color{ 2410 .r = @truncate(nums[i + 2]), 2411 .g = @truncate(nums[i + 3]), 2412 .b = @truncate(nums[i + 4]), 2413 }; 2414 if (nums[i] == 38) style.fg = c else style.bg = c; 2415 i += 4; 2416 } 2417 }, 2418 else => {}, 2419 } 2420 } 2421 } 2422 }; 2423 2424 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2425 pub const AnsiBackground = struct { 2426 grid: ?Grid, 2427 child: *Widget, 2428 art: AnsiArt, 2429 session: *Session, 2430 2431 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2432 var cw = child_widget; 2433 const child = allocator.create(Widget) catch |e| { 2434 cw.deinit(allocator); 2435 return e; 2436 }; 2437 child.* = cw; 2438 errdefer { 2439 child.deinit(allocator); 2440 allocator.destroy(child); 2441 } 2442 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2443 } 2444 2445 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2446 if (self.grid) |*grid| grid.deinit(); 2447 self.art.deinit(allocator); 2448 self.child.deinit(allocator); 2449 allocator.destroy(self.child); 2450 } 2451 2452 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2453 self.clearGrid(); 2454 2455 // make the wrapped page fill the whole available area 2456 try self.child.build(allocator, .{ 2457 .min_size = .{ 2458 .width = constraint.max_size.width orelse constraint.min_size.width, 2459 .height = constraint.max_size.height orelse constraint.min_size.height, 2460 }, 2461 .max_size = constraint.max_size, 2462 }, root_focus); 2463 2464 if (self.session.data.enable_ansi) { 2465 if (self.child.getGrid()) |fg| { 2466 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2467 } 2468 } 2469 } 2470 2471 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2472 try self.child.input(allocator, key, root_focus); 2473 } 2474 2475 pub fn clearGrid(self: *AnsiBackground) void { 2476 if (self.grid) |*grid| { 2477 grid.deinit(); 2478 self.grid = null; 2479 } 2480 self.child.clearGrid(); 2481 self.art.clearGrid(); 2482 } 2483 2484 pub fn getGrid(self: AnsiBackground) ?Grid { 2485 return self.grid orelse self.child.getGrid(); 2486 } 2487 2488 pub fn getFocus(self: *AnsiBackground) *Focus { 2489 return self.child.getFocus(); 2490 } 2491 2492 // which top corner the art hugs. 2493 const ArtAnchor = enum { top_left, top_right }; 2494 2495 // a foreground cell counts as blank if it's empty or a bare space without styling 2496 fn cellIsBlank(cell: Grid.Cell) bool { 2497 if (cell.rune) |rune| { 2498 return std.mem.eql(u8, rune, " ") and 2499 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2500 } 2501 return true; 2502 } 2503 2504 // the art is dimmed to this fraction of its brightness so foreground text 2505 // stays legible over it 2506 const art_brightness = 45; // percent 2507 2508 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2509 const c = color orelse return null; 2510 return .{ 2511 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2512 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2513 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2514 }; 2515 } 2516 2517 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2518 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2519 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2520 // snap such channels clear of that range so the backdrop doesn't blink or 2521 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2522 // actually render truecolor. 2523 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2524 const c = color orelse return null; 2525 const snap = struct { 2526 fn f(v: u8) u8 { 2527 return if (v >= 1 and v <= 9) 10 else v; 2528 } 2529 }.f; 2530 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2531 } 2532 2533 // composites ANSI art behind a foreground grid 2534 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2535 art.clearGrid(); 2536 try art.build(allocator, .{ 2537 .min_size = .{ .width = null, .height = null }, 2538 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2539 }, root_focus); 2540 2541 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2542 errdefer out.deinit(); 2543 2544 if (art.getGrid()) |art_grid| { 2545 const anchor_x = switch (anchor) { 2546 .top_left => 0, 2547 .top_right => foreground.size.width -| art_grid.size.width, 2548 }; 2549 for (0..art_grid.size.height) |y| { 2550 for (0..art_grid.size.width) |x| { 2551 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2552 if (src.rune == null) continue; 2553 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2554 const dst = &out.cells.items[idx]; 2555 // composite the dimmed art behind every cell so it shows 2556 // through the whole UI: a blank cell becomes the art, while a 2557 // cell with a glyph keeps the glyph but takes the dimmed art as 2558 // its background (replacing whatever background it had, which 2559 // would otherwise obscure the art). 2560 if (cellIsBlank(dst.*)) { 2561 dst.* = src; 2562 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2563 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2564 } else { 2565 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2566 } 2567 } 2568 } 2569 } 2570 return out; 2571 } 2572 }; 2573