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