╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2577 _ = root_focus; 2578 self.clearGrid(); 2579 2580 // parse into rows of cells, then copy into a rectangular grid 2581 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2582 defer { 2583 for (rows.items) |*row| row.deinit(allocator); 2584 rows.deinit(allocator); 2585 } 2586 var row: std.ArrayList(Grid.Cell) = .empty; 2587 errdefer row.deinit(allocator); 2588 2589 var style: Grid.Style = .{}; 2590 var width: usize = 0; 2591 const content = self.content; 2592 var i: usize = 0; 2593 while (i < content.len) { 2594 const byte = content[i]; 2595 if (byte == '\n') { 2596 width = @max(width, row.items.len); 2597 try rows.append(allocator, row); 2598 row = .empty; 2599 i += 1; 2600 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2601 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2602 var j = i + 2; 2603 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2604 if (j >= content.len) { 2605 i = content.len; // malformed trailing escape; stop 2606 } else { 2607 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2608 i = j + 1; 2609 } 2610 } else { 2611 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2612 const end = @min(content.len, i + len); 2613 const rune = content[i..end]; 2614 const transparent = end - i == 1 and rune[0] == ' ' and 2615 style.fg == null and style.bg == null and !style.inverted; 2616 try row.append(allocator, .{ 2617 .rune = if (transparent) null else rune, 2618 .style = style, 2619 }); 2620 i = end; 2621 } 2622 } 2623 // a trailing row with content but no closing newline 2624 if (row.items.len > 0) { 2625 width = @max(width, row.items.len); 2626 try rows.append(allocator, row); 2627 } else { 2628 row.deinit(allocator); 2629 } 2630 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2631 2632 const height = rows.items.len; 2633 if (width == 0 or height == 0) return; 2634 2635 const clamped_w = @min(width, constraint.max_size.width orelse width); 2636 const clamped_h = @min(height, constraint.max_size.height orelse height); 2637 2638 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2639 errdefer grid.deinit(); 2640 for (rows.items[0..clamped_h], 0..) |r, y| { 2641 const n = @min(r.items.len, clamped_w); 2642 for (r.items[0..n], 0..) |cell, x| { 2643 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2644 } 2645 } 2646 self.grid = grid; 2647 } 2648 2649 pub fn clearGrid(self: *AnsiArt) void { 2650 if (self.grid) |*grid| { 2651 grid.deinit(); 2652 self.grid = null; 2653 } 2654 } 2655 2656 pub fn getGrid(self: AnsiArt) ?Grid { 2657 return self.grid; 2658 } 2659 2660 pub fn getFocus(self: *AnsiArt) *Focus { 2661 return self.focus; 2662 } 2663 2664 fn applySgr(style: *Grid.Style, params: []const u8) void { 2665 var nums: [16]u32 = undefined; 2666 var n: usize = 0; 2667 var it = std.mem.splitScalar(u8, params, ';'); 2668 while (it.next()) |tok| { 2669 if (n >= nums.len) break; 2670 // an empty parameter (e.g. bare "\x1b[m") means 0 2671 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2672 n += 1; 2673 } 2674 if (n == 0) { 2675 style.* = .{}; 2676 return; 2677 } 2678 var i: usize = 0; 2679 while (i < n) : (i += 1) { 2680 switch (nums[i]) { 2681 0 => style.* = .{}, 2682 7 => style.inverted = true, 2683 27 => style.inverted = false, 2684 39 => style.fg = null, 2685 49 => style.bg = null, 2686 38, 48 => { 2687 // truecolor form: 38;2;r;g;b — anything else is ignored 2688 if (i + 4 < n and nums[i + 1] == 2) { 2689 const c = Grid.Color{ 2690 .r = @truncate(nums[i + 2]), 2691 .g = @truncate(nums[i + 3]), 2692 .b = @truncate(nums[i + 4]), 2693 }; 2694 if (nums[i] == 38) style.fg = c else style.bg = c; 2695 i += 4; 2696 } 2697 }, 2698 else => {}, 2699 } 2700 } 2701 } 2702 }; 2703 2704 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2705 pub const AnsiBackground = struct { 2706 grid: ?Grid, 2707 child: *Widget, 2708 art: AnsiArt, 2709 session: *Session, 2710 2711 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2712 var cw = child_widget; 2713 const child = allocator.create(Widget) catch |e| { 2714 cw.deinit(allocator); 2715 return e; 2716 }; 2717 child.* = cw; 2718 errdefer { 2719 child.deinit(allocator); 2720 allocator.destroy(child); 2721 } 2722 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2723 } 2724 2725 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2726 if (self.grid) |*grid| grid.deinit(); 2727 self.art.deinit(allocator); 2728 self.child.deinit(allocator); 2729 allocator.destroy(self.child); 2730 } 2731 2732 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2733 self.clearGrid(); 2734 2735 // make the wrapped page fill the whole available area 2736 try self.child.build(allocator, .{ 2737 .min_size = .{ 2738 .width = constraint.max_size.width orelse constraint.min_size.width, 2739 .height = constraint.max_size.height orelse constraint.min_size.height, 2740 }, 2741 .max_size = constraint.max_size, 2742 }, root_focus); 2743 2744 if (self.session.data.enable_ansi) { 2745 if (self.child.getGrid()) |fg| { 2746 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2747 } 2748 } 2749 } 2750 2751 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2752 try self.child.input(allocator, key, root_focus); 2753 } 2754 2755 pub fn clearGrid(self: *AnsiBackground) void { 2756 if (self.grid) |*grid| { 2757 grid.deinit(); 2758 self.grid = null; 2759 } 2760 self.child.clearGrid(); 2761 self.art.clearGrid(); 2762 } 2763 2764 pub fn getGrid(self: AnsiBackground) ?Grid { 2765 return self.grid orelse self.child.getGrid(); 2766 } 2767 2768 pub fn getFocus(self: *AnsiBackground) *Focus { 2769 return self.child.getFocus(); 2770 } 2771 2772 // which top corner the art hugs. 2773 const ArtAnchor = enum { top_left, top_right }; 2774 2775 // a foreground cell counts as blank if it's empty or a bare space without styling 2776 fn cellIsBlank(cell: Grid.Cell) bool { 2777 if (cell.rune) |rune| { 2778 return std.mem.eql(u8, rune, " ") and 2779 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2780 } 2781 return true; 2782 } 2783 2784 // the art is dimmed to this fraction of its brightness so foreground text 2785 // stays legible over it 2786 const art_brightness = 45; // percent 2787 2788 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2789 const c = color orelse return null; 2790 return .{ 2791 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2792 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2793 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2794 }; 2795 } 2796 2797 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2798 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2799 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2800 // snap such channels clear of that range so the backdrop doesn't blink or 2801 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2802 // actually render truecolor. 2803 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2804 const c = color orelse return null; 2805 const snap = struct { 2806 fn f(v: u8) u8 { 2807 return if (v >= 1 and v <= 9) 10 else v; 2808 } 2809 }.f; 2810 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2811 } 2812 2813 // composites ANSI art behind a foreground grid 2814 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2815 art.clearGrid(); 2816 try art.build(allocator, .{ 2817 .min_size = .{ .width = null, .height = null }, 2818 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2819 }, root_focus); 2820 2821 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2822 errdefer out.deinit(); 2823 2824 if (art.getGrid()) |art_grid| { 2825 const anchor_x = switch (anchor) { 2826 .top_left => 0, 2827 .top_right => foreground.size.width -| art_grid.size.width, 2828 }; 2829 for (0..art_grid.size.height) |y| { 2830 for (0..art_grid.size.width) |x| { 2831 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2832 if (src.rune == null) continue; 2833 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2834 const dst = &out.cells.items[idx]; 2835 // composite the dimmed art behind every cell so it shows 2836 // through the whole UI: a blank cell becomes the art, while a 2837 // cell with a glyph keeps the glyph but takes the dimmed art as 2838 // its background (replacing whatever background it had, which 2839 // would otherwise obscure the art). 2840 if (cellIsBlank(dst.*)) { 2841 dst.* = src; 2842 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2843 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2844 } else { 2845 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2846 dst.style.bg = background_maybe; 2847 // change the color of text overlaying ANSI art to ensure 2848 // it is readable. don't do this in the web UI, since we 2849 // have direct control of the theme there (also, inner 2850 // scroll panes allow text to move without a re-render 2851 // by the TUI, which would break this). also don't do this 2852 // if the text is explicitly styled. 2853 if (is_terminal and dst.style.fg == null) { 2854 if (background_maybe) |background| { 2855 // use near-black and near-white, which is easier on the eyes 2856 // while retaining high contrast 2857 const luminance = (@as(u32, background.r) * 299 + 2858 @as(u32, background.g) * 587 + 2859 @as(u32, background.b) * 114) / 1000; 2860 dst.style.fg = if (luminance >= 128) 2861 .{ .r = 16, .g = 16, .b = 16 } 2862 else 2863 .{ .r = 240, .g = 240, .b = 240 }; 2864 } 2865 } 2866 } 2867 } 2868 } 2869 } 2870 return out; 2871 } 2872 }; 2873