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