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