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