╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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