╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2114 2115 pub fn getGrid(self: AnsiBackground) ?Grid { 2116 return self.grid orelse self.child.getGrid(); 2117 } 2118 2119 pub fn getFocus(self: *AnsiBackground) *Focus { 2120 return self.child.getFocus(); 2121 } 2122 2123 // which top corner the art hugs. 2124 const ArtAnchor = enum { top_left, top_right }; 2125 2126 // a foreground cell counts as blank if it's empty or a bare space without styling 2127 fn cellIsBlank(cell: Grid.Cell) bool { 2128 if (cell.rune) |rune| { 2129 return std.mem.eql(u8, rune, " ") and 2130 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2131 } 2132 return true; 2133 } 2134 2135 // the art is dimmed to this fraction of its brightness so foreground text 2136 // stays legible over it 2137 const art_brightness = 45; // percent 2138 2139 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2140 const c = color orelse return null; 2141 return .{ 2142 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2143 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2144 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2145 }; 2146 } 2147 2148 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2149 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2150 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2151 // snap such channels clear of that range so the backdrop doesn't blink or 2152 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2153 // actually render truecolor. 2154 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2155 const c = color orelse return null; 2156 const snap = struct { 2157 fn f(v: u8) u8 { 2158 return if (v >= 1 and v <= 9) 10 else v; 2159 } 2160 }.f; 2161 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2162 } 2163 2164 // composites ANSI art behind a foreground grid 2165 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2166 art.clearGrid(); 2167 try art.build(allocator, .{ 2168 .min_size = .{ .width = null, .height = null }, 2169 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2170 }, root_focus); 2171 2172 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2173 errdefer out.deinit(); 2174 2175 if (art.getGrid()) |art_grid| { 2176 const anchor_x = switch (anchor) { 2177 .top_left => 0, 2178 .top_right => foreground.size.width -| art_grid.size.width, 2179 }; 2180 for (0..art_grid.size.height) |y| { 2181 for (0..art_grid.size.width) |x| { 2182 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2183 if (src.rune == null) continue; 2184 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2185 const dst = &out.cells.items[idx]; 2186 // composite the dimmed art behind every cell so it shows 2187 // through the whole UI: a blank cell becomes the art, while a 2188 // cell with a glyph keeps the glyph but takes the dimmed art as 2189 // its background (replacing whatever background it had, which 2190 // would otherwise obscure the art). 2191 if (cellIsBlank(dst.*)) { 2192 dst.* = src; 2193 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2194 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2195 } else { 2196 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2197 } 2198 } 2199 } 2200 } 2201 return out; 2202 } 2203 }; 2204