╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2992 2993 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2994 errdefer grid.deinit(); 2995 for (rows.items[0..clamped_h], 0..) |r, y| { 2996 const n = @min(r.items.len, clamped_w); 2997 for (r.items[0..n], 0..) |cell, x| { 2998 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2999 } 3000 } 3001 self.grid = grid; 3002 } 3003 3004 pub fn clearGrid(self: *AnsiArt) void { 3005 if (self.grid) |*grid| { 3006 grid.deinit(); 3007 self.grid = null; 3008 } 3009 } 3010 3011 pub fn getGrid(self: AnsiArt) ?Grid { 3012 return self.grid; 3013 } 3014 3015 pub fn getFocus(self: *AnsiArt) *Focus { 3016 return self.focus; 3017 } 3018 3019 fn applySgr(style: *Grid.Style, params: []const u8) void { 3020 var nums: [16]u32 = undefined; 3021 var n: usize = 0; 3022 var it = std.mem.splitScalar(u8, params, ';'); 3023 while (it.next()) |tok| { 3024 if (n >= nums.len) break; 3025 // an empty parameter (e.g. bare "\x1b[m") means 0 3026 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 3027 n += 1; 3028 } 3029 if (n == 0) { 3030 style.* = .{}; 3031 return; 3032 } 3033 var i: usize = 0; 3034 while (i < n) : (i += 1) { 3035 switch (nums[i]) { 3036 0 => style.* = .{}, 3037 7 => style.inverted = true, 3038 27 => style.inverted = false, 3039 39 => style.fg = null, 3040 49 => style.bg = null, 3041 38, 48 => { 3042 // truecolor form: 38;2;r;g;b — anything else is ignored 3043 if (i + 4 < n and nums[i + 1] == 2) { 3044 const c = Grid.Color{ 3045 .r = @truncate(nums[i + 2]), 3046 .g = @truncate(nums[i + 3]), 3047 .b = @truncate(nums[i + 4]), 3048 }; 3049 if (nums[i] == 38) style.fg = c else style.bg = c; 3050 i += 4; 3051 } 3052 }, 3053 else => {}, 3054 } 3055 } 3056 } 3057 }; 3058 3059 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 3060 pub const AnsiBackground = struct { 3061 grid: ?Grid, 3062 child: *Widget, 3063 art: AnsiArt, 3064 session: *Session, 3065 3066 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *Session) !AnsiBackground { 3067 var cw = child_widget; 3068 const child = allocator.create(Widget) catch |e| { 3069 cw.deinit(allocator); 3070 return e; 3071 }; 3072 child.* = cw; 3073 errdefer { 3074 child.deinit(allocator); 3075 allocator.destroy(child); 3076 } 3077 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 3078 } 3079 3080 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 3081 if (self.grid) |*grid| grid.deinit(); 3082 self.art.deinit(allocator); 3083 self.child.deinit(allocator); 3084 allocator.destroy(self.child); 3085 } 3086 3087 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 3088 self.clearGrid(); 3089 3090 // make the wrapped page fill the whole available area 3091 try self.child.build(allocator, .{ 3092 .min_size = .{ 3093 .width = constraint.max_size.width orelse constraint.min_size.width, 3094 .height = constraint.max_size.height orelse constraint.min_size.height, 3095 }, 3096 .max_size = constraint.max_size, 3097 }, root_focus); 3098 3099 if (self.session.data.enable_ansi) { 3100 self.art.content = self.session.data.ansi_art; 3101 if (self.child.getGrid()) |fg| { 3102 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 3103 } 3104 } 3105 } 3106 3107 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 3108 try self.child.input(allocator, key, root_focus); 3109 } 3110 3111 pub fn clearGrid(self: *AnsiBackground) void { 3112 if (self.grid) |*grid| { 3113 grid.deinit(); 3114 self.grid = null; 3115 } 3116 self.child.clearGrid(); 3117 self.art.clearGrid(); 3118 } 3119 3120 pub fn getGrid(self: AnsiBackground) ?Grid { 3121 return self.grid orelse self.child.getGrid(); 3122 } 3123 3124 pub fn getFocus(self: *AnsiBackground) *Focus { 3125 return self.child.getFocus(); 3126 } 3127 3128 // which top corner the art hugs. 3129 const ArtAnchor = enum { top_left, top_right }; 3130 3131 // a foreground cell counts as blank if it's empty or a bare space without styling 3132 fn cellIsBlank(cell: Grid.Cell) bool { 3133 if (cell.rune) |rune| { 3134 return std.mem.eql(u8, rune, " ") and 3135 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 3136 } 3137 return true; 3138 } 3139 3140 // the art is dimmed to this fraction of its brightness so foreground text 3141 // stays legible over it 3142 const art_brightness = 35; // percent 3143 3144 fn dimColor(color: ?Grid.Color) ?Grid.Color { 3145 const c = color orelse return null; 3146 return .{ 3147 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 3148 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 3149 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 3150 }; 3151 } 3152 3153 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 3154 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 3155 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 3156 // snap such channels clear of that range so the backdrop doesn't blink or 3157 // hide text there; the ≤9/255 shift is imperceptible on terminals that 3158 // actually render truecolor. 3159 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 3160 const c = color orelse return null; 3161 const snap = struct { 3162 fn f(v: u8) u8 { 3163 return if (v >= 1 and v <= 9) 10 else v; 3164 } 3165 }.f; 3166 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 3167 } 3168 3169 // composites ANSI art behind a foreground grid 3170 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 3171 art.clearGrid(); 3172 try art.build(allocator, .{ 3173 .min_size = .{ .width = null, .height = null }, 3174 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 3175 }, root_focus); 3176 3177 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 3178 errdefer out.deinit(); 3179 3180 if (art.getGrid()) |art_grid| { 3181 const anchor_x = switch (anchor) { 3182 .top_left => 0, 3183 .top_right => foreground.size.width -| art_grid.size.width, 3184 }; 3185 for (0..art_grid.size.height) |y| { 3186 for (0..art_grid.size.width) |x| { 3187 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 3188 if (src.rune == null) continue; 3189 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 3190 const dst = &out.cells.items[idx]; 3191 // composite the dimmed art behind every cell so it shows 3192 // through the whole UI: a blank cell becomes the art, while a 3193 // cell with a glyph keeps the glyph but takes the dimmed art as 3194 // its background (replacing whatever background it had, which 3195 // would otherwise obscure the art). 3196 if (cellIsBlank(dst.*)) { 3197 dst.* = src; 3198 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 3199 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 3200 } else { 3201 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 3202 dst.style.bg = background_maybe; 3203 // change the color of text overlaying ANSI art to ensure 3204 // it is readable. don't do this in the web UI, since we 3205 // have direct control of the theme there (also, inner 3206 // scroll panes allow text to move without a re-render 3207 // by the TUI, which would break this). also don't do this 3208 // if the text is explicitly styled. 3209 if (is_terminal and dst.style.fg == null) { 3210 if (background_maybe) |background| { 3211 // use near-black and near-white, which is easier on the eyes 3212 // while retaining high contrast 3213 const luminance = (@as(u32, background.r) * 299 + 3214 @as(u32, background.g) * 587 + 3215 @as(u32, background.b) * 114) / 1000; 3216 dst.style.fg = if (luminance >= 128) 3217 .{ .r = 16, .g = 16, .b = 16 } 3218 else 3219 .{ .r = 240, .g = 240, .b = 240 }; 3220 } 3221 } 3222 } 3223 } 3224 } 3225 } 3226 return out; 3227 } 3228 }; 3229