╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2599 author_email: ?[]const u8, 2600 ) !Author { 2601 const email = author_email orelse return .unknown; 2602 if (haxy_moment) |moment| { 2603 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2604 return .{ .user_name = user.event.name }; 2605 } 2606 } 2607 return .{ .email = try arena.allocator().dupe(u8, email) }; 2608 } 2609 }; 2610 2611 // the "a:" link to user `name`'s page. 2612 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2613 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2614 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2615 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2616 } 2617 2618 // a focusable " author " box showing `author`, linking to their user page 2619 // when it is a known user 2620 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2621 const text = switch (author) { 2622 .unknown => "", 2623 .email, .user_name => |t| t, 2624 }; 2625 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2626 errdefer tb.deinit(allocator); 2627 tb.getFocus().mode = .all; 2628 switch (author) { 2629 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2630 else => {}, 2631 } 2632 return tb; 2633 } 2634 2635 // native-TUI navigation 2636 pub const Nav = struct { 2637 root: Widget, 2638 // backs the current page (its Page data and the page-scoped strings its 2639 // widgets build). owned here, swapped on each navigation. session.page_arena 2640 // tracks whichever of these is current so widgets allocate into it. 2641 arena: *std.heap.ArenaAllocator, 2642 history: std.ArrayList(Entry), 2643 2644 // each retained page keeps its own arena, freed when the page leaves the 2645 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2646 // long-lived session from accumulating every page it ever visited. 2647 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2648 2649 // cap on retained back-history so the chain can't grow memory without bound 2650 const max_history: usize = 16; 2651 2652 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2653 session.back = .unavailable; 2654 const arena = try allocator.create(std.heap.ArenaAllocator); 2655 arena.* = std.heap.ArenaAllocator.init(allocator); 2656 errdefer { 2657 arena.deinit(); 2658 allocator.destroy(arena); 2659 } 2660 2661 session.page_arena = arena; 2662 const route = session.data.current_page; 2663 2664 const page = try arena.allocator().create(Page); 2665 page.* = try Page.init(arena, session, route); 2666 return .{ 2667 .root = try initRoot(allocator, page, session), 2668 .arena = arena, 2669 .history = .empty, 2670 }; 2671 } 2672 2673 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2674 self.root.deinit(allocator); 2675 freeArena(allocator, self.arena); 2676 for (self.history.items) |*entry| { 2677 entry.root.deinit(allocator); 2678 freeArena(allocator, entry.arena); 2679 } 2680 self.history.deinit(allocator); 2681 } 2682 2683 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2684 arena.deinit(); 2685 allocator.destroy(arena); 2686 } 2687 2688 // reconcile the displayed root with the session's navigation state. a 2689 // forward nav (current_page moved to a different parent page) pushes the 2690 // current root and builds the new page in a fresh arena; a back request 2691 // frees the current page and restores the previous one. when escape is 2692 // pressed with no history left we switch to the quit tab instead of quitting. 2693 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2694 // refresh: rebuild the current page in place from a current moment 2695 if (session.refresh_requested) { 2696 session.refresh_requested = false; 2697 if (session.haxy_moment != null or session.local != null) { 2698 const arena = try allocator.create(std.heap.ArenaAllocator); 2699 arena.* = std.heap.ArenaAllocator.init(allocator); 2700 errdefer freeArena(allocator, arena); 2701 2702 session.page_arena = arena; 2703 2704 const route = session.data.current_page; 2705 const page = try arena.allocator().create(Page); 2706 page.* = try Page.init(arena, session, route); 2707 const new_root = try initRoot(allocator, page, session); 2708 2709 self.root.deinit(allocator); 2710 freeArena(allocator, self.arena); 2711 self.root = new_root; 2712 self.arena = arena; 2713 } 2714 return; 2715 } 2716 2717 if (session.back == .requested) { 2718 if (self.history.pop()) |entry| { 2719 session.back = if (self.history.items.len == 0) .unavailable else .available; 2720 self.root.deinit(allocator); 2721 freeArena(allocator, self.arena); 2722 self.root = entry.root; 2723 self.arena = entry.arena; 2724 session.page_arena = entry.arena; 2725 session.data.current_page = entry.route; 2726 chooseAnsiArtForNavigation(session); 2727 return; 2728 } 2729 session.back = .unavailable; 2730 // nothing to go back to; switch to the quit confirmation 2731 const root_focus = self.root.getFocus(); 2732 var iter = root_focus.children.iterator(); 2733 while (iter.next()) |entry| { 2734 switch (entry.value_ptr.focus.kind) { 2735 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2736 root_focus.setFocus(entry.key_ptr.*); 2737 // a Stack only builds its selected child, so the quit 2738 // button isn't in the focus tree until a build runs with 2739 // the quit tab selected. build once, then send a 2740 // synthetic arrow_down to drop focus from the tab onto 2741 // the button so it's ready to confirm. 2742 try self.root.build(allocator, .{ 2743 .min_size = .{ .width = null, .height = 40 }, 2744 .max_size = .{ .width = 80, .height = null }, 2745 }, root_focus); 2746 try self.root.input(allocator, .arrow_down, root_focus); 2747 break; 2748 }, 2749 else => {}, 2750 } 2751 } 2752 return; 2753 } 2754 2755 // forward navigation: navigate() set next_page to the page to move to (a 2756 // cross-page link or tab change crossing pages) 2757 if (session.next_page) |route| { 2758 session.next_page = null; 2759 if (session.haxy_moment == null and session.local == null) return; 2760 const previous_route = session.data.current_page; 2761 // the page we navigated to becomes the current page 2762 session.data.current_page = route; 2763 session.back = .available; 2764 2765 const arena = try allocator.create(std.heap.ArenaAllocator); 2766 arena.* = std.heap.ArenaAllocator.init(allocator); 2767 errdefer freeArena(allocator, arena); 2768 2769 session.page_arena = arena; 2770 2771 const page = try arena.allocator().create(Page); 2772 page.* = try Page.init(arena, session, route); 2773 const new_root = try initRoot(allocator, page, session); 2774 2775 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2776 // drop the oldest entry (freeing its widget tree and arena) once over cap 2777 if (self.history.items.len > max_history) { 2778 var oldest = self.history.orderedRemove(0); 2779 oldest.root.deinit(allocator); 2780 freeArena(allocator, oldest.arena); 2781 } 2782 self.root = new_root; 2783 self.arena = arena; 2784 } 2785 } 2786 }; 2787 2788 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2789 const page_widget: Widget = switch (page.*) { 2790 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2791 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2792 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2793 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2794 }; 2795 2796 chooseAnsiArtForNavigation(session); 2797 2798 // on the TUI/SSH, the page sits above a one-row footer showing the url 2799 var root = if (session.is_terminal) blk: { 2800 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2801 errdefer box.deinit(allocator); 2802 const bg_id = bg_blk: { 2803 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2804 errdefer background.deinit(allocator); 2805 const id = background.getFocus().id; 2806 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2807 break :bg_blk id; 2808 }; 2809 { 2810 var footer = try widget.Footer.init(allocator, session); 2811 errdefer footer.deinit(allocator); 2812 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2813 } 2814 box.getFocus().child_id = bg_id; 2815 break :blk Widget{ .box = box }; 2816 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2817 errdefer root.deinit(allocator); 2818 2819 // input-owning views build their TextInputs in init, so reset their 2820 // registrations here 2821 session.text_inputs.clearRetainingCapacity(); 2822 2823 try root.build(allocator, .{ 2824 .min_size = .{ .width = null, .height = 40 }, 2825 .max_size = .{ .width = 100, .height = null }, 2826 }, root.getFocus()); 2827 2828 return root; 2829 } 2830 2831 // native and server-side sessions choose from the embedded collection. WASM 2832 // keeps the art serialized by the server instead. 2833 fn chooseAnsiArtForNavigation(session: *Session) void { 2834 if (comptime ansi_arts.len == 0) return; 2835 const io = session.io orelse return; 2836 var random: usize = undefined; 2837 io.random(std.mem.asBytes(&random)); 2838 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2839 } 2840