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