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