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