╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2396 pub fn init(allocator: std.mem.Allocator) !Spacer { 2397 return .{ 2398 .focus = try Focus.create(allocator, .container), 2399 .grid = null, 2400 }; 2401 } 2402 2403 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 2404 self.focus.destroy(allocator); 2405 if (self.grid) |*grid| { 2406 grid.deinit(); 2407 self.grid = null; 2408 } 2409 } 2410 2411 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2412 _ = root_focus; 2413 self.clearGrid(); 2414 const width = constraint.max_size.width orelse return; 2415 if (width == 0) return; 2416 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2417 } 2418 2419 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2420 _ = self; 2421 _ = allocator; 2422 _ = key; 2423 _ = root_focus; 2424 } 2425 2426 pub fn clearGrid(self: *Spacer) void { 2427 if (self.grid) |*grid| { 2428 grid.deinit(); 2429 self.grid = null; 2430 } 2431 } 2432 2433 pub fn getGrid(self: Spacer) ?Grid { 2434 return self.grid; 2435 } 2436 2437 pub fn getFocus(self: *Spacer) *Focus { 2438 return self.focus; 2439 } 2440 }; 2441 2442 // a one-row, non-focusable status bar showing the current page's url 2443 pub const Footer = struct { 2444 focus: *Focus, 2445 grid: ?Grid, 2446 session: *Session, 2447 arena: std.heap.ArenaAllocator, 2448 url: []const u8 = "", 2449 url_width: usize = 0, 2450 2451 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2452 return .{ 2453 .focus = try Focus.create(allocator, .container), 2454 .grid = null, 2455 .session = session, 2456 .arena = std.heap.ArenaAllocator.init(allocator), 2457 }; 2458 } 2459 2460 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2461 self.focus.destroy(allocator); 2462 if (self.grid) |*grid| grid.deinit(); 2463 self.arena.deinit(); 2464 } 2465 2466 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2467 _ = root_focus; 2468 self.clearGrid(); 2469 const width = constraint.max_size.width orelse return; 2470 if (width == 0) return; 2471 2472 _ = self.arena.reset(.retain_capacity); 2473 self.url = ""; 2474 self.url_width = 0; 2475 const aa = self.arena.allocator(); 2476 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2477 const text = terminalWebUrl(aa, self.session, path) catch return; 2478 self.url = text; 2479 2480 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2481 errdefer grid.deinit(); 2482 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2483 var i: usize = 0; 2484 while (utf8.nextCodepointSlice()) |ch| { 2485 if (i == width) break; 2486 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2487 i += 1; 2488 } 2489 self.url_width = i; 2490 self.grid = grid; 2491 } 2492 2493 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2494 _ = allocator; 2495 switch (key) { 2496 .mouse => |mouse| { 2497 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 2498 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 2499 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 2500 }, 2501 else => {}, 2502 } 2503 } 2504 2505 pub fn clearGrid(self: *Footer) void { 2506 if (self.grid) |*grid| { 2507 grid.deinit(); 2508 self.grid = null; 2509 } 2510 } 2511 2512 pub fn getGrid(self: Footer) ?Grid { 2513 return self.grid; 2514 } 2515 2516 pub fn getFocus(self: *Footer) *Focus { 2517 return self.focus; 2518 } 2519 }; 2520 2521 // a single-child wrapper that builds the child at its natural size and 2522 // positions its grid in the middle of the area granted by the parent 2523 // a bordered label, indented past the content it precedes 2524 pub const SectionLabel = struct { 2525 box: wgt.Box(Widget), 2526 2527 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2528 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2529 errdefer box.deinit(allocator); 2530 2531 { 2532 var gap = try wgt.Text(Widget).init(allocator, " "); 2533 errdefer gap.deinit(allocator); 2534 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2535 } 2536 2537 { 2538 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2539 errdefer tb.deinit(allocator); 2540 tb.getFocus().focusable = true; 2541 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2542 } 2543 2544 box.getFocus().child_id = box.children.keys()[1]; 2545 return .{ .box = box }; 2546 } 2547 2548 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2549 self.box.deinit(allocator); 2550 } 2551 2552 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2553 try self.box.build(allocator, constraint, root_focus); 2554 } 2555 2556 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2557 _ = self; 2558 _ = allocator; 2559 _ = key; 2560 _ = root_focus; 2561 } 2562 2563 pub fn clearGrid(self: *SectionLabel) void { 2564 self.box.clearGrid(); 2565 } 2566 2567 pub fn getGrid(self: SectionLabel) ?Grid { 2568 return self.box.getGrid(); 2569 } 2570 2571 pub fn getFocus(self: *SectionLabel) *Focus { 2572 return self.box.getFocus(); 2573 } 2574 }; 2575 2576 // a submit button, indented past its form's content. the web overlay POSTs 2577 // the button's form; terminal hosts detect presses through `buttonId`. 2578 pub const SubmitButton = struct { 2579 box: wgt.Box(Widget), 2580 2581 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2582 return initLabeled(allocator, "submit"); 2583 } 2584 2585 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2586 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2587 errdefer box.deinit(allocator); 2588 2589 { 2590 var gap = try wgt.Text(Widget).init(allocator, " "); 2591 errdefer gap.deinit(allocator); 2592 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2593 } 2594 2595 { 2596 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2597 errdefer button.deinit(allocator); 2598 button.getFocus().focusable = true; 2599 // the renderer distinguishes plain clickables from buttons that 2600 // should POST to a server route by this kind. 2601 button.getFocus().kind = .{ .custom = "submit" }; 2602 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2603 } 2604 2605 box.getFocus().child_id = box.children.keys()[1]; 2606 return .{ .box = box }; 2607 } 2608 2609 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2610 self.box.deinit(allocator); 2611 } 2612 2613 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2614 try self.box.build(allocator, constraint, root_focus); 2615 } 2616 2617 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2618 _ = self; 2619 _ = allocator; 2620 _ = key; 2621 _ = root_focus; 2622 } 2623 2624 pub fn clearGrid(self: *SubmitButton) void { 2625 self.box.clearGrid(); 2626 } 2627 2628 pub fn getGrid(self: SubmitButton) ?Grid { 2629 return self.box.getGrid(); 2630 } 2631 2632 pub fn getFocus(self: *SubmitButton) *Focus { 2633 return self.box.getFocus(); 2634 } 2635 2636 // the button's focus id, for click hit-testing 2637 pub fn buttonId(self: *const SubmitButton) usize { 2638 return self.box.children.keys()[1]; 2639 } 2640 }; 2641 2642 pub const Center = struct { 2643 focus: *Focus, 2644 grid: ?Grid, 2645 child: *Widget, 2646 2647 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2648 const child = try allocator.create(Widget); 2649 errdefer allocator.destroy(child); 2650 child.* = child_widget; 2651 return .{ 2652 .focus = try Focus.create(allocator, .container), 2653 .grid = null, 2654 .child = child, 2655 }; 2656 } 2657 2658 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2659 self.focus.destroy(allocator); 2660 if (self.grid) |*grid| { 2661 grid.deinit(); 2662 self.grid = null; 2663 } 2664 self.child.deinit(allocator); 2665 allocator.destroy(self.child); 2666 } 2667 2668 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2669 self.clearGrid(); 2670 self.getFocus().clear(); 2671 2672 // build the child without forcing it to fill min_size; it sizes 2673 // itself to its content within the available max. 2674 try self.child.build(allocator, .{ 2675 .min_size = .{ .width = null, .height = null }, 2676 .max_size = constraint.max_size, 2677 }, root_focus); 2678 2679 const child_grid = self.child.getGrid() orelse return; 2680 2681 // prefer max when given; otherwise grow to max(min, child) so that 2682 // when only a min is set (e.g. the wasm path passes viewport rows 2683 // as min_height with no max), we can still vertically center while 2684 // letting taller content extend past the min. 2685 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; 2686 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; 2687 if (width == 0 or height == 0) return; 2688 2689 const offset_x: usize = (width -| child_grid.size.width) / 2; 2690 const offset_y: usize = (height -| child_grid.size.height) / 2; 2691 2692 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2693 errdefer grid.deinit(); 2694 try grid.drawGrid(child_grid, offset_x, offset_y); 2695 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2696 self.getFocus().child_id = self.child.getFocus().id; 2697 2698 self.grid = grid; 2699 } 2700 2701 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2702 try self.child.input(allocator, key, root_focus); 2703 } 2704 2705 pub fn clearGrid(self: *Center) void { 2706 if (self.grid) |*grid| { 2707 grid.deinit(); 2708 self.grid = null; 2709 } 2710 self.child.clearGrid(); 2711 } 2712 2713 pub fn getGrid(self: Center) ?Grid { 2714 return self.grid; 2715 } 2716 2717 pub fn getFocus(self: *Center) *Focus { 2718 return self.focus; 2719 } 2720 }; 2721 2722 // renders truecolor ANSI art 2723 pub const AnsiArt = struct { 2724 focus: *Focus, 2725 grid: ?Grid, 2726 content: []const u8, 2727 2728 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2729 return .{ 2730 .focus = try Focus.create(allocator, .container), 2731 .grid = null, 2732 .content = content, 2733 }; 2734 } 2735 2736 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2737 self.focus.destroy(allocator); 2738 if (self.grid) |*grid| { 2739 grid.deinit(); 2740 self.grid = null; 2741 } 2742 } 2743 2744 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2745 _ = root_focus; 2746 self.clearGrid(); 2747 2748 // parse into rows of cells, then copy into a rectangular grid 2749 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2750 defer { 2751 for (rows.items) |*row| row.deinit(allocator); 2752 rows.deinit(allocator); 2753 } 2754 var row: std.ArrayList(Grid.Cell) = .empty; 2755 errdefer row.deinit(allocator); 2756 2757 var style: Grid.Style = .{}; 2758 var width: usize = 0; 2759 const content = self.content; 2760 var i: usize = 0; 2761 while (i < content.len) { 2762 const byte = content[i]; 2763 if (byte == '\n') { 2764 width = @max(width, row.items.len); 2765 try rows.append(allocator, row); 2766 row = .empty; 2767 i += 1; 2768 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2769 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2770 var j = i + 2; 2771 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2772 if (j >= content.len) { 2773 i = content.len; // malformed trailing escape; stop 2774 } else { 2775 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2776 i = j + 1; 2777 } 2778 } else { 2779 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2780 const end = @min(content.len, i + len); 2781 const rune = content[i..end]; 2782 const transparent = end - i == 1 and rune[0] == ' ' and 2783 style.fg == null and style.bg == null and !style.inverted; 2784 try row.append(allocator, .{ 2785 .rune = if (transparent) null else rune, 2786 .style = style, 2787 }); 2788 i = end; 2789 } 2790 } 2791 // a trailing row with content but no closing newline 2792 if (row.items.len > 0) { 2793 width = @max(width, row.items.len); 2794 try rows.append(allocator, row); 2795 } else { 2796 row.deinit(allocator); 2797 } 2798 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2799 2800 const height = rows.items.len; 2801 if (width == 0 or height == 0) return; 2802 2803 const clamped_w = @min(width, constraint.max_size.width orelse width); 2804 const clamped_h = @min(height, constraint.max_size.height orelse height); 2805 2806 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2807 errdefer grid.deinit(); 2808 for (rows.items[0..clamped_h], 0..) |r, y| { 2809 const n = @min(r.items.len, clamped_w); 2810 for (r.items[0..n], 0..) |cell, x| { 2811 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2812 } 2813 } 2814 self.grid = grid; 2815 } 2816 2817 pub fn clearGrid(self: *AnsiArt) void { 2818 if (self.grid) |*grid| { 2819 grid.deinit(); 2820 self.grid = null; 2821 } 2822 } 2823 2824 pub fn getGrid(self: AnsiArt) ?Grid { 2825 return self.grid; 2826 } 2827 2828 pub fn getFocus(self: *AnsiArt) *Focus { 2829 return self.focus; 2830 } 2831 2832 fn applySgr(style: *Grid.Style, params: []const u8) void { 2833 var nums: [16]u32 = undefined; 2834 var n: usize = 0; 2835 var it = std.mem.splitScalar(u8, params, ';'); 2836 while (it.next()) |tok| { 2837 if (n >= nums.len) break; 2838 // an empty parameter (e.g. bare "\x1b[m") means 0 2839 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2840 n += 1; 2841 } 2842 if (n == 0) { 2843 style.* = .{}; 2844 return; 2845 } 2846 var i: usize = 0; 2847 while (i < n) : (i += 1) { 2848 switch (nums[i]) { 2849 0 => style.* = .{}, 2850 7 => style.inverted = true, 2851 27 => style.inverted = false, 2852 39 => style.fg = null, 2853 49 => style.bg = null, 2854 38, 48 => { 2855 // truecolor form: 38;2;r;g;b — anything else is ignored 2856 if (i + 4 < n and nums[i + 1] == 2) { 2857 const c = Grid.Color{ 2858 .r = @truncate(nums[i + 2]), 2859 .g = @truncate(nums[i + 3]), 2860 .b = @truncate(nums[i + 4]), 2861 }; 2862 if (nums[i] == 38) style.fg = c else style.bg = c; 2863 i += 4; 2864 } 2865 }, 2866 else => {}, 2867 } 2868 } 2869 } 2870 }; 2871 2872 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2873 pub const AnsiBackground = struct { 2874 grid: ?Grid, 2875 child: *Widget, 2876 art: AnsiArt, 2877 session: *Session, 2878 2879 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2880 var cw = child_widget; 2881 const child = allocator.create(Widget) catch |e| { 2882 cw.deinit(allocator); 2883 return e; 2884 }; 2885 child.* = cw; 2886 errdefer { 2887 child.deinit(allocator); 2888 allocator.destroy(child); 2889 } 2890 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2891 } 2892 2893 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2894 if (self.grid) |*grid| grid.deinit(); 2895 self.art.deinit(allocator); 2896 self.child.deinit(allocator); 2897 allocator.destroy(self.child); 2898 } 2899 2900 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2901 self.clearGrid(); 2902 2903 // make the wrapped page fill the whole available area 2904 try self.child.build(allocator, .{ 2905 .min_size = .{ 2906 .width = constraint.max_size.width orelse constraint.min_size.width, 2907 .height = constraint.max_size.height orelse constraint.min_size.height, 2908 }, 2909 .max_size = constraint.max_size, 2910 }, root_focus); 2911 2912 if (self.session.data.enable_ansi) { 2913 if (self.child.getGrid()) |fg| { 2914 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2915 } 2916 } 2917 } 2918 2919 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2920 try self.child.input(allocator, key, root_focus); 2921 } 2922 2923 pub fn clearGrid(self: *AnsiBackground) void { 2924 if (self.grid) |*grid| { 2925 grid.deinit(); 2926 self.grid = null; 2927 } 2928 self.child.clearGrid(); 2929 self.art.clearGrid(); 2930 } 2931 2932 pub fn getGrid(self: AnsiBackground) ?Grid { 2933 return self.grid orelse self.child.getGrid(); 2934 } 2935 2936 pub fn getFocus(self: *AnsiBackground) *Focus { 2937 return self.child.getFocus(); 2938 } 2939 2940 // which top corner the art hugs. 2941 const ArtAnchor = enum { top_left, top_right }; 2942 2943 // a foreground cell counts as blank if it's empty or a bare space without styling 2944 fn cellIsBlank(cell: Grid.Cell) bool { 2945 if (cell.rune) |rune| { 2946 return std.mem.eql(u8, rune, " ") and 2947 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2948 } 2949 return true; 2950 } 2951 2952 // the art is dimmed to this fraction of its brightness so foreground text 2953 // stays legible over it 2954 const art_brightness = 45; // percent 2955 2956 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2957 const c = color orelse return null; 2958 return .{ 2959 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2960 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2961 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2962 }; 2963 } 2964 2965 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2966 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2967 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2968 // snap such channels clear of that range so the backdrop doesn't blink or 2969 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2970 // actually render truecolor. 2971 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2972 const c = color orelse return null; 2973 const snap = struct { 2974 fn f(v: u8) u8 { 2975 return if (v >= 1 and v <= 9) 10 else v; 2976 } 2977 }.f; 2978 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2979 } 2980 2981 // composites ANSI art behind a foreground grid 2982 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2983 art.clearGrid(); 2984 try art.build(allocator, .{ 2985 .min_size = .{ .width = null, .height = null }, 2986 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2987 }, root_focus); 2988 2989 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2990 errdefer out.deinit(); 2991 2992 if (art.getGrid()) |art_grid| { 2993 const anchor_x = switch (anchor) { 2994 .top_left => 0, 2995 .top_right => foreground.size.width -| art_grid.size.width, 2996 }; 2997 for (0..art_grid.size.height) |y| { 2998 for (0..art_grid.size.width) |x| { 2999 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 3000 if (src.rune == null) continue; 3001 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 3002 const dst = &out.cells.items[idx]; 3003 // composite the dimmed art behind every cell so it shows 3004 // through the whole UI: a blank cell becomes the art, while a 3005 // cell with a glyph keeps the glyph but takes the dimmed art as 3006 // its background (replacing whatever background it had, which 3007 // would otherwise obscure the art). 3008 if (cellIsBlank(dst.*)) { 3009 dst.* = src; 3010 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 3011 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 3012 } else { 3013 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 3014 dst.style.bg = background_maybe; 3015 // change the color of text overlaying ANSI art to ensure 3016 // it is readable. don't do this in the web UI, since we 3017 // have direct control of the theme there (also, inner 3018 // scroll panes allow text to move without a re-render 3019 // by the TUI, which would break this). also don't do this 3020 // if the text is explicitly styled. 3021 if (is_terminal and dst.style.fg == null) { 3022 if (background_maybe) |background| { 3023 // use near-black and near-white, which is easier on the eyes 3024 // while retaining high contrast 3025 const luminance = (@as(u32, background.r) * 299 + 3026 @as(u32, background.g) * 587 + 3027 @as(u32, background.b) * 114) / 1000; 3028 dst.style.fg = if (luminance >= 128) 3029 .{ .r = 16, .g = 16, .b = 16 } 3030 else 3031 .{ .r = 240, .g = 240, .b = 240 }; 3032 } 3033 } 3034 } 3035 } 3036 } 3037 } 3038 return out; 3039 } 3040 }; 3041