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