╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2621 const child = root_focus.children.get(focus_id) orelse return null; 2622 const custom = switch (child.focus.kind) { 2623 .custom => |c| c, 2624 else => return null, 2625 }; 2626 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2627 return custom[raw_link_prefix.len..]; 2628 } 2629 2630 fn requestRawLink(session: *Session, url: []const u8) !void { 2631 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2632 } 2633 2634 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2635 // an absolute url is already complete 2636 if (!std.mem.startsWith(u8, path, "/")) return path; 2637 return if (session.web_port) |port| 2638 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2639 else 2640 path; 2641 } 2642 2643 // if the focus target at focus_id is an `a:` link that should navigate (a 2644 // different parent page, or a different files directory within the repo page), 2645 // return its route; otherwise null. lets a host turn a click / enter on such a 2646 // link into a navigation rather than a focus change. 2647 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2648 const current = data.current_page; 2649 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2650 // a link to a different parent page always navigates; within a page, a 2651 // files-directory / commits-page / list-window change navigates while tab 2652 // links stay in-page. 2653 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; 2654 return null; 2655 } 2656 2657 // the route carried by an `ai:` focus target, or null for any other target 2658 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2659 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2660 } 2661 2662 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2663 const child = root_focus.children.get(focus_id) orelse return null; 2664 const custom = switch (child.focus.kind) { 2665 .custom => |c| c, 2666 else => return null, 2667 }; 2668 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2669 const path = custom[prefix.len..]; 2670 // local sessions build (and parse) links with the repo identity elided 2671 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2672 } 2673 2674 // a display author, resolved against the admin db at read time 2675 pub const Author = union(enum) { 2676 unknown, 2677 email: []const u8, // an email no user matches 2678 user_name: []const u8, 2679 2680 // the commit author line's email, resolved like initFromEmail 2681 pub fn init( 2682 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2683 arena: *std.heap.ArenaAllocator, 2684 author_line: []const u8, 2685 ) !Author { 2686 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2687 } 2688 2689 // from a record's stored author email: it resolves to the user currently 2690 // holding it, else stays the email. the result is allocated in `arena`, 2691 // so the email may borrow transient memory. 2692 pub fn initFromEmail( 2693 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2694 arena: *std.heap.ArenaAllocator, 2695 author_email: ?[]const u8, 2696 ) !Author { 2697 const email = author_email orelse return .unknown; 2698 if (haxy_moment) |moment| { 2699 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2700 return .{ .user_name = user.name }; 2701 } 2702 } 2703 return .{ .email = try arena.allocator().dupe(u8, email) }; 2704 } 2705 }; 2706 2707 // the "a:" link to user `name`'s page. 2708 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2709 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2710 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2711 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2712 } 2713 2714 // a focusable " author " box showing `author`, linking to their user page 2715 // when it is a known user 2716 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2717 const text = switch (author) { 2718 .unknown => "", 2719 .email, .user_name => |t| t, 2720 }; 2721 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = " author " }); 2722 errdefer tb.deinit(allocator); 2723 tb.getFocus().mode = .all; 2724 switch (author) { 2725 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2726 else => {}, 2727 } 2728 return tb; 2729 } 2730 2731 // native-TUI navigation 2732 pub const Nav = struct { 2733 root: Widget, 2734 // backs the current page (its Page data and the page-scoped strings its 2735 // widgets build). owned here, swapped on each navigation. session.page_arena 2736 // tracks whichever of these is current so widgets allocate into it. 2737 arena: *std.heap.ArenaAllocator, 2738 // the login the current root was built for 2739 user_id: ?[evt.event_id_size]u8, 2740 history: std.ArrayList(Entry), 2741 2742 // each retained page keeps its own arena, freed when the page leaves the 2743 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2744 // long-lived session from accumulating every page it ever visited. 2745 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2746 2747 // cap on retained back-history so the chain can't grow memory without bound 2748 const max_history: usize = 16; 2749 2750 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2751 session.back = .unavailable; 2752 const arena = try allocator.create(std.heap.ArenaAllocator); 2753 arena.* = std.heap.ArenaAllocator.init(allocator); 2754 errdefer { 2755 arena.deinit(); 2756 allocator.destroy(arena); 2757 } 2758 2759 return .{ 2760 .root = try initCurrentRoot(allocator, arena, session), 2761 .arena = arena, 2762 .user_id = session.userId(), 2763 .history = .empty, 2764 }; 2765 } 2766 2767 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2768 self.root.deinit(allocator); 2769 freeArena(allocator, self.arena); 2770 for (self.history.items) |*entry| { 2771 entry.root.deinit(allocator); 2772 freeArena(allocator, entry.arena); 2773 } 2774 self.history.deinit(allocator); 2775 } 2776 2777 // rebuild the current page in place from a current moment 2778 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2779 if (session.haxy_moment == null and session.local == null) return; 2780 const arena = try allocator.create(std.heap.ArenaAllocator); 2781 arena.* = std.heap.ArenaAllocator.init(allocator); 2782 errdefer freeArena(allocator, arena); 2783 2784 const new_root = try initCurrentRoot(allocator, arena, session); 2785 2786 self.root.deinit(allocator); 2787 freeArena(allocator, self.arena); 2788 self.root = new_root; 2789 self.arena = arena; 2790 self.user_id = session.userId(); 2791 } 2792 2793 // build the current page's root in `arena`, or the not-found page's when 2794 // the route names nothing 2795 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 2796 session.page_arena = arena; 2797 const page = try arena.allocator().create(Page); 2798 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 2799 error.NotFound => blk: { 2800 session.data.current_page = .not_found; 2801 break :blk .not_found; 2802 }, 2803 else => |e| return e, 2804 }; 2805 return initRoot(allocator, page, session); 2806 } 2807 2808 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2809 arena.deinit(); 2810 allocator.destroy(arena); 2811 } 2812 2813 // reconcile the displayed root with the session's navigation state. a 2814 // forward nav (current_page moved to a different parent page) pushes the 2815 // current root and builds the new page in a fresh arena; a back request 2816 // frees the current page and restores the previous one. when escape is 2817 // pressed with no history left we switch to the quit tab instead of quitting. 2818 // returns whether the root was replaced. 2819 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 2820 // refresh: rebuild the current page in place from a current moment 2821 if (session.refresh_requested) { 2822 session.refresh_requested = false; 2823 try self.rebuild(allocator, session); 2824 return true; 2825 } 2826 2827 if (session.back == .requested) { 2828 if (self.history.pop()) |entry| { 2829 session.back = if (self.history.items.len == 0) .unavailable else .available; 2830 self.root.deinit(allocator); 2831 freeArena(allocator, self.arena); 2832 self.root = entry.root; 2833 self.arena = entry.arena; 2834 self.user_id = entry.user_id; 2835 session.page_arena = entry.arena; 2836 session.data.current_page = entry.route; 2837 // a page built under another login is rebuilt rather than restored 2838 if (!std.meta.eql(entry.user_id, session.userId())) { 2839 try self.rebuild(allocator, session); 2840 return true; 2841 } 2842 chooseAnsiArtForNavigation(session); 2843 return true; 2844 } 2845 session.back = .unavailable; 2846 // nothing to go back to; switch to the quit confirmation 2847 const root_focus = self.root.getFocus(); 2848 var iter = root_focus.children.iterator(); 2849 while (iter.next()) |entry| { 2850 switch (entry.value_ptr.focus.kind) { 2851 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2852 root_focus.setFocus(entry.key_ptr.*); 2853 // a Stack only builds its selected child, so the quit 2854 // button isn't in the focus tree until a build runs with 2855 // the quit tab selected. build once, then send a 2856 // synthetic arrow_down to drop focus from the tab onto 2857 // the button so it's ready to confirm. 2858 try self.root.build(allocator, .{ 2859 .min_size = .{ .width = null, .height = 40 }, 2860 .max_size = .{ .width = 80, .height = null }, 2861 }, root_focus); 2862 try self.root.input(allocator, .arrow_down, root_focus); 2863 break; 2864 }, 2865 else => {}, 2866 } 2867 } 2868 return false; 2869 } 2870 2871 // forward navigation: navigate() set next_page to the page to move to (a 2872 // cross-page link or tab change crossing pages) 2873 if (session.next_page) |route| { 2874 session.next_page = null; 2875 if (session.haxy_moment == null and session.local == null) return false; 2876 const previous_route = session.data.current_page; 2877 // the page we navigated to becomes the current page 2878 session.data.current_page = route; 2879 session.back = .available; 2880 2881 const arena = try allocator.create(std.heap.ArenaAllocator); 2882 arena.* = std.heap.ArenaAllocator.init(allocator); 2883 errdefer freeArena(allocator, arena); 2884 2885 const new_root = try initCurrentRoot(allocator, arena, session); 2886 2887 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 2888 // drop the oldest entry (freeing its widget tree and arena) once over cap 2889 if (self.history.items.len > max_history) { 2890 var oldest = self.history.orderedRemove(0); 2891 oldest.root.deinit(allocator); 2892 freeArena(allocator, oldest.arena); 2893 } 2894 self.root = new_root; 2895 self.arena = arena; 2896 self.user_id = session.userId(); 2897 return true; 2898 } 2899 return false; 2900 } 2901 }; 2902 2903 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2904 const page_widget: Widget = switch (page.*) { 2905 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2906 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2907 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2908 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2909 .not_found => .{ .not_found = try .init(allocator, session) }, 2910 }; 2911 2912 chooseAnsiArtForNavigation(session); 2913 2914 // on the TUI/SSH, the page sits above a one-row footer showing its web 2915 // url, when there's a web UI to point at 2916 var root = if (session.is_terminal) blk: { 2917 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2918 errdefer box.deinit(allocator); 2919 const bg_id = bg_blk: { 2920 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2921 errdefer background.deinit(allocator); 2922 const id = background.getFocus().id; 2923 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2924 break :bg_blk id; 2925 }; 2926 if (session.web_port != null) { 2927 var footer = try widget.Footer.init(allocator, session); 2928 errdefer footer.deinit(allocator); 2929 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2930 } 2931 box.getFocus().child_id = bg_id; 2932 break :blk Widget{ .box = box }; 2933 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2934 errdefer root.deinit(allocator); 2935 2936 // input-owning views build their TextInputs in init, so reset their 2937 // registrations here 2938 session.text_inputs.clearRetainingCapacity(); 2939 2940 try root.build(allocator, .{ 2941 .min_size = .{ .width = null, .height = 40 }, 2942 .max_size = .{ .width = 100, .height = null }, 2943 }, root.getFocus()); 2944 2945 return root; 2946 } 2947 2948 // native and server-side sessions choose from the embedded collection. WASM 2949 // keeps the art serialized by the server instead. 2950 fn chooseAnsiArtForNavigation(session: *Session) void { 2951 if (comptime ansi_art.art.len == 0) return; 2952 if (session.data.current_page == .not_found) { 2953 session.data.ansi_art = ansi_art.not_found; 2954 return; 2955 } 2956 const io = session.io orelse return; 2957 var random: usize = undefined; 2958 io.random(std.mem.asBytes(&random)); 2959 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 2960 } 2961