╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3003 // focused widget is a text input 3004 fn focusedPane(root_focus: *const Focus) ?usize { 3005 var current = root_focus.grandchild_id orelse return null; 3006 switch ((root_focus.children.get(current) orelse return null).focus.kind) { 3007 .text_input, .text_input_password, .text_area => return null, 3008 else => {}, 3009 } 3010 while (root_focus.children.get(current)) |child| { 3011 if (child.focus.scroll) |info| if (info.direction != .horiz) return current; 3012 current = child.parent_id; 3013 } 3014 return null; 3015 } 3016 3017 // a link to bytes the server serves directly rather than to a page route, so 3018 // it never resolves to a RoutablePage 3019 pub const raw_link_prefix = "ax:"; 3020 3021 // a route already loaded into the current page; focusing it changes the url 3022 // without rebuilding the page 3023 pub const in_page_link_prefix = "ai:"; 3024 3025 // an in-page tab's base route, or the exact current route when it is selected 3026 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 3027 const target = if (selected) session.data.current_page else route; 3028 return session.page_arena.allocator().print("{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 3029 } 3030 3031 // a button the web renderer covers with a file picker that posts the chosen 3032 // file to the url after the prefix 3033 pub const file_input_prefix = "file:"; 3034 3035 // a submit button that overrides its enclosing form's action 3036 pub const submit_action_prefix = "submit:"; 3037 3038 // one option of a radio field, as `radio:<name>=<value>` 3039 pub const radio_prefix = "radio:"; 3040 3041 // the url an `ax:` link points at, or null for anything else. a host that 3042 // suppresses the anchor's own navigation needs this to follow the link. 3043 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 3044 const child = root_focus.children.get(focus_id) orelse return null; 3045 const custom = switch (child.focus.kind) { 3046 .custom => |c| c, 3047 else => return null, 3048 }; 3049 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 3050 return custom[raw_link_prefix.len..]; 3051 } 3052 3053 fn requestRawLink(session: *Session, url: []const u8) !void { 3054 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 3055 } 3056 3057 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 3058 // an absolute url is already complete 3059 if (!std.mem.startsWith(u8, path, "/")) return path; 3060 return if (session.web_url) |web_url| 3061 try allocator.print("{s}{s}", .{ web_url, path }) 3062 else 3063 path; 3064 } 3065 3066 // if the focus target at focus_id is an `a:` link that should navigate (a 3067 // different parent page, or a different files directory within the repo page), 3068 // return its route; otherwise null. lets a host turn a click / enter on such a 3069 // link into a navigation rather than a focus change. 3070 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 3071 const current = data.current_page; 3072 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 3073 // a link to a different parent page always navigates; within a page, a 3074 // files-directory / commits-page / list-window change navigates while tab 3075 // links stay in-page. 3076 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; 3077 return null; 3078 } 3079 3080 // the page the clicked cell links to, from a link inside a text box's content 3081 fn cellPageLink(root: *Widget, x: usize, y: usize, data: Session.Data) ?RoutablePage { 3082 const grid = root.getGrid() orelse return null; 3083 const cell = grid.cell(x, y) catch return null; 3084 const link = cell.link orelse return null; 3085 if (link.len == 0 or link[0] != '/') return null; 3086 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(link) else RoutablePage.fromUrl(link); 3087 } 3088 3089 // whether the `a:` focus target links to the page already shown, which a click 3090 // leaves alone rather than focusing 3091 pub fn currentPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) bool { 3092 const route = pageLink(root_focus, focus_id, data, "a:") orelse return false; 3093 return route.eql(data.current_page); 3094 } 3095 3096 // the route carried by an `ai:` focus target, or null for any other target 3097 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 3098 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 3099 } 3100 3101 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 3102 const child = root_focus.children.get(focus_id) orelse return null; 3103 const custom = switch (child.focus.kind) { 3104 .custom => |c| c, 3105 else => return null, 3106 }; 3107 if (!std.mem.startsWith(u8, custom, prefix)) return null; 3108 const path = custom[prefix.len..]; 3109 // local sessions build (and parse) links with the repo identity elided 3110 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 3111 } 3112 3113 // an assignee as the detail view shows it: the stored email, and the name of 3114 // the user holding it on this instance when there is one 3115 pub const Assignee = struct { 3116 email: []const u8, 3117 name: ?[]const u8 = null, 3118 3119 pub fn display(self: Assignee) []const u8 { 3120 return self.name orelse self.email; 3121 } 3122 }; 3123 3124 // a display author, resolved against the admin db at read time 3125 pub const Author = union(enum) { 3126 unknown, 3127 email: []const u8, // an email no user matches 3128 user_name: []const u8, 3129 3130 // the commit author line's email, resolved like initFromEmail 3131 pub fn init( 3132 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 3133 arena: *std.heap.ArenaAllocator, 3134 author_line: []const u8, 3135 ) !Author { 3136 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 3137 } 3138 3139 // from a record's stored author email: it resolves to the user currently 3140 // holding it, else stays the email. the result is allocated in `arena`, 3141 // so the email may borrow transient memory. 3142 pub fn initFromEmail( 3143 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 3144 arena: *std.heap.ArenaAllocator, 3145 author_email: ?[]const u8, 3146 ) !Author { 3147 const email = author_email orelse return .unknown; 3148 if (haxy_moment) |moment| { 3149 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 3150 return .{ .user_name = user.name }; 3151 } 3152 } 3153 return .{ .email = try arena.allocator().dupe(u8, email) }; 3154 } 3155 }; 3156 3157 // the "a:" link to user `name`'s page. 3158 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 3159 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 3160 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 3161 return page_arena.allocator().print("a:{s}", .{url}); 3162 } 3163 3164 // a focusable " author " box showing `author`, linking to their user page 3165 // when it is a known user 3166 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 3167 const text = switch (author) { 3168 .unknown => "", 3169 .email, .user_name => |t| t, 3170 }; 3171 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 3172 errdefer tb.deinit(allocator); 3173 tb.getFocus().mode = .all; 3174 switch (author) { 3175 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 3176 else => {}, 3177 } 3178 return tb; 3179 } 3180 3181 // native-TUI navigation 3182 pub const Nav = struct { 3183 root: Widget, 3184 // backs the current page (its Page data and the page-scoped strings its 3185 // widgets build). owned here, swapped on each navigation. session.page_arena 3186 // tracks whichever of these is current so widgets allocate into it. 3187 arena: *std.heap.ArenaAllocator, 3188 // the login the current root was built for 3189 user_id: ?[evt.event_id_size]u8, 3190 history: std.ArrayList(Entry), 3191 3192 // each retained page keeps its own arena, freed when the page leaves the 3193 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 3194 // long-lived session from accumulating every page it ever visited. 3195 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 3196 3197 // cap on retained back-history so the chain can't grow memory without bound 3198 const max_history: usize = 16; 3199 3200 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 3201 session.back = .unavailable; 3202 const arena = try allocator.create(std.heap.ArenaAllocator); 3203 arena.* = std.heap.ArenaAllocator.init(allocator); 3204 errdefer { 3205 arena.deinit(); 3206 allocator.destroy(arena); 3207 } 3208 3209 return .{ 3210 .root = try initCurrentRoot(allocator, arena, session), 3211 .arena = arena, 3212 .user_id = session.userId(), 3213 .history = .empty, 3214 }; 3215 } 3216 3217 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3218 self.root.deinit(allocator); 3219 freeArena(allocator, self.arena); 3220 for (self.history.items) |*entry| { 3221 entry.root.deinit(allocator); 3222 freeArena(allocator, entry.arena); 3223 } 3224 self.history.deinit(allocator); 3225 } 3226 3227 // rebuild the current page in place from a current moment 3228 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3229 if (session.haxy_moment == null and session.local == null) return; 3230 const arena = try allocator.create(std.heap.ArenaAllocator); 3231 arena.* = std.heap.ArenaAllocator.init(allocator); 3232 errdefer freeArena(allocator, arena); 3233 3234 const new_root = try initCurrentRoot(allocator, arena, session); 3235 3236 self.root.deinit(allocator); 3237 freeArena(allocator, self.arena); 3238 self.root = new_root; 3239 self.arena = arena; 3240 self.user_id = session.userId(); 3241 } 3242 3243 // build the current page's root in `arena`, or the not-found page's when 3244 // the route names nothing 3245 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3246 session.page_arena = arena; 3247 const page = try arena.allocator().create(Page); 3248 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3249 error.NotFound => blk: { 3250 session.data.current_page = .not_found; 3251 break :blk .not_found; 3252 }, 3253 else => |e| return e, 3254 }; 3255 return initRoot(allocator, page, session); 3256 } 3257 3258 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3259 arena.deinit(); 3260 allocator.destroy(arena); 3261 } 3262 3263 // reconcile the displayed root with the session's navigation state. a 3264 // forward nav (current_page moved to a different parent page) pushes the 3265 // current root and builds the new page in a fresh arena; a back request 3266 // frees the current page and restores the previous one. when escape is 3267 // pressed with no history left we switch to the quit tab instead of quitting. 3268 // returns whether the root was replaced. 3269 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3270 // refresh: rebuild the current page in place from a current moment 3271 if (session.refresh_requested) { 3272 session.refresh_requested = false; 3273 try self.rebuild(allocator, session); 3274 return true; 3275 } 3276 3277 if (session.back == .requested) { 3278 if (self.history.pop()) |entry| { 3279 session.back = if (self.history.items.len == 0) .unavailable else .available; 3280 self.root.deinit(allocator); 3281 freeArena(allocator, self.arena); 3282 self.root = entry.root; 3283 self.arena = entry.arena; 3284 self.user_id = entry.user_id; 3285 session.page_arena = entry.arena; 3286 session.data.current_page = entry.route; 3287 // a page built under another login is rebuilt rather than restored 3288 if (!std.meta.eql(entry.user_id, session.userId())) { 3289 try self.rebuild(allocator, session); 3290 return true; 3291 } 3292 chooseAnsiArtForNavigation(session); 3293 return true; 3294 } 3295 session.back = .unavailable; 3296 // nothing to go back to; switch to the quit confirmation 3297 const root_focus = self.root.getFocus(); 3298 var iter = root_focus.children.iterator(); 3299 while (iter.next()) |entry| { 3300 switch (entry.value_ptr.focus.kind) { 3301 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3302 root_focus.setFocus(entry.key_ptr.*); 3303 // a Stack only builds its selected child, so the quit 3304 // button isn't in the focus tree until a build runs with 3305 // the quit tab selected. build once, then send a 3306 // synthetic arrow_down to drop focus from the tab onto 3307 // the button so it's ready to confirm. 3308 try self.root.build(allocator, .{ 3309 .min_size = .{ .width = null, .height = 40 }, 3310 .max_size = .{ .width = 80, .height = null }, 3311 }, root_focus); 3312 try self.root.input(allocator, .arrow_down, root_focus); 3313 break; 3314 }, 3315 else => {}, 3316 } 3317 } 3318 return false; 3319 } 3320 3321 // forward navigation: navigate() set next_page to the page to move to (a 3322 // cross-page link or tab change crossing pages) 3323 if (session.next_page) |route| { 3324 session.next_page = null; 3325 if (session.haxy_moment == null and session.local == null) return false; 3326 const previous_route = session.data.current_page; 3327 // the page we navigated to becomes the current page 3328 session.data.current_page = route; 3329 session.back = .available; 3330 3331 const arena = try allocator.create(std.heap.ArenaAllocator); 3332 arena.* = std.heap.ArenaAllocator.init(allocator); 3333 errdefer freeArena(allocator, arena); 3334 3335 const new_root = try initCurrentRoot(allocator, arena, session); 3336 3337 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3338 // drop the oldest entry (freeing its widget tree and arena) once over cap 3339 if (self.history.items.len > max_history) { 3340 var oldest = self.history.orderedRemove(0); 3341 oldest.root.deinit(allocator); 3342 freeArena(allocator, oldest.arena); 3343 } 3344 self.root = new_root; 3345 self.arena = arena; 3346 self.user_id = session.userId(); 3347 return true; 3348 } 3349 return false; 3350 } 3351 }; 3352 3353 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3354 const page_widget: Widget = switch (page.*) { 3355 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3356 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3357 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3358 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3359 .not_found => .{ .not_found = try .init(allocator, session) }, 3360 }; 3361 3362 chooseAnsiArtForNavigation(session); 3363 3364 // on the TUI/SSH, the page sits above a one-row footer showing its web 3365 // url, when there's a web UI to point at 3366 var root = if (session.is_terminal) blk: { 3367 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3368 errdefer box.deinit(allocator); 3369 const bg_id = bg_blk: { 3370 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3371 errdefer background.deinit(allocator); 3372 const id = background.getFocus().id; 3373 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3374 break :bg_blk id; 3375 }; 3376 if (session.web_url != null) { 3377 var footer = try widget.Footer.init(allocator, session); 3378 errdefer footer.deinit(allocator); 3379 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3380 } 3381 box.getFocus().child_id = bg_id; 3382 break :blk Widget{ .box = box }; 3383 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3384 errdefer root.deinit(allocator); 3385 3386 // input-owning views build their TextInputs in init, so reset their 3387 // registrations here 3388 session.text_inputs.clearRetainingCapacity(); 3389 3390 try root.build(allocator, .{ 3391 .min_size = .{ .width = null, .height = 40 }, 3392 .max_size = .{ .width = 100, .height = null }, 3393 }, root.getFocus()); 3394 3395 return root; 3396 } 3397 3398 // native and server-side sessions choose from the embedded collection. WASM 3399 // keeps the art serialized by the server instead. 3400 fn chooseAnsiArtForNavigation(session: *Session) void { 3401 if (comptime ansi_art.art.len == 0) return; 3402 if (session.data.current_page == .not_found) { 3403 session.data.ansi_art = ansi_art.not_found; 3404 return; 3405 } 3406 const io = session.io orelse return; 3407 var random: usize = undefined; 3408 io.random(std.mem.asBytes(&random)); 3409 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3410 } 3411