╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3128 }, 3129 .max_size = constraint.max_size, 3130 }, root_focus); 3131 3132 if (!self.session.data.enable_ansi) return; 3133 const foreground = self.child.getGrid() orelse return; 3134 self.art.content = self.session.data.ansi_art; 3135 try self.buildArt(allocator, foreground.size, root_focus); 3136 if (!self.session.is_terminal) return; 3137 const art_grid = self.art.getGrid() orelse return; 3138 self.grid = try artBehind(allocator, foreground, art_grid); 3139 } 3140 3141 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 3142 try self.child.input(allocator, key, root_focus); 3143 } 3144 3145 pub fn clearGrid(self: *AnsiBackground) void { 3146 if (self.grid) |*grid| { 3147 grid.deinit(); 3148 self.grid = null; 3149 } 3150 self.child.clearGrid(); 3151 self.art.clearGrid(); 3152 } 3153 3154 pub fn getGrid(self: AnsiBackground) ?Grid { 3155 return self.grid orelse self.child.getGrid(); 3156 } 3157 3158 pub fn getFocus(self: *AnsiBackground) *Focus { 3159 return self.child.getFocus(); 3160 } 3161 3162 // a foreground cell counts as blank if it's empty or a bare space without styling 3163 fn cellIsBlank(cell: Grid.Cell) bool { 3164 if (cell.rune) |rune| { 3165 return std.mem.eql(u8, rune, " ") and 3166 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 3167 } 3168 return true; 3169 } 3170 3171 // the art is dimmed to this fraction of its brightness so foreground text 3172 // stays legible over it 3173 const art_brightness = 35; // percent 3174 3175 fn dimColor(color: ?Grid.Color) ?Grid.Color { 3176 const c = color orelse return null; 3177 return .{ 3178 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 3179 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 3180 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 3181 }; 3182 } 3183 3184 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 3185 try self.art.build(allocator, .{ 3186 .min_size = .{ .width = null, .height = null }, 3187 .max_size = .{ .width = size.width, .height = size.height }, 3188 }, root_focus); 3189 if (self.art.grid) |*grid| { 3190 for (grid.cells.items) |*cell| { 3191 cell.style.fg = dimColor(cell.style.fg); 3192 cell.style.bg = dimColor(cell.style.bg); 3193 } 3194 } 3195 } 3196 3197 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 3198 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 3199 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 3200 // snap such channels clear of that range so the backdrop doesn't blink or 3201 // hide text there; the ≤9/255 shift is imperceptible on terminals that 3202 // actually render truecolor. 3203 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 3204 const c = color orelse return null; 3205 const snap = struct { 3206 fn f(v: u8) u8 { 3207 return if (v >= 1 and v <= 9) 10 else v; 3208 } 3209 }.f; 3210 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 3211 } 3212 3213 // composites ANSI art behind a foreground grid 3214 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 3215 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 3216 errdefer out.deinit(); 3217 3218 const anchor_x = foreground.size.width -| art_grid.size.width; 3219 for (0..art_grid.size.height) |y| { 3220 for (0..art_grid.size.width) |x| { 3221 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 3222 if (src.rune == null) continue; 3223 const idx = try out.cells.at(.{ y, anchor_x + x }); 3224 const dst = &out.cells.items[idx]; 3225 // blank cells take the art; occupied cells take its background 3226 if (cellIsBlank(dst.*)) { 3227 dst.* = src; 3228 dst.style.fg = sgrSafe(dst.style.fg); 3229 dst.style.bg = sgrSafe(dst.style.bg); 3230 } else { 3231 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 3232 dst.style.bg = background_maybe; 3233 // change unstyled text to contrast with the art behind it 3234 if (dst.style.fg == null) { 3235 if (background_maybe) |background| { 3236 // use near-black and near-white, which is easier on the eyes 3237 // while retaining high contrast 3238 const luminance = (@as(u32, background.r) * 299 + 3239 @as(u32, background.g) * 587 + 3240 @as(u32, background.b) * 114) / 1000; 3241 dst.style.fg = if (luminance >= 128) 3242 .{ .r = 16, .g = 16, .b = 16 } 3243 else 3244 .{ .r = 240, .g = 240, .b = 240 }; 3245 } 3246 } 3247 } 3248 } 3249 } 3250 return out; 3251 } 3252 }; 3253