╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2246 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2247 var nav = try Nav.init(allocator, session); 2248 defer nav.deinit(allocator); 2249 2250 var terminal = try term.Terminal.init(io, allocator); 2251 var terminal_live = true; 2252 defer if (terminal_live) terminal.deinit(io); 2253 2254 // set term as active so it will be properly cooked 2255 // when a panic/segfault happens 2256 term.setActive(&terminal); 2257 defer term.setActive(null); 2258 2259 while (!terminal.shouldQuit()) { 2260 const grid_changed = try terminal.render(&nav.root); 2261 2262 // process any inputs. 2263 // 2264 // if the grid didn't change, then first do a blocking 2265 // read, so the thread will sleep until further input. 2266 // after that, all remaining reads are non-blocking so 2267 // we can process the rest of the queued inputs. 2268 // 2269 // if the grid *did* change, then only do non-blocking 2270 // reads. we do not want to sleep the thread because 2271 // there may be an animation that requires more looping. 2272 var blocking = !grid_changed; 2273 while (try terminal.readKey(io, blocking)) |key| { 2274 blocking = false; 2275 try inputKey(allocator, &nav.root, key, session); 2276 } 2277 2278 if (session.host_request) |request| { 2279 session.host_request = null; 2280 switch (request) { 2281 .show_copyable_text => |copyable_text| { 2282 term.setActive(null); 2283 terminal.deinit(io); 2284 terminal_live = false; 2285 showCopyableText(io, copyable_text) catch |show_err| { 2286 terminal = try term.Terminal.init(io, allocator); 2287 terminal_live = true; 2288 term.setActive(&terminal); 2289 return show_err; 2290 }; 2291 terminal = try term.Terminal.init(io, allocator); 2292 terminal_live = true; 2293 term.setActive(&terminal); 2294 }, 2295 .sync_events => { 2296 try nav.root.build(allocator, .{ 2297 .min_size = .{ .width = null, .height = null }, 2298 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2299 }, nav.root.getFocus()); 2300 _ = try terminal.render(&nav.root); 2301 try Repo.Events.performSync(allocator, session); 2302 }, 2303 } 2304 } 2305 2306 // persist queued actions when there's a repo; local mode has none, 2307 // so just apply in-memory 2308 if (repo_maybe) |repo| { 2309 try session.applyAndWritePending(io, allocator, repo); 2310 // reload so the next navigation builds its page from the current moment 2311 try session.reloadMoment(allocator, repo); 2312 } else { 2313 session.applyPending(); 2314 } 2315 2316 // reconcile navigation: forward to a new page, or back on escape. 2317 try nav.sync(allocator, session); 2318 2319 // the quit button (on the quit tab) asks the host to tear down. 2320 if (session.quit_requested) terminal.requestQuit(); 2321 2322 try nav.root.build(allocator, .{ 2323 .min_size = .{ .width = null, .height = null }, 2324 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2325 }, nav.root.getFocus()); 2326 } 2327 } 2328 2329 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2330 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2331 null 2332 else 2333 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2334 defer if (tty) |file| file.close(io); 2335 2336 const input = if (tty) |file| file else std.Io.File.stdin(); 2337 const output = if (tty) |file| file else std.Io.File.stdout(); 2338 2339 var write_buf: [1024]u8 = undefined; 2340 var writer = output.writer(io, &write_buf); 2341 try writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 2342 try writer.interface.flush(); 2343 2344 var read_buf: [1]u8 = undefined; 2345 var reader = input.reader(io, &read_buf); 2346 while (true) { 2347 const byte = try reader.interface.takeByte(); 2348 if (byte == '\r' or byte == '\n') return; 2349 } 2350 } 2351 2352 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2353 const root_focus = root.getFocus(); 2354 const focused_before = root_focus.grandchild_id; 2355 var activate_in_page_link = false; 2356 switch (key) { 2357 // request a navigation pop; the host's Nav.sync goes back a page, or 2358 // quits when there's no history left. 2359 .escape => { 2360 session.back = .requested; 2361 return; 2362 }, 2363 .ctrl => |letter| switch (letter) { 2364 'r' => { 2365 session.refresh_requested = true; 2366 return; 2367 }, 2368 else => try root.input(allocator, key, root_focus), 2369 }, 2370 .enter => { 2371 if (root_focus.grandchild_id) |gid| { 2372 // follow a cross-page link 2373 if (crossPageLink(root_focus, gid, session.data)) |route| { 2374 return session.navigate(route); 2375 } 2376 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2377 } 2378 activate_in_page_link = true; 2379 try root.input(allocator, key, root_focus); 2380 }, 2381 .mouse => |mouse| { 2382 if (mouse.action == .press and mouse.action.press == .left) { 2383 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2384 const focus_id = hit.id; 2385 if (hit.kind == .background) { 2386 root_focus.setFocus(focus_id); 2387 return; 2388 } 2389 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2390 if (session.back == .available) session.back = .requested; 2391 return; 2392 } 2393 // follow a cross-page link 2394 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2395 return session.navigate(route); 2396 } 2397 if (rawLink(root_focus, focus_id)) |url| { 2398 if (!mouse.ctrl) try requestRawLink(session, url); 2399 return; 2400 } 2401 root_focus.setFocus(focus_id); 2402 activate_in_page_link = true; 2403 } 2404 // forward the press into the widget tree so buttons (and any 2405 // future click-aware widgets) can react. widgets that don't 2406 // care about presses ignore it. 2407 try root.input(allocator, key, root_focus); 2408 } else { 2409 try root.input(allocator, key, root_focus); 2410 } 2411 }, 2412 else => try root.input(allocator, key, root_focus), 2413 } 2414 2415 const focused_after = root_focus.grandchild_id; 2416 if (!activate_in_page_link and focused_before == focused_after) return; 2417 const focus_id = focused_after orelse return; 2418 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2419 } 2420 2421 // a link to bytes the server serves directly rather than to a page route, so 2422 // it never resolves to a RoutablePage 2423 pub const raw_link_prefix = "ax:"; 2424 2425 // a route already loaded into the current page; focusing it changes the url 2426 // without rebuilding the page 2427 pub const in_page_link_prefix = "ai:"; 2428 2429 // an in-page tab's base route, or the exact current route when it is selected 2430 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2431 const target = if (selected) session.data.current_page else route; 2432 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2433 } 2434 2435 // a button the web renderer covers with a file picker that posts the chosen 2436 // file to the url after the prefix 2437 pub const file_input_prefix = "file:"; 2438 2439 // a submit button that overrides its enclosing form's action 2440 pub const submit_action_prefix = "submit:"; 2441 2442 // the url an `ax:` link points at, or null for anything else. a host that 2443 // suppresses the anchor's own navigation needs this to follow the link. 2444 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2445 const child = root_focus.children.get(focus_id) orelse return null; 2446 const custom = switch (child.focus.kind) { 2447 .custom => |c| c, 2448 else => return null, 2449 }; 2450 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2451 return custom[raw_link_prefix.len..]; 2452 } 2453 2454 fn requestRawLink(session: *Session, url: []const u8) !void { 2455 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2456 } 2457 2458 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2459 return if (session.web_port) |port| 2460 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2461 else 2462 path; 2463 } 2464 2465 // if the focus target at focus_id is an `a:` link that should navigate (a 2466 // different parent page, or a different files directory within the repo page), 2467 // return its route; otherwise null. lets a host turn a click / enter on such a 2468 // link into a navigation rather than a focus change. 2469 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2470 const current = data.current_page; 2471 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2472 // a link to a different parent page always navigates; within a page, a 2473 // files-directory / commits-page / list-window change navigates while tab 2474 // links stay in-page. 2475 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; 2476 return null; 2477 } 2478 2479 // the route carried by an `ai:` focus target, or null for any other target 2480 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2481 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2482 } 2483 2484 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2485 const child = root_focus.children.get(focus_id) orelse return null; 2486 const custom = switch (child.focus.kind) { 2487 .custom => |c| c, 2488 else => return null, 2489 }; 2490 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2491 const path = custom[prefix.len..]; 2492 // local sessions build (and parse) links with the repo identity elided 2493 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2494 } 2495 2496 // a display author, resolved against the admin db at read time 2497 pub const Author = union(enum) { 2498 unknown, 2499 email: []const u8, // an email no user matches 2500 user_name: []const u8, 2501 2502 // the commit author line's email, resolved like initFromEmail 2503 pub fn init( 2504 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2505 arena: *std.heap.ArenaAllocator, 2506 author_line: []const u8, 2507 ) !Author { 2508 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2509 } 2510 2511 // from a record's stored author email: it resolves to the user currently 2512 // holding it, else stays the email. the result is allocated in `arena`, 2513 // so the email may borrow transient memory. 2514 pub fn initFromEmail( 2515 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2516 arena: *std.heap.ArenaAllocator, 2517 author_email: ?[]const u8, 2518 ) !Author { 2519 const email = author_email orelse return .unknown; 2520 if (haxy_moment) |moment| { 2521 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2522 return .{ .user_name = user.event.name }; 2523 } 2524 } 2525 return .{ .email = try arena.allocator().dupe(u8, email) }; 2526 } 2527 }; 2528 2529 // the "a:" link to user `name`'s page. 2530 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2531 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2532 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2533 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2534 } 2535 2536 // a focusable " author " box showing `author`, linking to their user page 2537 // when it is a known user 2538 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2539 const text = switch (author) { 2540 .unknown => "", 2541 .email, .user_name => |t| t, 2542 }; 2543 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2544 errdefer tb.deinit(allocator); 2545 tb.getFocus().mode = .all; 2546 switch (author) { 2547 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2548 else => {}, 2549 } 2550 return tb; 2551 } 2552 2553 // native-TUI navigation 2554 pub const Nav = struct { 2555 root: Widget, 2556 // backs the current page (its Page data and the page-scoped strings its 2557 // widgets build). owned here, swapped on each navigation. session.page_arena 2558 // tracks whichever of these is current so widgets allocate into it. 2559 arena: *std.heap.ArenaAllocator, 2560 history: std.ArrayList(Entry), 2561 2562 // each retained page keeps its own arena, freed when the page leaves the 2563 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2564 // long-lived session from accumulating every page it ever visited. 2565 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2566 2567 // cap on retained back-history so the chain can't grow memory without bound 2568 const max_history: usize = 16; 2569 2570 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2571 session.back = .unavailable; 2572 const arena = try allocator.create(std.heap.ArenaAllocator); 2573 arena.* = std.heap.ArenaAllocator.init(allocator); 2574 errdefer { 2575 arena.deinit(); 2576 allocator.destroy(arena); 2577 } 2578 2579 session.page_arena = arena; 2580 const route = session.data.current_page; 2581 2582 const page = try arena.allocator().create(Page); 2583 page.* = try Page.init(arena, session, route); 2584 return .{ 2585 .root = try initRoot(allocator, page, session), 2586 .arena = arena, 2587 .history = .empty, 2588 }; 2589 } 2590 2591 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2592 self.root.deinit(allocator); 2593 freeArena(allocator, self.arena); 2594 for (self.history.items) |*entry| { 2595 entry.root.deinit(allocator); 2596 freeArena(allocator, entry.arena); 2597 } 2598 self.history.deinit(allocator); 2599 } 2600 2601 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2602 arena.deinit(); 2603 allocator.destroy(arena); 2604 } 2605 2606 // reconcile the displayed root with the session's navigation state. a 2607 // forward nav (current_page moved to a different parent page) pushes the 2608 // current root and builds the new page in a fresh arena; a back request 2609 // frees the current page and restores the previous one. when escape is 2610 // pressed with no history left we switch to the quit tab instead of quitting. 2611 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2612 // refresh: rebuild the current page in place from a current moment 2613 if (session.refresh_requested) { 2614 session.refresh_requested = false; 2615 if (session.haxy_moment != null or session.local != null) { 2616 const arena = try allocator.create(std.heap.ArenaAllocator); 2617 arena.* = std.heap.ArenaAllocator.init(allocator); 2618 errdefer freeArena(allocator, arena); 2619 2620 session.page_arena = arena; 2621 2622 const route = session.data.current_page; 2623 const page = try arena.allocator().create(Page); 2624 page.* = try Page.init(arena, session, route); 2625 const new_root = try initRoot(allocator, page, session); 2626 2627 self.root.deinit(allocator); 2628 freeArena(allocator, self.arena); 2629 self.root = new_root; 2630 self.arena = arena; 2631 } 2632 return; 2633 } 2634 2635 if (session.back == .requested) { 2636 if (self.history.pop()) |entry| { 2637 session.back = if (self.history.items.len == 0) .unavailable else .available; 2638 self.root.deinit(allocator); 2639 freeArena(allocator, self.arena); 2640 self.root = entry.root; 2641 self.arena = entry.arena; 2642 session.page_arena = entry.arena; 2643 session.data.current_page = entry.route; 2644 chooseAnsiArtForNavigation(session); 2645 return; 2646 } 2647 session.back = .unavailable; 2648 // nothing to go back to; switch to the quit confirmation 2649 const root_focus = self.root.getFocus(); 2650 var iter = root_focus.children.iterator(); 2651 while (iter.next()) |entry| { 2652 switch (entry.value_ptr.focus.kind) { 2653 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2654 root_focus.setFocus(entry.key_ptr.*); 2655 // a Stack only builds its selected child, so the quit 2656 // button isn't in the focus tree until a build runs with 2657 // the quit tab selected. build once, then send a 2658 // synthetic arrow_down to drop focus from the tab onto 2659 // the button so it's ready to confirm. 2660 try self.root.build(allocator, .{ 2661 .min_size = .{ .width = null, .height = 40 }, 2662 .max_size = .{ .width = 80, .height = null }, 2663 }, root_focus); 2664 try self.root.input(allocator, .arrow_down, root_focus); 2665 break; 2666 }, 2667 else => {}, 2668 } 2669 } 2670 return; 2671 } 2672 2673 // forward navigation: navigate() set next_page to the page to move to (a 2674 // cross-page link or tab change crossing pages) 2675 if (session.next_page) |route| { 2676 session.next_page = null; 2677 if (session.haxy_moment == null and session.local == null) return; 2678 const previous_route = session.data.current_page; 2679 // the page we navigated to becomes the current page 2680 session.data.current_page = route; 2681 session.back = .available; 2682 2683 const arena = try allocator.create(std.heap.ArenaAllocator); 2684 arena.* = std.heap.ArenaAllocator.init(allocator); 2685 errdefer freeArena(allocator, arena); 2686 2687 session.page_arena = arena; 2688 2689 const page = try arena.allocator().create(Page); 2690 page.* = try Page.init(arena, session, route); 2691 const new_root = try initRoot(allocator, page, session); 2692 2693 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2694 // drop the oldest entry (freeing its widget tree and arena) once over cap 2695 if (self.history.items.len > max_history) { 2696 var oldest = self.history.orderedRemove(0); 2697 oldest.root.deinit(allocator); 2698 freeArena(allocator, oldest.arena); 2699 } 2700 self.root = new_root; 2701 self.arena = arena; 2702 } 2703 } 2704 }; 2705 2706 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2707 const page_widget: Widget = switch (page.*) { 2708 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2709 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2710 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2711 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2712 }; 2713 2714 chooseAnsiArtForNavigation(session); 2715 2716 // on the TUI/SSH, the page sits above a one-row footer showing the url 2717 var root = if (session.is_terminal) blk: { 2718 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2719 errdefer box.deinit(allocator); 2720 const bg_id = bg_blk: { 2721 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2722 errdefer background.deinit(allocator); 2723 const id = background.getFocus().id; 2724 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2725 break :bg_blk id; 2726 }; 2727 { 2728 var footer = try widget.Footer.init(allocator, session); 2729 errdefer footer.deinit(allocator); 2730 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2731 } 2732 box.getFocus().child_id = bg_id; 2733 break :blk Widget{ .box = box }; 2734 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2735 errdefer root.deinit(allocator); 2736 2737 // input-owning views build their TextInputs in init, so reset their 2738 // registrations here 2739 session.text_inputs.clearRetainingCapacity(); 2740 2741 try root.build(allocator, .{ 2742 .min_size = .{ .width = null, .height = 40 }, 2743 .max_size = .{ .width = 100, .height = null }, 2744 }, root.getFocus()); 2745 2746 return root; 2747 } 2748 2749 // native and server-side sessions choose from the embedded collection. WASM 2750 // keeps the art serialized by the server instead. 2751 fn chooseAnsiArtForNavigation(session: *Session) void { 2752 if (comptime ansi_arts.len == 0) return; 2753 const io = session.io orelse return; 2754 var random: usize = undefined; 2755 io.random(std.mem.asBytes(&random)); 2756 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2757 } 2758