╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2236 2237 const clamped_w = @min(width, constraint.max_size.width orelse width); 2238 const clamped_h = @min(height, constraint.max_size.height orelse height); 2239 2240 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2241 errdefer grid.deinit(); 2242 for (rows.items[0..clamped_h], 0..) |r, y| { 2243 const n = @min(r.items.len, clamped_w); 2244 for (r.items[0..n], 0..) |cell, x| { 2245 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2246 } 2247 } 2248 self.grid = grid; 2249 } 2250 2251 pub fn clearGrid(self: *AnsiArt) void { 2252 if (self.grid) |*grid| { 2253 grid.deinit(); 2254 self.grid = null; 2255 } 2256 } 2257 2258 pub fn getGrid(self: AnsiArt) ?Grid { 2259 return self.grid; 2260 } 2261 2262 pub fn getFocus(self: *AnsiArt) *Focus { 2263 return self.focus; 2264 } 2265 2266 fn applySgr(style: *Grid.Style, params: []const u8) void { 2267 var nums: [16]u32 = undefined; 2268 var n: usize = 0; 2269 var it = std.mem.splitScalar(u8, params, ';'); 2270 while (it.next()) |tok| { 2271 if (n >= nums.len) break; 2272 // an empty parameter (e.g. bare "\x1b[m") means 0 2273 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2274 n += 1; 2275 } 2276 if (n == 0) { 2277 style.* = .{}; 2278 return; 2279 } 2280 var i: usize = 0; 2281 while (i < n) : (i += 1) { 2282 switch (nums[i]) { 2283 0 => style.* = .{}, 2284 7 => style.inverted = true, 2285 27 => style.inverted = false, 2286 39 => style.fg = null, 2287 49 => style.bg = null, 2288 38, 48 => { 2289 // truecolor form: 38;2;r;g;b — anything else is ignored 2290 if (i + 4 < n and nums[i + 1] == 2) { 2291 const c = Grid.Color{ 2292 .r = @truncate(nums[i + 2]), 2293 .g = @truncate(nums[i + 3]), 2294 .b = @truncate(nums[i + 4]), 2295 }; 2296 if (nums[i] == 38) style.fg = c else style.bg = c; 2297 i += 4; 2298 } 2299 }, 2300 else => {}, 2301 } 2302 } 2303 } 2304 }; 2305 2306 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2307 pub const AnsiBackground = struct { 2308 grid: ?Grid, 2309 child: *Widget, 2310 art: AnsiArt, 2311 session: *Session, 2312 2313 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2314 var cw = child_widget; 2315 const child = allocator.create(Widget) catch |e| { 2316 cw.deinit(allocator); 2317 return e; 2318 }; 2319 child.* = cw; 2320 errdefer { 2321 child.deinit(allocator); 2322 allocator.destroy(child); 2323 } 2324 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2325 } 2326 2327 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2328 if (self.grid) |*grid| grid.deinit(); 2329 self.art.deinit(allocator); 2330 self.child.deinit(allocator); 2331 allocator.destroy(self.child); 2332 } 2333 2334 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2335 self.clearGrid(); 2336 2337 // make the wrapped page fill the whole available area 2338 try self.child.build(allocator, .{ 2339 .min_size = .{ 2340 .width = constraint.max_size.width orelse constraint.min_size.width, 2341 .height = constraint.max_size.height orelse constraint.min_size.height, 2342 }, 2343 .max_size = constraint.max_size, 2344 }, root_focus); 2345 2346 if (self.session.data.enable_ansi) { 2347 if (self.child.getGrid()) |fg| { 2348 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2349 } 2350 } 2351 } 2352 2353 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2354 try self.child.input(allocator, key, root_focus); 2355 } 2356 2357 pub fn clearGrid(self: *AnsiBackground) void { 2358 if (self.grid) |*grid| { 2359 grid.deinit(); 2360 self.grid = null; 2361 } 2362 self.child.clearGrid(); 2363 self.art.clearGrid(); 2364 } 2365 2366 pub fn getGrid(self: AnsiBackground) ?Grid { 2367 return self.grid orelse self.child.getGrid(); 2368 } 2369 2370 pub fn getFocus(self: *AnsiBackground) *Focus { 2371 return self.child.getFocus(); 2372 } 2373 2374 // which top corner the art hugs. 2375 const ArtAnchor = enum { top_left, top_right }; 2376 2377 // a foreground cell counts as blank if it's empty or a bare space without styling 2378 fn cellIsBlank(cell: Grid.Cell) bool { 2379 if (cell.rune) |rune| { 2380 return std.mem.eql(u8, rune, " ") and 2381 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2382 } 2383 return true; 2384 } 2385 2386 // the art is dimmed to this fraction of its brightness so foreground text 2387 // stays legible over it 2388 const art_brightness = 45; // percent 2389 2390 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2391 const c = color orelse return null; 2392 return .{ 2393 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2394 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2395 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2396 }; 2397 } 2398 2399 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2400 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2401 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2402 // snap such channels clear of that range so the backdrop doesn't blink or 2403 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2404 // actually render truecolor. 2405 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2406 const c = color orelse return null; 2407 const snap = struct { 2408 fn f(v: u8) u8 { 2409 return if (v >= 1 and v <= 9) 10 else v; 2410 } 2411 }.f; 2412 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2413 } 2414 2415 // composites ANSI art behind a foreground grid 2416 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2417 art.clearGrid(); 2418 try art.build(allocator, .{ 2419 .min_size = .{ .width = null, .height = null }, 2420 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2421 }, root_focus); 2422 2423 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2424 errdefer out.deinit(); 2425 2426 if (art.getGrid()) |art_grid| { 2427 const anchor_x = switch (anchor) { 2428 .top_left => 0, 2429 .top_right => foreground.size.width -| art_grid.size.width, 2430 }; 2431 for (0..art_grid.size.height) |y| { 2432 for (0..art_grid.size.width) |x| { 2433 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2434 if (src.rune == null) continue; 2435 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2436 const dst = &out.cells.items[idx]; 2437 // composite the dimmed art behind every cell so it shows 2438 // through the whole UI: a blank cell becomes the art, while a 2439 // cell with a glyph keeps the glyph but takes the dimmed art as 2440 // its background (replacing whatever background it had, which 2441 // would otherwise obscure the art). 2442 if (cellIsBlank(dst.*)) { 2443 dst.* = src; 2444 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2445 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2446 } else { 2447 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2448 } 2449 } 2450 } 2451 } 2452 return out; 2453 } 2454 }; 2455