╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2541 try nav.root.build(allocator, .{ 2542 .min_size = .{ .width = null, .height = null }, 2543 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2544 }, nav.root.getFocus()); 2545 _ = try terminal.render(&nav.root); 2546 try Repo.Events.performSync(allocator, session); 2547 }, 2548 } 2549 } 2550 2551 // persist queued actions when there's a repo; local mode has none, 2552 // so just apply in-memory 2553 if (repo_maybe) |repo| { 2554 try session.applyAndWritePending(io, allocator, repo); 2555 // reload so the next navigation builds its page from the current moment 2556 try session.reloadMoment(allocator, repo); 2557 } else { 2558 session.applyPending(); 2559 } 2560 2561 // reconcile navigation: forward to a new page, or back on escape. 2562 try nav.sync(allocator, session); 2563 2564 // the quit button (on the quit tab) asks the host to tear down. 2565 if (session.quit_requested) terminal.requestQuit(); 2566 2567 try nav.root.build(allocator, .{ 2568 .min_size = .{ .width = null, .height = null }, 2569 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2570 }, nav.root.getFocus()); 2571 } 2572 } 2573 2574 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2575 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2576 null 2577 else 2578 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2579 defer if (tty) |file| file.close(io); 2580 2581 const input = if (tty) |file| file else std.Io.File.stdin(); 2582 const output = if (tty) |file| file else std.Io.File.stdout(); 2583 2584 var write_buf: [1024]u8 = undefined; 2585 var writer = output.writer(io, &write_buf); 2586 try writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 2587 try writer.interface.flush(); 2588 2589 var read_buf: [1]u8 = undefined; 2590 var reader = input.reader(io, &read_buf); 2591 while (true) { 2592 const byte = try reader.interface.takeByte(); 2593 if (byte == '\r' or byte == '\n') return; 2594 } 2595 } 2596 2597 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2598 const root_focus = root.getFocus(); 2599 const focused_before = root_focus.grandchild_id; 2600 var activate_in_page_link = false; 2601 switch (key) { 2602 // request a navigation pop; the host's Nav.sync goes back a page, or 2603 // quits when there's no history left. 2604 .escape => { 2605 session.back = .requested; 2606 return; 2607 }, 2608 .ctrl => |letter| switch (letter) { 2609 'r' => { 2610 session.refresh_requested = true; 2611 return; 2612 }, 2613 else => try root.input(allocator, key, root_focus), 2614 }, 2615 .enter => { 2616 if (root_focus.grandchild_id) |gid| { 2617 // follow a cross-page link 2618 if (crossPageLink(root_focus, gid, session.data)) |route| { 2619 return session.navigate(route); 2620 } 2621 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2622 } 2623 activate_in_page_link = true; 2624 try root.input(allocator, key, root_focus); 2625 }, 2626 .mouse => |mouse| { 2627 if (mouse.action == .press and mouse.action.press == .left) { 2628 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2629 const focus_id = hit.id; 2630 if (hit.kind == .background) { 2631 root_focus.setFocus(focus_id); 2632 return; 2633 } 2634 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2635 if (session.back == .available) session.back = .requested; 2636 return; 2637 } 2638 // follow a cross-page link 2639 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2640 return session.navigate(route); 2641 } 2642 if (rawLink(root_focus, focus_id)) |url| { 2643 if (!mouse.ctrl) try requestRawLink(session, url); 2644 return; 2645 } 2646 root_focus.setFocus(focus_id); 2647 activate_in_page_link = true; 2648 } 2649 // forward the press into the widget tree so buttons (and any 2650 // future click-aware widgets) can react. widgets that don't 2651 // care about presses ignore it. 2652 try root.input(allocator, key, root_focus); 2653 } else { 2654 try root.input(allocator, key, root_focus); 2655 } 2656 }, 2657 else => try root.input(allocator, key, root_focus), 2658 } 2659 2660 const focused_after = root_focus.grandchild_id; 2661 if (!activate_in_page_link and focused_before == focused_after) return; 2662 const focus_id = focused_after orelse return; 2663 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2664 } 2665 2666 // a link to bytes the server serves directly rather than to a page route, so 2667 // it never resolves to a RoutablePage 2668 pub const raw_link_prefix = "ax:"; 2669 2670 // a route already loaded into the current page; focusing it changes the url 2671 // without rebuilding the page 2672 pub const in_page_link_prefix = "ai:"; 2673 2674 // an in-page tab's base route, or the exact current route when it is selected 2675 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2676 const target = if (selected) session.data.current_page else route; 2677 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2678 } 2679 2680 // a button the web renderer covers with a file picker that posts the chosen 2681 // file to the url after the prefix 2682 pub const file_input_prefix = "file:"; 2683 2684 // a submit button that overrides its enclosing form's action 2685 pub const submit_action_prefix = "submit:"; 2686 2687 // one option of a radio field, as `radio:<name>=<value>` 2688 pub const radio_prefix = "radio:"; 2689 2690 // the url an `ax:` link points at, or null for anything else. a host that 2691 // suppresses the anchor's own navigation needs this to follow the link. 2692 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2693 const child = root_focus.children.get(focus_id) orelse return null; 2694 const custom = switch (child.focus.kind) { 2695 .custom => |c| c, 2696 else => return null, 2697 }; 2698 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2699 return custom[raw_link_prefix.len..]; 2700 } 2701 2702 fn requestRawLink(session: *Session, url: []const u8) !void { 2703 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2704 } 2705 2706 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2707 // an absolute url is already complete 2708 if (!std.mem.startsWith(u8, path, "/")) return path; 2709 return if (session.web_port) |port| 2710 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2711 else 2712 path; 2713 } 2714 2715 // if the focus target at focus_id is an `a:` link that should navigate (a 2716 // different parent page, or a different files directory within the repo page), 2717 // return its route; otherwise null. lets a host turn a click / enter on such a 2718 // link into a navigation rather than a focus change. 2719 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2720 const current = data.current_page; 2721 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2722 // a link to a different parent page always navigates; within a page, a 2723 // files-directory / commits-page / list-window change navigates while tab 2724 // links stay in-page. 2725 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.forkPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 2726 return null; 2727 } 2728 2729 // the route carried by an `ai:` focus target, or null for any other target 2730 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2731 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2732 } 2733 2734 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2735 const child = root_focus.children.get(focus_id) orelse return null; 2736 const custom = switch (child.focus.kind) { 2737 .custom => |c| c, 2738 else => return null, 2739 }; 2740 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2741 const path = custom[prefix.len..]; 2742 // local sessions build (and parse) links with the repo identity elided 2743 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2744 } 2745 2746 // a display author, resolved against the admin db at read time 2747 pub const Author = union(enum) { 2748 unknown, 2749 email: []const u8, // an email no user matches 2750 user_name: []const u8, 2751 2752 // the commit author line's email, resolved like initFromEmail 2753 pub fn init( 2754 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2755 arena: *std.heap.ArenaAllocator, 2756 author_line: []const u8, 2757 ) !Author { 2758 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2759 } 2760 2761 // from a record's stored author email: it resolves to the user currently 2762 // holding it, else stays the email. the result is allocated in `arena`, 2763 // so the email may borrow transient memory. 2764 pub fn initFromEmail( 2765 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2766 arena: *std.heap.ArenaAllocator, 2767 author_email: ?[]const u8, 2768 ) !Author { 2769 const email = author_email orelse return .unknown; 2770 if (haxy_moment) |moment| { 2771 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2772 return .{ .user_name = user.name }; 2773 } 2774 } 2775 return .{ .email = try arena.allocator().dupe(u8, email) }; 2776 } 2777 }; 2778 2779 // the "a:" link to user `name`'s page. 2780 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2781 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2782 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2783 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2784 } 2785 2786 // a focusable " author " box showing `author`, linking to their user page 2787 // when it is a known user 2788 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2789 const text = switch (author) { 2790 .unknown => "", 2791 .email, .user_name => |t| t, 2792 }; 2793 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = " author " }); 2794 errdefer tb.deinit(allocator); 2795 tb.getFocus().mode = .all; 2796 switch (author) { 2797 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2798 else => {}, 2799 } 2800 return tb; 2801 } 2802 2803 // native-TUI navigation 2804 pub const Nav = struct { 2805 root: Widget, 2806 // backs the current page (its Page data and the page-scoped strings its 2807 // widgets build). owned here, swapped on each navigation. session.page_arena 2808 // tracks whichever of these is current so widgets allocate into it. 2809 arena: *std.heap.ArenaAllocator, 2810 // the login the current root was built for 2811 user_id: ?[evt.event_id_size]u8, 2812 history: std.ArrayList(Entry), 2813 2814 // each retained page keeps its own arena, freed when the page leaves the 2815 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2816 // long-lived session from accumulating every page it ever visited. 2817 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2818 2819 // cap on retained back-history so the chain can't grow memory without bound 2820 const max_history: usize = 16; 2821 2822 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2823 session.back = .unavailable; 2824 const arena = try allocator.create(std.heap.ArenaAllocator); 2825 arena.* = std.heap.ArenaAllocator.init(allocator); 2826 errdefer { 2827 arena.deinit(); 2828 allocator.destroy(arena); 2829 } 2830 2831 return .{ 2832 .root = try initCurrentRoot(allocator, arena, session), 2833 .arena = arena, 2834 .user_id = session.userId(), 2835 .history = .empty, 2836 }; 2837 } 2838 2839 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2840 self.root.deinit(allocator); 2841 freeArena(allocator, self.arena); 2842 for (self.history.items) |*entry| { 2843 entry.root.deinit(allocator); 2844 freeArena(allocator, entry.arena); 2845 } 2846 self.history.deinit(allocator); 2847 } 2848 2849 // rebuild the current page in place from a current moment 2850 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2851 if (session.haxy_moment == null and session.local == null) return; 2852 const arena = try allocator.create(std.heap.ArenaAllocator); 2853 arena.* = std.heap.ArenaAllocator.init(allocator); 2854 errdefer freeArena(allocator, arena); 2855 2856 const new_root = try initCurrentRoot(allocator, arena, session); 2857 2858 self.root.deinit(allocator); 2859 freeArena(allocator, self.arena); 2860 self.root = new_root; 2861 self.arena = arena; 2862 self.user_id = session.userId(); 2863 } 2864 2865 // build the current page's root in `arena`, or the not-found page's when 2866 // the route names nothing 2867 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 2868 session.page_arena = arena; 2869 const page = try arena.allocator().create(Page); 2870 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 2871 error.NotFound => blk: { 2872 session.data.current_page = .not_found; 2873 break :blk .not_found; 2874 }, 2875 else => |e| return e, 2876 }; 2877 return initRoot(allocator, page, session); 2878 } 2879 2880 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2881 arena.deinit(); 2882 allocator.destroy(arena); 2883 } 2884 2885 // reconcile the displayed root with the session's navigation state. a 2886 // forward nav (current_page moved to a different parent page) pushes the 2887 // current root and builds the new page in a fresh arena; a back request 2888 // frees the current page and restores the previous one. when escape is 2889 // pressed with no history left we switch to the quit tab instead of quitting. 2890 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2891 // refresh: rebuild the current page in place from a current moment 2892 if (session.refresh_requested) { 2893 session.refresh_requested = false; 2894 try self.rebuild(allocator, session); 2895 return; 2896 } 2897 2898 if (session.back == .requested) { 2899 if (self.history.pop()) |entry| { 2900 session.back = if (self.history.items.len == 0) .unavailable else .available; 2901 self.root.deinit(allocator); 2902 freeArena(allocator, self.arena); 2903 self.root = entry.root; 2904 self.arena = entry.arena; 2905 self.user_id = entry.user_id; 2906 session.page_arena = entry.arena; 2907 session.data.current_page = entry.route; 2908 // a page built under another login is rebuilt rather than restored 2909 if (!std.meta.eql(entry.user_id, session.userId())) { 2910 try self.rebuild(allocator, session); 2911 return; 2912 } 2913 chooseAnsiArtForNavigation(session); 2914 return; 2915 } 2916 session.back = .unavailable; 2917 // nothing to go back to; switch to the quit confirmation 2918 const root_focus = self.root.getFocus(); 2919 var iter = root_focus.children.iterator(); 2920 while (iter.next()) |entry| { 2921 switch (entry.value_ptr.focus.kind) { 2922 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2923 root_focus.setFocus(entry.key_ptr.*); 2924 // a Stack only builds its selected child, so the quit 2925 // button isn't in the focus tree until a build runs with 2926 // the quit tab selected. build once, then send a 2927 // synthetic arrow_down to drop focus from the tab onto 2928 // the button so it's ready to confirm. 2929 try self.root.build(allocator, .{ 2930 .min_size = .{ .width = null, .height = 40 }, 2931 .max_size = .{ .width = 80, .height = null }, 2932 }, root_focus); 2933 try self.root.input(allocator, .arrow_down, root_focus); 2934 break; 2935 }, 2936 else => {}, 2937 } 2938 } 2939 return; 2940 } 2941 2942 // forward navigation: navigate() set next_page to the page to move to (a 2943 // cross-page link or tab change crossing pages) 2944 if (session.next_page) |route| { 2945 session.next_page = null; 2946 if (session.haxy_moment == null and session.local == null) return; 2947 const previous_route = session.data.current_page; 2948 // the page we navigated to becomes the current page 2949 session.data.current_page = route; 2950 session.back = .available; 2951 2952 const arena = try allocator.create(std.heap.ArenaAllocator); 2953 arena.* = std.heap.ArenaAllocator.init(allocator); 2954 errdefer freeArena(allocator, arena); 2955 2956 const new_root = try initCurrentRoot(allocator, arena, session); 2957 2958 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 2959 // drop the oldest entry (freeing its widget tree and arena) once over cap 2960 if (self.history.items.len > max_history) { 2961 var oldest = self.history.orderedRemove(0); 2962 oldest.root.deinit(allocator); 2963 freeArena(allocator, oldest.arena); 2964 } 2965 self.root = new_root; 2966 self.arena = arena; 2967 self.user_id = session.userId(); 2968 } 2969 } 2970 }; 2971 2972 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2973 const page_widget: Widget = switch (page.*) { 2974 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2975 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2976 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2977 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2978 .not_found => .{ .not_found = try .init(allocator, session) }, 2979 }; 2980 2981 chooseAnsiArtForNavigation(session); 2982 2983 // on the TUI/SSH, the page sits above a one-row footer showing its web 2984 // url, when there's a web UI to point at 2985 var root = if (session.is_terminal) blk: { 2986 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2987 errdefer box.deinit(allocator); 2988 const bg_id = bg_blk: { 2989 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2990 errdefer background.deinit(allocator); 2991 const id = background.getFocus().id; 2992 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2993 break :bg_blk id; 2994 }; 2995 if (session.web_port != null) { 2996 var footer = try widget.Footer.init(allocator, session); 2997 errdefer footer.deinit(allocator); 2998 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2999 } 3000 box.getFocus().child_id = bg_id; 3001 break :blk Widget{ .box = box }; 3002 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3003 errdefer root.deinit(allocator); 3004 3005 // input-owning views build their TextInputs in init, so reset their 3006 // registrations here 3007 session.text_inputs.clearRetainingCapacity(); 3008 3009 try root.build(allocator, .{ 3010 .min_size = .{ .width = null, .height = 40 }, 3011 .max_size = .{ .width = 100, .height = null }, 3012 }, root.getFocus()); 3013 3014 return root; 3015 } 3016 3017 // native and server-side sessions choose from the embedded collection. WASM 3018 // keeps the art serialized by the server instead. 3019 fn chooseAnsiArtForNavigation(session: *Session) void { 3020 if (comptime ansi_art.art.len == 0) return; 3021 if (session.data.current_page == .not_found) { 3022 session.data.ansi_art = ansi_art.not_found; 3023 return; 3024 } 3025 const io = session.io orelse return; 3026 var random: usize = undefined; 3027 io.random(std.mem.asBytes(&random)); 3028 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3029 } 3030