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