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