╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2064 return; 2065 } 2066 // follow a cross-page link 2067 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2068 return session.navigate(route); 2069 } 2070 if (rawLink(root_focus, focus_id)) |url| { 2071 if (!mouse.ctrl) try requestRawLink(session, url); 2072 return; 2073 } 2074 root_focus.setFocus(focus_id); 2075 activate_in_page_link = true; 2076 } 2077 // forward the press into the widget tree so buttons (and any 2078 // future click-aware widgets) can react. widgets that don't 2079 // care about presses ignore it. 2080 try root.input(allocator, key, root_focus); 2081 } else { 2082 try root.input(allocator, key, root_focus); 2083 } 2084 }, 2085 else => try root.input(allocator, key, root_focus), 2086 } 2087 2088 const focused_after = root_focus.grandchild_id; 2089 if (!activate_in_page_link and focused_before == focused_after) return; 2090 const focus_id = focused_after orelse return; 2091 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2092 } 2093 2094 // a link to bytes the server serves directly rather than to a page route, so 2095 // it never resolves to a RoutablePage 2096 pub const raw_link_prefix = "ax:"; 2097 2098 // a route already loaded into the current page; focusing it changes the url 2099 // without rebuilding the page 2100 pub const in_page_link_prefix = "ai:"; 2101 2102 // an in-page tab's base route, or the exact current route when it is selected 2103 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2104 const target = if (selected) session.data.current_page else route; 2105 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2106 } 2107 2108 // a button the web renderer covers with a file picker that posts the chosen 2109 // file to the url after the prefix 2110 pub const file_input_prefix = "file:"; 2111 2112 // a submit button that overrides its enclosing form's action 2113 pub const submit_action_prefix = "submit:"; 2114 2115 // the url an `ax:` link points at, or null for anything else. a host that 2116 // suppresses the anchor's own navigation needs this to follow the link. 2117 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2118 const child = root_focus.children.get(focus_id) orelse return null; 2119 const custom = switch (child.focus.kind) { 2120 .custom => |c| c, 2121 else => return null, 2122 }; 2123 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2124 return custom[raw_link_prefix.len..]; 2125 } 2126 2127 fn requestRawLink(session: *Session, url: []const u8) !void { 2128 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2129 } 2130 2131 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2132 return if (session.web_port) |port| 2133 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2134 else 2135 path; 2136 } 2137 2138 // if the focus target at focus_id is an `a:` link that should navigate (a 2139 // different parent page, or a different files directory within the repo page), 2140 // return its route; otherwise null. lets a host turn a click / enter on such a 2141 // link into a navigation rather than a focus change. 2142 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2143 const current = data.current_page; 2144 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2145 // a link to a different parent page always navigates; within a page, a 2146 // files-directory / commits-page / list-window change navigates while tab 2147 // links stay in-page. 2148 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; 2149 return null; 2150 } 2151 2152 // the route carried by an `ai:` focus target, or null for any other target 2153 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2154 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2155 } 2156 2157 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2158 const child = root_focus.children.get(focus_id) orelse return null; 2159 const custom = switch (child.focus.kind) { 2160 .custom => |c| c, 2161 else => return null, 2162 }; 2163 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2164 const path = custom[prefix.len..]; 2165 // local sessions build (and parse) links with the repo identity elided 2166 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2167 } 2168 2169 // a display author, resolved against the admin db at read time 2170 pub const Author = union(enum) { 2171 unknown, 2172 email: []const u8, // an email no user matches 2173 user_name: []const u8, 2174 2175 // the commit author line's email, resolved like initFromEmail 2176 pub fn init( 2177 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2178 arena: *std.heap.ArenaAllocator, 2179 author_line: []const u8, 2180 ) !Author { 2181 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2182 } 2183 2184 // from a record's stored author email: it resolves to the user currently 2185 // holding it, else stays the email. the result is allocated in `arena`, 2186 // so the email may borrow transient memory. 2187 pub fn initFromEmail( 2188 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2189 arena: *std.heap.ArenaAllocator, 2190 author_email: ?[]const u8, 2191 ) !Author { 2192 const email = author_email orelse return .unknown; 2193 if (haxy_moment) |moment| { 2194 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2195 return .{ .user_name = user.event.name }; 2196 } 2197 } 2198 return .{ .email = try arena.allocator().dupe(u8, email) }; 2199 } 2200 }; 2201 2202 // the "a:" link to user `name`'s page. 2203 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2204 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2205 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2206 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2207 } 2208 2209 // a focusable " author " box showing `author`, linking to their user page 2210 // when it is a known user 2211 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2212 const text = switch (author) { 2213 .unknown => "", 2214 .email, .user_name => |t| t, 2215 }; 2216 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2217 errdefer tb.deinit(allocator); 2218 tb.getFocus().mode = .all; 2219 switch (author) { 2220 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2221 else => {}, 2222 } 2223 return tb; 2224 } 2225 2226 // native-TUI navigation 2227 pub const Nav = struct { 2228 root: Widget, 2229 // backs the current page (its Page data and the page-scoped strings its 2230 // widgets build). owned here, swapped on each navigation. session.page_arena 2231 // tracks whichever of these is current so widgets allocate into it. 2232 arena: *std.heap.ArenaAllocator, 2233 history: std.ArrayList(Entry), 2234 2235 // each retained page keeps its own arena, freed when the page leaves the 2236 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2237 // long-lived session from accumulating every page it ever visited. 2238 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2239 2240 // cap on retained back-history so the chain can't grow memory without bound 2241 const max_history: usize = 16; 2242 2243 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2244 session.back = .unavailable; 2245 const arena = try allocator.create(std.heap.ArenaAllocator); 2246 arena.* = std.heap.ArenaAllocator.init(allocator); 2247 errdefer { 2248 arena.deinit(); 2249 allocator.destroy(arena); 2250 } 2251 2252 session.page_arena = arena; 2253 const route = session.data.current_page; 2254 2255 const page = try arena.allocator().create(Page); 2256 page.* = try Page.init(arena, session, route); 2257 return .{ 2258 .root = try initRoot(allocator, page, session), 2259 .arena = arena, 2260 .history = .empty, 2261 }; 2262 } 2263 2264 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2265 self.root.deinit(allocator); 2266 freeArena(allocator, self.arena); 2267 for (self.history.items) |*entry| { 2268 entry.root.deinit(allocator); 2269 freeArena(allocator, entry.arena); 2270 } 2271 self.history.deinit(allocator); 2272 } 2273 2274 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2275 arena.deinit(); 2276 allocator.destroy(arena); 2277 } 2278 2279 // reconcile the displayed root with the session's navigation state. a 2280 // forward nav (current_page moved to a different parent page) pushes the 2281 // current root and builds the new page in a fresh arena; a back request 2282 // frees the current page and restores the previous one. when escape is 2283 // pressed with no history left we switch to the quit tab instead of quitting. 2284 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2285 // refresh: rebuild the current page in place from a current moment 2286 if (session.refresh_requested) { 2287 session.refresh_requested = false; 2288 if (session.haxy_moment != null or session.local != null) { 2289 const arena = try allocator.create(std.heap.ArenaAllocator); 2290 arena.* = std.heap.ArenaAllocator.init(allocator); 2291 errdefer freeArena(allocator, arena); 2292 2293 session.page_arena = arena; 2294 2295 const route = session.data.current_page; 2296 const page = try arena.allocator().create(Page); 2297 page.* = try Page.init(arena, session, route); 2298 const new_root = try initRoot(allocator, page, session); 2299 2300 self.root.deinit(allocator); 2301 freeArena(allocator, self.arena); 2302 self.root = new_root; 2303 self.arena = arena; 2304 } 2305 return; 2306 } 2307 2308 if (session.back == .requested) { 2309 if (self.history.pop()) |entry| { 2310 session.back = if (self.history.items.len == 0) .unavailable else .available; 2311 self.root.deinit(allocator); 2312 freeArena(allocator, self.arena); 2313 self.root = entry.root; 2314 self.arena = entry.arena; 2315 session.page_arena = entry.arena; 2316 session.data.current_page = entry.route; 2317 chooseAnsiArtForNavigation(session); 2318 return; 2319 } 2320 session.back = .unavailable; 2321 // nothing to go back to; switch to the quit confirmation 2322 const root_focus = self.root.getFocus(); 2323 var iter = root_focus.children.iterator(); 2324 while (iter.next()) |entry| { 2325 switch (entry.value_ptr.focus.kind) { 2326 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2327 root_focus.setFocus(entry.key_ptr.*); 2328 // a Stack only builds its selected child, so the quit 2329 // button isn't in the focus tree until a build runs with 2330 // the quit tab selected. build once, then send a 2331 // synthetic arrow_down to drop focus from the tab onto 2332 // the button so it's ready to confirm. 2333 try self.root.build(allocator, .{ 2334 .min_size = .{ .width = null, .height = 40 }, 2335 .max_size = .{ .width = 80, .height = null }, 2336 }, root_focus); 2337 try self.root.input(allocator, .arrow_down, root_focus); 2338 break; 2339 }, 2340 else => {}, 2341 } 2342 } 2343 return; 2344 } 2345 2346 // forward navigation: navigate() set next_page to the page to move to (a 2347 // cross-page link or tab change crossing pages) 2348 if (session.next_page) |route| { 2349 session.next_page = null; 2350 if (session.haxy_moment == null and session.local == null) return; 2351 const previous_route = session.data.current_page; 2352 // the page we navigated to becomes the current page 2353 session.data.current_page = route; 2354 session.back = .available; 2355 2356 const arena = try allocator.create(std.heap.ArenaAllocator); 2357 arena.* = std.heap.ArenaAllocator.init(allocator); 2358 errdefer freeArena(allocator, arena); 2359 2360 session.page_arena = arena; 2361 2362 const page = try arena.allocator().create(Page); 2363 page.* = try Page.init(arena, session, route); 2364 const new_root = try initRoot(allocator, page, session); 2365 2366 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2367 // drop the oldest entry (freeing its widget tree and arena) once over cap 2368 if (self.history.items.len > max_history) { 2369 var oldest = self.history.orderedRemove(0); 2370 oldest.root.deinit(allocator); 2371 freeArena(allocator, oldest.arena); 2372 } 2373 self.root = new_root; 2374 self.arena = arena; 2375 } 2376 } 2377 }; 2378 2379 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2380 const page_widget: Widget = switch (page.*) { 2381 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2382 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2383 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2384 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2385 }; 2386 2387 chooseAnsiArtForNavigation(session); 2388 2389 // on the TUI/SSH, the page sits above a one-row footer showing the url 2390 var root = if (session.is_terminal) blk: { 2391 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2392 errdefer box.deinit(allocator); 2393 const bg_id = bg_blk: { 2394 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2395 errdefer background.deinit(allocator); 2396 const id = background.getFocus().id; 2397 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2398 break :bg_blk id; 2399 }; 2400 { 2401 var footer = try widget.Footer.init(allocator, session); 2402 errdefer footer.deinit(allocator); 2403 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2404 } 2405 box.getFocus().child_id = bg_id; 2406 break :blk Widget{ .box = box }; 2407 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2408 errdefer root.deinit(allocator); 2409 2410 // input-owning views build their TextInputs in init, so reset their 2411 // registrations here 2412 session.text_inputs.clearRetainingCapacity(); 2413 2414 try root.build(allocator, .{ 2415 .min_size = .{ .width = null, .height = 40 }, 2416 .max_size = .{ .width = 100, .height = null }, 2417 }, root.getFocus()); 2418 2419 return root; 2420 } 2421 2422 // native and server-side sessions choose from the embedded collection. WASM 2423 // keeps the art serialized by the server instead. 2424 fn chooseAnsiArtForNavigation(session: *Session) void { 2425 if (comptime ansi_arts.len == 0) return; 2426 const io = session.io orelse return; 2427 var random: usize = undefined; 2428 io.random(std.mem.asBytes(&random)); 2429 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2430 } 2431