╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2263 2264 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2265 if (self.grid) |*grid| grid.deinit(); 2266 self.art.deinit(allocator); 2267 self.child.deinit(allocator); 2268 allocator.destroy(self.child); 2269 } 2270 2271 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2272 self.clearGrid(); 2273 2274 // make the wrapped page fill the whole available area 2275 try self.child.build(allocator, .{ 2276 .min_size = .{ 2277 .width = constraint.max_size.width orelse constraint.min_size.width, 2278 .height = constraint.max_size.height orelse constraint.min_size.height, 2279 }, 2280 .max_size = constraint.max_size, 2281 }, root_focus); 2282 2283 if (self.session.data.enable_ansi) { 2284 if (self.child.getGrid()) |fg| { 2285 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2286 } 2287 } 2288 } 2289 2290 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2291 try self.child.input(allocator, key, root_focus); 2292 } 2293 2294 pub fn clearGrid(self: *AnsiBackground) void { 2295 if (self.grid) |*grid| { 2296 grid.deinit(); 2297 self.grid = null; 2298 } 2299 self.child.clearGrid(); 2300 self.art.clearGrid(); 2301 } 2302 2303 pub fn getGrid(self: AnsiBackground) ?Grid { 2304 return self.grid orelse self.child.getGrid(); 2305 } 2306 2307 pub fn getFocus(self: *AnsiBackground) *Focus { 2308 return self.child.getFocus(); 2309 } 2310 2311 // which top corner the art hugs. 2312 const ArtAnchor = enum { top_left, top_right }; 2313 2314 // a foreground cell counts as blank if it's empty or a bare space without styling 2315 fn cellIsBlank(cell: Grid.Cell) bool { 2316 if (cell.rune) |rune| { 2317 return std.mem.eql(u8, rune, " ") and 2318 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2319 } 2320 return true; 2321 } 2322 2323 // the art is dimmed to this fraction of its brightness so foreground text 2324 // stays legible over it 2325 const art_brightness = 45; // percent 2326 2327 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2328 const c = color orelse return null; 2329 return .{ 2330 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2331 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2332 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2333 }; 2334 } 2335 2336 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2337 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2338 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2339 // snap such channels clear of that range so the backdrop doesn't blink or 2340 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2341 // actually render truecolor. 2342 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2343 const c = color orelse return null; 2344 const snap = struct { 2345 fn f(v: u8) u8 { 2346 return if (v >= 1 and v <= 9) 10 else v; 2347 } 2348 }.f; 2349 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2350 } 2351 2352 // composites ANSI art behind a foreground grid 2353 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2354 art.clearGrid(); 2355 try art.build(allocator, .{ 2356 .min_size = .{ .width = null, .height = null }, 2357 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2358 }, root_focus); 2359 2360 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2361 errdefer out.deinit(); 2362 2363 if (art.getGrid()) |art_grid| { 2364 const anchor_x = switch (anchor) { 2365 .top_left => 0, 2366 .top_right => foreground.size.width -| art_grid.size.width, 2367 }; 2368 for (0..art_grid.size.height) |y| { 2369 for (0..art_grid.size.width) |x| { 2370 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2371 if (src.rune == null) continue; 2372 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2373 const dst = &out.cells.items[idx]; 2374 // composite the dimmed art behind every cell so it shows 2375 // through the whole UI: a blank cell becomes the art, while a 2376 // cell with a glyph keeps the glyph but takes the dimmed art as 2377 // its background (replacing whatever background it had, which 2378 // would otherwise obscure the art). 2379 if (cellIsBlank(dst.*)) { 2380 dst.* = src; 2381 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2382 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2383 } else { 2384 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2385 } 2386 } 2387 } 2388 } 2389 return out; 2390 } 2391 }; 2392