╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2170 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2171 const text = switch (author) { 2172 .unknown => "", 2173 .email, .user_name => |t| t, 2174 }; 2175 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2176 errdefer tb.deinit(allocator); 2177 tb.getFocus().mode = .all; 2178 switch (author) { 2179 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2180 else => {}, 2181 } 2182 return tb; 2183 } 2184 2185 // native-TUI navigation 2186 pub const Nav = struct { 2187 root: Widget, 2188 // backs the current page (its Page data and the page-scoped strings its 2189 // widgets build). owned here, swapped on each navigation. session.page_arena 2190 // tracks whichever of these is current so widgets allocate into it. 2191 arena: *std.heap.ArenaAllocator, 2192 history: std.ArrayList(Entry), 2193 2194 // each retained page keeps its own arena, freed when the page leaves the 2195 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2196 // long-lived session from accumulating every page it ever visited. 2197 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2198 2199 // cap on retained back-history so the chain can't grow memory without bound 2200 const max_history: usize = 16; 2201 2202 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2203 session.back = .unavailable; 2204 const arena = try allocator.create(std.heap.ArenaAllocator); 2205 arena.* = std.heap.ArenaAllocator.init(allocator); 2206 errdefer { 2207 arena.deinit(); 2208 allocator.destroy(arena); 2209 } 2210 2211 session.page_arena = arena; 2212 const route = session.data.current_page; 2213 2214 const page = try arena.allocator().create(Page); 2215 page.* = try Page.init(arena, session, route); 2216 return .{ 2217 .root = try initRoot(allocator, page, session), 2218 .arena = arena, 2219 .history = .empty, 2220 }; 2221 } 2222 2223 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2224 self.root.deinit(allocator); 2225 freeArena(allocator, self.arena); 2226 for (self.history.items) |*entry| { 2227 entry.root.deinit(allocator); 2228 freeArena(allocator, entry.arena); 2229 } 2230 self.history.deinit(allocator); 2231 } 2232 2233 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2234 arena.deinit(); 2235 allocator.destroy(arena); 2236 } 2237 2238 // reconcile the displayed root with the session's navigation state. a 2239 // forward nav (current_page moved to a different parent page) pushes the 2240 // current root and builds the new page in a fresh arena; a back request 2241 // frees the current page and restores the previous one. when escape is 2242 // pressed with no history left we switch to the quit tab instead of quitting. 2243 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2244 // refresh: rebuild the current page in place from a current moment 2245 if (session.refresh_requested) { 2246 session.refresh_requested = false; 2247 if (session.haxy_moment != null or session.local != null) { 2248 const arena = try allocator.create(std.heap.ArenaAllocator); 2249 arena.* = std.heap.ArenaAllocator.init(allocator); 2250 errdefer freeArena(allocator, arena); 2251 2252 session.page_arena = arena; 2253 2254 const route = session.data.current_page; 2255 const page = try arena.allocator().create(Page); 2256 page.* = try Page.init(arena, session, route); 2257 const new_root = try initRoot(allocator, page, session); 2258 2259 self.root.deinit(allocator); 2260 freeArena(allocator, self.arena); 2261 self.root = new_root; 2262 self.arena = arena; 2263 } 2264 return; 2265 } 2266 2267 if (session.back == .requested) { 2268 if (self.history.pop()) |entry| { 2269 session.back = if (self.history.items.len == 0) .unavailable else .available; 2270 self.root.deinit(allocator); 2271 freeArena(allocator, self.arena); 2272 self.root = entry.root; 2273 self.arena = entry.arena; 2274 session.page_arena = entry.arena; 2275 session.data.current_page = entry.route; 2276 chooseAnsiArtForNavigation(session); 2277 return; 2278 } 2279 session.back = .unavailable; 2280 // nothing to go back to; switch to the quit confirmation 2281 const root_focus = self.root.getFocus(); 2282 var iter = root_focus.children.iterator(); 2283 while (iter.next()) |entry| { 2284 switch (entry.value_ptr.focus.kind) { 2285 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2286 root_focus.setFocus(entry.key_ptr.*); 2287 // a Stack only builds its selected child, so the quit 2288 // button isn't in the focus tree until a build runs with 2289 // the quit tab selected. build once, then send a 2290 // synthetic arrow_down to drop focus from the tab onto 2291 // the button so it's ready to confirm. 2292 try self.root.build(allocator, .{ 2293 .min_size = .{ .width = null, .height = 40 }, 2294 .max_size = .{ .width = 80, .height = null }, 2295 }, root_focus); 2296 try self.root.input(allocator, .arrow_down, root_focus); 2297 break; 2298 }, 2299 else => {}, 2300 } 2301 } 2302 return; 2303 } 2304 2305 // forward navigation: navigate() set next_page to the page to move to (a 2306 // cross-page link or tab change crossing pages) 2307 if (session.next_page) |route| { 2308 session.next_page = null; 2309 if (session.haxy_moment == null and session.local == null) return; 2310 const previous_route = session.data.current_page; 2311 // the page we navigated to becomes the current page 2312 session.data.current_page = route; 2313 session.back = .available; 2314 2315 const arena = try allocator.create(std.heap.ArenaAllocator); 2316 arena.* = std.heap.ArenaAllocator.init(allocator); 2317 errdefer freeArena(allocator, arena); 2318 2319 session.page_arena = arena; 2320 2321 const page = try arena.allocator().create(Page); 2322 page.* = try Page.init(arena, session, route); 2323 const new_root = try initRoot(allocator, page, session); 2324 2325 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2326 // drop the oldest entry (freeing its widget tree and arena) once over cap 2327 if (self.history.items.len > max_history) { 2328 var oldest = self.history.orderedRemove(0); 2329 oldest.root.deinit(allocator); 2330 freeArena(allocator, oldest.arena); 2331 } 2332 self.root = new_root; 2333 self.arena = arena; 2334 } 2335 } 2336 }; 2337 2338 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2339 const page_widget: Widget = switch (page.*) { 2340 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2341 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2342 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2343 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2344 }; 2345 2346 chooseAnsiArtForNavigation(session); 2347 2348 // on the TUI/SSH, the page sits above a one-row footer showing the url 2349 var root = if (session.is_terminal) blk: { 2350 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2351 errdefer box.deinit(allocator); 2352 const bg_id = bg_blk: { 2353 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2354 errdefer background.deinit(allocator); 2355 const id = background.getFocus().id; 2356 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2357 break :bg_blk id; 2358 }; 2359 { 2360 var footer = try widget.Footer.init(allocator, session); 2361 errdefer footer.deinit(allocator); 2362 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2363 } 2364 box.getFocus().child_id = bg_id; 2365 break :blk Widget{ .box = box }; 2366 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2367 errdefer root.deinit(allocator); 2368 2369 // input-owning views build their TextInputs in init, so reset their 2370 // registrations here 2371 session.text_inputs.clearRetainingCapacity(); 2372 2373 try root.build(allocator, .{ 2374 .min_size = .{ .width = null, .height = 40 }, 2375 .max_size = .{ .width = 100, .height = null }, 2376 }, root.getFocus()); 2377 2378 return root; 2379 } 2380 2381 // native and server-side sessions choose from the embedded collection. WASM 2382 // keeps the art serialized by the server instead. 2383 fn chooseAnsiArtForNavigation(session: *Session) void { 2384 if (comptime ansi_arts.len == 0) return; 2385 const io = session.io orelse return; 2386 var random: usize = undefined; 2387 io.random(std.mem.asBytes(&random)); 2388 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2389 } 2390