╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2116 _ = root_focus; 2117 self.clearGrid(); 2118 2119 // parse into rows of cells, then copy into a rectangular grid 2120 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2121 defer { 2122 for (rows.items) |*row| row.deinit(allocator); 2123 rows.deinit(allocator); 2124 } 2125 var row: std.ArrayList(Grid.Cell) = .empty; 2126 errdefer row.deinit(allocator); 2127 2128 var style: Grid.Style = .{}; 2129 var width: usize = 0; 2130 const content = self.content; 2131 var i: usize = 0; 2132 while (i < content.len) { 2133 const byte = content[i]; 2134 if (byte == '\n') { 2135 width = @max(width, row.items.len); 2136 try rows.append(allocator, row); 2137 row = .empty; 2138 i += 1; 2139 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2140 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2141 var j = i + 2; 2142 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2143 if (j >= content.len) { 2144 i = content.len; // malformed trailing escape; stop 2145 } else { 2146 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2147 i = j + 1; 2148 } 2149 } else { 2150 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2151 const end = @min(content.len, i + len); 2152 const rune = content[i..end]; 2153 const transparent = end - i == 1 and rune[0] == ' ' and 2154 style.fg == null and style.bg == null and !style.inverted; 2155 try row.append(allocator, .{ 2156 .rune = if (transparent) null else rune, 2157 .style = style, 2158 }); 2159 i = end; 2160 } 2161 } 2162 // a trailing row with content but no closing newline 2163 if (row.items.len > 0) { 2164 width = @max(width, row.items.len); 2165 try rows.append(allocator, row); 2166 } else { 2167 row.deinit(allocator); 2168 } 2169 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2170 2171 const height = rows.items.len; 2172 if (width == 0 or height == 0) return; 2173 2174 const clamped_w = @min(width, constraint.max_size.width orelse width); 2175 const clamped_h = @min(height, constraint.max_size.height orelse height); 2176 2177 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2178 errdefer grid.deinit(); 2179 for (rows.items[0..clamped_h], 0..) |r, y| { 2180 const n = @min(r.items.len, clamped_w); 2181 for (r.items[0..n], 0..) |cell, x| { 2182 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2183 } 2184 } 2185 self.grid = grid; 2186 } 2187 2188 pub fn clearGrid(self: *AnsiArt) void { 2189 if (self.grid) |*grid| { 2190 grid.deinit(); 2191 self.grid = null; 2192 } 2193 } 2194 2195 pub fn getGrid(self: AnsiArt) ?Grid { 2196 return self.grid; 2197 } 2198 2199 pub fn getFocus(self: *AnsiArt) *Focus { 2200 return self.focus; 2201 } 2202 2203 fn applySgr(style: *Grid.Style, params: []const u8) void { 2204 var nums: [16]u32 = undefined; 2205 var n: usize = 0; 2206 var it = std.mem.splitScalar(u8, params, ';'); 2207 while (it.next()) |tok| { 2208 if (n >= nums.len) break; 2209 // an empty parameter (e.g. bare "\x1b[m") means 0 2210 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2211 n += 1; 2212 } 2213 if (n == 0) { 2214 style.* = .{}; 2215 return; 2216 } 2217 var i: usize = 0; 2218 while (i < n) : (i += 1) { 2219 switch (nums[i]) { 2220 0 => style.* = .{}, 2221 7 => style.inverted = true, 2222 27 => style.inverted = false, 2223 39 => style.fg = null, 2224 49 => style.bg = null, 2225 38, 48 => { 2226 // truecolor form: 38;2;r;g;b — anything else is ignored 2227 if (i + 4 < n and nums[i + 1] == 2) { 2228 const c = Grid.Color{ 2229 .r = @truncate(nums[i + 2]), 2230 .g = @truncate(nums[i + 3]), 2231 .b = @truncate(nums[i + 4]), 2232 }; 2233 if (nums[i] == 38) style.fg = c else style.bg = c; 2234 i += 4; 2235 } 2236 }, 2237 else => {}, 2238 } 2239 } 2240 } 2241 }; 2242 2243 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2244 pub const AnsiBackground = struct { 2245 grid: ?Grid, 2246 child: *Widget, 2247 art: AnsiArt, 2248 session: *Session, 2249 2250 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2251 var cw = child_widget; 2252 const child = allocator.create(Widget) catch |e| { 2253 cw.deinit(allocator); 2254 return e; 2255 }; 2256 child.* = cw; 2257 errdefer { 2258 child.deinit(allocator); 2259 allocator.destroy(child); 2260 } 2261 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2262 } 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