╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2153 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2154 if (self.grid) |*grid| grid.deinit(); 2155 self.art.deinit(allocator); 2156 self.child.deinit(allocator); 2157 allocator.destroy(self.child); 2158 } 2159 2160 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2161 self.clearGrid(); 2162 2163 // make the wrapped page fill the whole available area 2164 try self.child.build(allocator, .{ 2165 .min_size = .{ 2166 .width = constraint.max_size.width orelse constraint.min_size.width, 2167 .height = constraint.max_size.height orelse constraint.min_size.height, 2168 }, 2169 .max_size = constraint.max_size, 2170 }, root_focus); 2171 2172 if (self.session.data.enable_ansi) { 2173 if (self.child.getGrid()) |fg| { 2174 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2175 } 2176 } 2177 } 2178 2179 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2180 try self.child.input(allocator, key, root_focus); 2181 } 2182 2183 pub fn clearGrid(self: *AnsiBackground) void { 2184 if (self.grid) |*grid| { 2185 grid.deinit(); 2186 self.grid = null; 2187 } 2188 self.child.clearGrid(); 2189 self.art.clearGrid(); 2190 } 2191 2192 pub fn getGrid(self: AnsiBackground) ?Grid { 2193 return self.grid orelse self.child.getGrid(); 2194 } 2195 2196 pub fn getFocus(self: *AnsiBackground) *Focus { 2197 return self.child.getFocus(); 2198 } 2199 2200 // which top corner the art hugs. 2201 const ArtAnchor = enum { top_left, top_right }; 2202 2203 // a foreground cell counts as blank if it's empty or a bare space without styling 2204 fn cellIsBlank(cell: Grid.Cell) bool { 2205 if (cell.rune) |rune| { 2206 return std.mem.eql(u8, rune, " ") and 2207 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2208 } 2209 return true; 2210 } 2211 2212 // the art is dimmed to this fraction of its brightness so foreground text 2213 // stays legible over it 2214 const art_brightness = 45; // percent 2215 2216 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2217 const c = color orelse return null; 2218 return .{ 2219 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2220 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2221 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2222 }; 2223 } 2224 2225 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2226 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2227 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2228 // snap such channels clear of that range so the backdrop doesn't blink or 2229 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2230 // actually render truecolor. 2231 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2232 const c = color orelse return null; 2233 const snap = struct { 2234 fn f(v: u8) u8 { 2235 return if (v >= 1 and v <= 9) 10 else v; 2236 } 2237 }.f; 2238 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2239 } 2240 2241 // composites ANSI art behind a foreground grid 2242 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2243 art.clearGrid(); 2244 try art.build(allocator, .{ 2245 .min_size = .{ .width = null, .height = null }, 2246 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2247 }, root_focus); 2248 2249 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2250 errdefer out.deinit(); 2251 2252 if (art.getGrid()) |art_grid| { 2253 const anchor_x = switch (anchor) { 2254 .top_left => 0, 2255 .top_right => foreground.size.width -| art_grid.size.width, 2256 }; 2257 for (0..art_grid.size.height) |y| { 2258 for (0..art_grid.size.width) |x| { 2259 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2260 if (src.rune == null) continue; 2261 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2262 const dst = &out.cells.items[idx]; 2263 // composite the dimmed art behind every cell so it shows 2264 // through the whole UI: a blank cell becomes the art, while a 2265 // cell with a glyph keeps the glyph but takes the dimmed art as 2266 // its background (replacing whatever background it had, which 2267 // would otherwise obscure the art). 2268 if (cellIsBlank(dst.*)) { 2269 dst.* = src; 2270 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2271 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2272 } else { 2273 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2274 } 2275 } 2276 } 2277 } 2278 return out; 2279 } 2280 }; 2281