╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
812 } 813 814 // true when `a` and `b` are the same user paginated to a different repos 815 // window. switching between a user's tabs is in-page (header-handled), so 816 // only a changed `start` on the repos list navigates. 817 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 818 return switch (a) { 819 .user_repos => |aa| switch (b) { 820 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 821 else => false, 822 }, 823 else => false, 824 }; 825 } 826 827 // true when `a` and `b` are the same home list tab paginated to a different 828 // window. switching between the users/repos tabs is in-page (the home page 829 // holds both lists), so only a changed `after` on the same tab navigates. 830 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 831 return switch (a) { 832 .home_users => |aa| switch (b) { 833 .home_users => |bb| aa != bb, 834 else => false, 835 }, 836 .home_repos => |aa| switch (b) { 837 .home_repos => |bb| aa != bb, 838 else => false, 839 }, 840 else => false, 841 }; 842 } 843 }; 844 845 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 846 fn isUnreserved(c: u8) bool { 847 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 848 } 849 850 // percent-encode a ref name for use as a single url path segment. 851 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 852 var out: std.Io.Writer.Allocating = .init(aa); 853 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 854 return out.written(); 855 } 856 857 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 858 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 859 return struct { 860 pub const hex_len = hash.hexLen(repo_opts.hash); 861 862 // the concrete ref/oid (a null request resolves to the default branch) 863 ref_or_oid: RoutablePage.RefOrOid, 864 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 865 // the route layer — which splits on '/' — stores them encoded). duped 866 // into `aa`. 867 value: []const u8, 868 // the commit oid it points at 869 oid: [hex_len]u8, 870 871 const Self = @This(); 872 873 // resolve a requested ref/oid (null = the repo's default branch) to a 874 // concrete ref/oid plus the commit oid it points at. `requested_value` is 875 // the url-encoded form (as it appears in the route). null when it doesn't 876 // resolve (an unknown branch/tag, or a malformed/unknown oid). 877 pub fn init( 878 repo: *rp.Repo(repo_kind, repo_opts), 879 io: std.Io, 880 aa: std.mem.Allocator, 881 requested_ref_or_oid: ?RoutablePage.RefOrOid, 882 requested_value: []const u8, 883 ) !?Self { 884 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 885 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 886 var value: []const u8 = if (requested_ref_or_oid == null) 887 requested_value 888 else 889 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 890 // no ref named: fall back to HEAD's branch (or its oid when detached). 891 if (requested_ref_or_oid == null) { 892 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 893 if (repo.head(io, &head_buf)) |head| switch (head) { 894 .ref => |r| { 895 ref_or_oid = .branch; 896 value = r.name; 897 }, 898 .oid => |o| { 899 ref_or_oid = .object; 900 value = o; 901 }, 902 } else |_| {} 903 } 904 905 var oid: [hex_len]u8 = undefined; 906 switch (ref_or_oid) { 907 .object => { 908 if (value.len != hex_len) return null; 909 @memcpy(&oid, value); 910 }, 911 .branch, .tag => { 912 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 913 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 914 }, 915 } 916 // store url-encoded so the route layer can hold the value verbatim. 917 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 918 } 919 }; 920 } 921 922 // where a repo page reads its on-disk repo from 923 pub const RepoSource = struct { 924 path: []const u8, 925 repo_kind: rp.RepoKind, 926 }; 927 928 // per-connection mutable state. each SSH session / web session / local TUI 929 // run gets its own. 930 pub const Session = struct { 931 data: Data = .{}, 932 // session-lifetime allocations: login/user_id, persisted prefs, queued 933 // actions. lives as long as the connection. 934 arena: *std.heap.ArenaAllocator, 935 // current page's allocations: page data and the page-scoped strings widgets 936 // build (form actions, links). owned by Nav, swapped on each navigation, so 937 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 938 // where a render is one-shot, this points at the same arena as `arena`. 939 page_arena: *std.heap.ArenaAllocator, 940 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 941 // filesystem io and the path to <server>/repos, for opening on-disk repos 942 // during page construction. both null on wasm, which has no filesystem and 943 // rebuilds pages from the serialized snapshot rather than from disk. 944 io: ?std.Io = null, 945 repos_dir: ?[]const u8 = null, 946 // the single on-disk repo this session views, when running in local mode 947 // (haxy invoked with no arguments inside a repo). null on the server paths, 948 // which resolve repos from the admin db instead. 949 local: ?RepoSource = null, 950 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 951 // focus id -> the live TextInput, refreshed each frame by the views that own 952 // inputs. web/wasm form handling looks widgets up here by focus id. 953 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput(Widget)) = .empty, 954 // focus id -> the page-constant text the web overlay renders into an 955 // input initially, refreshed each frame alongside `text_inputs`. never 956 // the input's live content: the overlay html must not change as the 957 // user types, or the diff in _setOverlay would rebuild the element. 958 input_values: std.AutoHashMapUnmanaged(usize, []const u8) = .empty, 959 nav_back: bool = false, // set by input (escape) to request the native TUI pop a page; see Nav 960 refresh_requested: bool = false, // set by input (ctrl+r) 961 // a requested forward navigation. set this (via navigate) to move to a new 962 // page; Nav.sync builds it and then copies it into current_page, clearing 963 // this back to null. setting current_page directly only updates the url and 964 // does not navigate. 965 next_page: ?RoutablePage = null, 966 is_terminal: bool = false, // true on remote SSH and local TUI 967 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 968 // url. null on the web itself (no footer there). 969 web_port: ?u16 = null, 970 // a host operation requested by a widget. terminals present clone urls on 971 // their restored screen; wasm asks the browser to copy them. 972 host_request: ?HostRequest = null, 973 quit_requested: bool = false, 974 975 pub const HostRequest = union(enum) { 976 clone_url: []const u8, 977 }; 978 979 const Self = @This(); 980 981 // serializable data sent down to web client 982 pub const Data = struct { 983 user_id: ?[]const u8 = null, 984 // the logged-in user's name, for the views that show it 985 user_name: ?[]const u8 = null, 986 // a transient outcome to surface from the last /login POST attempt 987 login_failure: ?Home.Auth.Login.Failure = null, 988 current_page: RoutablePage = .default, 989 // whether to render the ANSI art backdrop 990 enable_ansi: bool = true, 991 // the bound clone-service ports. absent in local mode, where clone urls 992 // are not shown. 993 clone_http_port: ?u16 = null, 994 clone_ssh_port: ?u16 = null, 995 // true when this session views a single local repo. unlike `local` 996 // (the host-side filesystem source) this travels in the snapshot, so 997 // the wasm side also parses elided link urls and hides the multi-user 998 // chrome. 999 is_local: bool = false, 1000 }; 1001 1002 // a user-initiated state change. widgets enqueue these on the session during 1003 // input instead of mutating state or touching the DB themselves; the host 1004 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 1005 // side effects out of the widget tree and gives every render path one place to 1006 // turn a UI action into a state change + event. 1007 pub const Action = union(enum) { 1008 toggle_ansi, 1009 }; 1010 1011 pub fn init( 1012 arena: *std.heap.ArenaAllocator, 1013 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1014 data: Data, 1015 ) !Self { 1016 var session = Self{ 1017 .data = data, 1018 .arena = arena, 1019 // until a host swaps in a page-scoped arena (Nav does this), page 1020 // allocations land in the session arena. on the web path that's the 1021 // intended behavior, since the whole arena is per-request. 1022 .page_arena = arena, 1023 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 1024 }; 1025 try session.loadUser(); 1026 return session; 1027 } 1028 1029 // load the logged-in user's name and persisted preferences from the db 1030 pub fn loadUser(self: *Self) !void { 1031 const user_id = self.data.user_id orelse return; 1032 const moment = self.haxy_moment orelse return; 1033 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.arena, user_id)) |user| { 1034 self.data.user_name = user.event.name; 1035 self.data.enable_ansi = user.event.enable_ansi; 1036 } 1037 } 1038 1039 // the author for an event this session creates, or null when it may not 1040 // create one. local mode has no accounts, so it authors anonymously. 1041 pub fn eventAuthorEmail(self: *Self) !?[]const u8 { 1042 if (self.data.is_local) return "user@haxy"; // TODO: try the repo's git config user.email first 1043 const user_id = self.data.user_id orelse return null; 1044 const moment = self.haxy_moment orelse return null; 1045 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.page_arena, user_id)) orelse return null; 1046 return user.event.email; 1047 } 1048 1049 // queue an action for the host to drain this frame. 1050 pub fn push(self: *Self, action: Action) !void { 1051 try self.pending.append(self.arena.allocator(), action); 1052 } 1053 1054 // apply a single action's in-memory effect only (no persistence). 1055 fn apply(self: *Self, action: Action) void { 1056 switch (action) { 1057 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 1058 } 1059 } 1060 1061 // drain the session's queued actions by applying them to the session. 1062 // used on the wasm path, which has no repo to persist to. 1063 pub fn applyPending(self: *Self) void { 1064 for (self.pending.items) |action| self.apply(action); 1065 self.pending.clearRetainingCapacity(); 1066 } 1067 1068 // drain the session's queued actions by applying them to the session and 1069 // writing them to the db 1070 pub fn applyAndWritePending( 1071 self: *Self, 1072 io: std.Io, 1073 allocator: std.mem.Allocator, 1074 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1075 ) !void { 1076 defer self.pending.clearRetainingCapacity(); 1077 for (self.pending.items) |action| { 1078 self.apply(action); 1079 switch (action) { 1080 .toggle_ansi => if (self.data.user_id) |user_id| { 1081 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 1082 }, 1083 } 1084 } 1085 } 1086 1087 // reload the moment from the admin repo and re-read user preferences 1088 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 1089 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 1090 self.haxy_moment = moment; 1091 1092 const user_id = self.data.user_id orelse return; 1093 var arena = std.heap.ArenaAllocator.init(allocator); 1094 defer arena.deinit(); 1095 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, user_id)) |user| { 1096 self.data.enable_ansi = user.event.enable_ansi; 1097 } 1098 } 1099 1100 // request a forward navigation to `route`; the host consumes next_page 1101 // (Nav.sync on the terminal, the wasm tick on the web). 1102 pub fn navigate(self: *Session, route: RoutablePage) !void { 1103 self.next_page = route; 1104 } 1105 }; 1106 1107 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 1108 var nav = try Nav.init(allocator, session); 1109 defer nav.deinit(allocator); 1110 1111 var terminal = try term.Terminal.init(io, allocator); 1112 var terminal_live = true; 1113 defer if (terminal_live) terminal.deinit(io); 1114 1115 // set term as active so it will be properly cooked 1116 // when a panic/segfault happens 1117 term.setActive(&terminal); 1118 defer term.setActive(null); 1119 1120 var last_size = layout.Size{ .width = 0, .height = 0 }; 1121 var last_grid = try Grid.init(allocator, last_size); 1122 defer last_grid.deinit(); 1123 1124 while (!terminal.shouldQuit()) { 1125 const grid_changed = try terminal.render(&nav.root, &last_grid, &last_size); 1126 1127 // process any inputs. 1128 // 1129 // if the grid didn't change, then first do a blocking 1130 // read, so the thread will sleep until further input. 1131 // after that, all remaining reads are non-blocking so 1132 // we can process the rest of the queued inputs. 1133 // 1134 // if the grid *did* change, then only do non-blocking 1135 // reads. we do not want to sleep the thread because 1136 // there may be an animation that requires more looping. 1137 var blocking = !grid_changed; 1138 while (try terminal.readKey(io, blocking)) |key| { 1139 blocking = false; 1140 try inputKey(allocator, &nav.root, key, session); 1141 } 1142 1143 if (session.host_request) |request| { 1144 session.host_request = null; 1145 switch (request) { 1146 .clone_url => |url| { 1147 term.setActive(null); 1148 terminal.deinit(io); 1149 terminal_live = false; 1150 showCloneUrl(io, url) catch |show_err| { 1151 terminal = try term.Terminal.init(io, allocator); 1152 terminal_live = true; 1153 term.setActive(&terminal); 1154 return show_err; 1155 }; 1156 terminal = try term.Terminal.init(io, allocator); 1157 terminal_live = true; 1158 term.setActive(&terminal); 1159 last_size = .{ .width = 0, .height = 0 }; 1160 }, 1161 } 1162 } 1163 1164 // persist queued actions when there's a repo; local mode has none, 1165 // so just apply in-memory 1166 if (repo_maybe) |repo| { 1167 try session.applyAndWritePending(io, allocator, repo); 1168 // reload so the next navigation builds its page from the current moment 1169 try session.reloadMoment(allocator, repo); 1170 } else { 1171 session.applyPending(); 1172 } 1173 1174 // reconcile navigation: forward to a new page, or back on escape. 1175 try nav.sync(allocator, session); 1176 1177 // the quit button (on the quit tab) asks the host to tear down. 1178 if (session.quit_requested) terminal.requestQuit(); 1179 1180 try nav.root.build(allocator, .{ 1181 .min_size = .{ .width = null, .height = null }, 1182 .max_size = .{ .width = last_size.width, .height = last_size.height }, 1183 }, nav.root.getFocus()); 1184 } 1185 } 1186 1187 fn showCloneUrl(io: std.Io, url: []const u8) !void { 1188 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 1189 null 1190 else 1191 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 1192 defer if (tty) |file| file.close(io); 1193 1194 const input = if (tty) |file| file else std.Io.File.stdin(); 1195 const output = if (tty) |file| file else std.Io.File.stdout(); 1196 1197 var write_buf: [1024]u8 = undefined; 1198 var writer = output.writer(io, &write_buf); 1199 try writer.interface.print("\x1b[2J\x1b[H{s}\r\n\r\npress enter to go back\r\n", .{url}); 1200 try writer.interface.flush(); 1201 1202 var read_buf: [1]u8 = undefined; 1203 var reader = input.reader(io, &read_buf); 1204 while (true) { 1205 const byte = try reader.interface.takeByte(); 1206 if (byte == '\r' or byte == '\n') return; 1207 } 1208 } 1209 1210 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 1211 switch (key) { 1212 // request a navigation pop; the host's Nav.sync goes back a page, or 1213 // quits when there's no history left. 1214 .escape => session.nav_back = true, 1215 .ctrl => |letter| switch (letter) { 1216 'r' => session.refresh_requested = true, 1217 else => try root.input(allocator, key, root.getFocus()), 1218 }, 1219 .enter => { 1220 const root_focus = root.getFocus(); 1221 if (root_focus.grandchild_id) |gid| { 1222 // follow a cross-page link 1223 if (crossPageLink(root_focus, gid, session.data)) |route| { 1224 return session.navigate(route); 1225 } 1226 } 1227 try root.input(allocator, key, root_focus); 1228 }, 1229 .mouse => |mouse| { 1230 if (mouse.action == .press and mouse.action.press == .left) { 1231 const root_focus = root.getFocus(); 1232 var clicked: ?usize = null; 1233 var iter = root_focus.children.iterator(); 1234 while (iter.next()) |entry| { 1235 const child = entry.value_ptr.*; 1236 if (!child.focus.focusable) continue; 1237 const r = child.rect; 1238 if (mouse.x >= r.x and mouse.y >= r.y and 1239 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 1240 { 1241 clicked = entry.key_ptr.*; 1242 break; 1243 } 1244 } 1245 if (clicked) |focus_id| { 1246 // follow a cross-page link 1247 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 1248 return session.navigate(route); 1249 } 1250 root_focus.setFocus(focus_id); 1251 } 1252 // forward the press into the widget tree so buttons (and any 1253 // future click-aware widgets) can react. widgets that don't 1254 // care about presses ignore it. 1255 try root.input(allocator, key, root.getFocus()); 1256 } else { 1257 try root.input(allocator, key, root.getFocus()); 1258 } 1259 }, 1260 else => try root.input(allocator, key, root.getFocus()), 1261 } 1262 } 1263 1264 // if the focus target at focus_id is an `a:` link that should navigate (a 1265 // different parent page, or a different files directory within the repo page), 1266 // return its route; otherwise null. lets a host turn a click / enter on such a 1267 // link into a navigation rather than a focus change. 1268 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 1269 const current = data.current_page; 1270 const child = root_focus.children.get(focus_id) orelse return null; 1271 const custom = switch (child.focus.kind) { 1272 .custom => |c| c, 1273 else => return null, 1274 }; 1275 const a_prefix = "a:"; 1276 if (!std.mem.startsWith(u8, custom, a_prefix)) return null; 1277 const path = custom[a_prefix.len..]; 1278 // local sessions build (and parse) links with the repo identity elided 1279 const route = (if (data.is_local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path)) orelse return null; 1280 // a link to a different parent page always navigates; within a page, a 1281 // files-directory / commits-page / list-window change navigates while tab 1282 // links stay in-page (header-handled). 1283 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 1284 return null; 1285 } 1286 1287 // a display author, resolved against the admin db at read time 1288 pub const Author = union(enum) { 1289 unknown, 1290 email: []const u8, // an email no user matches 1291 user_name: []const u8, 1292 1293 // the commit author line's email, resolved like initFromEmail 1294 pub fn init( 1295 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1296 arena: *std.heap.ArenaAllocator, 1297 author_line: []const u8, 1298 ) !Author { 1299 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 1300 } 1301 1302 // from a record's stored author email: it resolves to the user currently 1303 // holding it, else stays the email. the result is allocated in `arena`, 1304 // so the email may borrow transient memory. 1305 pub fn initFromEmail( 1306 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1307 arena: *std.heap.ArenaAllocator, 1308 author_email: ?[]const u8, 1309 ) !Author { 1310 const email = author_email orelse return .unknown; 1311 if (haxy_moment) |moment| { 1312 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 1313 return .{ .user_name = user.event.name }; 1314 } 1315 } 1316 return .{ .email = try arena.allocator().dupe(u8, email) }; 1317 } 1318 }; 1319 1320 // the "a:" link to user `name`'s page. 1321 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 1322 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 1323 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 1324 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1325 } 1326 1327 // a focusable " author " box showing `author`, linking to their user page 1328 // when it is a known user 1329 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox(Widget) { 1330 const text = switch (author) { 1331 .unknown => "", 1332 .email, .user_name => |t| t, 1333 }; 1334 var tb = try wgt.TextBox(Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 1335 errdefer tb.deinit(allocator); 1336 tb.getFocus().focusable = true; 1337 switch (author) { 1338 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 1339 else => {}, 1340 } 1341 return tb; 1342 } 1343 1344 // native-TUI navigation 1345 pub const Nav = struct { 1346 root: Widget, 1347 route: RoutablePage, 1348 // backs the current page (its Page data and the page-scoped strings its 1349 // widgets build). owned here, swapped on each navigation. session.page_arena 1350 // tracks whichever of these is current so widgets allocate into it. 1351 arena: *std.heap.ArenaAllocator, 1352 history: std.ArrayList(Entry), 1353 1354 // each retained page keeps its own arena, freed when the page leaves the 1355 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 1356 // long-lived session from accumulating every page it ever visited. 1357 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 1358 1359 // cap on retained back-history so the chain can't grow memory without bound 1360 const max_history: usize = 16; 1361 1362 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 1363 const arena = try allocator.create(std.heap.ArenaAllocator); 1364 arena.* = std.heap.ArenaAllocator.init(allocator); 1365 errdefer { 1366 arena.deinit(); 1367 allocator.destroy(arena); 1368 } 1369 1370 session.page_arena = arena; 1371 const route = session.data.current_page; 1372 1373 const page = try arena.allocator().create(Page); 1374 page.* = try Page.init(arena, session, route); 1375 return .{ 1376 .root = try initRoot(allocator, page, session), 1377 .route = route, 1378 .arena = arena, 1379 .history = .empty, 1380 }; 1381 } 1382 1383 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 1384 self.root.deinit(allocator); 1385 freeArena(allocator, self.arena); 1386 for (self.history.items) |*entry| { 1387 entry.root.deinit(allocator); 1388 freeArena(allocator, entry.arena); 1389 } 1390 self.history.deinit(allocator); 1391 } 1392 1393 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 1394 arena.deinit(); 1395 allocator.destroy(arena); 1396 } 1397 1398 // reconcile the displayed root with the session's navigation state. a 1399 // forward nav (current_page moved to a different parent page) pushes the 1400 // current root and builds the new page in a fresh arena; a back request 1401 // frees the current page and restores the previous one. when escape is 1402 // pressed with no history left we switch to the quit tab instead of quitting. 1403 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 1404 // refresh: rebuild the current page in place from a current moment 1405 if (session.refresh_requested) { 1406 session.refresh_requested = false; 1407 if (session.haxy_moment != null or session.local != null) { 1408 const arena = try allocator.create(std.heap.ArenaAllocator); 1409 arena.* = std.heap.ArenaAllocator.init(allocator); 1410 errdefer freeArena(allocator, arena); 1411 1412 session.page_arena = arena; 1413 1414 const page = try arena.allocator().create(Page); 1415 page.* = try Page.init(arena, session, self.route); 1416 const new_root = try initRoot(allocator, page, session); 1417 1418 self.root.deinit(allocator); 1419 freeArena(allocator, self.arena); 1420 self.root = new_root; 1421 self.arena = arena; 1422 } 1423 return; 1424 } 1425 1426 if (session.nav_back) { 1427 session.nav_back = false; 1428 if (self.history.pop()) |entry| { 1429 self.root.deinit(allocator); 1430 freeArena(allocator, self.arena); 1431 self.root = entry.root; 1432 self.route = entry.route; 1433 self.arena = entry.arena; 1434 session.page_arena = entry.arena; 1435 session.data.current_page = entry.route; 1436 return; 1437 } 1438 // nothing to go back to; switch to the quit confirmation 1439 const root_focus = self.root.getFocus(); 1440 var iter = root_focus.children.iterator(); 1441 while (iter.next()) |entry| { 1442 switch (entry.value_ptr.focus.kind) { 1443 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1444 root_focus.setFocus(entry.key_ptr.*); 1445 // a Stack only builds its selected child, so the quit 1446 // button isn't in the focus tree until a build runs with 1447 // the quit tab selected. build once, then send a 1448 // synthetic arrow_down to drop focus from the tab onto 1449 // the button so it's ready to confirm. 1450 try self.root.build(allocator, .{ 1451 .min_size = .{ .width = null, .height = 40 }, 1452 .max_size = .{ .width = 80, .height = null }, 1453 }, root_focus); 1454 try self.root.input(allocator, .arrow_down, root_focus); 1455 break; 1456 }, 1457 else => {}, 1458 } 1459 } 1460 return; 1461 } 1462 1463 // forward navigation: navigate() set next_page to the page to move to (a 1464 // cross-page link or tab change crossing pages) 1465 if (session.next_page) |route| { 1466 session.next_page = null; 1467 if (session.haxy_moment == null and session.local == null) return; 1468 // the page we navigated to becomes the current page 1469 session.data.current_page = route; 1470 1471 const arena = try allocator.create(std.heap.ArenaAllocator); 1472 arena.* = std.heap.ArenaAllocator.init(allocator); 1473 errdefer freeArena(allocator, arena); 1474 1475 session.page_arena = arena; 1476 1477 const page = try arena.allocator().create(Page); 1478 page.* = try Page.init(arena, session, route); 1479 const new_root = try initRoot(allocator, page, session); 1480 1481 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 1482 // drop the oldest entry (freeing its widget tree and arena) once over cap 1483 if (self.history.items.len > max_history) { 1484 var oldest = self.history.orderedRemove(0); 1485 oldest.root.deinit(allocator); 1486 freeArena(allocator, oldest.arena); 1487 } 1488 self.root = new_root; 1489 self.route = route; 1490 self.arena = arena; 1491 } 1492 } 1493 }; 1494 1495 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 1496 const page_widget: Widget = switch (page.*) { 1497 .home => |*p| .{ .home = try .init(allocator, p, session) }, 1498 .user => |*p| .{ .user = try .init(allocator, p, session) }, 1499 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 1500 }; 1501 1502 const demon_art = @embedFile("embed/demon.ans"); 1503 1504 // on the TUI/SSH, the page sits above a one-row footer showing the url 1505 var root = if (session.is_terminal) blk: { 1506 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1507 errdefer box.deinit(allocator); 1508 const bg_id = bg_blk: { 1509 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 1510 errdefer background.deinit(allocator); 1511 const id = background.getFocus().id; 1512 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 1513 break :bg_blk id; 1514 }; 1515 { 1516 var footer = try Footer.init(allocator, session); 1517 errdefer footer.deinit(allocator); 1518 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 1519 } 1520 box.getFocus().child_id = bg_id; 1521 break :blk Widget{ .box = box }; 1522 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 1523 errdefer root.deinit(allocator); 1524 1525 // input-owning views build their TextInputs in init — so reset the 1526 // focus-id -> *TextInput map here 1527 session.text_inputs.clearRetainingCapacity(); 1528 session.input_values.clearRetainingCapacity(); 1529 1530 try root.build(allocator, .{ 1531 .min_size = .{ .width = null, .height = 40 }, 1532 .max_size = .{ .width = 100, .height = null }, 1533 }, root.getFocus()); 1534 1535 return root; 1536 } 1537 1538 pub const Widget = union(enum) { 1539 text: wgt.Text(Widget), 1540 box: wgt.Box(Widget), 1541 text_box: wgt.TextBox(Widget), 1542 text_input: wgt.TextInput(Widget), 1543 scroll: wgt.Scroll(Widget), 1544 stack: wgt.Stack(Widget), 1545 flow_box: FlowBox, 1546 flow_box_scroll: FlowBox.Scroll, 1547 tag_flow: TagFlow, 1548 spacer: Spacer, 1549 center: Center, 1550 section_label: SectionLabel, 1551 submit_button: SubmitButton, 1552 background: AnsiBackground, 1553 home: Home.View, 1554 user: User.View, 1555 repo: Repo.View, 1556 quit: Quit.View, 1557 unauthorized: Unauthorized.View, 1558 title: Title.View, 1559 sub_title: SubTitle.View, 1560 home_header: Home.Header.View, 1561 user_header: User.Header.View, 1562 repo_header: Repo.Header.View, 1563 repo_files_header: Repo.Files.Header.View, 1564 repo_commits_header: Repo.Commits.Header.View, 1565 repo_issues_header: Repo.Issues.Header, 1566 repo_files: Repo.Files.View, 1567 repo_commits: Repo.Commits.View, 1568 repo_refs: Repo.Refs.View, 1569 repo_issues: Repo.Issues.View, 1570 repo_comment: Repo.Comment.Item, 1571 clone_url: CloneUrl.View, 1572 home_users: Home.Users.View, 1573 home_repos: Home.Repos.View, 1574 auth_tab: Home.Header.AuthTab.View, 1575 home_settings: Home.Settings.View, 1576 home_auth: Home.Auth.View, 1577 footer: Footer, 1578 1579 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 1580 switch (self.*) { 1581 inline else => |*case| case.deinit(allocator), 1582 } 1583 } 1584 1585 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 1586 switch (self.*) { 1587 inline else => |*case| try case.build(allocator, constraint, root_focus), 1588 } 1589 } 1590 1591 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 1592 switch (self.*) { 1593 inline else => |*case| try case.input(allocator, key, root_focus), 1594 } 1595 } 1596 1597 pub fn clearGrid(self: *Widget) void { 1598 switch (self.*) { 1599 inline else => |*case| case.clearGrid(), 1600 } 1601 } 1602 1603 pub fn getGrid(self: Widget) ?Grid { 1604 switch (self) { 1605 inline else => |*case| return case.getGrid(), 1606 } 1607 } 1608 1609 pub fn getFocus(self: *Widget) *Focus { 1610 switch (self.*) { 1611 inline else => |*case| return case.getFocus(), 1612 } 1613 } 1614 }; 1615 1616 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 1617 // `scroll` to keep it visible. 1618 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 1619 const keys = box.children.keys(); 1620 if (keys.len == 0) return; 1621 const cur_id = box.getFocus().child_id orelse return; 1622 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 1623 const last: isize = @intCast(keys.len - 1); 1624 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 1625 if (next == @as(usize, @intCast(cur))) return; 1626 root_focus.setFocus(keys[next]); 1627 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 1628 } 1629 1630 pub const FlowBox = struct { 1631 focus: *Focus, 1632 grid: ?Grid, 1633 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1634 // backs the per-item strings the text boxes borrow: each box's `content` 1635 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 1636 // wholesale on each setItems so there's no per-string ownership to track. 1637 arena: std.heap.ArenaAllocator, 1638 // column count from the last build — FlowBox.Scroll.input uses it so arrow 1639 // up/down can step by a row's worth of items. 1640 last_cols: usize, 1641 options: Options, 1642 1643 const border_rows: usize = 2; 1644 1645 pub const Options = struct { 1646 cell_width: usize = 40, 1647 cell_height: usize = 3, 1648 }; 1649 1650 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1651 return .{ 1652 .focus = try Focus.create(allocator, .container), 1653 .grid = null, 1654 .text_boxes = .empty, 1655 .arena = std.heap.ArenaAllocator.init(allocator), 1656 .last_cols = 1, 1657 .options = options, 1658 }; 1659 } 1660 1661 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1662 self.focus.destroy(allocator); 1663 if (self.grid) |*grid| { 1664 grid.deinit(); 1665 self.grid = null; 1666 } 1667 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1668 self.text_boxes.deinit(allocator); 1669 self.arena.deinit(); 1670 } 1671 1672 // one flow item: its display text plus an optional link. a non-empty `link` 1673 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1674 // the web renderer turns into an anchor. 1675 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1676 1677 // both the item text and the link are copied into the arena, so the caller's 1678 // slices needn't outlive this call. 1679 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1680 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1681 self.text_boxes.clearAndFree(allocator); 1682 1683 // the old text boxes (and their borrowed content/links) are gone now, so 1684 // the arena that backed those strings can be reclaimed. 1685 _ = self.arena.reset(.retain_capacity); 1686 const aa = self.arena.allocator(); 1687 1688 self.focus.clear(); 1689 self.focus.child_id = null; 1690 1691 for (items) |item| { 1692 const line = try aa.dupe(u8, item.text); 1693 1694 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1695 errdefer text_box.deinit(allocator); 1696 text_box.getFocus().focusable = true; 1697 1698 if (item.link.len > 0) { 1699 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1700 } 1701 1702 try self.text_boxes.append(allocator, text_box); 1703 } 1704 1705 if (self.text_boxes.items.len > 0) { 1706 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1707 } 1708 } 1709 1710 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1711 self.clearGrid(); 1712 self.focus.clear(); 1713 1714 const cell_width = self.options.cell_width; 1715 const max_width = constraint.max_size.width orelse cell_width; 1716 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1717 self.last_cols = cols; 1718 1719 const count = self.text_boxes.items.len; 1720 if (count == 0) return; 1721 const slot_height = self.options.cell_height + border_rows; 1722 if (slot_height == 0) return; 1723 1724 // build at the slot size so every text box fits a single grid cell 1725 for (self.text_boxes.items) |*tb| { 1726 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1727 try tb.build(allocator, .{ 1728 .min_size = .{ .width = cell_width, .height = null }, 1729 .max_size = .{ .width = cell_width, .height = slot_height }, 1730 }, root_focus); 1731 } 1732 1733 const rows = (count + cols - 1) / cols; 1734 const content_height = rows * slot_height; 1735 const total_width = cols * cell_width; 1736 if (total_width == 0 or content_height == 0) return; 1737 1738 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1739 errdefer grid.deinit(); 1740 1741 for (self.text_boxes.items, 0..) |*tb, i| { 1742 const tb_grid = tb.getGrid() orelse continue; 1743 const col = i % cols; 1744 const row = i / cols; 1745 const cell_x = col * cell_width; 1746 const cell_y = row * slot_height; 1747 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1748 try grid.drawGrid(tb_grid, cell_x, cell_y); 1749 } 1750 1751 self.grid = grid; 1752 } 1753 1754 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1755 _ = self; 1756 _ = allocator; 1757 _ = key; 1758 _ = root_focus; 1759 } 1760 1761 pub fn clearGrid(self: *FlowBox) void { 1762 if (self.grid) |*grid| { 1763 grid.deinit(); 1764 self.grid = null; 1765 } 1766 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1767 } 1768 1769 pub fn getGrid(self: FlowBox) ?Grid { 1770 return self.grid; 1771 } 1772 1773 pub fn getFocus(self: *FlowBox) *Focus { 1774 return self.focus; 1775 } 1776 1777 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1778 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1779 const slot_height = self.options.cell_height + border_rows; 1780 const col = index % self.last_cols; 1781 const row = index / self.last_cols; 1782 return .{ 1783 .x = @intCast(col * self.options.cell_width), 1784 .y = @intCast(row * slot_height), 1785 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1786 }; 1787 } 1788 1789 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1790 for (self.text_boxes.items, 0..) |tb, i| { 1791 if (tb.box.focus.id == focus_id) return i; 1792 } 1793 return null; 1794 } 1795 1796 pub const Scroll = struct { 1797 scroll: wgt.Scroll(Widget), 1798 1799 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1800 var layout_inner = try FlowBox.init(allocator, options); 1801 errdefer layout_inner.deinit(allocator); 1802 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1803 errdefer scroll.deinit(allocator); 1804 return .{ .scroll = scroll }; 1805 } 1806 1807 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1808 self.scroll.deinit(allocator); 1809 } 1810 1811 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1812 self.scroll.x = 0; 1813 self.scroll.y = 0; 1814 try self.inner().setItems(allocator, items); 1815 } 1816 1817 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1818 try self.scroll.build(allocator, constraint, root_focus); 1819 } 1820 1821 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1822 _ = allocator; 1823 const in = self.inner(); 1824 const child_id = in.focus.child_id orelse return; 1825 const current_index = in.indexOfFocusId(child_id) orelse return; 1826 const count = in.text_boxes.items.len; 1827 if (count == 0) return; 1828 const cols = in.last_cols; 1829 const slot_height = in.options.cell_height + FlowBox.border_rows; 1830 1831 var index = current_index; 1832 switch (key) { 1833 .arrow_up => index -|= cols, 1834 .arrow_down => if (index + cols < count) { 1835 index += cols; 1836 }, 1837 .arrow_left => index -|= 1, 1838 .arrow_right => if (index + 1 < count) { 1839 index += 1; 1840 }, 1841 .home => index = 0, 1842 .end => index = count - 1, 1843 .page_up => { 1844 if (self.scroll.grid) |grid| if (slot_height > 0) { 1845 const rows_per_page = grid.size.height / slot_height; 1846 index -|= rows_per_page * cols; 1847 }; 1848 }, 1849 .page_down => { 1850 if (self.scroll.grid) |grid| if (slot_height > 0) { 1851 const rows_per_page = grid.size.height / slot_height; 1852 index = @min(index + rows_per_page * cols, count - 1); 1853 }; 1854 }, 1855 // scroll wheel moves the focused cell by a full row so the 1856 // viewport (via scrollToRect) follows in row-sized steps, 1857 // matching how a scroll wheel feels in a grid view. 1858 .mouse => |mouse| switch (mouse.action) { 1859 .scroll => |dir| switch (dir) { 1860 .up => index -|= cols, 1861 .down => if (index + cols < count) { 1862 index += cols; 1863 }, 1864 }, 1865 else => {}, 1866 }, 1867 else => {}, 1868 } 1869 1870 if (index != current_index) { 1871 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1872 if (in.cellRect(index)) |rect| { 1873 self.scroll.scrollToRect(rect); 1874 } 1875 } 1876 } 1877 1878 pub fn clearGrid(self: *Scroll) void { 1879 self.scroll.clearGrid(); 1880 } 1881 1882 pub fn getGrid(self: Scroll) ?Grid { 1883 return self.scroll.getGrid(); 1884 } 1885 1886 pub fn getFocus(self: *Scroll) *Focus { 1887 return self.scroll.getFocus(); 1888 } 1889 1890 pub fn getSelectedIndex(self: Scroll) ?usize { 1891 const in = self.scroll.child.flow_box; 1892 const child_id = in.focus.child_id orelse return null; 1893 return in.indexOfFocusId(child_id); 1894 } 1895 1896 fn inner(self: *Scroll) *FlowBox { 1897 return &self.scroll.child.flow_box; 1898 } 1899 }; 1900 }; 1901 1902 // a left-to-right flow of variable-width focusable items that wraps at the 1903 // available width, like words in a paragraph. unlike FlowBox there is no cell 1904 // grid: each item is as wide as its text. selection movement is driven by the 1905 // owning view via indexOfFocusId/verticalNeighbor. 1906 pub const TagFlow = struct { 1907 focus: *Focus, 1908 grid: ?Grid, 1909 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1910 // last build's item rects (content space), for vertical navigation and 1911 // scroll-into-view. 1912 rects: std.ArrayList(layout.IRect), 1913 // backs the per-item strings the text boxes borrow. 1914 arena: std.heap.ArenaAllocator, 1915 1916 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1917 1918 pub fn init(allocator: std.mem.Allocator) !TagFlow { 1919 return .{ 1920 .focus = try Focus.create(allocator, .container), 1921 .grid = null, 1922 .text_boxes = .empty, 1923 .rects = .empty, 1924 .arena = std.heap.ArenaAllocator.init(allocator), 1925 }; 1926 } 1927 1928 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 1929 self.focus.destroy(allocator); 1930 if (self.grid) |*grid| { 1931 grid.deinit(); 1932 self.grid = null; 1933 } 1934 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1935 self.text_boxes.deinit(allocator); 1936 self.rects.deinit(allocator); 1937 self.arena.deinit(); 1938 } 1939 1940 // both the item text and the link are copied into the arena, so the caller's 1941 // slices needn't outlive this call. 1942 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 1943 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1944 self.text_boxes.clearAndFree(allocator); 1945 1946 _ = self.arena.reset(.retain_capacity); 1947 const aa = self.arena.allocator(); 1948 1949 self.focus.clear(); 1950 self.focus.child_id = null; 1951 1952 for (items) |item| { 1953 const line = try aa.dupe(u8, item.text); 1954 1955 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1956 errdefer text_box.deinit(allocator); 1957 text_box.getFocus().focusable = true; 1958 1959 if (item.link.len > 0) { 1960 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1961 } 1962 1963 try self.text_boxes.append(allocator, text_box); 1964 } 1965 1966 if (self.text_boxes.items.len > 0) { 1967 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1968 } 1969 } 1970 1971 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1972 self.clearGrid(); 1973 self.focus.clear(); 1974 1975 if (self.text_boxes.items.len == 0) return; 1976 const max_width = constraint.max_size.width orelse 80; 1977 if (max_width == 0) return; 1978 1979 // lay the items out left to right, wrapping to a new row when the next 1980 // one wouldn't fit. 1981 self.rects.clearRetainingCapacity(); 1982 var x: usize = 0; 1983 var y: usize = 0; 1984 var row_height: usize = 0; 1985 var total_width: usize = 0; 1986 for (self.text_boxes.items) |*tb| { 1987 try tb.build(allocator, .{ 1988 .min_size = .{ .width = null, .height = null }, 1989 .max_size = .{ .width = max_width, .height = null }, 1990 }, root_focus); 1991 const tb_grid_maybe = tb.getGrid(); 1992 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1993 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1994 if (x > 0 and x + w > max_width) { 1995 x = 0; 1996 y += row_height; 1997 row_height = 0; 1998 } 1999 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 2000 if (h > row_height) row_height = h; 2001 x += w; 2002 if (x > total_width) total_width = x; 2003 } 2004 2005 const total_height = y + row_height; 2006 if (total_width == 0 or total_height == 0) return; 2007 2008 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 2009 errdefer grid.deinit(); 2010 2011 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 2012 const tb_grid = tb.getGrid() orelse continue; 2013 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 2014 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 2015 } 2016 2017 self.grid = grid; 2018 } 2019 2020 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2021 _ = self; 2022 _ = allocator; 2023 _ = key; 2024 _ = root_focus; 2025 } 2026 2027 pub fn clearGrid(self: *TagFlow) void { 2028 if (self.grid) |*grid| { 2029 grid.deinit(); 2030 self.grid = null; 2031 } 2032 for (self.text_boxes.items) |*tb| tb.clearGrid(); 2033 } 2034 2035 pub fn getGrid(self: TagFlow) ?Grid { 2036 return self.grid; 2037 } 2038 2039 pub fn getFocus(self: *TagFlow) *Focus { 2040 return self.focus; 2041 } 2042 2043 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 2044 for (self.text_boxes.items, 0..) |tb, i| { 2045 if (tb.box.focus.id == focus_id) return i; 2046 } 2047 return null; 2048 } 2049 2050 // the first item of the row below (when `downward`) or above, or null at 2051 // the flow's edge. items are laid out in order, so y is nondecreasing. 2052 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 2053 const rects = self.rects.items; 2054 if (index >= rects.len) return null; 2055 const cur_y = rects[index].y; 2056 if (downward) { 2057 for (rects[index + 1 ..], index + 1..) |r, i| { 2058 if (r.y > cur_y) return i; 2059 } 2060 return null; 2061 } 2062 // walk the earlier rows; the last one visited is the row above. 2063 var prev_first: ?usize = null; 2064 var i: usize = 0; 2065 while (i < rects.len and rects[i].y < cur_y) { 2066 prev_first = i; 2067 const y = rects[i].y; 2068 while (i < rects.len and rects[i].y == y) i += 1; 2069 } 2070 return prev_first; 2071 } 2072 }; 2073 2074 // an invisible widget that fills the horizontal space granted by its parent. 2075 // used inside Box(horiz) with a min_size so the box reserves space for the 2076 // children that follow, pushing them to the right. 2077 pub const Spacer = struct { 2078 focus: *Focus, 2079 grid: ?Grid, 2080 2081 pub fn init(allocator: std.mem.Allocator) !Spacer { 2082 return .{ 2083 .focus = try Focus.create(allocator, .container), 2084 .grid = null, 2085 }; 2086 } 2087 2088 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 2089 self.focus.destroy(allocator); 2090 if (self.grid) |*grid| { 2091 grid.deinit(); 2092 self.grid = null; 2093 } 2094 } 2095 2096 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2097 _ = root_focus; 2098 self.clearGrid(); 2099 const width = constraint.max_size.width orelse return; 2100 if (width == 0) return; 2101 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2102 } 2103 2104 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2105 _ = self; 2106 _ = allocator; 2107 _ = key; 2108 _ = root_focus; 2109 } 2110 2111 pub fn clearGrid(self: *Spacer) void { 2112 if (self.grid) |*grid| { 2113 grid.deinit(); 2114 self.grid = null; 2115 } 2116 } 2117 2118 pub fn getGrid(self: Spacer) ?Grid { 2119 return self.grid; 2120 } 2121 2122 pub fn getFocus(self: *Spacer) *Focus { 2123 return self.focus; 2124 } 2125 }; 2126 2127 // a one-row, non-focusable status bar showing the current page's url 2128 pub const Footer = struct { 2129 focus: *Focus, 2130 grid: ?Grid, 2131 session: *Session, 2132 arena: std.heap.ArenaAllocator, 2133 2134 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2135 return .{ 2136 .focus = try Focus.create(allocator, .container), 2137 .grid = null, 2138 .session = session, 2139 .arena = std.heap.ArenaAllocator.init(allocator), 2140 }; 2141 } 2142 2143 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2144 self.focus.destroy(allocator); 2145 if (self.grid) |*grid| grid.deinit(); 2146 self.arena.deinit(); 2147 } 2148 2149 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2150 _ = root_focus; 2151 self.clearGrid(); 2152 const width = constraint.max_size.width orelse return; 2153 if (width == 0) return; 2154 2155 _ = self.arena.reset(.retain_capacity); 2156 const aa = self.arena.allocator(); 2157 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2158 const text = if (self.session.web_port) |port| 2159 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 2160 else 2161 path; 2162 2163 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2164 errdefer grid.deinit(); 2165 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2166 var i: usize = 0; 2167 while (utf8.nextCodepointSlice()) |ch| { 2168 if (i == width) break; 2169 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2170 i += 1; 2171 } 2172 self.grid = grid; 2173 } 2174 2175 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2176 _ = self; 2177 _ = allocator; 2178 _ = key; 2179 _ = root_focus; 2180 } 2181 2182 pub fn clearGrid(self: *Footer) void { 2183 if (self.grid) |*grid| { 2184 grid.deinit(); 2185 self.grid = null; 2186 } 2187 } 2188 2189 pub fn getGrid(self: Footer) ?Grid { 2190 return self.grid; 2191 } 2192 2193 pub fn getFocus(self: *Footer) *Focus { 2194 return self.focus; 2195 } 2196 }; 2197 2198 // a single-child wrapper that builds the child at its natural size and 2199 // positions its grid in the middle of the area granted by the parent 2200 // a bordered label, indented past the content it precedes 2201 pub const SectionLabel = struct { 2202 box: wgt.Box(Widget), 2203 2204 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2205 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2206 errdefer box.deinit(allocator); 2207 2208 { 2209 var gap = try wgt.Text(Widget).init(allocator, " "); 2210 errdefer gap.deinit(allocator); 2211 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2212 } 2213 2214 { 2215 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2216 errdefer tb.deinit(allocator); 2217 tb.getFocus().focusable = true; 2218 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2219 } 2220 2221 box.getFocus().child_id = box.children.keys()[1]; 2222 return .{ .box = box }; 2223 } 2224 2225 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2226 self.box.deinit(allocator); 2227 } 2228 2229 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2230 try self.box.build(allocator, constraint, root_focus); 2231 } 2232 2233 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2234 _ = self; 2235 _ = allocator; 2236 _ = key; 2237 _ = root_focus; 2238 } 2239 2240 pub fn clearGrid(self: *SectionLabel) void { 2241 self.box.clearGrid(); 2242 } 2243 2244 pub fn getGrid(self: SectionLabel) ?Grid { 2245 return self.box.getGrid(); 2246 } 2247 2248 pub fn getFocus(self: *SectionLabel) *Focus { 2249 return self.box.getFocus(); 2250 } 2251 }; 2252 2253 // a submit button, indented past its form's content. the web overlay POSTs 2254 // the button's form; terminal hosts detect presses through `buttonId`. 2255 pub const SubmitButton = struct { 2256 box: wgt.Box(Widget), 2257 2258 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2259 return initLabeled(allocator, "submit"); 2260 } 2261 2262 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2263 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2264 errdefer box.deinit(allocator); 2265 2266 { 2267 var gap = try wgt.Text(Widget).init(allocator, " "); 2268 errdefer gap.deinit(allocator); 2269 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2270 } 2271 2272 { 2273 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2274 errdefer button.deinit(allocator); 2275 button.getFocus().focusable = true; 2276 // the renderer distinguishes plain clickables from buttons that 2277 // should POST to a server route by this kind. 2278 button.getFocus().kind = .{ .custom = "submit" }; 2279 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2280 } 2281 2282 box.getFocus().child_id = box.children.keys()[1]; 2283 return .{ .box = box }; 2284 } 2285 2286 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2287 self.box.deinit(allocator); 2288 } 2289 2290 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2291 try self.box.build(allocator, constraint, root_focus); 2292 } 2293 2294 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2295 _ = self; 2296 _ = allocator; 2297 _ = key; 2298 _ = root_focus; 2299 } 2300 2301 pub fn clearGrid(self: *SubmitButton) void { 2302 self.box.clearGrid(); 2303 } 2304 2305 pub fn getGrid(self: SubmitButton) ?Grid { 2306 return self.box.getGrid(); 2307 } 2308 2309 pub fn getFocus(self: *SubmitButton) *Focus { 2310 return self.box.getFocus(); 2311 } 2312 2313 // the button's focus id, for click hit-testing 2314 pub fn buttonId(self: *const SubmitButton) usize { 2315 return self.box.children.keys()[1]; 2316 } 2317 }; 2318 2319 pub const Center = struct { 2320 focus: *Focus, 2321 grid: ?Grid, 2322 child: *Widget, 2323 2324 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2325 const child = try allocator.create(Widget); 2326 errdefer allocator.destroy(child); 2327 child.* = child_widget; 2328 return .{ 2329 .focus = try Focus.create(allocator, .container), 2330 .grid = null, 2331 .child = child, 2332 }; 2333 } 2334 2335 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2336 self.focus.destroy(allocator); 2337 if (self.grid) |*grid| { 2338 grid.deinit(); 2339 self.grid = null; 2340 } 2341 self.child.deinit(allocator); 2342 allocator.destroy(self.child); 2343 } 2344 2345 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2346 self.clearGrid(); 2347 self.getFocus().clear(); 2348 2349 // build the child without forcing it to fill min_size; it sizes 2350 // itself to its content within the available max. 2351 try self.child.build(allocator, .{ 2352 .min_size = .{ .width = null, .height = null }, 2353 .max_size = constraint.max_size, 2354 }, root_focus); 2355 2356 const child_grid = self.child.getGrid() orelse return; 2357 2358 // prefer max when given; otherwise grow to max(min, child) so that 2359 // when only a min is set (e.g. the wasm path passes viewport rows 2360 // as min_height with no max), we can still vertically center while 2361 // letting taller content extend past the min. 2362 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 2363 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 2364 if (width == 0 or height == 0) return; 2365 2366 const offset_x: usize = (width -| child_grid.size.width) / 2; 2367 const offset_y: usize = (height -| child_grid.size.height) / 2; 2368 2369 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2370 errdefer grid.deinit(); 2371 try grid.drawGrid(child_grid, offset_x, offset_y); 2372 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2373 self.getFocus().child_id = self.child.getFocus().id; 2374 2375 self.grid = grid; 2376 } 2377 2378 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2379 try self.child.input(allocator, key, root_focus); 2380 } 2381 2382 pub fn clearGrid(self: *Center) void { 2383 if (self.grid) |*grid| { 2384 grid.deinit(); 2385 self.grid = null; 2386 } 2387 self.child.clearGrid(); 2388 } 2389 2390 pub fn getGrid(self: Center) ?Grid { 2391 return self.grid; 2392 } 2393 2394 pub fn getFocus(self: *Center) *Focus { 2395 return self.focus; 2396 } 2397 }; 2398 2399 // renders truecolor ANSI art 2400 pub const AnsiArt = struct { 2401 focus: *Focus, 2402 grid: ?Grid, 2403 content: []const u8, 2404 2405 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2406 return .{ 2407 .focus = try Focus.create(allocator, .container), 2408 .grid = null, 2409 .content = content, 2410 }; 2411 } 2412 2413 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2414 self.focus.destroy(allocator); 2415 if (self.grid) |*grid| { 2416 grid.deinit(); 2417 self.grid = null; 2418 } 2419 } 2420 2421 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2422 _ = root_focus; 2423 self.clearGrid(); 2424 2425 // parse into rows of cells, then copy into a rectangular grid 2426 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2427 defer { 2428 for (rows.items) |*row| row.deinit(allocator); 2429 rows.deinit(allocator); 2430 } 2431 var row: std.ArrayList(Grid.Cell) = .empty; 2432 errdefer row.deinit(allocator); 2433 2434 var style: Grid.Style = .{}; 2435 var width: usize = 0; 2436 const content = self.content; 2437 var i: usize = 0; 2438 while (i < content.len) { 2439 const byte = content[i]; 2440 if (byte == '\n') { 2441 width = @max(width, row.items.len); 2442 try rows.append(allocator, row); 2443 row = .empty; 2444 i += 1; 2445 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2446 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2447 var j = i + 2; 2448 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2449 if (j >= content.len) { 2450 i = content.len; // malformed trailing escape; stop 2451 } else { 2452 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2453 i = j + 1; 2454 } 2455 } else { 2456 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2457 const end = @min(content.len, i + len); 2458 const rune = content[i..end]; 2459 const transparent = end - i == 1 and rune[0] == ' ' and 2460 style.fg == null and style.bg == null and !style.inverted; 2461 try row.append(allocator, .{ 2462 .rune = if (transparent) null else rune, 2463 .style = style, 2464 }); 2465 i = end; 2466 } 2467 } 2468 // a trailing row with content but no closing newline 2469 if (row.items.len > 0) { 2470 width = @max(width, row.items.len); 2471 try rows.append(allocator, row); 2472 } else { 2473 row.deinit(allocator); 2474 } 2475 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2476 2477 const height = rows.items.len; 2478 if (width == 0 or height == 0) return; 2479 2480 const clamped_w = @min(width, constraint.max_size.width orelse width); 2481 const clamped_h = @min(height, constraint.max_size.height orelse height); 2482 2483 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2484 errdefer grid.deinit(); 2485 for (rows.items[0..clamped_h], 0..) |r, y| { 2486 const n = @min(r.items.len, clamped_w); 2487 for (r.items[0..n], 0..) |cell, x| { 2488 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2489 } 2490 } 2491 self.grid = grid; 2492 } 2493 2494 pub fn clearGrid(self: *AnsiArt) void { 2495 if (self.grid) |*grid| { 2496 grid.deinit(); 2497 self.grid = null; 2498 } 2499 } 2500 2501 pub fn getGrid(self: AnsiArt) ?Grid { 2502 return self.grid; 2503 } 2504 2505 pub fn getFocus(self: *AnsiArt) *Focus { 2506 return self.focus; 2507 } 2508 2509 fn applySgr(style: *Grid.Style, params: []const u8) void { 2510 var nums: [16]u32 = undefined; 2511 var n: usize = 0; 2512 var it = std.mem.splitScalar(u8, params, ';'); 2513 while (it.next()) |tok| { 2514 if (n >= nums.len) break; 2515 // an empty parameter (e.g. bare "\x1b[m") means 0 2516 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2517 n += 1; 2518 } 2519 if (n == 0) { 2520 style.* = .{}; 2521 return; 2522 } 2523 var i: usize = 0; 2524 while (i < n) : (i += 1) { 2525 switch (nums[i]) { 2526 0 => style.* = .{}, 2527 7 => style.inverted = true, 2528 27 => style.inverted = false, 2529 39 => style.fg = null, 2530 49 => style.bg = null, 2531 38, 48 => { 2532 // truecolor form: 38;2;r;g;b — anything else is ignored 2533 if (i + 4 < n and nums[i + 1] == 2) { 2534 const c = Grid.Color{ 2535 .r = @truncate(nums[i + 2]), 2536 .g = @truncate(nums[i + 3]), 2537 .b = @truncate(nums[i + 4]), 2538 }; 2539 if (nums[i] == 38) style.fg = c else style.bg = c; 2540 i += 4; 2541 } 2542 }, 2543 else => {}, 2544 } 2545 } 2546 } 2547 }; 2548 2549 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2550 pub const AnsiBackground = struct { 2551 grid: ?Grid, 2552 child: *Widget, 2553 art: AnsiArt, 2554 session: *Session, 2555 2556 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2557 var cw = child_widget; 2558 const child = allocator.create(Widget) catch |e| { 2559 cw.deinit(allocator); 2560 return e; 2561 }; 2562 child.* = cw; 2563 errdefer { 2564 child.deinit(allocator); 2565 allocator.destroy(child); 2566 } 2567 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2568 } 2569 2570 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2571 if (self.grid) |*grid| grid.deinit(); 2572 self.art.deinit(allocator); 2573 self.child.deinit(allocator); 2574 allocator.destroy(self.child); 2575 } 2576 2577 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2578 self.clearGrid(); 2579 2580 // make the wrapped page fill the whole available area 2581 try self.child.build(allocator, .{ 2582 .min_size = .{ 2583 .width = constraint.max_size.width orelse constraint.min_size.width, 2584 .height = constraint.max_size.height orelse constraint.min_size.height, 2585 }, 2586 .max_size = constraint.max_size, 2587 }, root_focus); 2588 2589 if (self.session.data.enable_ansi) { 2590 if (self.child.getGrid()) |fg| { 2591 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2592 } 2593 } 2594 } 2595 2596 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2597 try self.child.input(allocator, key, root_focus); 2598 } 2599 2600 pub fn clearGrid(self: *AnsiBackground) void { 2601 if (self.grid) |*grid| { 2602 grid.deinit(); 2603 self.grid = null; 2604 } 2605 self.child.clearGrid(); 2606 self.art.clearGrid(); 2607 } 2608 2609 pub fn getGrid(self: AnsiBackground) ?Grid { 2610 return self.grid orelse self.child.getGrid(); 2611 } 2612 2613 pub fn getFocus(self: *AnsiBackground) *Focus { 2614 return self.child.getFocus(); 2615 } 2616 2617 // which top corner the art hugs. 2618 const ArtAnchor = enum { top_left, top_right }; 2619 2620 // a foreground cell counts as blank if it's empty or a bare space without styling 2621 fn cellIsBlank(cell: Grid.Cell) bool { 2622 if (cell.rune) |rune| { 2623 return std.mem.eql(u8, rune, " ") and 2624 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2625 } 2626 return true; 2627 } 2628 2629 // the art is dimmed to this fraction of its brightness so foreground text 2630 // stays legible over it 2631 const art_brightness = 45; // percent 2632 2633 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2634 const c = color orelse return null; 2635 return .{ 2636 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2637 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2638 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2639 }; 2640 } 2641 2642 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2643 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2644 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2645 // snap such channels clear of that range so the backdrop doesn't blink or 2646 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2647 // actually render truecolor. 2648 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2649 const c = color orelse return null; 2650 const snap = struct { 2651 fn f(v: u8) u8 { 2652 return if (v >= 1 and v <= 9) 10 else v; 2653 } 2654 }.f; 2655 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2656 } 2657 2658 // composites ANSI art behind a foreground grid 2659 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2660 art.clearGrid(); 2661 try art.build(allocator, .{ 2662 .min_size = .{ .width = null, .height = null }, 2663 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2664 }, root_focus); 2665 2666 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2667 errdefer out.deinit(); 2668 2669 if (art.getGrid()) |art_grid| { 2670 const anchor_x = switch (anchor) { 2671 .top_left => 0, 2672 .top_right => foreground.size.width -| art_grid.size.width, 2673 }; 2674 for (0..art_grid.size.height) |y| { 2675 for (0..art_grid.size.width) |x| { 2676 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2677 if (src.rune == null) continue; 2678 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2679 const dst = &out.cells.items[idx]; 2680 // composite the dimmed art behind every cell so it shows 2681 // through the whole UI: a blank cell becomes the art, while a 2682 // cell with a glyph keeps the glyph but takes the dimmed art as 2683 // its background (replacing whatever background it had, which 2684 // would otherwise obscure the art). 2685 if (cellIsBlank(dst.*)) { 2686 dst.* = src; 2687 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2688 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2689 } else { 2690 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2691 dst.style.bg = background_maybe; 2692 // change the color of text overlaying ANSI art to ensure 2693 // it is readable. don't do this in the web UI, since we 2694 // have direct control of the theme there (also, inner 2695 // scroll panes allow text to move without a re-render 2696 // by the TUI, which would break this). also don't do this 2697 // if the text is explicitly styled. 2698 if (is_terminal and dst.style.fg == null) { 2699 if (background_maybe) |background| { 2700 // use near-black and near-white, which is easier on the eyes 2701 // while retaining high contrast 2702 const luminance = (@as(u32, background.r) * 299 + 2703 @as(u32, background.g) * 587 + 2704 @as(u32, background.b) * 114) / 1000; 2705 dst.style.fg = if (luminance >= 128) 2706 .{ .r = 16, .g = 16, .b = 16 } 2707 else 2708 .{ .r = 240, .g = 240, .b = 240 }; 2709 } 2710 } 2711 } 2712 } 2713 } 2714 } 2715 return out; 2716 } 2717 }; 2718