╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2467 } 2468 // a trailing row with content but no closing newline 2469 if (row.items.len > 0) { 2470 width = @max(width, row.items.len); 2471 try rows.append(allocator, row); 2472 } else { 2473 row.deinit(allocator); 2474 } 2475 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2476 2477 const height = rows.items.len; 2478 if (width == 0 or height == 0) return; 2479 2480 const clamped_w = @min(width, constraint.max_size.width orelse width); 2481 const clamped_h = @min(height, constraint.max_size.height orelse height); 2482 2483 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2484 errdefer grid.deinit(); 2485 for (rows.items[0..clamped_h], 0..) |r, y| { 2486 const n = @min(r.items.len, clamped_w); 2487 for (r.items[0..n], 0..) |cell, x| { 2488 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2489 } 2490 } 2491 self.grid = grid; 2492 } 2493 2494 pub fn clearGrid(self: *AnsiArt) void { 2495 if (self.grid) |*grid| { 2496 grid.deinit(); 2497 self.grid = null; 2498 } 2499 } 2500 2501 pub fn getGrid(self: AnsiArt) ?Grid { 2502 return self.grid; 2503 } 2504 2505 pub fn getFocus(self: *AnsiArt) *Focus { 2506 return self.focus; 2507 } 2508 2509 fn applySgr(style: *Grid.Style, params: []const u8) void { 2510 var nums: [16]u32 = undefined; 2511 var n: usize = 0; 2512 var it = std.mem.splitScalar(u8, params, ';'); 2513 while (it.next()) |tok| { 2514 if (n >= nums.len) break; 2515 // an empty parameter (e.g. bare "\x1b[m") means 0 2516 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2517 n += 1; 2518 } 2519 if (n == 0) { 2520 style.* = .{}; 2521 return; 2522 } 2523 var i: usize = 0; 2524 while (i < n) : (i += 1) { 2525 switch (nums[i]) { 2526 0 => style.* = .{}, 2527 7 => style.inverted = true, 2528 27 => style.inverted = false, 2529 39 => style.fg = null, 2530 49 => style.bg = null, 2531 38, 48 => { 2532 // truecolor form: 38;2;r;g;b — anything else is ignored 2533 if (i + 4 < n and nums[i + 1] == 2) { 2534 const c = Grid.Color{ 2535 .r = @truncate(nums[i + 2]), 2536 .g = @truncate(nums[i + 3]), 2537 .b = @truncate(nums[i + 4]), 2538 }; 2539 if (nums[i] == 38) style.fg = c else style.bg = c; 2540 i += 4; 2541 } 2542 }, 2543 else => {}, 2544 } 2545 } 2546 } 2547 }; 2548 2549 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2550 pub const AnsiBackground = struct { 2551 grid: ?Grid, 2552 child: *Widget, 2553 art: AnsiArt, 2554 session: *Session, 2555 2556 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2557 var cw = child_widget; 2558 const child = allocator.create(Widget) catch |e| { 2559 cw.deinit(allocator); 2560 return e; 2561 }; 2562 child.* = cw; 2563 errdefer { 2564 child.deinit(allocator); 2565 allocator.destroy(child); 2566 } 2567 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2568 } 2569 2570 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2571 if (self.grid) |*grid| grid.deinit(); 2572 self.art.deinit(allocator); 2573 self.child.deinit(allocator); 2574 allocator.destroy(self.child); 2575 } 2576 2577 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2578 self.clearGrid(); 2579 2580 // make the wrapped page fill the whole available area 2581 try self.child.build(allocator, .{ 2582 .min_size = .{ 2583 .width = constraint.max_size.width orelse constraint.min_size.width, 2584 .height = constraint.max_size.height orelse constraint.min_size.height, 2585 }, 2586 .max_size = constraint.max_size, 2587 }, root_focus); 2588 2589 if (self.session.data.enable_ansi) { 2590 if (self.child.getGrid()) |fg| { 2591 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2592 } 2593 } 2594 } 2595 2596 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2597 try self.child.input(allocator, key, root_focus); 2598 } 2599 2600 pub fn clearGrid(self: *AnsiBackground) void { 2601 if (self.grid) |*grid| { 2602 grid.deinit(); 2603 self.grid = null; 2604 } 2605 self.child.clearGrid(); 2606 self.art.clearGrid(); 2607 } 2608 2609 pub fn getGrid(self: AnsiBackground) ?Grid { 2610 return self.grid orelse self.child.getGrid(); 2611 } 2612 2613 pub fn getFocus(self: *AnsiBackground) *Focus { 2614 return self.child.getFocus(); 2615 } 2616 2617 // which top corner the art hugs. 2618 const ArtAnchor = enum { top_left, top_right }; 2619 2620 // a foreground cell counts as blank if it's empty or a bare space without styling 2621 fn cellIsBlank(cell: Grid.Cell) bool { 2622 if (cell.rune) |rune| { 2623 return std.mem.eql(u8, rune, " ") and 2624 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2625 } 2626 return true; 2627 } 2628 2629 // the art is dimmed to this fraction of its brightness so foreground text 2630 // stays legible over it 2631 const art_brightness = 45; // percent 2632 2633 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2634 const c = color orelse return null; 2635 return .{ 2636 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2637 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2638 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2639 }; 2640 } 2641 2642 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2643 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2644 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2645 // snap such channels clear of that range so the backdrop doesn't blink or 2646 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2647 // actually render truecolor. 2648 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2649 const c = color orelse return null; 2650 const snap = struct { 2651 fn f(v: u8) u8 { 2652 return if (v >= 1 and v <= 9) 10 else v; 2653 } 2654 }.f; 2655 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2656 } 2657 2658 // composites ANSI art behind a foreground grid 2659 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2660 art.clearGrid(); 2661 try art.build(allocator, .{ 2662 .min_size = .{ .width = null, .height = null }, 2663 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2664 }, root_focus); 2665 2666 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2667 errdefer out.deinit(); 2668 2669 if (art.getGrid()) |art_grid| { 2670 const anchor_x = switch (anchor) { 2671 .top_left => 0, 2672 .top_right => foreground.size.width -| art_grid.size.width, 2673 }; 2674 for (0..art_grid.size.height) |y| { 2675 for (0..art_grid.size.width) |x| { 2676 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2677 if (src.rune == null) continue; 2678 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2679 const dst = &out.cells.items[idx]; 2680 // composite the dimmed art behind every cell so it shows 2681 // through the whole UI: a blank cell becomes the art, while a 2682 // cell with a glyph keeps the glyph but takes the dimmed art as 2683 // its background (replacing whatever background it had, which 2684 // would otherwise obscure the art). 2685 if (cellIsBlank(dst.*)) { 2686 dst.* = src; 2687 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2688 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2689 } else { 2690 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2691 dst.style.bg = background_maybe; 2692 // change the color of text overlaying ANSI art to ensure 2693 // it is readable. don't do this in the web UI, since we 2694 // have direct control of the theme there (also, inner 2695 // scroll panes allow text to move without a re-render 2696 // by the TUI, which would break this). also don't do this 2697 // if the text is explicitly styled. 2698 if (is_terminal and dst.style.fg == null) { 2699 if (background_maybe) |background| { 2700 // use near-black and near-white, which is easier on the eyes 2701 // while retaining high contrast 2702 const luminance = (@as(u32, background.r) * 299 + 2703 @as(u32, background.g) * 587 + 2704 @as(u32, background.b) * 114) / 1000; 2705 dst.style.fg = if (luminance >= 128) 2706 .{ .r = 16, .g = 16, .b = 16 } 2707 else 2708 .{ .r = 240, .g = 240, .b = 240 }; 2709 } 2710 } 2711 } 2712 } 2713 } 2714 } 2715 return out; 2716 } 2717 }; 2718