╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2721 // the commit author line's email, resolved like initFromEmail 2722 pub fn init( 2723 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2724 arena: *std.heap.ArenaAllocator, 2725 author_line: []const u8, 2726 ) !Author { 2727 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2728 } 2729 2730 // from a record's stored author email: it resolves to the user currently 2731 // holding it, else stays the email. the result is allocated in `arena`, 2732 // so the email may borrow transient memory. 2733 pub fn initFromEmail( 2734 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2735 arena: *std.heap.ArenaAllocator, 2736 author_email: ?[]const u8, 2737 ) !Author { 2738 const email = author_email orelse return .unknown; 2739 if (haxy_moment) |moment| { 2740 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2741 return .{ .user_name = user.name }; 2742 } 2743 } 2744 return .{ .email = try arena.allocator().dupe(u8, email) }; 2745 } 2746 }; 2747 2748 // the "a:" link to user `name`'s page. 2749 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2750 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2751 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2752 return page_arena.allocator().print("a:{s}", .{url}); 2753 } 2754 2755 // a focusable " author " box showing `author`, linking to their user page 2756 // when it is a known user 2757 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2758 const text = switch (author) { 2759 .unknown => "", 2760 .email, .user_name => |t| t, 2761 }; 2762 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 2763 errdefer tb.deinit(allocator); 2764 tb.getFocus().mode = .all; 2765 switch (author) { 2766 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2767 else => {}, 2768 } 2769 return tb; 2770 } 2771 2772 // native-TUI navigation 2773 pub const Nav = struct { 2774 root: Widget, 2775 // backs the current page (its Page data and the page-scoped strings its 2776 // widgets build). owned here, swapped on each navigation. session.page_arena 2777 // tracks whichever of these is current so widgets allocate into it. 2778 arena: *std.heap.ArenaAllocator, 2779 // the login the current root was built for 2780 user_id: ?[evt.event_id_size]u8, 2781 history: std.ArrayList(Entry), 2782 2783 // each retained page keeps its own arena, freed when the page leaves the 2784 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2785 // long-lived session from accumulating every page it ever visited. 2786 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2787 2788 // cap on retained back-history so the chain can't grow memory without bound 2789 const max_history: usize = 16; 2790 2791 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2792 session.back = .unavailable; 2793 const arena = try allocator.create(std.heap.ArenaAllocator); 2794 arena.* = std.heap.ArenaAllocator.init(allocator); 2795 errdefer { 2796 arena.deinit(); 2797 allocator.destroy(arena); 2798 } 2799 2800 return .{ 2801 .root = try initCurrentRoot(allocator, arena, session), 2802 .arena = arena, 2803 .user_id = session.userId(), 2804 .history = .empty, 2805 }; 2806 } 2807 2808 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2809 self.root.deinit(allocator); 2810 freeArena(allocator, self.arena); 2811 for (self.history.items) |*entry| { 2812 entry.root.deinit(allocator); 2813 freeArena(allocator, entry.arena); 2814 } 2815 self.history.deinit(allocator); 2816 } 2817 2818 // rebuild the current page in place from a current moment 2819 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2820 if (session.haxy_moment == null and session.local == null) return; 2821 const arena = try allocator.create(std.heap.ArenaAllocator); 2822 arena.* = std.heap.ArenaAllocator.init(allocator); 2823 errdefer freeArena(allocator, arena); 2824 2825 const new_root = try initCurrentRoot(allocator, arena, session); 2826 2827 self.root.deinit(allocator); 2828 freeArena(allocator, self.arena); 2829 self.root = new_root; 2830 self.arena = arena; 2831 self.user_id = session.userId(); 2832 } 2833 2834 // build the current page's root in `arena`, or the not-found page's when 2835 // the route names nothing 2836 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 2837 session.page_arena = arena; 2838 const page = try arena.allocator().create(Page); 2839 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 2840 error.NotFound => blk: { 2841 session.data.current_page = .not_found; 2842 break :blk .not_found; 2843 }, 2844 else => |e| return e, 2845 }; 2846 return initRoot(allocator, page, session); 2847 } 2848 2849 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2850 arena.deinit(); 2851 allocator.destroy(arena); 2852 } 2853 2854 // reconcile the displayed root with the session's navigation state. a 2855 // forward nav (current_page moved to a different parent page) pushes the 2856 // current root and builds the new page in a fresh arena; a back request 2857 // frees the current page and restores the previous one. when escape is 2858 // pressed with no history left we switch to the quit tab instead of quitting. 2859 // returns whether the root was replaced. 2860 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 2861 // refresh: rebuild the current page in place from a current moment 2862 if (session.refresh_requested) { 2863 session.refresh_requested = false; 2864 try self.rebuild(allocator, session); 2865 return true; 2866 } 2867 2868 if (session.back == .requested) { 2869 if (self.history.pop()) |entry| { 2870 session.back = if (self.history.items.len == 0) .unavailable else .available; 2871 self.root.deinit(allocator); 2872 freeArena(allocator, self.arena); 2873 self.root = entry.root; 2874 self.arena = entry.arena; 2875 self.user_id = entry.user_id; 2876 session.page_arena = entry.arena; 2877 session.data.current_page = entry.route; 2878 // a page built under another login is rebuilt rather than restored 2879 if (!std.meta.eql(entry.user_id, session.userId())) { 2880 try self.rebuild(allocator, session); 2881 return true; 2882 } 2883 chooseAnsiArtForNavigation(session); 2884 return true; 2885 } 2886 session.back = .unavailable; 2887 // nothing to go back to; switch to the quit confirmation 2888 const root_focus = self.root.getFocus(); 2889 var iter = root_focus.children.iterator(); 2890 while (iter.next()) |entry| { 2891 switch (entry.value_ptr.focus.kind) { 2892 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2893 root_focus.setFocus(entry.key_ptr.*); 2894 // a Stack only builds its selected child, so the quit 2895 // button isn't in the focus tree until a build runs with 2896 // the quit tab selected. build once, then send a 2897 // synthetic arrow_down to drop focus from the tab onto 2898 // the button so it's ready to confirm. 2899 try self.root.build(allocator, .{ 2900 .min_size = .{ .width = null, .height = 40 }, 2901 .max_size = .{ .width = 80, .height = null }, 2902 }, root_focus); 2903 try self.root.input(allocator, .arrow_down, root_focus); 2904 break; 2905 }, 2906 else => {}, 2907 } 2908 } 2909 return false; 2910 } 2911 2912 // forward navigation: navigate() set next_page to the page to move to (a 2913 // cross-page link or tab change crossing pages) 2914 if (session.next_page) |route| { 2915 session.next_page = null; 2916 if (session.haxy_moment == null and session.local == null) return false; 2917 const previous_route = session.data.current_page; 2918 // the page we navigated to becomes the current page 2919 session.data.current_page = route; 2920 session.back = .available; 2921 2922 const arena = try allocator.create(std.heap.ArenaAllocator); 2923 arena.* = std.heap.ArenaAllocator.init(allocator); 2924 errdefer freeArena(allocator, arena); 2925 2926 const new_root = try initCurrentRoot(allocator, arena, session); 2927 2928 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 2929 // drop the oldest entry (freeing its widget tree and arena) once over cap 2930 if (self.history.items.len > max_history) { 2931 var oldest = self.history.orderedRemove(0); 2932 oldest.root.deinit(allocator); 2933 freeArena(allocator, oldest.arena); 2934 } 2935 self.root = new_root; 2936 self.arena = arena; 2937 self.user_id = session.userId(); 2938 return true; 2939 } 2940 return false; 2941 } 2942 }; 2943 2944 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2945 const page_widget: Widget = switch (page.*) { 2946 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2947 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2948 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2949 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2950 .not_found => .{ .not_found = try .init(allocator, session) }, 2951 }; 2952 2953 chooseAnsiArtForNavigation(session); 2954 2955 // on the TUI/SSH, the page sits above a one-row footer showing its web 2956 // url, when there's a web UI to point at 2957 var root = if (session.is_terminal) blk: { 2958 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2959 errdefer box.deinit(allocator); 2960 const bg_id = bg_blk: { 2961 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2962 errdefer background.deinit(allocator); 2963 const id = background.getFocus().id; 2964 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2965 break :bg_blk id; 2966 }; 2967 if (session.web_port != null) { 2968 var footer = try widget.Footer.init(allocator, session); 2969 errdefer footer.deinit(allocator); 2970 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2971 } 2972 box.getFocus().child_id = bg_id; 2973 break :blk Widget{ .box = box }; 2974 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2975 errdefer root.deinit(allocator); 2976 2977 // input-owning views build their TextInputs in init, so reset their 2978 // registrations here 2979 session.text_inputs.clearRetainingCapacity(); 2980 2981 try root.build(allocator, .{ 2982 .min_size = .{ .width = null, .height = 40 }, 2983 .max_size = .{ .width = 100, .height = null }, 2984 }, root.getFocus()); 2985 2986 return root; 2987 } 2988 2989 // native and server-side sessions choose from the embedded collection. WASM 2990 // keeps the art serialized by the server instead. 2991 fn chooseAnsiArtForNavigation(session: *Session) void { 2992 if (comptime ansi_art.art.len == 0) return; 2993 if (session.data.current_page == .not_found) { 2994 session.data.ansi_art = ansi_art.not_found; 2995 return; 2996 } 2997 const io = session.io orelse return; 2998 var random: usize = undefined; 2999 io.random(std.mem.asBytes(&random)); 3000 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3001 } 3002