╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2417 return session.navigate(route); 2418 } 2419 if (rawLink(root_focus, focus_id)) |url| { 2420 if (!mouse.ctrl) try requestRawLink(session, url); 2421 return; 2422 } 2423 root_focus.setFocus(focus_id); 2424 activate_in_page_link = true; 2425 } 2426 // forward the press into the widget tree so buttons (and any 2427 // future click-aware widgets) can react. widgets that don't 2428 // care about presses ignore it. 2429 try root.input(allocator, key, root_focus); 2430 } else { 2431 try root.input(allocator, key, root_focus); 2432 } 2433 }, 2434 else => try root.input(allocator, key, root_focus), 2435 } 2436 2437 const focused_after = root_focus.grandchild_id; 2438 if (!activate_in_page_link and focused_before == focused_after) return; 2439 const focus_id = focused_after orelse return; 2440 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2441 } 2442 2443 // a link to bytes the server serves directly rather than to a page route, so 2444 // it never resolves to a RoutablePage 2445 pub const raw_link_prefix = "ax:"; 2446 2447 // a route already loaded into the current page; focusing it changes the url 2448 // without rebuilding the page 2449 pub const in_page_link_prefix = "ai:"; 2450 2451 // an in-page tab's base route, or the exact current route when it is selected 2452 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2453 const target = if (selected) session.data.current_page else route; 2454 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2455 } 2456 2457 // a button the web renderer covers with a file picker that posts the chosen 2458 // file to the url after the prefix 2459 pub const file_input_prefix = "file:"; 2460 2461 // a submit button that overrides its enclosing form's action 2462 pub const submit_action_prefix = "submit:"; 2463 2464 // the url an `ax:` link points at, or null for anything else. a host that 2465 // suppresses the anchor's own navigation needs this to follow the link. 2466 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2467 const child = root_focus.children.get(focus_id) orelse return null; 2468 const custom = switch (child.focus.kind) { 2469 .custom => |c| c, 2470 else => return null, 2471 }; 2472 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2473 return custom[raw_link_prefix.len..]; 2474 } 2475 2476 fn requestRawLink(session: *Session, url: []const u8) !void { 2477 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2478 } 2479 2480 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2481 return if (session.web_port) |port| 2482 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2483 else 2484 path; 2485 } 2486 2487 // if the focus target at focus_id is an `a:` link that should navigate (a 2488 // different parent page, or a different files directory within the repo page), 2489 // return its route; otherwise null. lets a host turn a click / enter on such a 2490 // link into a navigation rather than a focus change. 2491 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2492 const current = data.current_page; 2493 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2494 // a link to a different parent page always navigates; within a page, a 2495 // files-directory / commits-page / list-window change navigates while tab 2496 // links stay in-page. 2497 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; 2498 return null; 2499 } 2500 2501 // the route carried by an `ai:` focus target, or null for any other target 2502 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2503 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2504 } 2505 2506 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2507 const child = root_focus.children.get(focus_id) orelse return null; 2508 const custom = switch (child.focus.kind) { 2509 .custom => |c| c, 2510 else => return null, 2511 }; 2512 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2513 const path = custom[prefix.len..]; 2514 // local sessions build (and parse) links with the repo identity elided 2515 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2516 } 2517 2518 // a display author, resolved against the admin db at read time 2519 pub const Author = union(enum) { 2520 unknown, 2521 email: []const u8, // an email no user matches 2522 user_name: []const u8, 2523 2524 // the commit author line's email, resolved like initFromEmail 2525 pub fn init( 2526 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2527 arena: *std.heap.ArenaAllocator, 2528 author_line: []const u8, 2529 ) !Author { 2530 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2531 } 2532 2533 // from a record's stored author email: it resolves to the user currently 2534 // holding it, else stays the email. the result is allocated in `arena`, 2535 // so the email may borrow transient memory. 2536 pub fn initFromEmail( 2537 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2538 arena: *std.heap.ArenaAllocator, 2539 author_email: ?[]const u8, 2540 ) !Author { 2541 const email = author_email orelse return .unknown; 2542 if (haxy_moment) |moment| { 2543 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2544 return .{ .user_name = user.event.name }; 2545 } 2546 } 2547 return .{ .email = try arena.allocator().dupe(u8, email) }; 2548 } 2549 }; 2550 2551 // the "a:" link to user `name`'s page. 2552 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2553 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2554 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2555 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2556 } 2557 2558 // a focusable " author " box showing `author`, linking to their user page 2559 // when it is a known user 2560 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2561 const text = switch (author) { 2562 .unknown => "", 2563 .email, .user_name => |t| t, 2564 }; 2565 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2566 errdefer tb.deinit(allocator); 2567 tb.getFocus().mode = .all; 2568 switch (author) { 2569 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2570 else => {}, 2571 } 2572 return tb; 2573 } 2574 2575 // native-TUI navigation 2576 pub const Nav = struct { 2577 root: Widget, 2578 // backs the current page (its Page data and the page-scoped strings its 2579 // widgets build). owned here, swapped on each navigation. session.page_arena 2580 // tracks whichever of these is current so widgets allocate into it. 2581 arena: *std.heap.ArenaAllocator, 2582 history: std.ArrayList(Entry), 2583 2584 // each retained page keeps its own arena, freed when the page leaves the 2585 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2586 // long-lived session from accumulating every page it ever visited. 2587 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2588 2589 // cap on retained back-history so the chain can't grow memory without bound 2590 const max_history: usize = 16; 2591 2592 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2593 session.back = .unavailable; 2594 const arena = try allocator.create(std.heap.ArenaAllocator); 2595 arena.* = std.heap.ArenaAllocator.init(allocator); 2596 errdefer { 2597 arena.deinit(); 2598 allocator.destroy(arena); 2599 } 2600 2601 session.page_arena = arena; 2602 const route = session.data.current_page; 2603 2604 const page = try arena.allocator().create(Page); 2605 page.* = try Page.init(arena, session, route); 2606 return .{ 2607 .root = try initRoot(allocator, page, session), 2608 .arena = arena, 2609 .history = .empty, 2610 }; 2611 } 2612 2613 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2614 self.root.deinit(allocator); 2615 freeArena(allocator, self.arena); 2616 for (self.history.items) |*entry| { 2617 entry.root.deinit(allocator); 2618 freeArena(allocator, entry.arena); 2619 } 2620 self.history.deinit(allocator); 2621 } 2622 2623 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2624 arena.deinit(); 2625 allocator.destroy(arena); 2626 } 2627 2628 // reconcile the displayed root with the session's navigation state. a 2629 // forward nav (current_page moved to a different parent page) pushes the 2630 // current root and builds the new page in a fresh arena; a back request 2631 // frees the current page and restores the previous one. when escape is 2632 // pressed with no history left we switch to the quit tab instead of quitting. 2633 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2634 // refresh: rebuild the current page in place from a current moment 2635 if (session.refresh_requested) { 2636 session.refresh_requested = false; 2637 if (session.haxy_moment != null or session.local != null) { 2638 const arena = try allocator.create(std.heap.ArenaAllocator); 2639 arena.* = std.heap.ArenaAllocator.init(allocator); 2640 errdefer freeArena(allocator, arena); 2641 2642 session.page_arena = arena; 2643 2644 const route = session.data.current_page; 2645 const page = try arena.allocator().create(Page); 2646 page.* = try Page.init(arena, session, route); 2647 const new_root = try initRoot(allocator, page, session); 2648 2649 self.root.deinit(allocator); 2650 freeArena(allocator, self.arena); 2651 self.root = new_root; 2652 self.arena = arena; 2653 } 2654 return; 2655 } 2656 2657 if (session.back == .requested) { 2658 if (self.history.pop()) |entry| { 2659 session.back = if (self.history.items.len == 0) .unavailable else .available; 2660 self.root.deinit(allocator); 2661 freeArena(allocator, self.arena); 2662 self.root = entry.root; 2663 self.arena = entry.arena; 2664 session.page_arena = entry.arena; 2665 session.data.current_page = entry.route; 2666 chooseAnsiArtForNavigation(session); 2667 return; 2668 } 2669 session.back = .unavailable; 2670 // nothing to go back to; switch to the quit confirmation 2671 const root_focus = self.root.getFocus(); 2672 var iter = root_focus.children.iterator(); 2673 while (iter.next()) |entry| { 2674 switch (entry.value_ptr.focus.kind) { 2675 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2676 root_focus.setFocus(entry.key_ptr.*); 2677 // a Stack only builds its selected child, so the quit 2678 // button isn't in the focus tree until a build runs with 2679 // the quit tab selected. build once, then send a 2680 // synthetic arrow_down to drop focus from the tab onto 2681 // the button so it's ready to confirm. 2682 try self.root.build(allocator, .{ 2683 .min_size = .{ .width = null, .height = 40 }, 2684 .max_size = .{ .width = 80, .height = null }, 2685 }, root_focus); 2686 try self.root.input(allocator, .arrow_down, root_focus); 2687 break; 2688 }, 2689 else => {}, 2690 } 2691 } 2692 return; 2693 } 2694 2695 // forward navigation: navigate() set next_page to the page to move to (a 2696 // cross-page link or tab change crossing pages) 2697 if (session.next_page) |route| { 2698 session.next_page = null; 2699 if (session.haxy_moment == null and session.local == null) return; 2700 const previous_route = session.data.current_page; 2701 // the page we navigated to becomes the current page 2702 session.data.current_page = route; 2703 session.back = .available; 2704 2705 const arena = try allocator.create(std.heap.ArenaAllocator); 2706 arena.* = std.heap.ArenaAllocator.init(allocator); 2707 errdefer freeArena(allocator, arena); 2708 2709 session.page_arena = arena; 2710 2711 const page = try arena.allocator().create(Page); 2712 page.* = try Page.init(arena, session, route); 2713 const new_root = try initRoot(allocator, page, session); 2714 2715 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2716 // drop the oldest entry (freeing its widget tree and arena) once over cap 2717 if (self.history.items.len > max_history) { 2718 var oldest = self.history.orderedRemove(0); 2719 oldest.root.deinit(allocator); 2720 freeArena(allocator, oldest.arena); 2721 } 2722 self.root = new_root; 2723 self.arena = arena; 2724 } 2725 } 2726 }; 2727 2728 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2729 const page_widget: Widget = switch (page.*) { 2730 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2731 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2732 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2733 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2734 }; 2735 2736 chooseAnsiArtForNavigation(session); 2737 2738 // on the TUI/SSH, the page sits above a one-row footer showing the url 2739 var root = if (session.is_terminal) blk: { 2740 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2741 errdefer box.deinit(allocator); 2742 const bg_id = bg_blk: { 2743 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2744 errdefer background.deinit(allocator); 2745 const id = background.getFocus().id; 2746 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2747 break :bg_blk id; 2748 }; 2749 { 2750 var footer = try widget.Footer.init(allocator, session); 2751 errdefer footer.deinit(allocator); 2752 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2753 } 2754 box.getFocus().child_id = bg_id; 2755 break :blk Widget{ .box = box }; 2756 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2757 errdefer root.deinit(allocator); 2758 2759 // input-owning views build their TextInputs in init, so reset their 2760 // registrations here 2761 session.text_inputs.clearRetainingCapacity(); 2762 2763 try root.build(allocator, .{ 2764 .min_size = .{ .width = null, .height = 40 }, 2765 .max_size = .{ .width = 100, .height = null }, 2766 }, root.getFocus()); 2767 2768 return root; 2769 } 2770 2771 // native and server-side sessions choose from the embedded collection. WASM 2772 // keeps the art serialized by the server instead. 2773 fn chooseAnsiArtForNavigation(session: *Session) void { 2774 if (comptime ansi_arts.len == 0) return; 2775 const io = session.io orelse return; 2776 var random: usize = undefined; 2777 io.random(std.mem.asBytes(&random)); 2778 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2779 } 2780