╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2945 author_email: ?[]const u8, 2946 ) !Author { 2947 const email = author_email orelse return .unknown; 2948 if (haxy_moment) |moment| { 2949 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2950 return .{ .user_name = user.name }; 2951 } 2952 } 2953 return .{ .email = try arena.allocator().dupe(u8, email) }; 2954 } 2955 }; 2956 2957 // the "a:" link to user `name`'s page. 2958 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2959 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2960 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2961 return page_arena.allocator().print("a:{s}", .{url}); 2962 } 2963 2964 // a focusable " author " box showing `author`, linking to their user page 2965 // when it is a known user 2966 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2967 const text = switch (author) { 2968 .unknown => "", 2969 .email, .user_name => |t| t, 2970 }; 2971 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 2972 errdefer tb.deinit(allocator); 2973 tb.getFocus().mode = .all; 2974 switch (author) { 2975 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2976 else => {}, 2977 } 2978 return tb; 2979 } 2980 2981 // native-TUI navigation 2982 pub const Nav = struct { 2983 root: Widget, 2984 // backs the current page (its Page data and the page-scoped strings its 2985 // widgets build). owned here, swapped on each navigation. session.page_arena 2986 // tracks whichever of these is current so widgets allocate into it. 2987 arena: *std.heap.ArenaAllocator, 2988 // the login the current root was built for 2989 user_id: ?[evt.event_id_size]u8, 2990 history: std.ArrayList(Entry), 2991 2992 // each retained page keeps its own arena, freed when the page leaves the 2993 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2994 // long-lived session from accumulating every page it ever visited. 2995 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2996 2997 // cap on retained back-history so the chain can't grow memory without bound 2998 const max_history: usize = 16; 2999 3000 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 3001 session.back = .unavailable; 3002 const arena = try allocator.create(std.heap.ArenaAllocator); 3003 arena.* = std.heap.ArenaAllocator.init(allocator); 3004 errdefer { 3005 arena.deinit(); 3006 allocator.destroy(arena); 3007 } 3008 3009 return .{ 3010 .root = try initCurrentRoot(allocator, arena, session), 3011 .arena = arena, 3012 .user_id = session.userId(), 3013 .history = .empty, 3014 }; 3015 } 3016 3017 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3018 self.root.deinit(allocator); 3019 freeArena(allocator, self.arena); 3020 for (self.history.items) |*entry| { 3021 entry.root.deinit(allocator); 3022 freeArena(allocator, entry.arena); 3023 } 3024 self.history.deinit(allocator); 3025 } 3026 3027 // rebuild the current page in place from a current moment 3028 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3029 if (session.haxy_moment == null and session.local == null) return; 3030 const arena = try allocator.create(std.heap.ArenaAllocator); 3031 arena.* = std.heap.ArenaAllocator.init(allocator); 3032 errdefer freeArena(allocator, arena); 3033 3034 const new_root = try initCurrentRoot(allocator, arena, session); 3035 3036 self.root.deinit(allocator); 3037 freeArena(allocator, self.arena); 3038 self.root = new_root; 3039 self.arena = arena; 3040 self.user_id = session.userId(); 3041 } 3042 3043 // build the current page's root in `arena`, or the not-found page's when 3044 // the route names nothing 3045 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3046 session.page_arena = arena; 3047 const page = try arena.allocator().create(Page); 3048 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3049 error.NotFound => blk: { 3050 session.data.current_page = .not_found; 3051 break :blk .not_found; 3052 }, 3053 else => |e| return e, 3054 }; 3055 return initRoot(allocator, page, session); 3056 } 3057 3058 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3059 arena.deinit(); 3060 allocator.destroy(arena); 3061 } 3062 3063 // reconcile the displayed root with the session's navigation state. a 3064 // forward nav (current_page moved to a different parent page) pushes the 3065 // current root and builds the new page in a fresh arena; a back request 3066 // frees the current page and restores the previous one. when escape is 3067 // pressed with no history left we switch to the quit tab instead of quitting. 3068 // returns whether the root was replaced. 3069 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3070 // refresh: rebuild the current page in place from a current moment 3071 if (session.refresh_requested) { 3072 session.refresh_requested = false; 3073 try self.rebuild(allocator, session); 3074 return true; 3075 } 3076 3077 if (session.back == .requested) { 3078 if (self.history.pop()) |entry| { 3079 session.back = if (self.history.items.len == 0) .unavailable else .available; 3080 self.root.deinit(allocator); 3081 freeArena(allocator, self.arena); 3082 self.root = entry.root; 3083 self.arena = entry.arena; 3084 self.user_id = entry.user_id; 3085 session.page_arena = entry.arena; 3086 session.data.current_page = entry.route; 3087 // a page built under another login is rebuilt rather than restored 3088 if (!std.meta.eql(entry.user_id, session.userId())) { 3089 try self.rebuild(allocator, session); 3090 return true; 3091 } 3092 chooseAnsiArtForNavigation(session); 3093 return true; 3094 } 3095 session.back = .unavailable; 3096 // nothing to go back to; switch to the quit confirmation 3097 const root_focus = self.root.getFocus(); 3098 var iter = root_focus.children.iterator(); 3099 while (iter.next()) |entry| { 3100 switch (entry.value_ptr.focus.kind) { 3101 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3102 root_focus.setFocus(entry.key_ptr.*); 3103 // a Stack only builds its selected child, so the quit 3104 // button isn't in the focus tree until a build runs with 3105 // the quit tab selected. build once, then send a 3106 // synthetic arrow_down to drop focus from the tab onto 3107 // the button so it's ready to confirm. 3108 try self.root.build(allocator, .{ 3109 .min_size = .{ .width = null, .height = 40 }, 3110 .max_size = .{ .width = 80, .height = null }, 3111 }, root_focus); 3112 try self.root.input(allocator, .arrow_down, root_focus); 3113 break; 3114 }, 3115 else => {}, 3116 } 3117 } 3118 return false; 3119 } 3120 3121 // forward navigation: navigate() set next_page to the page to move to (a 3122 // cross-page link or tab change crossing pages) 3123 if (session.next_page) |route| { 3124 session.next_page = null; 3125 if (session.haxy_moment == null and session.local == null) return false; 3126 const previous_route = session.data.current_page; 3127 // the page we navigated to becomes the current page 3128 session.data.current_page = route; 3129 session.back = .available; 3130 3131 const arena = try allocator.create(std.heap.ArenaAllocator); 3132 arena.* = std.heap.ArenaAllocator.init(allocator); 3133 errdefer freeArena(allocator, arena); 3134 3135 const new_root = try initCurrentRoot(allocator, arena, session); 3136 3137 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3138 // drop the oldest entry (freeing its widget tree and arena) once over cap 3139 if (self.history.items.len > max_history) { 3140 var oldest = self.history.orderedRemove(0); 3141 oldest.root.deinit(allocator); 3142 freeArena(allocator, oldest.arena); 3143 } 3144 self.root = new_root; 3145 self.arena = arena; 3146 self.user_id = session.userId(); 3147 return true; 3148 } 3149 return false; 3150 } 3151 }; 3152 3153 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3154 const page_widget: Widget = switch (page.*) { 3155 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3156 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3157 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3158 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3159 .not_found => .{ .not_found = try .init(allocator, session) }, 3160 }; 3161 3162 chooseAnsiArtForNavigation(session); 3163 3164 // on the TUI/SSH, the page sits above a one-row footer showing its web 3165 // url, when there's a web UI to point at 3166 var root = if (session.is_terminal) blk: { 3167 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3168 errdefer box.deinit(allocator); 3169 const bg_id = bg_blk: { 3170 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3171 errdefer background.deinit(allocator); 3172 const id = background.getFocus().id; 3173 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3174 break :bg_blk id; 3175 }; 3176 if (session.web_port != null) { 3177 var footer = try widget.Footer.init(allocator, session); 3178 errdefer footer.deinit(allocator); 3179 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3180 } 3181 box.getFocus().child_id = bg_id; 3182 break :blk Widget{ .box = box }; 3183 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3184 errdefer root.deinit(allocator); 3185 3186 // input-owning views build their TextInputs in init, so reset their 3187 // registrations here 3188 session.text_inputs.clearRetainingCapacity(); 3189 3190 try root.build(allocator, .{ 3191 .min_size = .{ .width = null, .height = 40 }, 3192 .max_size = .{ .width = 100, .height = null }, 3193 }, root.getFocus()); 3194 3195 return root; 3196 } 3197 3198 // native and server-side sessions choose from the embedded collection. WASM 3199 // keeps the art serialized by the server instead. 3200 fn chooseAnsiArtForNavigation(session: *Session) void { 3201 if (comptime ansi_art.art.len == 0) return; 3202 if (session.data.current_page == .not_found) { 3203 session.data.ansi_art = ansi_art.not_found; 3204 return; 3205 } 3206 const io = session.io orelse return; 3207 var random: usize = undefined; 3208 io.random(std.mem.asBytes(&random)); 3209 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3210 } 3211