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