╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2544 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 2545 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 2546 } 2547 2548 self.grid = grid; 2549 } 2550 2551 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2552 _ = self; 2553 _ = allocator; 2554 _ = key; 2555 _ = root_focus; 2556 } 2557 2558 pub fn clearGrid(self: *TagFlow) void { 2559 if (self.grid) |*grid| { 2560 grid.deinit(); 2561 self.grid = null; 2562 } 2563 for (self.text_boxes.items) |*tb| tb.clearGrid(); 2564 } 2565 2566 pub fn getGrid(self: TagFlow) ?Grid { 2567 return self.grid; 2568 } 2569 2570 pub fn getFocus(self: *TagFlow) *Focus { 2571 return self.focus; 2572 } 2573 2574 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 2575 for (self.text_boxes.items, 0..) |tb, i| { 2576 if (tb.box.focus.id == focus_id) return i; 2577 } 2578 return null; 2579 } 2580 2581 // the first item of the row below (when `downward`) or above, or null at 2582 // the flow's edge. items are laid out in order, so y is nondecreasing. 2583 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 2584 const rects = self.rects.items; 2585 if (index >= rects.len) return null; 2586 const cur_y = rects[index].y; 2587 if (downward) { 2588 for (rects[index + 1 ..], index + 1..) |r, i| { 2589 if (r.y > cur_y) return i; 2590 } 2591 return null; 2592 } 2593 // walk the earlier rows; the last one visited is the row above. 2594 var prev_first: ?usize = null; 2595 var i: usize = 0; 2596 while (i < rects.len and rects[i].y < cur_y) { 2597 prev_first = i; 2598 const y = rects[i].y; 2599 while (i < rects.len and rects[i].y == y) i += 1; 2600 } 2601 return prev_first; 2602 } 2603 }; 2604 2605 // an invisible widget that fills the horizontal space granted by its parent. 2606 // used inside Box(horiz) with a min_size so the box reserves space for the 2607 // children that follow, pushing them to the right. 2608 pub const Spacer = struct { 2609 focus: *Focus, 2610 grid: ?Grid, 2611 2612 pub fn init(allocator: std.mem.Allocator) !Spacer { 2613 return .{ 2614 .focus = try Focus.create(allocator, .container), 2615 .grid = null, 2616 }; 2617 } 2618 2619 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 2620 self.focus.destroy(allocator); 2621 if (self.grid) |*grid| { 2622 grid.deinit(); 2623 self.grid = null; 2624 } 2625 } 2626 2627 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2628 _ = root_focus; 2629 self.clearGrid(); 2630 const width = constraint.max_size.width orelse return; 2631 if (width == 0) return; 2632 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2633 } 2634 2635 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2636 _ = self; 2637 _ = allocator; 2638 _ = key; 2639 _ = root_focus; 2640 } 2641 2642 pub fn clearGrid(self: *Spacer) void { 2643 if (self.grid) |*grid| { 2644 grid.deinit(); 2645 self.grid = null; 2646 } 2647 } 2648 2649 pub fn getGrid(self: Spacer) ?Grid { 2650 return self.grid; 2651 } 2652 2653 pub fn getFocus(self: *Spacer) *Focus { 2654 return self.focus; 2655 } 2656 }; 2657 2658 // a one-row, non-focusable status bar showing the current page's url 2659 pub const Footer = struct { 2660 focus: *Focus, 2661 grid: ?Grid, 2662 session: *Session, 2663 arena: std.heap.ArenaAllocator, 2664 url: []const u8 = "", 2665 url_width: usize = 0, 2666 2667 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2668 return .{ 2669 .focus = try Focus.create(allocator, .container), 2670 .grid = null, 2671 .session = session, 2672 .arena = std.heap.ArenaAllocator.init(allocator), 2673 }; 2674 } 2675 2676 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2677 self.focus.destroy(allocator); 2678 if (self.grid) |*grid| grid.deinit(); 2679 self.arena.deinit(); 2680 } 2681 2682 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2683 _ = root_focus; 2684 self.clearGrid(); 2685 const width = constraint.max_size.width orelse return; 2686 if (width == 0) return; 2687 2688 _ = self.arena.reset(.retain_capacity); 2689 self.url = ""; 2690 self.url_width = 0; 2691 const aa = self.arena.allocator(); 2692 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2693 const text = terminalWebUrl(aa, self.session, path) catch return; 2694 self.url = text; 2695 2696 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2697 errdefer grid.deinit(); 2698 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2699 var i: usize = 0; 2700 while (utf8.nextCodepointSlice()) |ch| { 2701 if (i == width) break; 2702 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2703 i += 1; 2704 } 2705 self.url_width = i; 2706 self.grid = grid; 2707 } 2708 2709 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2710 _ = allocator; 2711 switch (key) { 2712 .mouse => |mouse| { 2713 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 2714 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 2715 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 2716 }, 2717 else => {}, 2718 } 2719 } 2720 2721 pub fn clearGrid(self: *Footer) void { 2722 if (self.grid) |*grid| { 2723 grid.deinit(); 2724 self.grid = null; 2725 } 2726 } 2727 2728 pub fn getGrid(self: Footer) ?Grid { 2729 return self.grid; 2730 } 2731 2732 pub fn getFocus(self: *Footer) *Focus { 2733 return self.focus; 2734 } 2735 }; 2736 2737 // a single-child wrapper that builds the child at its natural size and 2738 // positions its grid in the middle of the area granted by the parent 2739 // a bordered label, indented past the content it precedes 2740 pub const SectionLabel = struct { 2741 box: wgt.Box(Widget), 2742 2743 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2744 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2745 errdefer box.deinit(allocator); 2746 2747 { 2748 var gap = try wgt.Text(Widget).init(allocator, " "); 2749 errdefer gap.deinit(allocator); 2750 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2751 } 2752 2753 { 2754 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2755 errdefer tb.deinit(allocator); 2756 tb.getFocus().focusable = true; 2757 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2758 } 2759 2760 box.getFocus().child_id = box.children.keys()[1]; 2761 return .{ .box = box }; 2762 } 2763 2764 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2765 self.box.deinit(allocator); 2766 } 2767 2768 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2769 try self.box.build(allocator, constraint, root_focus); 2770 } 2771 2772 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2773 _ = self; 2774 _ = allocator; 2775 _ = key; 2776 _ = root_focus; 2777 } 2778 2779 pub fn clearGrid(self: *SectionLabel) void { 2780 self.box.clearGrid(); 2781 } 2782 2783 pub fn getGrid(self: SectionLabel) ?Grid { 2784 return self.box.getGrid(); 2785 } 2786 2787 pub fn getFocus(self: *SectionLabel) *Focus { 2788 return self.box.getFocus(); 2789 } 2790 }; 2791 2792 // a submit button, indented past its form's content. the web overlay POSTs 2793 // the button's form; terminal hosts detect presses through `buttonId`. 2794 pub const SubmitButton = struct { 2795 box: wgt.Box(Widget), 2796 2797 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2798 return initLabeled(allocator, "submit"); 2799 } 2800 2801 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2802 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2803 errdefer box.deinit(allocator); 2804 2805 { 2806 var gap = try wgt.Text(Widget).init(allocator, " "); 2807 errdefer gap.deinit(allocator); 2808 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2809 } 2810 2811 { 2812 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2813 errdefer button.deinit(allocator); 2814 button.getFocus().focusable = true; 2815 // the renderer distinguishes plain clickables from buttons that 2816 // should POST to a server route by this kind. 2817 button.getFocus().kind = .{ .custom = "submit" }; 2818 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2819 } 2820 2821 box.getFocus().child_id = box.children.keys()[1]; 2822 return .{ .box = box }; 2823 } 2824 2825 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2826 self.box.deinit(allocator); 2827 } 2828 2829 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2830 try self.box.build(allocator, constraint, root_focus); 2831 } 2832 2833 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2834 _ = self; 2835 _ = allocator; 2836 _ = key; 2837 _ = root_focus; 2838 } 2839 2840 pub fn clearGrid(self: *SubmitButton) void { 2841 self.box.clearGrid(); 2842 } 2843 2844 pub fn getGrid(self: SubmitButton) ?Grid { 2845 return self.box.getGrid(); 2846 } 2847 2848 pub fn getFocus(self: *SubmitButton) *Focus { 2849 return self.box.getFocus(); 2850 } 2851 2852 // the button's focus id, for click hit-testing 2853 pub fn buttonId(self: *const SubmitButton) usize { 2854 return self.box.children.keys()[1]; 2855 } 2856 }; 2857 2858 pub const Center = struct { 2859 focus: *Focus, 2860 grid: ?Grid, 2861 child: *Widget, 2862 2863 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2864 const child = try allocator.create(Widget); 2865 errdefer allocator.destroy(child); 2866 child.* = child_widget; 2867 return .{ 2868 .focus = try Focus.create(allocator, .container), 2869 .grid = null, 2870 .child = child, 2871 }; 2872 } 2873 2874 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2875 self.focus.destroy(allocator); 2876 if (self.grid) |*grid| { 2877 grid.deinit(); 2878 self.grid = null; 2879 } 2880 self.child.deinit(allocator); 2881 allocator.destroy(self.child); 2882 } 2883 2884 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2885 self.clearGrid(); 2886 self.getFocus().clear(); 2887 2888 // build the child without forcing it to fill min_size; it sizes 2889 // itself to its content within the available max. 2890 try self.child.build(allocator, .{ 2891 .min_size = .{ .width = null, .height = null }, 2892 .max_size = constraint.max_size, 2893 }, root_focus); 2894 2895 const child_grid = self.child.getGrid() orelse return; 2896 2897 // prefer max when given; otherwise grow to max(min, child) so that 2898 // when only a min is set (e.g. the wasm path passes viewport rows 2899 // as min_height with no max), we can still vertically center while 2900 // letting taller content extend past the min. 2901 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; 2902 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; 2903 if (width == 0 or height == 0) return; 2904 2905 const offset_x: usize = (width -| child_grid.size.width) / 2; 2906 const offset_y: usize = (height -| child_grid.size.height) / 2; 2907 2908 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2909 errdefer grid.deinit(); 2910 try grid.drawGrid(child_grid, offset_x, offset_y); 2911 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2912 self.getFocus().child_id = self.child.getFocus().id; 2913 2914 self.grid = grid; 2915 } 2916 2917 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2918 try self.child.input(allocator, key, root_focus); 2919 } 2920 2921 pub fn clearGrid(self: *Center) void { 2922 if (self.grid) |*grid| { 2923 grid.deinit(); 2924 self.grid = null; 2925 } 2926 self.child.clearGrid(); 2927 } 2928 2929 pub fn getGrid(self: Center) ?Grid { 2930 return self.grid; 2931 } 2932 2933 pub fn getFocus(self: *Center) *Focus { 2934 return self.focus; 2935 } 2936 }; 2937 2938 // renders truecolor ANSI art 2939 pub const AnsiArt = struct { 2940 focus: *Focus, 2941 grid: ?Grid, 2942 content: []const u8, 2943 2944 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2945 return .{ 2946 .focus = try Focus.create(allocator, .container), 2947 .grid = null, 2948 .content = content, 2949 }; 2950 } 2951 2952 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2953 self.focus.destroy(allocator); 2954 if (self.grid) |*grid| { 2955 grid.deinit(); 2956 self.grid = null; 2957 } 2958 } 2959 2960 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2961 _ = root_focus; 2962 self.clearGrid(); 2963 2964 // parse into rows of cells, then copy into a rectangular grid 2965 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2966 defer { 2967 for (rows.items) |*row| row.deinit(allocator); 2968 rows.deinit(allocator); 2969 } 2970 var row: std.ArrayList(Grid.Cell) = .empty; 2971 errdefer row.deinit(allocator); 2972 2973 var style: Grid.Style = .{}; 2974 var width: usize = 0; 2975 const content = self.content; 2976 var i: usize = 0; 2977 while (i < content.len) { 2978 const byte = content[i]; 2979 if (byte == '\n') { 2980 width = @max(width, row.items.len); 2981 try rows.append(allocator, row); 2982 row = .empty; 2983 i += 1; 2984 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2985 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2986 var j = i + 2; 2987 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2988 if (j >= content.len) { 2989 i = content.len; // malformed trailing escape; stop 2990 } else { 2991 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2992 i = j + 1; 2993 } 2994 } else { 2995 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2996 const end = @min(content.len, i + len); 2997 const rune = content[i..end]; 2998 // a foreground color does not make a space visible. only an 2999 // effective background makes it opaque; foreground state often 3000 // remains active through the padding at the end of a row. 3001 const effective_background = if (style.inverted) style.fg else style.bg; 3002 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 3003 try row.append(allocator, .{ 3004 .rune = if (transparent) null else rune, 3005 .style = style, 3006 }); 3007 i = end; 3008 } 3009 } 3010 // a trailing row with content but no closing newline 3011 if (row.items.len > 0) { 3012 width = @max(width, row.items.len); 3013 try rows.append(allocator, row); 3014 } else { 3015 row.deinit(allocator); 3016 } 3017 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 3018 3019 const height = rows.items.len; 3020 if (width == 0 or height == 0) return; 3021 3022 const clamped_w = @min(width, constraint.max_size.width orelse width); 3023 const clamped_h = @min(height, constraint.max_size.height orelse height); 3024 3025 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 3026 errdefer grid.deinit(); 3027 for (rows.items[0..clamped_h], 0..) |r, y| { 3028 const n = @min(r.items.len, clamped_w); 3029 for (r.items[0..n], 0..) |cell, x| { 3030 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 3031 } 3032 } 3033 self.grid = grid; 3034 } 3035 3036 pub fn clearGrid(self: *AnsiArt) void { 3037 if (self.grid) |*grid| { 3038 grid.deinit(); 3039 self.grid = null; 3040 } 3041 } 3042 3043 pub fn getGrid(self: AnsiArt) ?Grid { 3044 return self.grid; 3045 } 3046 3047 pub fn getFocus(self: *AnsiArt) *Focus { 3048 return self.focus; 3049 } 3050 3051 fn applySgr(style: *Grid.Style, params: []const u8) void { 3052 var nums: [16]u32 = undefined; 3053 var n: usize = 0; 3054 var it = std.mem.splitScalar(u8, params, ';'); 3055 while (it.next()) |tok| { 3056 if (n >= nums.len) break; 3057 // an empty parameter (e.g. bare "\x1b[m") means 0 3058 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 3059 n += 1; 3060 } 3061 if (n == 0) { 3062 style.* = .{}; 3063 return; 3064 } 3065 var i: usize = 0; 3066 while (i < n) : (i += 1) { 3067 switch (nums[i]) { 3068 0 => style.* = .{}, 3069 7 => style.inverted = true, 3070 27 => style.inverted = false, 3071 39 => style.fg = null, 3072 49 => style.bg = null, 3073 38, 48 => { 3074 // truecolor form: 38;2;r;g;b — anything else is ignored 3075 if (i + 4 < n and nums[i + 1] == 2) { 3076 const c = Grid.Color{ 3077 .r = @truncate(nums[i + 2]), 3078 .g = @truncate(nums[i + 3]), 3079 .b = @truncate(nums[i + 4]), 3080 }; 3081 if (nums[i] == 38) style.fg = c else style.bg = c; 3082 i += 4; 3083 } 3084 }, 3085 else => {}, 3086 } 3087 } 3088 } 3089 }; 3090 3091 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 3092 pub const AnsiBackground = struct { 3093 grid: ?Grid, 3094 child: *Widget, 3095 art: AnsiArt, 3096 session: *Session, 3097 3098 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *Session) !AnsiBackground { 3099 var cw = child_widget; 3100 const child = allocator.create(Widget) catch |e| { 3101 cw.deinit(allocator); 3102 return e; 3103 }; 3104 child.* = cw; 3105 errdefer { 3106 child.deinit(allocator); 3107 allocator.destroy(child); 3108 } 3109 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 3110 } 3111 3112 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 3113 if (self.grid) |*grid| grid.deinit(); 3114 self.art.deinit(allocator); 3115 self.child.deinit(allocator); 3116 allocator.destroy(self.child); 3117 } 3118 3119 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 3120 self.clearGrid(); 3121 3122 // make the wrapped page fill the whole available area 3123 try self.child.build(allocator, .{ 3124 .min_size = .{ 3125 .width = constraint.max_size.width orelse constraint.min_size.width, 3126 .height = constraint.max_size.height orelse constraint.min_size.height, 3127 }, 3128 .max_size = constraint.max_size, 3129 }, root_focus); 3130 3131 if (!self.session.data.enable_ansi) return; 3132 const foreground = self.child.getGrid() orelse return; 3133 self.art.content = self.session.data.ansi_art; 3134 try self.buildArt(allocator, foreground.size, root_focus); 3135 if (!self.session.is_terminal) return; 3136 const art_grid = self.art.getGrid() orelse return; 3137 self.grid = try artBehind(allocator, foreground, art_grid); 3138 } 3139 3140 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 3141 try self.child.input(allocator, key, root_focus); 3142 } 3143 3144 pub fn clearGrid(self: *AnsiBackground) void { 3145 if (self.grid) |*grid| { 3146 grid.deinit(); 3147 self.grid = null; 3148 } 3149 self.child.clearGrid(); 3150 self.art.clearGrid(); 3151 } 3152 3153 pub fn getGrid(self: AnsiBackground) ?Grid { 3154 return self.grid orelse self.child.getGrid(); 3155 } 3156 3157 pub fn getFocus(self: *AnsiBackground) *Focus { 3158 return self.child.getFocus(); 3159 } 3160 3161 // a foreground cell counts as blank if it's empty or a bare space without styling 3162 fn cellIsBlank(cell: Grid.Cell) bool { 3163 if (cell.rune) |rune| { 3164 return std.mem.eql(u8, rune, " ") and 3165 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 3166 } 3167 return true; 3168 } 3169 3170 // the art is dimmed to this fraction of its brightness so foreground text 3171 // stays legible over it 3172 const art_brightness = 35; // percent 3173 3174 fn dimColor(color: ?Grid.Color) ?Grid.Color { 3175 const c = color orelse return null; 3176 return .{ 3177 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 3178 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 3179 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 3180 }; 3181 } 3182 3183 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 3184 try self.art.build(allocator, .{ 3185 .min_size = .{ .width = null, .height = null }, 3186 .max_size = .{ .width = size.width, .height = size.height }, 3187 }, root_focus); 3188 if (self.art.grid) |*grid| { 3189 for (grid.cells.items) |*cell| { 3190 cell.style.fg = dimColor(cell.style.fg); 3191 cell.style.bg = dimColor(cell.style.bg); 3192 } 3193 } 3194 } 3195 3196 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 3197 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 3198 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 3199 // snap such channels clear of that range so the backdrop doesn't blink or 3200 // hide text there; the ≤9/255 shift is imperceptible on terminals that 3201 // actually render truecolor. 3202 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 3203 const c = color orelse return null; 3204 const snap = struct { 3205 fn f(v: u8) u8 { 3206 return if (v >= 1 and v <= 9) 10 else v; 3207 } 3208 }.f; 3209 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 3210 } 3211 3212 // composites ANSI art behind a foreground grid 3213 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 3214 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 3215 errdefer out.deinit(); 3216 3217 const anchor_x = foreground.size.width -| art_grid.size.width; 3218 for (0..art_grid.size.height) |y| { 3219 for (0..art_grid.size.width) |x| { 3220 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 3221 if (src.rune == null) continue; 3222 const idx = try out.cells.at(.{ y, anchor_x + x }); 3223 const dst = &out.cells.items[idx]; 3224 // blank cells take the art; occupied cells take its background 3225 if (cellIsBlank(dst.*)) { 3226 dst.* = src; 3227 dst.style.fg = sgrSafe(dst.style.fg); 3228 dst.style.bg = sgrSafe(dst.style.bg); 3229 } else { 3230 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 3231 dst.style.bg = background_maybe; 3232 // change unstyled text to contrast with the art behind it 3233 if (dst.style.fg == null) { 3234 if (background_maybe) |background| { 3235 // use near-black and near-white, which is easier on the eyes 3236 // while retaining high contrast 3237 const luminance = (@as(u32, background.r) * 299 + 3238 @as(u32, background.g) * 587 + 3239 @as(u32, background.b) * 114) / 1000; 3240 dst.style.fg = if (luminance >= 128) 3241 .{ .r = 16, .g = 16, .b = 16 } 3242 else 3243 .{ .r = 240, .g = 240, .b = 240 }; 3244 } 3245 } 3246 } 3247 } 3248 } 3249 return out; 3250 } 3251 }; 3252