╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2421 error.SameBranch => .same_branch, 2422 error.UnrelatedBranches => .unrelated_branches, 2423 else => null, 2424 }; 2425 } 2426 }; 2427 2428 pub const RepoFailure = enum { 2429 required_name, 2430 invalid_name, 2431 name_taken, 2432 2433 pub fn fromError(err: anyerror) ?RepoFailure { 2434 return switch (err) { 2435 error.NameEmpty => .required_name, 2436 error.NameTooLong, error.InvalidName => .invalid_name, 2437 error.NameTaken => .name_taken, 2438 else => null, 2439 }; 2440 } 2441 }; 2442 2443 pub const MembersFailure = enum { not_found, already_added }; 2444 2445 pub const BranchesFailure = enum { 2446 not_found, 2447 already_added, 2448 too_many, 2449 too_long, 2450 2451 pub fn fromError(err: anyerror) ?BranchesFailure { 2452 return switch (err) { 2453 error.BranchProtected => .already_added, 2454 error.TooManyProtectedBranches => .too_many, 2455 else => null, 2456 }; 2457 } 2458 }; 2459 2460 pub const AssignmentFailure = enum { not_found, not_an_email, already_assigned }; 2461 2462 pub const DeleteFailure = enum { wrong_confirm, wrong_password }; 2463 2464 pub const KeyFailure = enum { 2465 invalid, 2466 unsupported, 2467 already_added, 2468 too_many, 2469 2470 pub fn fromError(err: anyerror) ?KeyFailure { 2471 return switch (err) { 2472 error.InvalidKey => .invalid, 2473 error.UnsupportedKey => .unsupported, 2474 error.KeyAdded => .already_added, 2475 error.TooManyKeys => .too_many, 2476 else => null, 2477 }; 2478 } 2479 }; 2480 2481 pub const UserFailure = enum { 2482 required_name, 2483 invalid_name, 2484 name_taken, 2485 required_email, 2486 email_taken, 2487 required_password, 2488 password_mismatch, 2489 wrong_password, 2490 2491 pub fn fromError(err: anyerror) ?UserFailure { 2492 return switch (err) { 2493 error.NameEmpty => .required_name, 2494 error.NameTooShort, error.NameTooLong, error.InvalidName => .invalid_name, 2495 error.NameTaken => .name_taken, 2496 error.EmailEmpty => .required_email, 2497 error.EmailTaken => .email_taken, 2498 error.PasswordEmpty => .required_password, 2499 error.PasswordMismatch => .password_mismatch, 2500 error.WrongPassword => .wrong_password, 2501 else => null, 2502 }; 2503 } 2504 }; 2505 2506 login: struct { 2507 failure: LoginFailure, 2508 username: []const u8, 2509 }, 2510 user: struct { 2511 failure: UserFailure, 2512 // passwords are never retained 2513 fields: struct { 2514 name: []const u8, 2515 email: []const u8, 2516 }, 2517 }, 2518 user_settings: struct { 2519 failure: UserFailure, 2520 // passwords are never retained 2521 fields: struct { email: []const u8 }, 2522 }, 2523 user_keys: struct { 2524 failure: KeyFailure, 2525 key: []const u8, 2526 }, 2527 repo: struct { 2528 failure: RepoFailure, 2529 fields: struct { 2530 name: []const u8, 2531 description: []const u8, 2532 hash: hash.HashKind, 2533 access: evt.Repo.Access, 2534 }, 2535 }, 2536 repo_settings: struct { 2537 failure: RepoFailure, 2538 fields: struct { 2539 name: []const u8, 2540 description: []const u8, 2541 access: evt.Repo.Access, 2542 discuss_role: ?evt.Repo.Role, 2543 issue_role: ?evt.Repo.Role, 2544 patch_role: ?evt.Repo.Role, 2545 }, 2546 }, 2547 repo_members: struct { 2548 failure: MembersFailure, 2549 name: []const u8, 2550 }, 2551 repo_branches: struct { 2552 failure: BranchesFailure, 2553 name: []const u8, 2554 }, 2555 assignment: struct { 2556 failure: AssignmentFailure, 2557 text: []const u8, 2558 }, 2559 delete: struct { 2560 failure: DeleteFailure, 2561 // a page can hold two delete forms, so this names the failed one 2562 target: std.meta.Tag(Delete.Target), 2563 // the password is never retained 2564 confirm: []const u8, 2565 }, 2566 issue: struct { 2567 failure: ThreadFailure, 2568 fields: ?struct { 2569 title: []const u8, 2570 labels: []const u8, 2571 description: []const u8, 2572 } = null, 2573 }, 2574 patch: struct { 2575 failure: PatchFailure, 2576 fields: ?struct { 2577 title: []const u8, 2578 labels: []const u8, 2579 description: []const u8, 2580 target_branch: []const u8, 2581 source_branch: []const u8 = "", 2582 source_kind: Repo.Patches.Source.Kind = .fork, 2583 } = null, 2584 }, 2585 discussion: struct { 2586 failure: ThreadFailure, 2587 fields: ?struct { 2588 title: []const u8, 2589 labels: []const u8, 2590 description: []const u8, 2591 } = null, 2592 }, 2593 }; 2594 2595 const Self = @This(); 2596 2597 // serializable data sent down to web client 2598 pub const Data = struct { 2599 // the server's reference time for relative timestamps on this page. 2600 now: i64 = 0, 2601 user_id: ?[]const u8 = null, 2602 // the logged-in user's name and email, for the views that show them 2603 user_name: ?[]const u8 = null, 2604 user_email: ?[]const u8 = null, 2605 // the logged-in user's unread notifications, refreshed on each page build 2606 unread_notifs: u64 = 0, 2607 // a failed form retained long enough to render it for correction 2608 form_feedback: ?FormFeedback = null, 2609 // a transient local event-sync error 2610 sync_failure: ?[]const u8 = null, 2611 // a failed terminal undo retained on the current tab 2612 undo_failure: ?[]const u8 = null, 2613 current_page: RoutablePage = .default, 2614 // the selected ANSI art. the server serializes one image for WASM so 2615 // the browser does not need the whole build-generated collection. 2616 ansi_art: []const u8 = "", 2617 // where git clones and pushes go. absent in local mode, where remote 2618 // urls are not shown. 2619 git_http_url: ?[]const u8 = null, 2620 git_ssh_url: ?[]const u8 = null, 2621 git_ssh_prefix: []const u8 = "", 2622 // whether this session views a local repo or the server. unlike `local` 2623 // (the host-side filesystem source) this travels in the snapshot, so 2624 // the wasm side also parses elided link urls and hides the multi-user 2625 // chrome. 2626 host_kind: evt.HostKind = .server, 2627 }; 2628 2629 pub fn init( 2630 arena: *std.heap.ArenaAllocator, 2631 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2632 data: Data, 2633 ) !Self { 2634 var session = Self{ 2635 .data = data, 2636 .arena = arena, 2637 // until a host swaps in a page-scoped arena (Nav does this), page 2638 // allocations land in the session arena. on the web path that's the 2639 // intended behavior, since the whole arena is per-request. 2640 .page_arena = arena, 2641 .admin_repo = repo, 2642 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 2643 }; 2644 try session.loadUser(); 2645 return session; 2646 } 2647 2648 // web views share the page's reference time; terminals use the live clock. 2649 pub fn nowSeconds(self: *const Self) !i64 { 2650 if (builtin.target.cpu.arch == .wasm32 or !self.is_terminal) return self.data.now; 2651 const io = self.io orelse return error.NoClock; 2652 return std.math.cast(i64, std.Io.Timestamp.now(io, .real).toSeconds()) orelse error.TimestampOutOfRange; 2653 } 2654 2655 // load the logged-in user's name and persisted preferences from the db 2656 pub fn loadUser(self: *Self) !void { 2657 const user_id = self.userId() orelse return; 2658 const moment = self.haxy_moment orelse return; 2659 const user = (try activeUser(moment, self.arena, user_id)) orelse return self.logOut(); 2660 self.data.user_name = user.event.name; 2661 self.data.user_email = user.event.email; 2662 } 2663 2664 // forget the logged-in user, on a logout or once their account is removed 2665 pub fn logOut(self: *Self) void { 2666 self.data.user_id = null; 2667 self.data.user_name = null; 2668 self.data.user_email = null; 2669 } 2670 2671 // log in as the account the session's ssh key last logged into with a 2672 // password, while that account still holds the key 2673 pub fn logInWithSshKey(self: *Self) !void { 2674 const key = self.ssh_key orelse return; 2675 const admin_repo = self.admin_repo orelse return; 2676 const moment = self.haxy_moment orelse return; 2677 const fingerprint = evt.User.fingerprintOfKey(key) orelse return; 2678 const user_id = (try evt.readLoginKeyUser(try admin_repo.core.latestMoment(), &fingerprint)) orelse return; 2679 if (!try evt.User.holdsKey(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.arena, &user_id, &fingerprint)) return; 2680 self.data.user_id = try self.arena.allocator().dupe(u8, &user_id); 2681 try self.loadUser(); 2682 } 2683 2684 // add the session's ssh key to the user who just logged in with a 2685 // password, which proves they hold it, and let it log them in from now on 2686 pub fn rememberSshKey(self: *Self) !void { 2687 if (builtin.target.cpu.arch == .wasm32) return; 2688 const key = self.ssh_key orelse return; 2689 const io = self.io orelse return; 2690 const users_dir = self.users_dir orelse return; 2691 const admin_repo = self.admin_repo orelse return; 2692 const user_id = self.userId() orelse return; 2693 const fingerprint = evt.User.fingerprintOfKey(key) orelse return; 2694 const allocator = self.arena.child_allocator; 2695 2696 const day = (std.time.epoch.EpochSeconds{ .secs = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()) }).getEpochDay().calculateYearDay(); 2697 const month = day.calculateMonthDay(); 2698 const line = try std.fmt.allocPrint(allocator, "{s} added {d:0>4}-{d:0>2}-{d:0>2} via login", .{ key, day.year, month.month.numeric(), month.day_index + 1 }); 2699 defer allocator.free(line); 2700 evt.addUserKey(io, allocator, users_dir, admin_repo, &user_id, line) catch |err| switch (err) { 2701 // it's already there, or there's no room for it 2702 error.KeyAdded, error.TooManyKeys => {}, 2703 else => return err, 2704 }; 2705 try evt.setLoginKey(io, admin_repo, &fingerprint, &user_id, .trust); 2706 self.haxy_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2707 } 2708 2709 // stop the session's ssh key logging the user in, on a logout 2710 pub fn forgetSshKey(self: *Self) !void { 2711 if (builtin.target.cpu.arch == .wasm32) return; 2712 const key = self.ssh_key orelse return; 2713 const io = self.io orelse return; 2714 const admin_repo = self.admin_repo orelse return; 2715 const user_id = self.userId() orelse return; 2716 const fingerprint = evt.User.fingerprintOfKey(key) orelse return; 2717 try evt.setLoginKey(io, admin_repo, &fingerprint, &user_id, .forget); 2718 } 2719 2720 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 2721 const bytes = self.data.user_id orelse return null; 2722 if (bytes.len != evt.event_id_size) return null; 2723 var id: [evt.event_id_size]u8 = undefined; 2724 @memcpy(&id, bytes); 2725 return id; 2726 } 2727 2728 pub fn formFeedback(self: *const Self, comptime tag: std.meta.Tag(FormFeedback)) ?@FieldType(FormFeedback, @tagName(tag)) { 2729 const feedback = self.data.form_feedback orelse return null; 2730 if (std.meta.activeTag(feedback) != tag) return null; 2731 return @field(feedback, @tagName(tag)); 2732 } 2733 2734 pub fn clearFormFeedback(self: *Self) void { 2735 self.data.form_feedback = null; 2736 } 2737 2738 // the actor for a write to the repo `identity` names, or null when the 2739 // session may not write there with at least `min_role` or in the 2740 // `thread_kind` tab. local mode always writes. 2741 pub fn authorize(self: *Self, identity: []const u8, min_role: evt.Repo.Role, thread_kind: ?evt.EventKind) !?Actor { 2742 if (self.data.host_kind == .local) { 2743 const author = blk: { 2744 const src = self.local orelse break :blk local_author_fallback; 2745 const io = self.io orelse break :blk local_author_fallback; 2746 break :blk try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 2747 }; 2748 return .{ .author = author, .role = .owner }; 2749 } 2750 const user_id = self.userId() orelse return null; 2751 // the session's moment predates this input, so read the roles afresh 2752 const admin_repo = self.admin_repo orelse return null; 2753 const io = self.io orelse return null; 2754 const users_dir = self.users_dir orelse return null; 2755 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2756 return switch (try authorizeUser(io, moment, self.page_arena, users_dir, user_id, identity, min_role, thread_kind)) { 2757 .actor => |actor| actor, 2758 .login_required, .repo_not_found, .forbidden => null, 2759 }; 2760 } 2761 2762 // where this session's event writes go. a server session always has a users dir and an admin repo 2763 pub fn eventHost(self: *const Self) evt.Host { 2764 return switch (self.data.host_kind) { 2765 .local => .local, 2766 .server => .{ .server = .{ 2767 .users_dir = self.users_dir orelse unreachable, 2768 .admin_repo_path = (self.admin_repo orelse unreachable).core.work_path, 2769 } }, 2770 }; 2771 } 2772 2773 // reload the moment from the admin repo and log out a removed user 2774 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 2775 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 2776 self.haxy_moment = moment; 2777 2778 const user_id = self.userId() orelse return; 2779 var arena = std.heap.ArenaAllocator.init(allocator); 2780 defer arena.deinit(); 2781 if (try activeUser(moment, &arena, user_id) == null) self.logOut(); 2782 } 2783 2784 // request a forward navigation to `route`; the host consumes next_page 2785 // (Nav.sync on the terminal, the wasm tick on the web). 2786 pub fn navigate(self: *Session, route: RoutablePage) !void { 2787 self.clearFormFeedback(); 2788 self.next_page = route; 2789 } 2790 }; 2791 2792 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2793 var nav = try Nav.init(allocator, session); 2794 defer nav.deinit(allocator); 2795 2796 var terminal = try term.Terminal.init(io, allocator); 2797 terminal.render_state.no_color = !session.color; 2798 var terminal_live = true; 2799 defer if (terminal_live) terminal.deinit(io); 2800 2801 // set term as active so it will be properly cooked 2802 // when a panic/segfault happens 2803 term.setActive(&terminal); 2804 defer term.setActive(null); 2805 2806 try terminal.queryBackground(); 2807 2808 while (!terminal.shouldQuit()) { 2809 const grid_changed = try terminal.render(&nav.root); 2810 2811 // process any inputs. 2812 // 2813 // if the grid didn't change, then first do a blocking 2814 // read, so the thread will sleep until further input. 2815 // after that, all remaining reads are non-blocking so 2816 // we can process the rest of the queued inputs. 2817 // 2818 // if the grid *did* change, then only do non-blocking 2819 // reads. we do not want to sleep the thread because 2820 // there may be an animation that requires more looping. 2821 var blocking = !grid_changed; 2822 while (try terminal.readKey(io, blocking)) |key| { 2823 blocking = false; 2824 try inputKey(allocator, &nav.root, key, session); 2825 } 2826 2827 if (session.host_request) |request| { 2828 session.host_request = null; 2829 switch (request) { 2830 .show_copyable_text => |copyable_text| { 2831 term.setActive(null); 2832 terminal.deinit(io); 2833 terminal_live = false; 2834 showCopyableText(io, copyable_text) catch |show_err| { 2835 terminal = try term.Terminal.init(io, allocator); 2836 terminal_live = true; 2837 term.setActive(&terminal); 2838 return show_err; 2839 }; 2840 terminal = try term.Terminal.init(io, allocator); 2841 terminal.render_state.no_color = !session.color; 2842 terminal_live = true; 2843 term.setActive(&terminal); 2844 }, 2845 .undo => |target| Repo.Undo.handleRequest(allocator, session, target), 2846 .sync_events => { 2847 try nav.root.build(allocator, .{ 2848 .min_size = .{ .width = null, .height = null }, 2849 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2850 }, nav.root.getFocus()); 2851 _ = try terminal.render(&nav.root); 2852 try Repo.Events.performSync(allocator, session); 2853 }, 2854 } 2855 } 2856 2857 // reload so the next navigation builds its page from the current moment 2858 if (repo_maybe) |repo| try session.reloadMoment(allocator, repo); 2859 2860 // reconcile navigation: forward to a new page, or back on escape. 2861 // the terminal background can change at any time, so recheck it on each page 2862 if (try nav.sync(allocator, session)) try terminal.queryBackground(); 2863 2864 // the quit button (on the quit tab) asks the host to tear down. 2865 if (session.quit_requested) terminal.requestQuit(); 2866 2867 try nav.root.build(allocator, .{ 2868 .min_size = .{ .width = null, .height = null }, 2869 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2870 }, nav.root.getFocus()); 2871 } 2872 } 2873 2874 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2875 const tty: ?std.Io.File = if (builtin.target.os.tag == .windows) 2876 null 2877 else 2878 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2879 defer if (tty) |file| file.close(io); 2880 2881 const input = if (tty) |file| file else std.Io.File.stdin(); 2882 const output = if (tty) |file| file else std.Io.File.stdout(); 2883 2884 var write_buf: [1024]u8 = undefined; 2885 var writer = output.writer(io, &write_buf); 2886 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}); 2887 try writer.interface.flush(); 2888 2889 var read_buf: [1]u8 = undefined; 2890 var reader = input.reader(io, &read_buf); 2891 while (true) { 2892 const byte = try reader.interface.takeByte(); 2893 if (byte == '\r' or byte == '\n') return; 2894 } 2895 } 2896 2897 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2898 const root_focus = root.getFocus(); 2899 const focused_before = root_focus.grandchild_id; 2900 var activate_in_page_link = false; 2901 switch (key) { 2902 // request a navigation pop; the host's Nav.sync goes back a page, or 2903 // quits when there's no history left. 2904 .escape => { 2905 session.back = .requested; 2906 return; 2907 }, 2908 .ctrl => |letter| switch (letter) { 2909 'r' => { 2910 session.refresh_requested = true; 2911 return; 2912 }, 2913 else => try root.input(allocator, key, root_focus), 2914 }, 2915 .enter => { 2916 if (root_focus.grandchild_id) |gid| { 2917 // follow a cross-page link 2918 if (crossPageLink(root_focus, gid, session.data)) |route| { 2919 return session.navigate(route); 2920 } 2921 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2922 } 2923 activate_in_page_link = true; 2924 try root.input(allocator, key, root_focus); 2925 }, 2926 .mouse => |mouse| { 2927 if (mouse.button == .left) { 2928 if (cellPageLink(root, mouse.x, mouse.y, session.data)) |route| return session.navigate(route); 2929 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2930 const focus_id = hit.id; 2931 if (hit.kind == .background) { 2932 root_focus.setFocus(focus_id); 2933 return; 2934 } 2935 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2936 if (session.back == .available) session.back = .requested; 2937 return; 2938 } 2939 // follow a cross-page link 2940 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2941 return session.navigate(route); 2942 } 2943 if (currentPageLink(root_focus, focus_id, session.data)) return; 2944 if (rawLink(root_focus, focus_id)) |url| { 2945 if (!mouse.ctrl) try requestRawLink(session, url); 2946 return; 2947 } 2948 root_focus.setFocus(focus_id); 2949 activate_in_page_link = true; 2950 } 2951 // forward the press into the widget tree so buttons (and any 2952 // future click-aware widgets) can react. widgets that don't 2953 // care about presses ignore it. 2954 try root.input(allocator, key, root_focus); 2955 } else { 2956 try root.input(allocator, key, root_focus); 2957 } 2958 }, 2959 .event => |event| switch (event) { 2960 .background => |rgb| session.terminal_background = rgb, 2961 .resize => try root.input(allocator, key, root_focus), 2962 }, 2963 // a scroll tick goes to the pane under the pointer, focusing it first. 2964 // from outside any pane (the tabs, a sub-header) or from a text input 2965 // it moves like the arrows instead, so scrolling up from a search box 2966 // reaches the header and stops there. 2967 .scroll_up, .scroll_down => |position| { 2968 if (focusedPane(root_focus)) |pane| if (scrollUnder(root_focus, position)) |under| { 2969 if (under != pane) root_focus.setFocus(under); 2970 }; 2971 try root.input(allocator, key, root_focus); 2972 }, 2973 else => try root.input(allocator, key, root_focus), 2974 } 2975 2976 const focused_after = root_focus.grandchild_id; 2977 if (!activate_in_page_link and focused_before == focused_after) return; 2978 const focus_id = focused_after orelse return; 2979 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2980 } 2981 2982 // the innermost vertically scrolling widget laid out under `position` 2983 fn scrollUnder(root_focus: *const Focus, position: xitui.input.Position) ?usize { 2984 var found: ?usize = null; 2985 var found_area: usize = std.math.maxInt(usize); 2986 var iter = root_focus.children.iterator(); 2987 while (iter.next()) |entry| { 2988 const info = entry.value_ptr.focus.scroll orelse continue; 2989 if (info.direction == .horiz) continue; 2990 const r = entry.value_ptr.rect; 2991 if (position.x < r.x or position.y < r.y or position.x >= r.x + r.size.width or position.y >= r.y + r.size.height) continue; 2992 // a nested scroll lies within its parent, so the smallest is innermost 2993 const area = r.size.width * r.size.height; 2994 if (area < found_area) { 2995 found = entry.key_ptr.*; 2996 found_area = area; 2997 } 2998 } 2999 return found; 3000 } 3001 3002 // the innermost vertically scrolling widget the focus lies in, unless the 3003 // focused widget is a text input 3004 fn focusedPane(root_focus: *const Focus) ?usize { 3005 var current = root_focus.grandchild_id orelse return null; 3006 switch ((root_focus.children.get(current) orelse return null).focus.kind) { 3007 .text_input, .text_input_password, .text_area => return null, 3008 else => {}, 3009 } 3010 while (root_focus.children.get(current)) |child| { 3011 if (child.focus.scroll) |info| if (info.direction != .horiz) return current; 3012 current = child.parent_id; 3013 } 3014 return null; 3015 } 3016 3017 // a link to bytes the server serves directly rather than to a page route, so 3018 // it never resolves to a RoutablePage 3019 pub const raw_link_prefix = "ax:"; 3020 3021 // a route already loaded into the current page; focusing it changes the url 3022 // without rebuilding the page 3023 pub const in_page_link_prefix = "ai:"; 3024 3025 // an in-page tab's base route, or the exact current route when it is selected 3026 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 3027 const target = if (selected) session.data.current_page else route; 3028 return session.page_arena.allocator().print("{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 3029 } 3030 3031 // a button the web renderer covers with a file picker that posts the chosen 3032 // file to the url after the prefix 3033 pub const file_input_prefix = "file:"; 3034 3035 // a submit button that overrides its enclosing form's action 3036 pub const submit_action_prefix = "submit:"; 3037 3038 // one option of a radio field, as `radio:<name>=<value>` 3039 pub const radio_prefix = "radio:"; 3040 3041 // the url an `ax:` link points at, or null for anything else. a host that 3042 // suppresses the anchor's own navigation needs this to follow the link. 3043 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 3044 const child = root_focus.children.get(focus_id) orelse return null; 3045 const custom = switch (child.focus.kind) { 3046 .custom => |c| c, 3047 else => return null, 3048 }; 3049 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 3050 return custom[raw_link_prefix.len..]; 3051 } 3052 3053 fn requestRawLink(session: *Session, url: []const u8) !void { 3054 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 3055 } 3056 3057 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 3058 // an absolute url is already complete 3059 if (!std.mem.startsWith(u8, path, "/")) return path; 3060 return if (session.web_url) |web_url| 3061 try allocator.print("{s}{s}", .{ web_url, path }) 3062 else 3063 path; 3064 } 3065 3066 // if the focus target at focus_id is an `a:` link that should navigate (a 3067 // different parent page, or a different files directory within the repo page), 3068 // return its route; otherwise null. lets a host turn a click / enter on such a 3069 // link into a navigation rather than a focus change. 3070 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 3071 const current = data.current_page; 3072 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 3073 // a link to a different parent page always navigates; within a page, a 3074 // files-directory / commits-page / list-window change navigates while tab 3075 // links stay in-page. 3076 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; 3077 return null; 3078 } 3079 3080 // the page the clicked cell links to, from a link inside a text box's content 3081 fn cellPageLink(root: *Widget, x: usize, y: usize, data: Session.Data) ?RoutablePage { 3082 const grid = root.getGrid() orelse return null; 3083 const cell = grid.cell(x, y) catch return null; 3084 const link = cell.link orelse return null; 3085 if (link.len == 0 or link[0] != '/') return null; 3086 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(link) else RoutablePage.fromUrl(link); 3087 } 3088 3089 // whether the `a:` focus target links to the page already shown, which a click 3090 // leaves alone rather than focusing 3091 pub fn currentPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) bool { 3092 const route = pageLink(root_focus, focus_id, data, "a:") orelse return false; 3093 return route.eql(data.current_page); 3094 } 3095 3096 // the route carried by an `ai:` focus target, or null for any other target 3097 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 3098 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 3099 } 3100 3101 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 3102 const child = root_focus.children.get(focus_id) orelse return null; 3103 const custom = switch (child.focus.kind) { 3104 .custom => |c| c, 3105 else => return null, 3106 }; 3107 if (!std.mem.startsWith(u8, custom, prefix)) return null; 3108 const path = custom[prefix.len..]; 3109 // local sessions build (and parse) links with the repo identity elided 3110 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 3111 } 3112 3113 // an assignee as the detail view shows it: the stored email, and the name of 3114 // the user holding it on this instance when there is one 3115 pub const Assignee = struct { 3116 email: []const u8, 3117 name: ?[]const u8 = null, 3118 3119 pub fn display(self: Assignee) []const u8 { 3120 return self.name orelse self.email; 3121 } 3122 }; 3123 3124 // a display author, resolved against the admin db at read time 3125 pub const Author = union(enum) { 3126 unknown, 3127 email: []const u8, // an email no user matches 3128 user_name: []const u8, 3129 3130 // the commit author line's email, resolved like initFromEmail 3131 pub fn init( 3132 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 3133 arena: *std.heap.ArenaAllocator, 3134 author_line: []const u8, 3135 ) !Author { 3136 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 3137 } 3138 3139 // from a record's stored author email: it resolves to the user currently 3140 // holding it, else stays the email. the result is allocated in `arena`, 3141 // so the email may borrow transient memory. 3142 pub fn initFromEmail( 3143 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 3144 arena: *std.heap.ArenaAllocator, 3145 author_email: ?[]const u8, 3146 ) !Author { 3147 const email = author_email orelse return .unknown; 3148 if (haxy_moment) |moment| { 3149 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 3150 return .{ .user_name = user.name }; 3151 } 3152 } 3153 return .{ .email = try arena.allocator().dupe(u8, email) }; 3154 } 3155 }; 3156 3157 // the "a:" link to user `name`'s page. 3158 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 3159 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 3160 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 3161 return page_arena.allocator().print("a:{s}", .{url}); 3162 } 3163 3164 // a focusable " author " box showing `author`, linking to their user page 3165 // when it is a known user 3166 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 3167 const text = switch (author) { 3168 .unknown => "", 3169 .email, .user_name => |t| t, 3170 }; 3171 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 3172 errdefer tb.deinit(allocator); 3173 tb.getFocus().mode = .all; 3174 switch (author) { 3175 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 3176 else => {}, 3177 } 3178 return tb; 3179 } 3180 3181 // native-TUI navigation 3182 pub const Nav = struct { 3183 root: Widget, 3184 // backs the current page (its Page data and the page-scoped strings its 3185 // widgets build). owned here, swapped on each navigation. session.page_arena 3186 // tracks whichever of these is current so widgets allocate into it. 3187 arena: *std.heap.ArenaAllocator, 3188 // the login the current root was built for 3189 user_id: ?[evt.event_id_size]u8, 3190 history: std.ArrayList(Entry), 3191 3192 // each retained page keeps its own arena, freed when the page leaves the 3193 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 3194 // long-lived session from accumulating every page it ever visited. 3195 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 3196 3197 // cap on retained back-history so the chain can't grow memory without bound 3198 const max_history: usize = 16; 3199 3200 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 3201 session.back = .unavailable; 3202 const arena = try allocator.create(std.heap.ArenaAllocator); 3203 arena.* = std.heap.ArenaAllocator.init(allocator); 3204 errdefer { 3205 arena.deinit(); 3206 allocator.destroy(arena); 3207 } 3208 3209 return .{ 3210 .root = try initCurrentRoot(allocator, arena, session), 3211 .arena = arena, 3212 .user_id = session.userId(), 3213 .history = .empty, 3214 }; 3215 } 3216 3217 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3218 self.root.deinit(allocator); 3219 freeArena(allocator, self.arena); 3220 for (self.history.items) |*entry| { 3221 entry.root.deinit(allocator); 3222 freeArena(allocator, entry.arena); 3223 } 3224 self.history.deinit(allocator); 3225 } 3226 3227 // rebuild the current page in place from a current moment 3228 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3229 if (session.haxy_moment == null and session.local == null) return; 3230 const arena = try allocator.create(std.heap.ArenaAllocator); 3231 arena.* = std.heap.ArenaAllocator.init(allocator); 3232 errdefer freeArena(allocator, arena); 3233 3234 const new_root = try initCurrentRoot(allocator, arena, session); 3235 3236 self.root.deinit(allocator); 3237 freeArena(allocator, self.arena); 3238 self.root = new_root; 3239 self.arena = arena; 3240 self.user_id = session.userId(); 3241 } 3242 3243 // build the current page's root in `arena`, or the not-found page's when 3244 // the route names nothing 3245 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3246 session.page_arena = arena; 3247 const page = try arena.allocator().create(Page); 3248 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3249 error.NotFound => blk: { 3250 session.data.current_page = .not_found; 3251 break :blk .not_found; 3252 }, 3253 else => |e| return e, 3254 }; 3255 return initRoot(allocator, page, session); 3256 } 3257 3258 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3259 arena.deinit(); 3260 allocator.destroy(arena); 3261 } 3262 3263 // reconcile the displayed root with the session's navigation state. a 3264 // forward nav (current_page moved to a different parent page) pushes the 3265 // current root and builds the new page in a fresh arena; a back request 3266 // frees the current page and restores the previous one. when escape is 3267 // pressed with no history left we switch to the quit tab instead of quitting. 3268 // returns whether the root was replaced. 3269 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3270 // refresh: rebuild the current page in place from a current moment 3271 if (session.refresh_requested) { 3272 session.refresh_requested = false; 3273 try self.rebuild(allocator, session); 3274 return true; 3275 } 3276 3277 if (session.back == .requested) { 3278 if (self.history.pop()) |entry| { 3279 session.back = if (self.history.items.len == 0) .unavailable else .available; 3280 self.root.deinit(allocator); 3281 freeArena(allocator, self.arena); 3282 self.root = entry.root; 3283 self.arena = entry.arena; 3284 self.user_id = entry.user_id; 3285 session.page_arena = entry.arena; 3286 session.data.current_page = entry.route; 3287 // a page built under another login is rebuilt rather than restored 3288 if (!std.meta.eql(entry.user_id, session.userId())) { 3289 try self.rebuild(allocator, session); 3290 return true; 3291 } 3292 chooseAnsiArtForNavigation(session); 3293 return true; 3294 } 3295 session.back = .unavailable; 3296 // nothing to go back to; switch to the quit confirmation 3297 const root_focus = self.root.getFocus(); 3298 var iter = root_focus.children.iterator(); 3299 while (iter.next()) |entry| { 3300 switch (entry.value_ptr.focus.kind) { 3301 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3302 root_focus.setFocus(entry.key_ptr.*); 3303 // a Stack only builds its selected child, so the quit 3304 // button isn't in the focus tree until a build runs with 3305 // the quit tab selected. build once, then send a 3306 // synthetic arrow_down to drop focus from the tab onto 3307 // the button so it's ready to confirm. 3308 try self.root.build(allocator, .{ 3309 .min_size = .{ .width = null, .height = 40 }, 3310 .max_size = .{ .width = 80, .height = null }, 3311 }, root_focus); 3312 try self.root.input(allocator, .arrow_down, root_focus); 3313 break; 3314 }, 3315 else => {}, 3316 } 3317 } 3318 return false; 3319 } 3320 3321 // forward navigation: navigate() set next_page to the page to move to (a 3322 // cross-page link or tab change crossing pages) 3323 if (session.next_page) |route| { 3324 session.next_page = null; 3325 if (session.haxy_moment == null and session.local == null) return false; 3326 const previous_route = session.data.current_page; 3327 // the page we navigated to becomes the current page 3328 session.data.current_page = route; 3329 session.back = .available; 3330 3331 const arena = try allocator.create(std.heap.ArenaAllocator); 3332 arena.* = std.heap.ArenaAllocator.init(allocator); 3333 errdefer freeArena(allocator, arena); 3334 3335 const new_root = try initCurrentRoot(allocator, arena, session); 3336 3337 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3338 // drop the oldest entry (freeing its widget tree and arena) once over cap 3339 if (self.history.items.len > max_history) { 3340 var oldest = self.history.orderedRemove(0); 3341 oldest.root.deinit(allocator); 3342 freeArena(allocator, oldest.arena); 3343 } 3344 self.root = new_root; 3345 self.arena = arena; 3346 self.user_id = session.userId(); 3347 return true; 3348 } 3349 return false; 3350 } 3351 }; 3352 3353 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3354 const page_widget: Widget = switch (page.*) { 3355 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3356 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3357 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3358 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3359 .not_found => .{ .not_found = try .init(allocator, session) }, 3360 }; 3361 3362 chooseAnsiArtForNavigation(session); 3363 3364 // on the TUI/SSH, the page sits above a one-row footer showing its web 3365 // url, when there's a web UI to point at 3366 var root = if (session.is_terminal) blk: { 3367 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3368 errdefer box.deinit(allocator); 3369 const bg_id = bg_blk: { 3370 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3371 errdefer background.deinit(allocator); 3372 const id = background.getFocus().id; 3373 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3374 break :bg_blk id; 3375 }; 3376 if (session.web_url != null) { 3377 var footer = try widget.Footer.init(allocator, session); 3378 errdefer footer.deinit(allocator); 3379 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3380 } 3381 box.getFocus().child_id = bg_id; 3382 break :blk Widget{ .box = box }; 3383 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3384 errdefer root.deinit(allocator); 3385 3386 // input-owning views build their TextInputs in init, so reset their 3387 // registrations here 3388 session.text_inputs.clearRetainingCapacity(); 3389 3390 try root.build(allocator, .{ 3391 .min_size = .{ .width = null, .height = 40 }, 3392 .max_size = .{ .width = 100, .height = null }, 3393 }, root.getFocus()); 3394 3395 return root; 3396 } 3397 3398 // native and server-side sessions choose from the embedded collection. WASM 3399 // keeps the art serialized by the server instead. 3400 fn chooseAnsiArtForNavigation(session: *Session) void { 3401 if (comptime ansi_art.art.len == 0) return; 3402 if (session.data.current_page == .not_found) { 3403 session.data.ansi_art = ansi_art.not_found; 3404 return; 3405 } 3406 const io = session.io orelse return; 3407 var random: usize = undefined; 3408 io.random(std.mem.asBytes(&random)); 3409 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3410 } 3411