╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2948 // the user holding it on this instance when there is one 2949 pub const Assignee = struct { 2950 email: []const u8, 2951 name: ?[]const u8 = null, 2952 2953 pub fn display(self: Assignee) []const u8 { 2954 return self.name orelse self.email; 2955 } 2956 }; 2957 2958 // a display author, resolved against the admin db at read time 2959 pub const Author = union(enum) { 2960 unknown, 2961 email: []const u8, // an email no user matches 2962 user_name: []const u8, 2963 2964 // the commit author line's email, resolved like initFromEmail 2965 pub fn init( 2966 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2967 arena: *std.heap.ArenaAllocator, 2968 author_line: []const u8, 2969 ) !Author { 2970 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2971 } 2972 2973 // from a record's stored author email: it resolves to the user currently 2974 // holding it, else stays the email. the result is allocated in `arena`, 2975 // so the email may borrow transient memory. 2976 pub fn initFromEmail( 2977 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2978 arena: *std.heap.ArenaAllocator, 2979 author_email: ?[]const u8, 2980 ) !Author { 2981 const email = author_email orelse return .unknown; 2982 if (haxy_moment) |moment| { 2983 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2984 return .{ .user_name = user.name }; 2985 } 2986 } 2987 return .{ .email = try arena.allocator().dupe(u8, email) }; 2988 } 2989 }; 2990 2991 // the "a:" link to user `name`'s page. 2992 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2993 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2994 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2995 return page_arena.allocator().print("a:{s}", .{url}); 2996 } 2997 2998 // a focusable " author " box showing `author`, linking to their user page 2999 // when it is a known user 3000 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 3001 const text = switch (author) { 3002 .unknown => "", 3003 .email, .user_name => |t| t, 3004 }; 3005 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 3006 errdefer tb.deinit(allocator); 3007 tb.getFocus().mode = .all; 3008 switch (author) { 3009 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 3010 else => {}, 3011 } 3012 return tb; 3013 } 3014 3015 // native-TUI navigation 3016 pub const Nav = struct { 3017 root: Widget, 3018 // backs the current page (its Page data and the page-scoped strings its 3019 // widgets build). owned here, swapped on each navigation. session.page_arena 3020 // tracks whichever of these is current so widgets allocate into it. 3021 arena: *std.heap.ArenaAllocator, 3022 // the login the current root was built for 3023 user_id: ?[evt.event_id_size]u8, 3024 history: std.ArrayList(Entry), 3025 3026 // each retained page keeps its own arena, freed when the page leaves the 3027 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 3028 // long-lived session from accumulating every page it ever visited. 3029 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 3030 3031 // cap on retained back-history so the chain can't grow memory without bound 3032 const max_history: usize = 16; 3033 3034 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 3035 session.back = .unavailable; 3036 const arena = try allocator.create(std.heap.ArenaAllocator); 3037 arena.* = std.heap.ArenaAllocator.init(allocator); 3038 errdefer { 3039 arena.deinit(); 3040 allocator.destroy(arena); 3041 } 3042 3043 return .{ 3044 .root = try initCurrentRoot(allocator, arena, session), 3045 .arena = arena, 3046 .user_id = session.userId(), 3047 .history = .empty, 3048 }; 3049 } 3050 3051 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3052 self.root.deinit(allocator); 3053 freeArena(allocator, self.arena); 3054 for (self.history.items) |*entry| { 3055 entry.root.deinit(allocator); 3056 freeArena(allocator, entry.arena); 3057 } 3058 self.history.deinit(allocator); 3059 } 3060 3061 // rebuild the current page in place from a current moment 3062 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3063 if (session.haxy_moment == null and session.local == null) return; 3064 const arena = try allocator.create(std.heap.ArenaAllocator); 3065 arena.* = std.heap.ArenaAllocator.init(allocator); 3066 errdefer freeArena(allocator, arena); 3067 3068 const new_root = try initCurrentRoot(allocator, arena, session); 3069 3070 self.root.deinit(allocator); 3071 freeArena(allocator, self.arena); 3072 self.root = new_root; 3073 self.arena = arena; 3074 self.user_id = session.userId(); 3075 } 3076 3077 // build the current page's root in `arena`, or the not-found page's when 3078 // the route names nothing 3079 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3080 session.page_arena = arena; 3081 const page = try arena.allocator().create(Page); 3082 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3083 error.NotFound => blk: { 3084 session.data.current_page = .not_found; 3085 break :blk .not_found; 3086 }, 3087 else => |e| return e, 3088 }; 3089 return initRoot(allocator, page, session); 3090 } 3091 3092 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3093 arena.deinit(); 3094 allocator.destroy(arena); 3095 } 3096 3097 // reconcile the displayed root with the session's navigation state. a 3098 // forward nav (current_page moved to a different parent page) pushes the 3099 // current root and builds the new page in a fresh arena; a back request 3100 // frees the current page and restores the previous one. when escape is 3101 // pressed with no history left we switch to the quit tab instead of quitting. 3102 // returns whether the root was replaced. 3103 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3104 // refresh: rebuild the current page in place from a current moment 3105 if (session.refresh_requested) { 3106 session.refresh_requested = false; 3107 try self.rebuild(allocator, session); 3108 return true; 3109 } 3110 3111 if (session.back == .requested) { 3112 if (self.history.pop()) |entry| { 3113 session.back = if (self.history.items.len == 0) .unavailable else .available; 3114 self.root.deinit(allocator); 3115 freeArena(allocator, self.arena); 3116 self.root = entry.root; 3117 self.arena = entry.arena; 3118 self.user_id = entry.user_id; 3119 session.page_arena = entry.arena; 3120 session.data.current_page = entry.route; 3121 // a page built under another login is rebuilt rather than restored 3122 if (!std.meta.eql(entry.user_id, session.userId())) { 3123 try self.rebuild(allocator, session); 3124 return true; 3125 } 3126 chooseAnsiArtForNavigation(session); 3127 return true; 3128 } 3129 session.back = .unavailable; 3130 // nothing to go back to; switch to the quit confirmation 3131 const root_focus = self.root.getFocus(); 3132 var iter = root_focus.children.iterator(); 3133 while (iter.next()) |entry| { 3134 switch (entry.value_ptr.focus.kind) { 3135 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3136 root_focus.setFocus(entry.key_ptr.*); 3137 // a Stack only builds its selected child, so the quit 3138 // button isn't in the focus tree until a build runs with 3139 // the quit tab selected. build once, then send a 3140 // synthetic arrow_down to drop focus from the tab onto 3141 // the button so it's ready to confirm. 3142 try self.root.build(allocator, .{ 3143 .min_size = .{ .width = null, .height = 40 }, 3144 .max_size = .{ .width = 80, .height = null }, 3145 }, root_focus); 3146 try self.root.input(allocator, .arrow_down, root_focus); 3147 break; 3148 }, 3149 else => {}, 3150 } 3151 } 3152 return false; 3153 } 3154 3155 // forward navigation: navigate() set next_page to the page to move to (a 3156 // cross-page link or tab change crossing pages) 3157 if (session.next_page) |route| { 3158 session.next_page = null; 3159 if (session.haxy_moment == null and session.local == null) return false; 3160 const previous_route = session.data.current_page; 3161 // the page we navigated to becomes the current page 3162 session.data.current_page = route; 3163 session.back = .available; 3164 3165 const arena = try allocator.create(std.heap.ArenaAllocator); 3166 arena.* = std.heap.ArenaAllocator.init(allocator); 3167 errdefer freeArena(allocator, arena); 3168 3169 const new_root = try initCurrentRoot(allocator, arena, session); 3170 3171 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3172 // drop the oldest entry (freeing its widget tree and arena) once over cap 3173 if (self.history.items.len > max_history) { 3174 var oldest = self.history.orderedRemove(0); 3175 oldest.root.deinit(allocator); 3176 freeArena(allocator, oldest.arena); 3177 } 3178 self.root = new_root; 3179 self.arena = arena; 3180 self.user_id = session.userId(); 3181 return true; 3182 } 3183 return false; 3184 } 3185 }; 3186 3187 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3188 const page_widget: Widget = switch (page.*) { 3189 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3190 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3191 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3192 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3193 .not_found => .{ .not_found = try .init(allocator, session) }, 3194 }; 3195 3196 chooseAnsiArtForNavigation(session); 3197 3198 // on the TUI/SSH, the page sits above a one-row footer showing its web 3199 // url, when there's a web UI to point at 3200 var root = if (session.is_terminal) blk: { 3201 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3202 errdefer box.deinit(allocator); 3203 const bg_id = bg_blk: { 3204 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3205 errdefer background.deinit(allocator); 3206 const id = background.getFocus().id; 3207 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3208 break :bg_blk id; 3209 }; 3210 if (session.web_port != null) { 3211 var footer = try widget.Footer.init(allocator, session); 3212 errdefer footer.deinit(allocator); 3213 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3214 } 3215 box.getFocus().child_id = bg_id; 3216 break :blk Widget{ .box = box }; 3217 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3218 errdefer root.deinit(allocator); 3219 3220 // input-owning views build their TextInputs in init, so reset their 3221 // registrations here 3222 session.text_inputs.clearRetainingCapacity(); 3223 3224 try root.build(allocator, .{ 3225 .min_size = .{ .width = null, .height = 40 }, 3226 .max_size = .{ .width = 100, .height = null }, 3227 }, root.getFocus()); 3228 3229 return root; 3230 } 3231 3232 // native and server-side sessions choose from the embedded collection. WASM 3233 // keeps the art serialized by the server instead. 3234 fn chooseAnsiArtForNavigation(session: *Session) void { 3235 if (comptime ansi_art.art.len == 0) return; 3236 if (session.data.current_page == .not_found) { 3237 session.data.ansi_art = ansi_art.not_found; 3238 return; 3239 } 3240 const io = session.io orelse return; 3241 var random: usize = undefined; 3242 io.random(std.mem.asBytes(&random)); 3243 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3244 } 3245