╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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