╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2241 } 2242 2243 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2244 self.focus.destroy(allocator); 2245 if (self.grid) |*grid| grid.deinit(); 2246 self.arena.deinit(); 2247 } 2248 2249 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2250 _ = root_focus; 2251 self.clearGrid(); 2252 const width = constraint.max_size.width orelse return; 2253 if (width == 0) return; 2254 2255 _ = self.arena.reset(.retain_capacity); 2256 self.url = ""; 2257 self.url_width = 0; 2258 const aa = self.arena.allocator(); 2259 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2260 const text = if (self.session.web_port) |port| 2261 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 2262 else 2263 path; 2264 self.url = text; 2265 2266 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2267 errdefer grid.deinit(); 2268 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2269 var i: usize = 0; 2270 while (utf8.nextCodepointSlice()) |ch| { 2271 if (i == width) break; 2272 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2273 i += 1; 2274 } 2275 self.url_width = i; 2276 self.grid = grid; 2277 } 2278 2279 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2280 _ = allocator; 2281 switch (key) { 2282 .mouse => |mouse| { 2283 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 2284 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 2285 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 2286 }, 2287 else => {}, 2288 } 2289 } 2290 2291 pub fn clearGrid(self: *Footer) void { 2292 if (self.grid) |*grid| { 2293 grid.deinit(); 2294 self.grid = null; 2295 } 2296 } 2297 2298 pub fn getGrid(self: Footer) ?Grid { 2299 return self.grid; 2300 } 2301 2302 pub fn getFocus(self: *Footer) *Focus { 2303 return self.focus; 2304 } 2305 }; 2306 2307 // a single-child wrapper that builds the child at its natural size and 2308 // positions its grid in the middle of the area granted by the parent 2309 // a bordered label, indented past the content it precedes 2310 pub const SectionLabel = struct { 2311 box: wgt.Box(Widget), 2312 2313 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2314 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2315 errdefer box.deinit(allocator); 2316 2317 { 2318 var gap = try wgt.Text(Widget).init(allocator, " "); 2319 errdefer gap.deinit(allocator); 2320 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2321 } 2322 2323 { 2324 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2325 errdefer tb.deinit(allocator); 2326 tb.getFocus().focusable = true; 2327 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2328 } 2329 2330 box.getFocus().child_id = box.children.keys()[1]; 2331 return .{ .box = box }; 2332 } 2333 2334 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2335 self.box.deinit(allocator); 2336 } 2337 2338 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2339 try self.box.build(allocator, constraint, root_focus); 2340 } 2341 2342 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2343 _ = self; 2344 _ = allocator; 2345 _ = key; 2346 _ = root_focus; 2347 } 2348 2349 pub fn clearGrid(self: *SectionLabel) void { 2350 self.box.clearGrid(); 2351 } 2352 2353 pub fn getGrid(self: SectionLabel) ?Grid { 2354 return self.box.getGrid(); 2355 } 2356 2357 pub fn getFocus(self: *SectionLabel) *Focus { 2358 return self.box.getFocus(); 2359 } 2360 }; 2361 2362 // a submit button, indented past its form's content. the web overlay POSTs 2363 // the button's form; terminal hosts detect presses through `buttonId`. 2364 pub const SubmitButton = struct { 2365 box: wgt.Box(Widget), 2366 2367 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2368 return initLabeled(allocator, "submit"); 2369 } 2370 2371 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2372 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2373 errdefer box.deinit(allocator); 2374 2375 { 2376 var gap = try wgt.Text(Widget).init(allocator, " "); 2377 errdefer gap.deinit(allocator); 2378 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2379 } 2380 2381 { 2382 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2383 errdefer button.deinit(allocator); 2384 button.getFocus().focusable = true; 2385 // the renderer distinguishes plain clickables from buttons that 2386 // should POST to a server route by this kind. 2387 button.getFocus().kind = .{ .custom = "submit" }; 2388 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2389 } 2390 2391 box.getFocus().child_id = box.children.keys()[1]; 2392 return .{ .box = box }; 2393 } 2394 2395 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2396 self.box.deinit(allocator); 2397 } 2398 2399 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2400 try self.box.build(allocator, constraint, root_focus); 2401 } 2402 2403 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2404 _ = self; 2405 _ = allocator; 2406 _ = key; 2407 _ = root_focus; 2408 } 2409 2410 pub fn clearGrid(self: *SubmitButton) void { 2411 self.box.clearGrid(); 2412 } 2413 2414 pub fn getGrid(self: SubmitButton) ?Grid { 2415 return self.box.getGrid(); 2416 } 2417 2418 pub fn getFocus(self: *SubmitButton) *Focus { 2419 return self.box.getFocus(); 2420 } 2421 2422 // the button's focus id, for click hit-testing 2423 pub fn buttonId(self: *const SubmitButton) usize { 2424 return self.box.children.keys()[1]; 2425 } 2426 }; 2427 2428 pub const Center = struct { 2429 focus: *Focus, 2430 grid: ?Grid, 2431 child: *Widget, 2432 2433 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2434 const child = try allocator.create(Widget); 2435 errdefer allocator.destroy(child); 2436 child.* = child_widget; 2437 return .{ 2438 .focus = try Focus.create(allocator, .container), 2439 .grid = null, 2440 .child = child, 2441 }; 2442 } 2443 2444 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2445 self.focus.destroy(allocator); 2446 if (self.grid) |*grid| { 2447 grid.deinit(); 2448 self.grid = null; 2449 } 2450 self.child.deinit(allocator); 2451 allocator.destroy(self.child); 2452 } 2453 2454 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2455 self.clearGrid(); 2456 self.getFocus().clear(); 2457 2458 // build the child without forcing it to fill min_size; it sizes 2459 // itself to its content within the available max. 2460 try self.child.build(allocator, .{ 2461 .min_size = .{ .width = null, .height = null }, 2462 .max_size = constraint.max_size, 2463 }, root_focus); 2464 2465 const child_grid = self.child.getGrid() orelse return; 2466 2467 // prefer max when given; otherwise grow to max(min, child) so that 2468 // when only a min is set (e.g. the wasm path passes viewport rows 2469 // as min_height with no max), we can still vertically center while 2470 // letting taller content extend past the min. 2471 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 2472 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 2473 if (width == 0 or height == 0) return; 2474 2475 const offset_x: usize = (width -| child_grid.size.width) / 2; 2476 const offset_y: usize = (height -| child_grid.size.height) / 2; 2477 2478 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2479 errdefer grid.deinit(); 2480 try grid.drawGrid(child_grid, offset_x, offset_y); 2481 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2482 self.getFocus().child_id = self.child.getFocus().id; 2483 2484 self.grid = grid; 2485 } 2486 2487 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2488 try self.child.input(allocator, key, root_focus); 2489 } 2490 2491 pub fn clearGrid(self: *Center) void { 2492 if (self.grid) |*grid| { 2493 grid.deinit(); 2494 self.grid = null; 2495 } 2496 self.child.clearGrid(); 2497 } 2498 2499 pub fn getGrid(self: Center) ?Grid { 2500 return self.grid; 2501 } 2502 2503 pub fn getFocus(self: *Center) *Focus { 2504 return self.focus; 2505 } 2506 }; 2507 2508 // renders truecolor ANSI art 2509 pub const AnsiArt = struct { 2510 focus: *Focus, 2511 grid: ?Grid, 2512 content: []const u8, 2513 2514 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2515 return .{ 2516 .focus = try Focus.create(allocator, .container), 2517 .grid = null, 2518 .content = content, 2519 }; 2520 } 2521 2522 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2523 self.focus.destroy(allocator); 2524 if (self.grid) |*grid| { 2525 grid.deinit(); 2526 self.grid = null; 2527 } 2528 } 2529 2530 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2531 _ = root_focus; 2532 self.clearGrid(); 2533 2534 // parse into rows of cells, then copy into a rectangular grid 2535 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2536 defer { 2537 for (rows.items) |*row| row.deinit(allocator); 2538 rows.deinit(allocator); 2539 } 2540 var row: std.ArrayList(Grid.Cell) = .empty; 2541 errdefer row.deinit(allocator); 2542 2543 var style: Grid.Style = .{}; 2544 var width: usize = 0; 2545 const content = self.content; 2546 var i: usize = 0; 2547 while (i < content.len) { 2548 const byte = content[i]; 2549 if (byte == '\n') { 2550 width = @max(width, row.items.len); 2551 try rows.append(allocator, row); 2552 row = .empty; 2553 i += 1; 2554 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2555 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2556 var j = i + 2; 2557 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2558 if (j >= content.len) { 2559 i = content.len; // malformed trailing escape; stop 2560 } else { 2561 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2562 i = j + 1; 2563 } 2564 } else { 2565 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2566 const end = @min(content.len, i + len); 2567 const rune = content[i..end]; 2568 const transparent = end - i == 1 and rune[0] == ' ' and 2569 style.fg == null and style.bg == null and !style.inverted; 2570 try row.append(allocator, .{ 2571 .rune = if (transparent) null else rune, 2572 .style = style, 2573 }); 2574 i = end; 2575 } 2576 } 2577 // a trailing row with content but no closing newline 2578 if (row.items.len > 0) { 2579 width = @max(width, row.items.len); 2580 try rows.append(allocator, row); 2581 } else { 2582 row.deinit(allocator); 2583 } 2584 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2585 2586 const height = rows.items.len; 2587 if (width == 0 or height == 0) return; 2588 2589 const clamped_w = @min(width, constraint.max_size.width orelse width); 2590 const clamped_h = @min(height, constraint.max_size.height orelse height); 2591 2592 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2593 errdefer grid.deinit(); 2594 for (rows.items[0..clamped_h], 0..) |r, y| { 2595 const n = @min(r.items.len, clamped_w); 2596 for (r.items[0..n], 0..) |cell, x| { 2597 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2598 } 2599 } 2600 self.grid = grid; 2601 } 2602 2603 pub fn clearGrid(self: *AnsiArt) void { 2604 if (self.grid) |*grid| { 2605 grid.deinit(); 2606 self.grid = null; 2607 } 2608 } 2609 2610 pub fn getGrid(self: AnsiArt) ?Grid { 2611 return self.grid; 2612 } 2613 2614 pub fn getFocus(self: *AnsiArt) *Focus { 2615 return self.focus; 2616 } 2617 2618 fn applySgr(style: *Grid.Style, params: []const u8) void { 2619 var nums: [16]u32 = undefined; 2620 var n: usize = 0; 2621 var it = std.mem.splitScalar(u8, params, ';'); 2622 while (it.next()) |tok| { 2623 if (n >= nums.len) break; 2624 // an empty parameter (e.g. bare "\x1b[m") means 0 2625 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2626 n += 1; 2627 } 2628 if (n == 0) { 2629 style.* = .{}; 2630 return; 2631 } 2632 var i: usize = 0; 2633 while (i < n) : (i += 1) { 2634 switch (nums[i]) { 2635 0 => style.* = .{}, 2636 7 => style.inverted = true, 2637 27 => style.inverted = false, 2638 39 => style.fg = null, 2639 49 => style.bg = null, 2640 38, 48 => { 2641 // truecolor form: 38;2;r;g;b — anything else is ignored 2642 if (i + 4 < n and nums[i + 1] == 2) { 2643 const c = Grid.Color{ 2644 .r = @truncate(nums[i + 2]), 2645 .g = @truncate(nums[i + 3]), 2646 .b = @truncate(nums[i + 4]), 2647 }; 2648 if (nums[i] == 38) style.fg = c else style.bg = c; 2649 i += 4; 2650 } 2651 }, 2652 else => {}, 2653 } 2654 } 2655 } 2656 }; 2657 2658 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2659 pub const AnsiBackground = struct { 2660 grid: ?Grid, 2661 child: *Widget, 2662 art: AnsiArt, 2663 session: *Session, 2664 2665 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2666 var cw = child_widget; 2667 const child = allocator.create(Widget) catch |e| { 2668 cw.deinit(allocator); 2669 return e; 2670 }; 2671 child.* = cw; 2672 errdefer { 2673 child.deinit(allocator); 2674 allocator.destroy(child); 2675 } 2676 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2677 } 2678 2679 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2680 if (self.grid) |*grid| grid.deinit(); 2681 self.art.deinit(allocator); 2682 self.child.deinit(allocator); 2683 allocator.destroy(self.child); 2684 } 2685 2686 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2687 self.clearGrid(); 2688 2689 // make the wrapped page fill the whole available area 2690 try self.child.build(allocator, .{ 2691 .min_size = .{ 2692 .width = constraint.max_size.width orelse constraint.min_size.width, 2693 .height = constraint.max_size.height orelse constraint.min_size.height, 2694 }, 2695 .max_size = constraint.max_size, 2696 }, root_focus); 2697 2698 if (self.session.data.enable_ansi) { 2699 if (self.child.getGrid()) |fg| { 2700 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2701 } 2702 } 2703 } 2704 2705 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2706 try self.child.input(allocator, key, root_focus); 2707 } 2708 2709 pub fn clearGrid(self: *AnsiBackground) void { 2710 if (self.grid) |*grid| { 2711 grid.deinit(); 2712 self.grid = null; 2713 } 2714 self.child.clearGrid(); 2715 self.art.clearGrid(); 2716 } 2717 2718 pub fn getGrid(self: AnsiBackground) ?Grid { 2719 return self.grid orelse self.child.getGrid(); 2720 } 2721 2722 pub fn getFocus(self: *AnsiBackground) *Focus { 2723 return self.child.getFocus(); 2724 } 2725 2726 // which top corner the art hugs. 2727 const ArtAnchor = enum { top_left, top_right }; 2728 2729 // a foreground cell counts as blank if it's empty or a bare space without styling 2730 fn cellIsBlank(cell: Grid.Cell) bool { 2731 if (cell.rune) |rune| { 2732 return std.mem.eql(u8, rune, " ") and 2733 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2734 } 2735 return true; 2736 } 2737 2738 // the art is dimmed to this fraction of its brightness so foreground text 2739 // stays legible over it 2740 const art_brightness = 45; // percent 2741 2742 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2743 const c = color orelse return null; 2744 return .{ 2745 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2746 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2747 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2748 }; 2749 } 2750 2751 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2752 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2753 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2754 // snap such channels clear of that range so the backdrop doesn't blink or 2755 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2756 // actually render truecolor. 2757 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2758 const c = color orelse return null; 2759 const snap = struct { 2760 fn f(v: u8) u8 { 2761 return if (v >= 1 and v <= 9) 10 else v; 2762 } 2763 }.f; 2764 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2765 } 2766 2767 // composites ANSI art behind a foreground grid 2768 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2769 art.clearGrid(); 2770 try art.build(allocator, .{ 2771 .min_size = .{ .width = null, .height = null }, 2772 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2773 }, root_focus); 2774 2775 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2776 errdefer out.deinit(); 2777 2778 if (art.getGrid()) |art_grid| { 2779 const anchor_x = switch (anchor) { 2780 .top_left => 0, 2781 .top_right => foreground.size.width -| art_grid.size.width, 2782 }; 2783 for (0..art_grid.size.height) |y| { 2784 for (0..art_grid.size.width) |x| { 2785 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2786 if (src.rune == null) continue; 2787 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2788 const dst = &out.cells.items[idx]; 2789 // composite the dimmed art behind every cell so it shows 2790 // through the whole UI: a blank cell becomes the art, while a 2791 // cell with a glyph keeps the glyph but takes the dimmed art as 2792 // its background (replacing whatever background it had, which 2793 // would otherwise obscure the art). 2794 if (cellIsBlank(dst.*)) { 2795 dst.* = src; 2796 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2797 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2798 } else { 2799 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2800 dst.style.bg = background_maybe; 2801 // change the color of text overlaying ANSI art to ensure 2802 // it is readable. don't do this in the web UI, since we 2803 // have direct control of the theme there (also, inner 2804 // scroll panes allow text to move without a re-render 2805 // by the TUI, which would break this). also don't do this 2806 // if the text is explicitly styled. 2807 if (is_terminal and dst.style.fg == null) { 2808 if (background_maybe) |background| { 2809 // use near-black and near-white, which is easier on the eyes 2810 // while retaining high contrast 2811 const luminance = (@as(u32, background.r) * 299 + 2812 @as(u32, background.g) * 587 + 2813 @as(u32, background.b) * 114) / 1000; 2814 dst.style.fg = if (luminance >= 128) 2815 .{ .r = 16, .g = 16, .b = 16 } 2816 else 2817 .{ .r = 240, .g = 240, .b = 240 }; 2818 } 2819 } 2820 } 2821 } 2822 } 2823 } 2824 return out; 2825 } 2826 }; 2827