╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2506 allocator.destroy(child); 2507 } 2508 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2509 } 2510 2511 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2512 if (self.grid) |*grid| grid.deinit(); 2513 self.art.deinit(allocator); 2514 self.child.deinit(allocator); 2515 allocator.destroy(self.child); 2516 } 2517 2518 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2519 self.clearGrid(); 2520 2521 // make the wrapped page fill the whole available area 2522 try self.child.build(allocator, .{ 2523 .min_size = .{ 2524 .width = constraint.max_size.width orelse constraint.min_size.width, 2525 .height = constraint.max_size.height orelse constraint.min_size.height, 2526 }, 2527 .max_size = constraint.max_size, 2528 }, root_focus); 2529 2530 if (self.session.data.enable_ansi) { 2531 if (self.child.getGrid()) |fg| { 2532 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2533 } 2534 } 2535 } 2536 2537 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2538 try self.child.input(allocator, key, root_focus); 2539 } 2540 2541 pub fn clearGrid(self: *AnsiBackground) void { 2542 if (self.grid) |*grid| { 2543 grid.deinit(); 2544 self.grid = null; 2545 } 2546 self.child.clearGrid(); 2547 self.art.clearGrid(); 2548 } 2549 2550 pub fn getGrid(self: AnsiBackground) ?Grid { 2551 return self.grid orelse self.child.getGrid(); 2552 } 2553 2554 pub fn getFocus(self: *AnsiBackground) *Focus { 2555 return self.child.getFocus(); 2556 } 2557 2558 // which top corner the art hugs. 2559 const ArtAnchor = enum { top_left, top_right }; 2560 2561 // a foreground cell counts as blank if it's empty or a bare space without styling 2562 fn cellIsBlank(cell: Grid.Cell) bool { 2563 if (cell.rune) |rune| { 2564 return std.mem.eql(u8, rune, " ") and 2565 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2566 } 2567 return true; 2568 } 2569 2570 // the art is dimmed to this fraction of its brightness so foreground text 2571 // stays legible over it 2572 const art_brightness = 45; // percent 2573 2574 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2575 const c = color orelse return null; 2576 return .{ 2577 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2578 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2579 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2580 }; 2581 } 2582 2583 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2584 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2585 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2586 // snap such channels clear of that range so the backdrop doesn't blink or 2587 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2588 // actually render truecolor. 2589 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2590 const c = color orelse return null; 2591 const snap = struct { 2592 fn f(v: u8) u8 { 2593 return if (v >= 1 and v <= 9) 10 else v; 2594 } 2595 }.f; 2596 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2597 } 2598 2599 // composites ANSI art behind a foreground grid 2600 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2601 art.clearGrid(); 2602 try art.build(allocator, .{ 2603 .min_size = .{ .width = null, .height = null }, 2604 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2605 }, root_focus); 2606 2607 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2608 errdefer out.deinit(); 2609 2610 if (art.getGrid()) |art_grid| { 2611 const anchor_x = switch (anchor) { 2612 .top_left => 0, 2613 .top_right => foreground.size.width -| art_grid.size.width, 2614 }; 2615 for (0..art_grid.size.height) |y| { 2616 for (0..art_grid.size.width) |x| { 2617 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2618 if (src.rune == null) continue; 2619 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2620 const dst = &out.cells.items[idx]; 2621 // composite the dimmed art behind every cell so it shows 2622 // through the whole UI: a blank cell becomes the art, while a 2623 // cell with a glyph keeps the glyph but takes the dimmed art as 2624 // its background (replacing whatever background it had, which 2625 // would otherwise obscure the art). 2626 if (cellIsBlank(dst.*)) { 2627 dst.* = src; 2628 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2629 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2630 } else { 2631 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2632 dst.style.bg = background_maybe; 2633 // change the color of text overlaying ANSI art to ensure 2634 // it is readable. don't do this in the web UI, since we 2635 // have direct control of the theme there (also, inner 2636 // scroll panes allow text to move without a re-render 2637 // by the TUI, which would break this). also don't do this 2638 // if the text is explicitly styled. 2639 if (is_terminal and dst.style.fg == null) { 2640 if (background_maybe) |background| { 2641 // use near-black and near-white, which is easier on the eyes 2642 // while retaining high contrast 2643 const luminance = (@as(u32, background.r) * 299 + 2644 @as(u32, background.g) * 587 + 2645 @as(u32, background.b) * 114) / 1000; 2646 dst.style.fg = if (luminance >= 128) 2647 .{ .r = 16, .g = 16, .b = 16 } 2648 else 2649 .{ .r = 240, .g = 240, .b = 240 }; 2650 } 2651 } 2652 } 2653 } 2654 } 2655 } 2656 return out; 2657 } 2658 }; 2659