╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2332 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2333 // a link to a different parent page always navigates; within a page, a 2334 // files-directory / commits-page / list-window change navigates while tab 2335 // links stay in-page. 2336 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; 2337 return null; 2338 } 2339 2340 // the route carried by an `ai:` focus target, or null for any other target 2341 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2342 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2343 } 2344 2345 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2346 const child = root_focus.children.get(focus_id) orelse return null; 2347 const custom = switch (child.focus.kind) { 2348 .custom => |c| c, 2349 else => return null, 2350 }; 2351 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2352 const path = custom[prefix.len..]; 2353 // local sessions build (and parse) links with the repo identity elided 2354 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2355 } 2356 2357 // a display author, resolved against the admin db at read time 2358 pub const Author = union(enum) { 2359 unknown, 2360 email: []const u8, // an email no user matches 2361 user_name: []const u8, 2362 2363 // the commit author line's email, resolved like initFromEmail 2364 pub fn init( 2365 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2366 arena: *std.heap.ArenaAllocator, 2367 author_line: []const u8, 2368 ) !Author { 2369 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2370 } 2371 2372 // from a record's stored author email: it resolves to the user currently 2373 // holding it, else stays the email. the result is allocated in `arena`, 2374 // so the email may borrow transient memory. 2375 pub fn initFromEmail( 2376 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2377 arena: *std.heap.ArenaAllocator, 2378 author_email: ?[]const u8, 2379 ) !Author { 2380 const email = author_email orelse return .unknown; 2381 if (haxy_moment) |moment| { 2382 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2383 return .{ .user_name = user.event.name }; 2384 } 2385 } 2386 return .{ .email = try arena.allocator().dupe(u8, email) }; 2387 } 2388 }; 2389 2390 // the "a:" link to user `name`'s page. 2391 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2392 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2393 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2394 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2395 } 2396 2397 // a focusable " author " box showing `author`, linking to their user page 2398 // when it is a known user 2399 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2400 const text = switch (author) { 2401 .unknown => "", 2402 .email, .user_name => |t| t, 2403 }; 2404 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2405 errdefer tb.deinit(allocator); 2406 tb.getFocus().mode = .all; 2407 switch (author) { 2408 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2409 else => {}, 2410 } 2411 return tb; 2412 } 2413 2414 // native-TUI navigation 2415 pub const Nav = struct { 2416 root: Widget, 2417 // backs the current page (its Page data and the page-scoped strings its 2418 // widgets build). owned here, swapped on each navigation. session.page_arena 2419 // tracks whichever of these is current so widgets allocate into it. 2420 arena: *std.heap.ArenaAllocator, 2421 history: std.ArrayList(Entry), 2422 2423 // each retained page keeps its own arena, freed when the page leaves the 2424 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2425 // long-lived session from accumulating every page it ever visited. 2426 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2427 2428 // cap on retained back-history so the chain can't grow memory without bound 2429 const max_history: usize = 16; 2430 2431 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2432 session.back = .unavailable; 2433 const arena = try allocator.create(std.heap.ArenaAllocator); 2434 arena.* = std.heap.ArenaAllocator.init(allocator); 2435 errdefer { 2436 arena.deinit(); 2437 allocator.destroy(arena); 2438 } 2439 2440 session.page_arena = arena; 2441 const route = session.data.current_page; 2442 2443 const page = try arena.allocator().create(Page); 2444 page.* = try Page.init(arena, session, route); 2445 return .{ 2446 .root = try initRoot(allocator, page, session), 2447 .arena = arena, 2448 .history = .empty, 2449 }; 2450 } 2451 2452 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2453 self.root.deinit(allocator); 2454 freeArena(allocator, self.arena); 2455 for (self.history.items) |*entry| { 2456 entry.root.deinit(allocator); 2457 freeArena(allocator, entry.arena); 2458 } 2459 self.history.deinit(allocator); 2460 } 2461 2462 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2463 arena.deinit(); 2464 allocator.destroy(arena); 2465 } 2466 2467 // reconcile the displayed root with the session's navigation state. a 2468 // forward nav (current_page moved to a different parent page) pushes the 2469 // current root and builds the new page in a fresh arena; a back request 2470 // frees the current page and restores the previous one. when escape is 2471 // pressed with no history left we switch to the quit tab instead of quitting. 2472 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2473 // refresh: rebuild the current page in place from a current moment 2474 if (session.refresh_requested) { 2475 session.refresh_requested = false; 2476 if (session.haxy_moment != null or session.local != null) { 2477 const arena = try allocator.create(std.heap.ArenaAllocator); 2478 arena.* = std.heap.ArenaAllocator.init(allocator); 2479 errdefer freeArena(allocator, arena); 2480 2481 session.page_arena = arena; 2482 2483 const route = session.data.current_page; 2484 const page = try arena.allocator().create(Page); 2485 page.* = try Page.init(arena, session, route); 2486 const new_root = try initRoot(allocator, page, session); 2487 2488 self.root.deinit(allocator); 2489 freeArena(allocator, self.arena); 2490 self.root = new_root; 2491 self.arena = arena; 2492 } 2493 return; 2494 } 2495 2496 if (session.back == .requested) { 2497 if (self.history.pop()) |entry| { 2498 session.back = if (self.history.items.len == 0) .unavailable else .available; 2499 self.root.deinit(allocator); 2500 freeArena(allocator, self.arena); 2501 self.root = entry.root; 2502 self.arena = entry.arena; 2503 session.page_arena = entry.arena; 2504 session.data.current_page = entry.route; 2505 chooseAnsiArtForNavigation(session); 2506 return; 2507 } 2508 session.back = .unavailable; 2509 // nothing to go back to; switch to the quit confirmation 2510 const root_focus = self.root.getFocus(); 2511 var iter = root_focus.children.iterator(); 2512 while (iter.next()) |entry| { 2513 switch (entry.value_ptr.focus.kind) { 2514 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2515 root_focus.setFocus(entry.key_ptr.*); 2516 // a Stack only builds its selected child, so the quit 2517 // button isn't in the focus tree until a build runs with 2518 // the quit tab selected. build once, then send a 2519 // synthetic arrow_down to drop focus from the tab onto 2520 // the button so it's ready to confirm. 2521 try self.root.build(allocator, .{ 2522 .min_size = .{ .width = null, .height = 40 }, 2523 .max_size = .{ .width = 80, .height = null }, 2524 }, root_focus); 2525 try self.root.input(allocator, .arrow_down, root_focus); 2526 break; 2527 }, 2528 else => {}, 2529 } 2530 } 2531 return; 2532 } 2533 2534 // forward navigation: navigate() set next_page to the page to move to (a 2535 // cross-page link or tab change crossing pages) 2536 if (session.next_page) |route| { 2537 session.next_page = null; 2538 if (session.haxy_moment == null and session.local == null) return; 2539 const previous_route = session.data.current_page; 2540 // the page we navigated to becomes the current page 2541 session.data.current_page = route; 2542 session.back = .available; 2543 2544 const arena = try allocator.create(std.heap.ArenaAllocator); 2545 arena.* = std.heap.ArenaAllocator.init(allocator); 2546 errdefer freeArena(allocator, arena); 2547 2548 session.page_arena = arena; 2549 2550 const page = try arena.allocator().create(Page); 2551 page.* = try Page.init(arena, session, route); 2552 const new_root = try initRoot(allocator, page, session); 2553 2554 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2555 // drop the oldest entry (freeing its widget tree and arena) once over cap 2556 if (self.history.items.len > max_history) { 2557 var oldest = self.history.orderedRemove(0); 2558 oldest.root.deinit(allocator); 2559 freeArena(allocator, oldest.arena); 2560 } 2561 self.root = new_root; 2562 self.arena = arena; 2563 } 2564 } 2565 }; 2566 2567 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2568 const page_widget: Widget = switch (page.*) { 2569 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2570 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2571 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2572 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2573 }; 2574 2575 chooseAnsiArtForNavigation(session); 2576 2577 // on the TUI/SSH, the page sits above a one-row footer showing the url 2578 var root = if (session.is_terminal) blk: { 2579 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2580 errdefer box.deinit(allocator); 2581 const bg_id = bg_blk: { 2582 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2583 errdefer background.deinit(allocator); 2584 const id = background.getFocus().id; 2585 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2586 break :bg_blk id; 2587 }; 2588 { 2589 var footer = try widget.Footer.init(allocator, session); 2590 errdefer footer.deinit(allocator); 2591 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2592 } 2593 box.getFocus().child_id = bg_id; 2594 break :blk Widget{ .box = box }; 2595 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2596 errdefer root.deinit(allocator); 2597 2598 // input-owning views build their TextInputs in init, so reset their 2599 // registrations here 2600 session.text_inputs.clearRetainingCapacity(); 2601 2602 try root.build(allocator, .{ 2603 .min_size = .{ .width = null, .height = 40 }, 2604 .max_size = .{ .width = 100, .height = null }, 2605 }, root.getFocus()); 2606 2607 return root; 2608 } 2609 2610 // native and server-side sessions choose from the embedded collection. WASM 2611 // keeps the art serialized by the server instead. 2612 fn chooseAnsiArtForNavigation(session: *Session) void { 2613 if (comptime ansi_arts.len == 0) return; 2614 const io = session.io orelse return; 2615 var random: usize = undefined; 2616 io.random(std.mem.asBytes(&random)); 2617 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2618 } 2619