╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2739 const end = @min(content.len, i + len); 2740 const rune = content[i..end]; 2741 const transparent = end - i == 1 and rune[0] == ' ' and 2742 style.fg == null and style.bg == null and !style.inverted; 2743 try row.append(allocator, .{ 2744 .rune = if (transparent) null else rune, 2745 .style = style, 2746 }); 2747 i = end; 2748 } 2749 } 2750 // a trailing row with content but no closing newline 2751 if (row.items.len > 0) { 2752 width = @max(width, row.items.len); 2753 try rows.append(allocator, row); 2754 } else { 2755 row.deinit(allocator); 2756 } 2757 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2758 2759 const height = rows.items.len; 2760 if (width == 0 or height == 0) return; 2761 2762 const clamped_w = @min(width, constraint.max_size.width orelse width); 2763 const clamped_h = @min(height, constraint.max_size.height orelse height); 2764 2765 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2766 errdefer grid.deinit(); 2767 for (rows.items[0..clamped_h], 0..) |r, y| { 2768 const n = @min(r.items.len, clamped_w); 2769 for (r.items[0..n], 0..) |cell, x| { 2770 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2771 } 2772 } 2773 self.grid = grid; 2774 } 2775 2776 pub fn clearGrid(self: *AnsiArt) void { 2777 if (self.grid) |*grid| { 2778 grid.deinit(); 2779 self.grid = null; 2780 } 2781 } 2782 2783 pub fn getGrid(self: AnsiArt) ?Grid { 2784 return self.grid; 2785 } 2786 2787 pub fn getFocus(self: *AnsiArt) *Focus { 2788 return self.focus; 2789 } 2790 2791 fn applySgr(style: *Grid.Style, params: []const u8) void { 2792 var nums: [16]u32 = undefined; 2793 var n: usize = 0; 2794 var it = std.mem.splitScalar(u8, params, ';'); 2795 while (it.next()) |tok| { 2796 if (n >= nums.len) break; 2797 // an empty parameter (e.g. bare "\x1b[m") means 0 2798 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2799 n += 1; 2800 } 2801 if (n == 0) { 2802 style.* = .{}; 2803 return; 2804 } 2805 var i: usize = 0; 2806 while (i < n) : (i += 1) { 2807 switch (nums[i]) { 2808 0 => style.* = .{}, 2809 7 => style.inverted = true, 2810 27 => style.inverted = false, 2811 39 => style.fg = null, 2812 49 => style.bg = null, 2813 38, 48 => { 2814 // truecolor form: 38;2;r;g;b — anything else is ignored 2815 if (i + 4 < n and nums[i + 1] == 2) { 2816 const c = Grid.Color{ 2817 .r = @truncate(nums[i + 2]), 2818 .g = @truncate(nums[i + 3]), 2819 .b = @truncate(nums[i + 4]), 2820 }; 2821 if (nums[i] == 38) style.fg = c else style.bg = c; 2822 i += 4; 2823 } 2824 }, 2825 else => {}, 2826 } 2827 } 2828 } 2829 }; 2830 2831 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2832 pub const AnsiBackground = struct { 2833 grid: ?Grid, 2834 child: *Widget, 2835 art: AnsiArt, 2836 session: *Session, 2837 2838 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2839 var cw = child_widget; 2840 const child = allocator.create(Widget) catch |e| { 2841 cw.deinit(allocator); 2842 return e; 2843 }; 2844 child.* = cw; 2845 errdefer { 2846 child.deinit(allocator); 2847 allocator.destroy(child); 2848 } 2849 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2850 } 2851 2852 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2853 if (self.grid) |*grid| grid.deinit(); 2854 self.art.deinit(allocator); 2855 self.child.deinit(allocator); 2856 allocator.destroy(self.child); 2857 } 2858 2859 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2860 self.clearGrid(); 2861 2862 // make the wrapped page fill the whole available area 2863 try self.child.build(allocator, .{ 2864 .min_size = .{ 2865 .width = constraint.max_size.width orelse constraint.min_size.width, 2866 .height = constraint.max_size.height orelse constraint.min_size.height, 2867 }, 2868 .max_size = constraint.max_size, 2869 }, root_focus); 2870 2871 if (self.session.data.enable_ansi) { 2872 if (self.child.getGrid()) |fg| { 2873 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2874 } 2875 } 2876 } 2877 2878 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2879 try self.child.input(allocator, key, root_focus); 2880 } 2881 2882 pub fn clearGrid(self: *AnsiBackground) void { 2883 if (self.grid) |*grid| { 2884 grid.deinit(); 2885 self.grid = null; 2886 } 2887 self.child.clearGrid(); 2888 self.art.clearGrid(); 2889 } 2890 2891 pub fn getGrid(self: AnsiBackground) ?Grid { 2892 return self.grid orelse self.child.getGrid(); 2893 } 2894 2895 pub fn getFocus(self: *AnsiBackground) *Focus { 2896 return self.child.getFocus(); 2897 } 2898 2899 // which top corner the art hugs. 2900 const ArtAnchor = enum { top_left, top_right }; 2901 2902 // a foreground cell counts as blank if it's empty or a bare space without styling 2903 fn cellIsBlank(cell: Grid.Cell) bool { 2904 if (cell.rune) |rune| { 2905 return std.mem.eql(u8, rune, " ") and 2906 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2907 } 2908 return true; 2909 } 2910 2911 // the art is dimmed to this fraction of its brightness so foreground text 2912 // stays legible over it 2913 const art_brightness = 45; // percent 2914 2915 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2916 const c = color orelse return null; 2917 return .{ 2918 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2919 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2920 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2921 }; 2922 } 2923 2924 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2925 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2926 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2927 // snap such channels clear of that range so the backdrop doesn't blink or 2928 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2929 // actually render truecolor. 2930 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2931 const c = color orelse return null; 2932 const snap = struct { 2933 fn f(v: u8) u8 { 2934 return if (v >= 1 and v <= 9) 10 else v; 2935 } 2936 }.f; 2937 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2938 } 2939 2940 // composites ANSI art behind a foreground grid 2941 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2942 art.clearGrid(); 2943 try art.build(allocator, .{ 2944 .min_size = .{ .width = null, .height = null }, 2945 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2946 }, root_focus); 2947 2948 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2949 errdefer out.deinit(); 2950 2951 if (art.getGrid()) |art_grid| { 2952 const anchor_x = switch (anchor) { 2953 .top_left => 0, 2954 .top_right => foreground.size.width -| art_grid.size.width, 2955 }; 2956 for (0..art_grid.size.height) |y| { 2957 for (0..art_grid.size.width) |x| { 2958 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2959 if (src.rune == null) continue; 2960 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2961 const dst = &out.cells.items[idx]; 2962 // composite the dimmed art behind every cell so it shows 2963 // through the whole UI: a blank cell becomes the art, while a 2964 // cell with a glyph keeps the glyph but takes the dimmed art as 2965 // its background (replacing whatever background it had, which 2966 // would otherwise obscure the art). 2967 if (cellIsBlank(dst.*)) { 2968 dst.* = src; 2969 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2970 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2971 } else { 2972 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2973 dst.style.bg = background_maybe; 2974 // change the color of text overlaying ANSI art to ensure 2975 // it is readable. don't do this in the web UI, since we 2976 // have direct control of the theme there (also, inner 2977 // scroll panes allow text to move without a re-render 2978 // by the TUI, which would break this). also don't do this 2979 // if the text is explicitly styled. 2980 if (is_terminal and dst.style.fg == null) { 2981 if (background_maybe) |background| { 2982 // use near-black and near-white, which is easier on the eyes 2983 // while retaining high contrast 2984 const luminance = (@as(u32, background.r) * 299 + 2985 @as(u32, background.g) * 587 + 2986 @as(u32, background.b) * 114) / 1000; 2987 dst.style.fg = if (luminance >= 128) 2988 .{ .r = 16, .g = 16, .b = 16 } 2989 else 2990 .{ .r = 240, .g = 240, .b = 240 }; 2991 } 2992 } 2993 } 2994 } 2995 } 2996 } 2997 return out; 2998 } 2999 }; 3000