╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2452 // process any inputs. 2453 // 2454 // if the grid didn't change, then first do a blocking 2455 // read, so the thread will sleep until further input. 2456 // after that, all remaining reads are non-blocking so 2457 // we can process the rest of the queued inputs. 2458 // 2459 // if the grid *did* change, then only do non-blocking 2460 // reads. we do not want to sleep the thread because 2461 // there may be an animation that requires more looping. 2462 var blocking = !grid_changed; 2463 while (try terminal.readKey(io, blocking)) |key| { 2464 blocking = false; 2465 try inputKey(allocator, &nav.root, key, session); 2466 } 2467 2468 if (session.host_request) |request| { 2469 session.host_request = null; 2470 switch (request) { 2471 .show_copyable_text => |copyable_text| { 2472 term.setActive(null); 2473 terminal.deinit(io); 2474 terminal_live = false; 2475 showCopyableText(io, copyable_text) catch |show_err| { 2476 terminal = try term.Terminal.init(io, allocator); 2477 terminal_live = true; 2478 term.setActive(&terminal); 2479 return show_err; 2480 }; 2481 terminal = try term.Terminal.init(io, allocator); 2482 terminal_live = true; 2483 term.setActive(&terminal); 2484 }, 2485 .undo => |target| Repo.Undo.handleRequest(allocator, session, target), 2486 .sync_events => { 2487 try nav.root.build(allocator, .{ 2488 .min_size = .{ .width = null, .height = null }, 2489 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2490 }, nav.root.getFocus()); 2491 _ = try terminal.render(&nav.root); 2492 try Repo.Events.performSync(allocator, session); 2493 }, 2494 } 2495 } 2496 2497 // persist queued actions when there's a repo; local mode has none, 2498 // so just apply in-memory 2499 if (repo_maybe) |repo| { 2500 try session.applyAndWritePending(io, allocator, repo); 2501 // reload so the next navigation builds its page from the current moment 2502 try session.reloadMoment(allocator, repo); 2503 } else { 2504 session.applyPending(); 2505 } 2506 2507 // reconcile navigation: forward to a new page, or back on escape. 2508 try nav.sync(allocator, session); 2509 2510 // the quit button (on the quit tab) asks the host to tear down. 2511 if (session.quit_requested) terminal.requestQuit(); 2512 2513 try nav.root.build(allocator, .{ 2514 .min_size = .{ .width = null, .height = null }, 2515 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2516 }, nav.root.getFocus()); 2517 } 2518 } 2519 2520 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2521 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2522 null 2523 else 2524 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2525 defer if (tty) |file| file.close(io); 2526 2527 const input = if (tty) |file| file else std.Io.File.stdin(); 2528 const output = if (tty) |file| file else std.Io.File.stdout(); 2529 2530 var write_buf: [1024]u8 = undefined; 2531 var writer = output.writer(io, &write_buf); 2532 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}); 2533 try writer.interface.flush(); 2534 2535 var read_buf: [1]u8 = undefined; 2536 var reader = input.reader(io, &read_buf); 2537 while (true) { 2538 const byte = try reader.interface.takeByte(); 2539 if (byte == '\r' or byte == '\n') return; 2540 } 2541 } 2542 2543 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2544 const root_focus = root.getFocus(); 2545 const focused_before = root_focus.grandchild_id; 2546 var activate_in_page_link = false; 2547 switch (key) { 2548 // request a navigation pop; the host's Nav.sync goes back a page, or 2549 // quits when there's no history left. 2550 .escape => { 2551 session.back = .requested; 2552 return; 2553 }, 2554 .ctrl => |letter| switch (letter) { 2555 'r' => { 2556 session.refresh_requested = true; 2557 return; 2558 }, 2559 else => try root.input(allocator, key, root_focus), 2560 }, 2561 .enter => { 2562 if (root_focus.grandchild_id) |gid| { 2563 // follow a cross-page link 2564 if (crossPageLink(root_focus, gid, session.data)) |route| { 2565 return session.navigate(route); 2566 } 2567 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2568 } 2569 activate_in_page_link = true; 2570 try root.input(allocator, key, root_focus); 2571 }, 2572 .mouse => |mouse| { 2573 if (mouse.action == .press and mouse.action.press == .left) { 2574 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2575 const focus_id = hit.id; 2576 if (hit.kind == .background) { 2577 root_focus.setFocus(focus_id); 2578 return; 2579 } 2580 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2581 if (session.back == .available) session.back = .requested; 2582 return; 2583 } 2584 // follow a cross-page link 2585 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2586 return session.navigate(route); 2587 } 2588 if (rawLink(root_focus, focus_id)) |url| { 2589 if (!mouse.ctrl) try requestRawLink(session, url); 2590 return; 2591 } 2592 root_focus.setFocus(focus_id); 2593 activate_in_page_link = true; 2594 } 2595 // forward the press into the widget tree so buttons (and any 2596 // future click-aware widgets) can react. widgets that don't 2597 // care about presses ignore it. 2598 try root.input(allocator, key, root_focus); 2599 } else { 2600 try root.input(allocator, key, root_focus); 2601 } 2602 }, 2603 else => try root.input(allocator, key, root_focus), 2604 } 2605 2606 const focused_after = root_focus.grandchild_id; 2607 if (!activate_in_page_link and focused_before == focused_after) return; 2608 const focus_id = focused_after orelse return; 2609 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2610 } 2611 2612 // a link to bytes the server serves directly rather than to a page route, so 2613 // it never resolves to a RoutablePage 2614 pub const raw_link_prefix = "ax:"; 2615 2616 // a route already loaded into the current page; focusing it changes the url 2617 // without rebuilding the page 2618 pub const in_page_link_prefix = "ai:"; 2619 2620 // an in-page tab's base route, or the exact current route when it is selected 2621 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2622 const target = if (selected) session.data.current_page else route; 2623 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2624 } 2625 2626 // a button the web renderer covers with a file picker that posts the chosen 2627 // file to the url after the prefix 2628 pub const file_input_prefix = "file:"; 2629 2630 // a submit button that overrides its enclosing form's action 2631 pub const submit_action_prefix = "submit:"; 2632 2633 // one option of a radio field, as `radio:<name>=<value>` 2634 pub const radio_prefix = "radio:"; 2635 2636 // the url an `ax:` link points at, or null for anything else. a host that 2637 // suppresses the anchor's own navigation needs this to follow the link. 2638 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2639 const child = root_focus.children.get(focus_id) orelse return null; 2640 const custom = switch (child.focus.kind) { 2641 .custom => |c| c, 2642 else => return null, 2643 }; 2644 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2645 return custom[raw_link_prefix.len..]; 2646 } 2647 2648 fn requestRawLink(session: *Session, url: []const u8) !void { 2649 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2650 } 2651 2652 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2653 // an absolute url is already complete 2654 if (!std.mem.startsWith(u8, path, "/")) return path; 2655 return if (session.web_port) |port| 2656 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2657 else 2658 path; 2659 } 2660 2661 // if the focus target at focus_id is an `a:` link that should navigate (a 2662 // different parent page, or a different files directory within the repo page), 2663 // return its route; otherwise null. lets a host turn a click / enter on such a 2664 // link into a navigation rather than a focus change. 2665 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2666 const current = data.current_page; 2667 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2668 // a link to a different parent page always navigates; within a page, a 2669 // files-directory / commits-page / list-window change navigates while tab 2670 // links stay in-page. 2671 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; 2672 return null; 2673 } 2674 2675 // the route carried by an `ai:` focus target, or null for any other target 2676 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2677 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2678 } 2679 2680 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2681 const child = root_focus.children.get(focus_id) orelse return null; 2682 const custom = switch (child.focus.kind) { 2683 .custom => |c| c, 2684 else => return null, 2685 }; 2686 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2687 const path = custom[prefix.len..]; 2688 // local sessions build (and parse) links with the repo identity elided 2689 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2690 } 2691 2692 // a display author, resolved against the admin db at read time 2693 pub const Author = union(enum) { 2694 unknown, 2695 email: []const u8, // an email no user matches 2696 user_name: []const u8, 2697 2698 // the commit author line's email, resolved like initFromEmail 2699 pub fn init( 2700 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2701 arena: *std.heap.ArenaAllocator, 2702 author_line: []const u8, 2703 ) !Author { 2704 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2705 } 2706 2707 // from a record's stored author email: it resolves to the user currently 2708 // holding it, else stays the email. the result is allocated in `arena`, 2709 // so the email may borrow transient memory. 2710 pub fn initFromEmail( 2711 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2712 arena: *std.heap.ArenaAllocator, 2713 author_email: ?[]const u8, 2714 ) !Author { 2715 const email = author_email orelse return .unknown; 2716 if (haxy_moment) |moment| { 2717 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2718 return .{ .user_name = user.name }; 2719 } 2720 } 2721 return .{ .email = try arena.allocator().dupe(u8, email) }; 2722 } 2723 }; 2724 2725 // the "a:" link to user `name`'s page. 2726 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2727 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2728 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2729 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2730 } 2731 2732 // a focusable " author " box showing `author`, linking to their user page 2733 // when it is a known user 2734 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2735 const text = switch (author) { 2736 .unknown => "", 2737 .email, .user_name => |t| t, 2738 }; 2739 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = " author " }); 2740 errdefer tb.deinit(allocator); 2741 tb.getFocus().mode = .all; 2742 switch (author) { 2743 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2744 else => {}, 2745 } 2746 return tb; 2747 } 2748 2749 // native-TUI navigation 2750 pub const Nav = struct { 2751 root: Widget, 2752 // backs the current page (its Page data and the page-scoped strings its 2753 // widgets build). owned here, swapped on each navigation. session.page_arena 2754 // tracks whichever of these is current so widgets allocate into it. 2755 arena: *std.heap.ArenaAllocator, 2756 // the login the current root was built for 2757 user_id: ?[evt.event_id_size]u8, 2758 history: std.ArrayList(Entry), 2759 2760 // each retained page keeps its own arena, freed when the page leaves the 2761 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2762 // long-lived session from accumulating every page it ever visited. 2763 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2764 2765 // cap on retained back-history so the chain can't grow memory without bound 2766 const max_history: usize = 16; 2767 2768 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2769 session.back = .unavailable; 2770 const arena = try allocator.create(std.heap.ArenaAllocator); 2771 arena.* = std.heap.ArenaAllocator.init(allocator); 2772 errdefer { 2773 arena.deinit(); 2774 allocator.destroy(arena); 2775 } 2776 2777 return .{ 2778 .root = try initCurrentRoot(allocator, arena, session), 2779 .arena = arena, 2780 .user_id = session.userId(), 2781 .history = .empty, 2782 }; 2783 } 2784 2785 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2786 self.root.deinit(allocator); 2787 freeArena(allocator, self.arena); 2788 for (self.history.items) |*entry| { 2789 entry.root.deinit(allocator); 2790 freeArena(allocator, entry.arena); 2791 } 2792 self.history.deinit(allocator); 2793 } 2794 2795 // rebuild the current page in place from a current moment 2796 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2797 if (session.haxy_moment == null and session.local == null) return; 2798 const arena = try allocator.create(std.heap.ArenaAllocator); 2799 arena.* = std.heap.ArenaAllocator.init(allocator); 2800 errdefer freeArena(allocator, arena); 2801 2802 const new_root = try initCurrentRoot(allocator, arena, session); 2803 2804 self.root.deinit(allocator); 2805 freeArena(allocator, self.arena); 2806 self.root = new_root; 2807 self.arena = arena; 2808 self.user_id = session.userId(); 2809 } 2810 2811 // build the current page's root in `arena`, or the not-found page's when 2812 // the route names nothing 2813 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 2814 session.page_arena = arena; 2815 const page = try arena.allocator().create(Page); 2816 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 2817 error.NotFound => blk: { 2818 session.data.current_page = .not_found; 2819 break :blk .not_found; 2820 }, 2821 else => |e| return e, 2822 }; 2823 return initRoot(allocator, page, session); 2824 } 2825 2826 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2827 arena.deinit(); 2828 allocator.destroy(arena); 2829 } 2830 2831 // reconcile the displayed root with the session's navigation state. a 2832 // forward nav (current_page moved to a different parent page) pushes the 2833 // current root and builds the new page in a fresh arena; a back request 2834 // frees the current page and restores the previous one. when escape is 2835 // pressed with no history left we switch to the quit tab instead of quitting. 2836 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2837 // refresh: rebuild the current page in place from a current moment 2838 if (session.refresh_requested) { 2839 session.refresh_requested = false; 2840 try self.rebuild(allocator, session); 2841 return; 2842 } 2843 2844 if (session.back == .requested) { 2845 if (self.history.pop()) |entry| { 2846 session.back = if (self.history.items.len == 0) .unavailable else .available; 2847 self.root.deinit(allocator); 2848 freeArena(allocator, self.arena); 2849 self.root = entry.root; 2850 self.arena = entry.arena; 2851 self.user_id = entry.user_id; 2852 session.page_arena = entry.arena; 2853 session.data.current_page = entry.route; 2854 // a page built under another login is rebuilt rather than restored 2855 if (!std.meta.eql(entry.user_id, session.userId())) { 2856 try self.rebuild(allocator, session); 2857 return; 2858 } 2859 chooseAnsiArtForNavigation(session); 2860 return; 2861 } 2862 session.back = .unavailable; 2863 // nothing to go back to; switch to the quit confirmation 2864 const root_focus = self.root.getFocus(); 2865 var iter = root_focus.children.iterator(); 2866 while (iter.next()) |entry| { 2867 switch (entry.value_ptr.focus.kind) { 2868 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2869 root_focus.setFocus(entry.key_ptr.*); 2870 // a Stack only builds its selected child, so the quit 2871 // button isn't in the focus tree until a build runs with 2872 // the quit tab selected. build once, then send a 2873 // synthetic arrow_down to drop focus from the tab onto 2874 // the button so it's ready to confirm. 2875 try self.root.build(allocator, .{ 2876 .min_size = .{ .width = null, .height = 40 }, 2877 .max_size = .{ .width = 80, .height = null }, 2878 }, root_focus); 2879 try self.root.input(allocator, .arrow_down, root_focus); 2880 break; 2881 }, 2882 else => {}, 2883 } 2884 } 2885 return; 2886 } 2887 2888 // forward navigation: navigate() set next_page to the page to move to (a 2889 // cross-page link or tab change crossing pages) 2890 if (session.next_page) |route| { 2891 session.next_page = null; 2892 if (session.haxy_moment == null and session.local == null) return; 2893 const previous_route = session.data.current_page; 2894 // the page we navigated to becomes the current page 2895 session.data.current_page = route; 2896 session.back = .available; 2897 2898 const arena = try allocator.create(std.heap.ArenaAllocator); 2899 arena.* = std.heap.ArenaAllocator.init(allocator); 2900 errdefer freeArena(allocator, arena); 2901 2902 const new_root = try initCurrentRoot(allocator, arena, session); 2903 2904 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 2905 // drop the oldest entry (freeing its widget tree and arena) once over cap 2906 if (self.history.items.len > max_history) { 2907 var oldest = self.history.orderedRemove(0); 2908 oldest.root.deinit(allocator); 2909 freeArena(allocator, oldest.arena); 2910 } 2911 self.root = new_root; 2912 self.arena = arena; 2913 self.user_id = session.userId(); 2914 } 2915 } 2916 }; 2917 2918 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2919 const page_widget: Widget = switch (page.*) { 2920 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2921 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2922 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2923 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2924 .not_found => .{ .not_found = try .init(allocator, session) }, 2925 }; 2926 2927 chooseAnsiArtForNavigation(session); 2928 2929 // on the TUI/SSH, the page sits above a one-row footer showing its web 2930 // url, when there's a web UI to point at 2931 var root = if (session.is_terminal) blk: { 2932 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2933 errdefer box.deinit(allocator); 2934 const bg_id = bg_blk: { 2935 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2936 errdefer background.deinit(allocator); 2937 const id = background.getFocus().id; 2938 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2939 break :bg_blk id; 2940 }; 2941 if (session.web_port != null) { 2942 var footer = try widget.Footer.init(allocator, session); 2943 errdefer footer.deinit(allocator); 2944 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2945 } 2946 box.getFocus().child_id = bg_id; 2947 break :blk Widget{ .box = box }; 2948 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2949 errdefer root.deinit(allocator); 2950 2951 // input-owning views build their TextInputs in init, so reset their 2952 // registrations here 2953 session.text_inputs.clearRetainingCapacity(); 2954 2955 try root.build(allocator, .{ 2956 .min_size = .{ .width = null, .height = 40 }, 2957 .max_size = .{ .width = 100, .height = null }, 2958 }, root.getFocus()); 2959 2960 return root; 2961 } 2962 2963 // native and server-side sessions choose from the embedded collection. WASM 2964 // keeps the art serialized by the server instead. 2965 fn chooseAnsiArtForNavigation(session: *Session) void { 2966 if (comptime ansi_art.art.len == 0) return; 2967 if (session.data.current_page == .not_found) { 2968 session.data.ansi_art = ansi_art.not_found; 2969 return; 2970 } 2971 const io = session.io orelse return; 2972 var random: usize = undefined; 2973 io.random(std.mem.asBytes(&random)); 2974 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 2975 } 2976