╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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