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