╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2200 pub const file_input_prefix = "file:"; 2201 2202 // a submit button that overrides its enclosing form's action 2203 pub const submit_action_prefix = "submit:"; 2204 2205 // the url an `ax:` link points at, or null for anything else. a host that 2206 // suppresses the anchor's own navigation needs this to follow the link. 2207 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2208 const child = root_focus.children.get(focus_id) orelse return null; 2209 const custom = switch (child.focus.kind) { 2210 .custom => |c| c, 2211 else => return null, 2212 }; 2213 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2214 return custom[raw_link_prefix.len..]; 2215 } 2216 2217 fn requestRawLink(session: *Session, url: []const u8) !void { 2218 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2219 } 2220 2221 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2222 return if (session.web_port) |port| 2223 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2224 else 2225 path; 2226 } 2227 2228 // if the focus target at focus_id is an `a:` link that should navigate (a 2229 // different parent page, or a different files directory within the repo page), 2230 // return its route; otherwise null. lets a host turn a click / enter on such a 2231 // link into a navigation rather than a focus change. 2232 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2233 const current = data.current_page; 2234 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2235 // a link to a different parent page always navigates; within a page, a 2236 // files-directory / commits-page / list-window change navigates while tab 2237 // links stay in-page. 2238 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; 2239 return null; 2240 } 2241 2242 // the route carried by an `ai:` focus target, or null for any other target 2243 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2244 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2245 } 2246 2247 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2248 const child = root_focus.children.get(focus_id) orelse return null; 2249 const custom = switch (child.focus.kind) { 2250 .custom => |c| c, 2251 else => return null, 2252 }; 2253 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2254 const path = custom[prefix.len..]; 2255 // local sessions build (and parse) links with the repo identity elided 2256 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2257 } 2258 2259 // a display author, resolved against the admin db at read time 2260 pub const Author = union(enum) { 2261 unknown, 2262 email: []const u8, // an email no user matches 2263 user_name: []const u8, 2264 2265 // the commit author line's email, resolved like initFromEmail 2266 pub fn init( 2267 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2268 arena: *std.heap.ArenaAllocator, 2269 author_line: []const u8, 2270 ) !Author { 2271 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2272 } 2273 2274 // from a record's stored author email: it resolves to the user currently 2275 // holding it, else stays the email. the result is allocated in `arena`, 2276 // so the email may borrow transient memory. 2277 pub fn initFromEmail( 2278 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2279 arena: *std.heap.ArenaAllocator, 2280 author_email: ?[]const u8, 2281 ) !Author { 2282 const email = author_email orelse return .unknown; 2283 if (haxy_moment) |moment| { 2284 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2285 return .{ .user_name = user.event.name }; 2286 } 2287 } 2288 return .{ .email = try arena.allocator().dupe(u8, email) }; 2289 } 2290 }; 2291 2292 // the "a:" link to user `name`'s page. 2293 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2294 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2295 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2296 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2297 } 2298 2299 // a focusable " author " box showing `author`, linking to their user page 2300 // when it is a known user 2301 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2302 const text = switch (author) { 2303 .unknown => "", 2304 .email, .user_name => |t| t, 2305 }; 2306 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2307 errdefer tb.deinit(allocator); 2308 tb.getFocus().mode = .all; 2309 switch (author) { 2310 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2311 else => {}, 2312 } 2313 return tb; 2314 } 2315 2316 // native-TUI navigation 2317 pub const Nav = struct { 2318 root: Widget, 2319 // backs the current page (its Page data and the page-scoped strings its 2320 // widgets build). owned here, swapped on each navigation. session.page_arena 2321 // tracks whichever of these is current so widgets allocate into it. 2322 arena: *std.heap.ArenaAllocator, 2323 history: std.ArrayList(Entry), 2324 2325 // each retained page keeps its own arena, freed when the page leaves the 2326 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2327 // long-lived session from accumulating every page it ever visited. 2328 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2329 2330 // cap on retained back-history so the chain can't grow memory without bound 2331 const max_history: usize = 16; 2332 2333 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2334 session.back = .unavailable; 2335 const arena = try allocator.create(std.heap.ArenaAllocator); 2336 arena.* = std.heap.ArenaAllocator.init(allocator); 2337 errdefer { 2338 arena.deinit(); 2339 allocator.destroy(arena); 2340 } 2341 2342 session.page_arena = arena; 2343 const route = session.data.current_page; 2344 2345 const page = try arena.allocator().create(Page); 2346 page.* = try Page.init(arena, session, route); 2347 return .{ 2348 .root = try initRoot(allocator, page, session), 2349 .arena = arena, 2350 .history = .empty, 2351 }; 2352 } 2353 2354 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2355 self.root.deinit(allocator); 2356 freeArena(allocator, self.arena); 2357 for (self.history.items) |*entry| { 2358 entry.root.deinit(allocator); 2359 freeArena(allocator, entry.arena); 2360 } 2361 self.history.deinit(allocator); 2362 } 2363 2364 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2365 arena.deinit(); 2366 allocator.destroy(arena); 2367 } 2368 2369 // reconcile the displayed root with the session's navigation state. a 2370 // forward nav (current_page moved to a different parent page) pushes the 2371 // current root and builds the new page in a fresh arena; a back request 2372 // frees the current page and restores the previous one. when escape is 2373 // pressed with no history left we switch to the quit tab instead of quitting. 2374 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2375 // refresh: rebuild the current page in place from a current moment 2376 if (session.refresh_requested) { 2377 session.refresh_requested = false; 2378 if (session.haxy_moment != null or session.local != null) { 2379 const arena = try allocator.create(std.heap.ArenaAllocator); 2380 arena.* = std.heap.ArenaAllocator.init(allocator); 2381 errdefer freeArena(allocator, arena); 2382 2383 session.page_arena = arena; 2384 2385 const route = session.data.current_page; 2386 const page = try arena.allocator().create(Page); 2387 page.* = try Page.init(arena, session, route); 2388 const new_root = try initRoot(allocator, page, session); 2389 2390 self.root.deinit(allocator); 2391 freeArena(allocator, self.arena); 2392 self.root = new_root; 2393 self.arena = arena; 2394 } 2395 return; 2396 } 2397 2398 if (session.back == .requested) { 2399 if (self.history.pop()) |entry| { 2400 session.back = if (self.history.items.len == 0) .unavailable else .available; 2401 self.root.deinit(allocator); 2402 freeArena(allocator, self.arena); 2403 self.root = entry.root; 2404 self.arena = entry.arena; 2405 session.page_arena = entry.arena; 2406 session.data.current_page = entry.route; 2407 chooseAnsiArtForNavigation(session); 2408 return; 2409 } 2410 session.back = .unavailable; 2411 // nothing to go back to; switch to the quit confirmation 2412 const root_focus = self.root.getFocus(); 2413 var iter = root_focus.children.iterator(); 2414 while (iter.next()) |entry| { 2415 switch (entry.value_ptr.focus.kind) { 2416 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2417 root_focus.setFocus(entry.key_ptr.*); 2418 // a Stack only builds its selected child, so the quit 2419 // button isn't in the focus tree until a build runs with 2420 // the quit tab selected. build once, then send a 2421 // synthetic arrow_down to drop focus from the tab onto 2422 // the button so it's ready to confirm. 2423 try self.root.build(allocator, .{ 2424 .min_size = .{ .width = null, .height = 40 }, 2425 .max_size = .{ .width = 80, .height = null }, 2426 }, root_focus); 2427 try self.root.input(allocator, .arrow_down, root_focus); 2428 break; 2429 }, 2430 else => {}, 2431 } 2432 } 2433 return; 2434 } 2435 2436 // forward navigation: navigate() set next_page to the page to move to (a 2437 // cross-page link or tab change crossing pages) 2438 if (session.next_page) |route| { 2439 session.next_page = null; 2440 if (session.haxy_moment == null and session.local == null) return; 2441 const previous_route = session.data.current_page; 2442 // the page we navigated to becomes the current page 2443 session.data.current_page = route; 2444 session.back = .available; 2445 2446 const arena = try allocator.create(std.heap.ArenaAllocator); 2447 arena.* = std.heap.ArenaAllocator.init(allocator); 2448 errdefer freeArena(allocator, arena); 2449 2450 session.page_arena = arena; 2451 2452 const page = try arena.allocator().create(Page); 2453 page.* = try Page.init(arena, session, route); 2454 const new_root = try initRoot(allocator, page, session); 2455 2456 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2457 // drop the oldest entry (freeing its widget tree and arena) once over cap 2458 if (self.history.items.len > max_history) { 2459 var oldest = self.history.orderedRemove(0); 2460 oldest.root.deinit(allocator); 2461 freeArena(allocator, oldest.arena); 2462 } 2463 self.root = new_root; 2464 self.arena = arena; 2465 } 2466 } 2467 }; 2468 2469 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2470 const page_widget: Widget = switch (page.*) { 2471 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2472 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2473 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2474 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2475 }; 2476 2477 chooseAnsiArtForNavigation(session); 2478 2479 // on the TUI/SSH, the page sits above a one-row footer showing the url 2480 var root = if (session.is_terminal) blk: { 2481 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2482 errdefer box.deinit(allocator); 2483 const bg_id = bg_blk: { 2484 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2485 errdefer background.deinit(allocator); 2486 const id = background.getFocus().id; 2487 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2488 break :bg_blk id; 2489 }; 2490 { 2491 var footer = try widget.Footer.init(allocator, session); 2492 errdefer footer.deinit(allocator); 2493 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2494 } 2495 box.getFocus().child_id = bg_id; 2496 break :blk Widget{ .box = box }; 2497 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2498 errdefer root.deinit(allocator); 2499 2500 // input-owning views build their TextInputs in init, so reset their 2501 // registrations here 2502 session.text_inputs.clearRetainingCapacity(); 2503 2504 try root.build(allocator, .{ 2505 .min_size = .{ .width = null, .height = 40 }, 2506 .max_size = .{ .width = 100, .height = null }, 2507 }, root.getFocus()); 2508 2509 return root; 2510 } 2511 2512 // native and server-side sessions choose from the embedded collection. WASM 2513 // keeps the art serialized by the server instead. 2514 fn chooseAnsiArtForNavigation(session: *Session) void { 2515 if (comptime ansi_arts.len == 0) return; 2516 const io = session.io orelse return; 2517 var random: usize = undefined; 2518 io.random(std.mem.asBytes(&random)); 2519 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2520 } 2521