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