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