╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
68 69 pub fn init(arena: *std.heap.ArenaAllocator, session: *Session, route: RoutablePage) !Page { 70 // the repo page can build without a moment in local mode; the home and 71 // user pages always read from the admin db. 72 return switch (route.parent()) { 73 .home => .{ .home = try Home.init(arena, session.haxy_moment orelse return error.NoMoment, switch (route) { 74 .home_users => |start| start, 75 else => 0, 76 }, switch (route) { 77 .home_repos => |start| start, 78 else => 0, 79 }) }, 80 .user => blk: { 81 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 82 break :blk switch (route) { 83 .user_repos => |u| .{ .user = try User.init(arena, session, haxy_moment, u.name, u.start, 0) }, 84 .user_forks => |u| .{ .user = try User.init(arena, session, haxy_moment, u.name, 0, u.start) }, 85 .user_settings, .user_auth => |name| .{ .user = try User.init(arena, session, haxy_moment, name, 0, 0) }, 86 else => return error.UnexpectedRoute, 87 }; 88 }, 89 .repo => switch (route) { 90 .repo_files, .repo_commits, .repo_diff, .repo_refs, .repo_issues, .repo_patches, .repo_discussions, .repo_events, .repo_settings, .repo_auth => .{ .repo = try Repo.init(arena, session, route) }, 91 else => return error.UnexpectedRoute, 92 }, 93 .fork => switch (route) { 94 .fork_patch, .fork_diff, .fork_files, .fork_commits, .fork_settings, .fork_auth => .{ .fork = try Fork.init(arena, session, route) }, 95 else => return error.UnexpectedRoute, 96 }, 97 }; 98 } 99 }; 100 101 // what the server hands to the client (and what main_wasm parses on _start). 102 // keeps Page free of any per-request session state. 103 pub const Snapshot = struct { 104 page: Page, 105 session: Session.Data = .{}, 106 }; 107 108 // a top-level "page" the user can navigate to. the user_* variants are the 109 // tabs of a user page; they each carry the user's name and all map to the 110 // .user parent page, so switching between them stays on that page (and just 111 // updates the url) rather than navigating away. 112 pub const RoutablePage = union(enum) { 113 home_users: usize, // 0 = first page 114 home_repos: usize, // 0 = first page 115 home_settings, 116 home_auth, 117 user_repos: struct { name: Array(evt.User.name_max_len), start: usize = 0 }, 118 user_forks: struct { name: Array(evt.User.name_max_len), start: usize = 0 }, 119 user_settings: Array(evt.User.name_max_len), 120 user_auth: Array(evt.User.name_max_len), 121 repo_files: RepoFilesRoute, 122 repo_commits: RepoCommitsRoute, 123 repo_diff: RepoDiffRoute, 124 repo_refs: struct { 125 name: Array(repo_route_max_len), 126 kind: RefKind = .branch, 127 // the url-encoded ref name `kind`'s column windows from ("" = the 128 // first window; the other column always shows its first window). 129 from: Array(ref_route_max_len) = .{}, 130 }, 131 repo_issues: struct { 132 name: Array(repo_route_max_len), 133 // the tag the list is filtered to, url-encoded ("" = unfiltered). 134 tag: Array(issue_tag_route_max_len) = .{}, 135 // the issue the route names: the list's window root, or the issue the 136 // edit/description/resolve view shows. 137 selected: Array(evt.event_id_size * 2) = .{}, 138 // the resolve view's comma-separated list of fields prefilled from 139 // their side of the conflict. 140 theirs: Array(theirs_route_max_len) = .{}, 141 // the view the url names; a route with `selected` derives its view 142 // from that issue's status instead. 143 view: IssuesView = .open, 144 // the selected issue's comment window (0 = the first window). 145 comments_start: usize = 0, 146 // the comment shown in the detail pane (empty = the issue thread). 147 comment: Array(evt.event_id_size * 2) = .{}, 148 }, 149 repo_patches: struct { 150 name: Array(repo_route_max_len), 151 tag: Array(patch_tag_route_max_len) = .{}, 152 selected: Array(evt.event_id_size * 2) = .{}, 153 theirs: Array(theirs_route_max_len) = .{}, 154 view: PatchesView = .open, 155 comments_start: usize = 0, 156 comment: Array(evt.event_id_size * 2) = .{}, 157 }, 158 repo_discussions: struct { 159 name: Array(repo_route_max_len), 160 tag: Array(discussion_tag_route_max_len) = .{}, 161 selected: Array(evt.event_id_size * 2) = .{}, 162 view: DiscussionsView = .recent, 163 comments_start: usize = 0, 164 comment: Array(evt.event_id_size * 2) = .{}, 165 }, 166 repo_events: RepoEventsRoute, 167 repo_settings: Array(repo_route_max_len), 168 repo_auth: Array(repo_route_max_len), 169 fork_patch: ForkRoute, 170 fork_diff: struct { fork: ForkRoute, start: usize = 0, path: Array(repo_route_max_len) = .{} }, 171 fork_files: ForkFilesRoute, 172 fork_commits: ForkCommitsRoute, 173 fork_settings: ForkRoute, 174 fork_auth: ForkRoute, 175 176 pub const default: RoutablePage = .{ .home_repos = 0 }; 177 178 pub const RefKind = enum { branch, tag }; 179 180 pub const IssuesView = enum { open, closed, tags, new, edit, description, new_comment, edit_comment, remove, conflicts, resolve }; 181 182 pub const PatchesView = enum { open, closed, merged, tags, new, drafts, edit, publish, merge, description, new_comment, edit_comment, remove, conflicts, resolve }; 183 184 pub const DiscussionsView = enum { recent, tags, new, edit, description, new_comment, edit_comment, remove }; 185 186 pub const EventsView = enum { active, removed }; 187 188 pub const RefOrOid = enum { 189 branch, 190 tag, 191 object, 192 }; 193 194 // the "key:value" segments of a url tail, collected in any order 195 const Params = struct { 196 const ParamKey = enum { start, line, branch, tag, object, base, patchrev, theirs }; 197 198 // a branch:/tag:/object: param; the value stays url-encoded 199 const RefParam = struct { kind: RefOrOid, value: []const u8 }; 200 201 values: std.enums.EnumArray(ParamKey, ?[]const u8) = .initFill(null), 202 203 // consume the leading "key:value" segments, stopping at the first 204 // segment that isn't one 205 fn scanPairs(self: *Params, segments: *std.mem.SplitIterator(u8, .scalar)) error{BadUrl}!void { 206 while (segments.peek()) |seg| { 207 const colon = std.mem.indexOfScalar(u8, seg, ':') orelse return; 208 const key = std.meta.stringToEnum(ParamKey, seg[0..colon]) orelse return; 209 const value = seg[colon + 1 ..]; 210 if (value.len == 0) return error.BadUrl; 211 if (self.values.get(key) != null) return error.BadUrl; 212 self.values.set(key, value); 213 _ = segments.next(); 214 } 215 } 216 217 // consume every remaining segment: pairs plus at most one bare word, 218 // which is returned 219 fn scanTail(self: *Params, segments: *std.mem.SplitIterator(u8, .scalar)) error{BadUrl}!?[]const u8 { 220 var word: ?[]const u8 = null; 221 while (true) { 222 try self.scanPairs(segments); 223 const seg = segments.next() orelse return word; 224 if (seg.len == 0 or word != null) return error.BadUrl; 225 word = seg; 226 } 227 } 228 229 // true when every set param is one of `allowed` 230 fn only(self: Params, allowed: []const ParamKey) bool { 231 for (std.enums.values(ParamKey)) |key| { 232 if (self.values.get(key) == null) continue; 233 if (std.mem.indexOfScalar(ParamKey, allowed, key) == null) return false; 234 } 235 return true; 236 } 237 238 // the "start:<n>" window param (0 when absent), null when malformed 239 fn start(self: Params) ?usize { 240 const value = self.values.get(.start) orelse return 0; 241 return std.fmt.parseInt(usize, value, 10) catch null; 242 } 243 244 // the "line:<n>" first-line param (0 when absent), null when malformed 245 fn line(self: Params) ?usize { 246 const value = self.values.get(.line) orelse return 0; 247 return std.fmt.parseInt(usize, value, 10) catch null; 248 } 249 250 // the single branch:/tag:/object: param, null when none is set 251 fn ref(self: Params) error{BadUrl}!?RefParam { 252 var result: ?RefParam = null; 253 inline for (@typeInfo(RefOrOid).@"enum".fields) |field| { 254 if (self.values.get(@field(ParamKey, field.name))) |value| { 255 if (result != null) return error.BadUrl; 256 result = .{ .kind = @field(RefOrOid, field.name), .value = value }; 257 } 258 } 259 return result; 260 } 261 }; 262 263 const user_segment = "/user/"; 264 const repo_segment = "/repo/"; 265 const fork_segment = "/fork/"; 266 const files_seg = "files"; 267 const commits_seg = "commits"; 268 // ends a commits route naming its commit's full message 269 const message_seg = "message"; 270 // the `Params` key spellings the url emitters use 271 const tag_filter_seg = @tagName(Params.ParamKey.tag) ++ ":"; 272 const start_seg = @tagName(Params.ParamKey.start) ++ ":"; 273 const line_seg = @tagName(Params.ParamKey.line) ++ ":"; 274 const base_seg = @tagName(Params.ParamKey.base) ++ ":"; 275 const theirs_seg = @tagName(Params.ParamKey.theirs) ++ ":"; 276 const issue_seg = "issue:"; 277 const patch_seg = "patch:"; 278 const discuss_seg = "discuss:"; 279 const comment_seg = "comment:"; 280 // marks a files or commits route's trailing path: the value is the raw 281 // remainder of the url, so it must come last 282 const path_seg = "path:"; 283 284 // a repo route is "username/reponame", optionally followed by the files 285 // tab's "/files/<refkind>:<refvalue>[/line:<n>][/path:<dir>]"; the bare 286 // pair means the default branch's files root. a local session's routes 287 // elide the identity: "" at the bare root, else starting at the tail's "/". 288 pub const repo_route_max_len = 1024; 289 pub const repo_identity_max_len = evt.User.name_max_len + 1 + evt.Repo.name_max_len; 290 291 // true when a repo route's stored string elides its identity (local mode). 292 // unambiguous because a full string always starts with its owner segment. 293 fn identityElided(s: []const u8) bool { 294 return s.len == 0 or s[0] == '/'; 295 } 296 297 // a url-encoded tag can grow to three bytes per source byte. 298 pub const issue_tag_route_max_len = evt.Issue.tag_max_len * 3; 299 pub const patch_tag_route_max_len = evt.Patch.tag_max_len * 3; 300 pub const discussion_tag_route_max_len = evt.Discussion.tag_max_len * 3; 301 302 // caps the resolve view's theirs: field list (a longer one doesn't route). 303 pub const theirs_route_max_len = 512; 304 305 // caps a url-encoded ref name in a route (a longer name doesn't route). 306 pub const ref_route_max_len = 512; 307 308 // the identity stored by every repo route. Local routes elide it. 309 pub const RepoIdentity = struct { 310 identity: []const u8, // "owner/name" 311 owner: []const u8, 312 name: []const u8, 313 314 pub fn parse(s: []const u8) ?RepoIdentity { 315 var result: RepoIdentity = .{ 316 .identity = "", 317 .owner = "", 318 .name = "", 319 }; 320 if (identityElided(s)) { 321 return result; 322 } else { 323 var id_segments = std.mem.splitScalar(u8, s, '/'); 324 const owner = id_segments.next() orelse return null; 325 const name = id_segments.next() orelse return null; 326 if (owner.len == 0 or name.len == 0) return null; 327 result.identity = s[0 .. owner.len + 1 + name.len]; 328 result.owner = owner; 329 result.name = name; 330 } 331 return result; 332 } 333 }; 334 335 pub const RepoFilesRoute = struct { 336 name: Array(repo_identity_max_len), 337 ref_kind: ?RefOrOid = null, 338 ref_value: Array(ref_route_max_len) = .{}, 339 patchrev_id: Array(evt.event_id_size * 2) = .{}, 340 path: Array(repo_route_max_len) = .{}, 341 // the selected file's first visible line (1-based; 0 = unset). 342 line: usize = 0, 343 }; 344 345 pub const RepoDiffRoute = struct { 346 name: Array(repo_identity_max_len), 347 ref_or_oid: ?RefOrOid = null, 348 value: Array(ref_route_max_len) = .{}, 349 base_oid: Array(ref_route_max_len) = .{}, 350 patchrev_id: Array(evt.event_id_size * 2) = .{}, 351 start: usize = 0, 352 path: Array(repo_route_max_len) = .{}, 353 }; 354 355 pub const RepoCommitsRoute = struct { 356 name: Array(repo_identity_max_len), 357 ref_or_oid: ?RefOrOid = null, 358 value: Array(ref_route_max_len) = .{}, 359 base_oid: Array(ref_route_max_len) = .{}, 360 // what the pane shows for the commit the log walks from. 361 content: Content = .{ .diff = .{} }, 362 363 // a diff window and the message are alternatives, so the window's 364 // params can't be named alongside the message. 365 pub const Content = union(enum) { 366 diff: struct { 367 // the hunk index the window starts at. 368 start: usize = 0, 369 // the file the diff is filtered to ("" = every file). 370 path: Array(repo_route_max_len) = .{}, 371 }, 372 message, 373 }; 374 }; 375 376 pub const RepoEventsRoute = struct { 377 name: Array(repo_identity_max_len), 378 view: EventsView = .active, 379 kind: ?evt.EventKind = null, 380 selected: Array(evt.event_id_size * 2) = .{}, 381 }; 382 383 pub const ForkRoute = struct { 384 name: Array(repo_identity_max_len), 385 id: Array(evt.event_id_size * 2), 386 }; 387 388 pub const ForkFilesRoute = struct { 389 fork: ForkRoute, 390 oid: Array(ref_route_max_len) = .{}, 391 path: Array(repo_route_max_len) = .{}, 392 line: usize = 0, 393 }; 394 395 pub const ForkCommitsRoute = struct { 396 fork: ForkRoute, 397 oid: Array(ref_route_max_len) = .{}, 398 content: RepoCommitsRoute.Content = .{ .diff = .{} }, 399 }; 400 401 // where the shared files and commits views are mounted. fork routes always 402 // read their implicit `patch` branch unless they name an object directly. 403 pub const RepoLocation = union(enum) { 404 repo: []const u8, 405 fork: struct { 406 identity: []const u8, 407 id: []const u8, 408 }, 409 410 pub fn dupe(self: RepoLocation, allocator: std.mem.Allocator) !RepoLocation { 411 return switch (self) { 412 .repo => |identity_value| .{ .repo = try allocator.dupe(u8, identity_value) }, 413 .fork => |f| .{ .fork = .{ 414 .identity = try allocator.dupe(u8, f.identity), 415 .id = try allocator.dupe(u8, f.id), 416 } }, 417 }; 418 } 419 420 pub fn filesRoute(self: RepoLocation, ref_or_oid: RefOrOid, value: []const u8, path: []const u8, line: usize) ?RoutablePage { 421 return switch (self) { 422 .repo => |repo_identity| repoFilesRoute(repo_identity, ref_or_oid, value, path, line), 423 .fork => |f| if (ref_or_oid == .branch and std.mem.eql(u8, value, "patch")) 424 forkFilesRoute(f.identity, f.id, "", path, line) 425 else if (ref_or_oid == .object) 426 forkFilesRoute(f.identity, f.id, value, path, line) 427 else 428 null, 429 }; 430 } 431 432 pub fn commitsRoute(self: RepoLocation, ref_or_oid: ?RefOrOid, value: []const u8, start: usize, path: []const u8, base_oid: []const u8) ?RoutablePage { 433 return switch (self) { 434 .repo => |repo_identity| repoCommitsRoute(repo_identity, ref_or_oid, value, start, path, base_oid), 435 .fork => |f| if (ref_or_oid == null or (ref_or_oid == .branch and std.mem.eql(u8, value, "patch"))) 436 forkCommitsRoute(f.identity, f.id, "", start, path) 437 else if (ref_or_oid == .object) 438 forkCommitsRoute(f.identity, f.id, value, start, path) 439 else 440 null, 441 }; 442 } 443 444 pub fn commitMessageRoute(self: RepoLocation, ref_or_oid: RefOrOid, value: []const u8, base_oid: []const u8) ?RoutablePage { 445 return switch (self) { 446 .repo => |repo_identity| repoCommitMessageRoute(repo_identity, ref_or_oid, value, base_oid), 447 .fork => |f| if (ref_or_oid == .object) forkCommitMessageRoute(f.identity, f.id, value) else null, 448 }; 449 } 450 }; 451 452 fn initForkRoute(identity: []const u8, id: []const u8) ?ForkRoute { 453 if (id.len != evt.event_id_size * 2) return null; 454 return .{ 455 .name = Array(repo_identity_max_len).from(identity) orelse return null, 456 .id = Array(evt.event_id_size * 2).from(id) orelse return null, 457 }; 458 } 459 460 pub fn forkPatchRoute(identity: []const u8, id: []const u8) ?RoutablePage { 461 return .{ .fork_patch = initForkRoute(identity, id) orelse return null }; 462 } 463 464 pub fn forkDiffRoute(identity: []const u8, id: []const u8, start: usize, path: []const u8) ?RoutablePage { 465 return .{ .fork_diff = .{ 466 .fork = initForkRoute(identity, id) orelse return null, 467 .start = start, 468 .path = Array(repo_route_max_len).from(path) orelse return null, 469 } }; 470 } 471 472 pub fn forkFilesRoute(identity: []const u8, id: []const u8, oid: []const u8, path: []const u8, line: usize) ?RoutablePage { 473 return .{ .fork_files = .{ 474 .fork = initForkRoute(identity, id) orelse return null, 475 .oid = Array(ref_route_max_len).from(oid) orelse return null, 476 .path = Array(repo_route_max_len).from(path) orelse return null, 477 .line = line, 478 } }; 479 } 480 481 pub fn forkCommitsRoute(identity: []const u8, id: []const u8, oid: []const u8, start: usize, path: []const u8) ?RoutablePage { 482 return .{ .fork_commits = .{ 483 .fork = initForkRoute(identity, id) orelse return null, 484 .oid = Array(ref_route_max_len).from(oid) orelse return null, 485 .content = .{ .diff = .{ 486 .start = start, 487 .path = Array(repo_route_max_len).from(path) orelse return null, 488 } }, 489 } }; 490 } 491 492 pub fn forkCommitMessageRoute(identity: []const u8, id: []const u8, oid: []const u8) ?RoutablePage { 493 if (oid.len == 0) return null; 494 return .{ .fork_commits = .{ 495 .fork = initForkRoute(identity, id) orelse return null, 496 .oid = Array(ref_route_max_len).from(oid) orelse return null, 497 .content = .message, 498 } }; 499 } 500 501 pub fn forkSettingsRoute(identity: []const u8, id: []const u8) ?RoutablePage { 502 return .{ .fork_settings = initForkRoute(identity, id) orelse return null }; 503 } 504 505 pub fn forkAuthRoute(identity: []const u8, id: []const u8) ?RoutablePage { 506 return .{ .fork_auth = initForkRoute(identity, id) orelse return null }; 507 } 508 509 // build a `.repo_files` route (a null ref_kind = the bare default-branch 510 // root). null if the result doesn't fit the inline name. 511 pub fn repoFilesRoute(identity: []const u8, ref_kind: ?RefOrOid, ref_value: []const u8, dir: []const u8, line: usize) ?RoutablePage { 512 if (ref_kind == null and (ref_value.len != 0 or dir.len != 0)) return null; 513 if (ref_kind != null and ref_value.len == 0 and dir.len != 0) return null; 514 return .{ .repo_files = .{ 515 .name = Array(repo_identity_max_len).from(identity) orelse return null, 516 .ref_kind = ref_kind, 517 .ref_value = Array(ref_route_max_len).from(ref_value) orelse return null, 518 .path = Array(repo_route_max_len).from(dir) orelse return null, 519 .line = line, 520 } }; 521 } 522 523 pub fn repoPatchRevFilesRoute(identity: []const u8, id: []const u8, path: []const u8, line: usize) ?RoutablePage { 524 _ = evt.parseEventId(id) catch return null; 525 return .{ .repo_files = .{ 526 .name = Array(repo_identity_max_len).from(identity) orelse return null, 527 .patchrev_id = Array(evt.event_id_size * 2).from(id) orelse return null, 528 .path = Array(repo_route_max_len).from(path) orelse return null, 529 .line = line, 530 } }; 531 } 532 533 // build a `.repo_commits` route (a null ref_or_oid = the default branch). 534 // always carries the `commits` marker so the bare route doesn't collide 535 // with the files root. a non-empty `path` filters the diff pane to that 536 // file and requires a ref. 537 pub fn repoCommitsRoute(identity: []const u8, ref_or_oid: ?RefOrOid, value: []const u8, start: usize, path: []const u8, base_oid: []const u8) ?RoutablePage { 538 if (ref_or_oid == null and (value.len != 0 or path.len != 0)) return null; 539 return .{ .repo_commits = .{ 540 .name = Array(repo_identity_max_len).from(identity) orelse return null, 541 .ref_or_oid = ref_or_oid, 542 .value = Array(ref_route_max_len).from(value) orelse return null, 543 .base_oid = Array(ref_route_max_len).from(base_oid) orelse return null, 544 .content = .{ .diff = .{ 545 .start = start, 546 .path = Array(repo_route_max_len).from(path) orelse return null, 547 } }, 548 } }; 549 } 550 551 pub fn repoDiffRoute(identity: []const u8, ref_or_oid: ?RefOrOid, value: []const u8, start: usize, path: []const u8, base_oid: []const u8) ?RoutablePage { 552 if (base_oid.len == 0 or (ref_or_oid == null and value.len != 0)) return null; 553 return .{ .repo_diff = .{ 554 .name = Array(repo_identity_max_len).from(identity) orelse return null, 555 .ref_or_oid = ref_or_oid, 556 .value = Array(ref_route_max_len).from(value) orelse return null, 557 .base_oid = Array(ref_route_max_len).from(base_oid) orelse return null, 558 .start = start, 559 .path = Array(repo_route_max_len).from(path) orelse return null, 560 } }; 561 } 562 563 pub fn repoPatchRevDiffRoute(identity: []const u8, id: []const u8, start: usize, path: []const u8) ?RoutablePage { 564 _ = evt.parseEventId(id) catch return null; 565 return .{ .repo_diff = .{ 566 .name = Array(repo_identity_max_len).from(identity) orelse return null, 567 .patchrev_id = Array(evt.event_id_size * 2).from(id) orelse return null, 568 .start = start, 569 .path = Array(repo_route_max_len).from(path) orelse return null, 570 } }; 571 } 572 573 // build a `.repo_commits` route showing the ref/oid's commit message in 574 // full, which is the only content that page renders. 575 pub fn repoCommitMessageRoute(identity: []const u8, ref_or_oid: RefOrOid, value: []const u8, base_oid: []const u8) ?RoutablePage { 576 if (value.len == 0) return null; 577 return .{ .repo_commits = .{ 578 .name = Array(repo_identity_max_len).from(identity) orelse return null, 579 .ref_or_oid = ref_or_oid, 580 .value = Array(ref_route_max_len).from(value) orelse return null, 581 .base_oid = Array(ref_route_max_len).from(base_oid) orelse return null, 582 .content = .message, 583 } }; 584 } 585 586 // build a `.repo_refs` route windowing `kind`'s column from the 587 // url-encoded ref name `from` ("" = the first window). 588 pub fn repoRefsRoute(identity: []const u8, kind: RefKind, from: []const u8) ?RoutablePage { 589 return .{ .repo_refs = .{ 590 .name = Array(repo_route_max_len).from(identity) orelse return null, 591 .kind = kind, 592 .from = Array(ref_route_max_len).from(from) orelse return null, 593 } }; 594 } 595 596 // build the events list or a window rooted at one event. 597 pub fn repoEventsRoute(identity: []const u8, view: EventsView, kind: ?evt.EventKind, selected: []const u8) ?RoutablePage { 598 if ((kind == null) != (selected.len == 0)) return null; 599 return .{ .repo_events = .{ 600 .name = Array(repo_identity_max_len).from(identity) orelse return null, 601 .view = view, 602 .kind = kind, 603 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 604 } }; 605 } 606 607 // build a `.repo_issues` route for "owner/name" (identity) showing 608 // `status`'s list, filtered to the url-encoded `tag` ("" = unfiltered) and 609 // rooted at the issue with hex event id `selected` ("" = the first window). 610 pub fn repoIssuesRoute(identity: []const u8, status: evt.Issue.Status, tag: []const u8, selected: []const u8) ?RoutablePage { 611 return .{ .repo_issues = .{ 612 .name = Array(repo_route_max_len).from(identity) orelse return null, 613 .tag = Array(issue_tag_route_max_len).from(tag) orelse return null, 614 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 615 .view = switch (status) { 616 .open => .open, 617 .closed => .closed, 618 }, 619 } }; 620 } 621 622 pub fn repoPatchesRoute(identity: []const u8, status: evt.Patch.StatusKind, tag: []const u8, selected: []const u8) ?RoutablePage { 623 return .{ .repo_patches = .{ 624 .name = Array(repo_route_max_len).from(identity) orelse return null, 625 .tag = Array(patch_tag_route_max_len).from(tag) orelse return null, 626 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 627 .view = switch (status) { 628 .open => .open, 629 .closed => .closed, 630 .merged => .merged, 631 }, 632 } }; 633 } 634 635 pub fn repoPatchesDraftsRoute(identity: []const u8) ?RoutablePage { 636 return .{ .repo_patches = .{ 637 .name = Array(repo_route_max_len).from(identity) orelse return null, 638 .view = .drafts, 639 } }; 640 } 641 642 pub fn repoPatchPublishRoute(identity: []const u8, selected: []const u8) ?RoutablePage { 643 if (selected.len == 0) return null; 644 var route = repoPatchesRoute(identity, .open, "", selected) orelse return null; 645 switch (route) { 646 .repo_patches => |*patch| patch.view = .publish, 647 else => unreachable, 648 } 649 return route; 650 } 651 652 pub fn repoPatchMergeRoute(identity: []const u8, selected: []const u8) ?RoutablePage { 653 if (selected.len == 0) return null; 654 var route = repoPatchesRoute(identity, .open, "", selected) orelse return null; 655 switch (route) { 656 .repo_patches => |*patch| patch.view = .merge, 657 else => unreachable, 658 } 659 return route; 660 } 661 662 pub fn repoDiscussionsRoute(identity: []const u8, tag: []const u8, selected: []const u8) ?RoutablePage { 663 return .{ .repo_discussions = .{ 664 .name = Array(repo_route_max_len).from(identity) orelse return null, 665 .tag = Array(discussion_tag_route_max_len).from(tag) orelse return null, 666 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 667 } }; 668 } 669 670 // build the default list route for a thread event kind. 671 pub fn repoThreadRoute(kind: evt.EventKind, identity: []const u8, tag: []const u8, selected: []const u8) ?RoutablePage { 672 return switch (kind) { 673 .issue => repoIssuesRoute(identity, .open, tag, selected), 674 .patch => repoPatchesRoute(identity, .open, tag, selected), 675 .discuss => repoDiscussionsRoute(identity, tag, selected), 676 else => null, 677 }; 678 } 679 680 // build a selected thread route at one of its comment windows. 681 pub fn repoThreadCommentsRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8, start: usize) ?RoutablePage { 682 var route = repoThreadRoute(kind, identity, "", selected) orelse return null; 683 switch (route) { 684 .repo_issues => |*thread| thread.comments_start = start, 685 .repo_patches => |*thread| thread.comments_start = start, 686 .repo_discussions => |*thread| thread.comments_start = start, 687 else => unreachable, 688 } 689 return route; 690 } 691 692 // build a comment permalink route at one of its immediate-reply windows. 693 pub fn repoThreadCommentRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8, start: usize) ?RoutablePage { 694 if (thread_id.len == 0 or comment_id.len == 0) return null; 695 var route = repoThreadCommentsRoute(kind, identity, thread_id, start) orelse return null; 696 const comment = Array(evt.event_id_size * 2).from(comment_id) orelse return null; 697 switch (route) { 698 .repo_issues => |*thread| thread.comment = comment, 699 .repo_patches => |*thread| thread.comment = comment, 700 .repo_discussions => |*thread| thread.comment = comment, 701 else => unreachable, 702 } 703 return route; 704 } 705 706 // build the form route for replying to a thread or one of its comments. 707 pub fn repoThreadCommentNewRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, parent_id: []const u8) ?RoutablePage { 708 if (thread_id.len == 0) return null; 709 var route = repoThreadCommentsRoute(kind, identity, thread_id, 0) orelse return null; 710 const parent_event_id = Array(evt.event_id_size * 2).from(parent_id) orelse return null; 711 switch (route) { 712 .repo_issues => |*thread| { 713 thread.comment = parent_event_id; 714 thread.view = .new_comment; 715 }, 716 .repo_patches => |*thread| { 717 thread.comment = parent_event_id; 718 thread.view = .new_comment; 719 }, 720 .repo_discussions => |*thread| { 721 thread.comment = parent_event_id; 722 thread.view = .new_comment; 723 }, 724 else => unreachable, 725 } 726 return route; 727 } 728 729 // build the form route for editing a comment. 730 pub fn repoThreadCommentEditRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?RoutablePage { 731 var route = repoThreadCommentRoute(kind, identity, thread_id, comment_id, 0) orelse return null; 732 switch (route) { 733 .repo_issues => |*thread| thread.view = .edit_comment, 734 .repo_patches => |*thread| thread.view = .edit_comment, 735 .repo_discussions => |*thread| thread.view = .edit_comment, 736 else => unreachable, 737 } 738 return route; 739 } 740 741 // build the confirmation route for removing a thread or comment. 742 pub fn repoThreadRemoveRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?RoutablePage { 743 var route = if (comment_id.len == 0) 744 repoThreadCommentsRoute(kind, identity, thread_id, 0) orelse return null 745 else 746 repoThreadCommentRoute(kind, identity, thread_id, comment_id, 0) orelse return null; 747 switch (route) { 748 .repo_issues => |*thread| thread.view = .remove, 749 .repo_patches => |*thread| thread.view = .remove, 750 .repo_discussions => |*thread| thread.view = .remove, 751 else => unreachable, 752 } 753 return route; 754 } 755 756 // build a thread tags route, keeping its url-encoded tag filter. 757 pub fn repoThreadTagsRoute(kind: evt.EventKind, identity: []const u8, tag: []const u8) ?RoutablePage { 758 var route = repoThreadRoute(kind, identity, tag, "") orelse return null; 759 switch (route) { 760 .repo_issues => |*thread| thread.view = .tags, 761 .repo_patches => |*thread| thread.view = .tags, 762 .repo_discussions => |*thread| thread.view = .tags, 763 else => unreachable, 764 } 765 return route; 766 } 767 768 // build a new-thread form route. 769 pub fn repoThreadNewRoute(kind: evt.EventKind, identity: []const u8) ?RoutablePage { 770 var route = repoThreadRoute(kind, identity, "", "") orelse return null; 771 switch (route) { 772 .repo_issues => |*thread| thread.view = .new, 773 .repo_patches => |*thread| thread.view = .new, 774 .repo_discussions => |*thread| thread.view = .new, 775 else => unreachable, 776 } 777 return route; 778 } 779 780 // build the edit form route for a thread. 781 pub fn repoThreadEditRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8) ?RoutablePage { 782 if (selected.len == 0) return null; 783 var route = repoThreadRoute(kind, identity, "", selected) orelse return null; 784 switch (route) { 785 .repo_issues => |*thread| thread.view = .edit, 786 .repo_patches => |*thread| thread.view = .edit, 787 .repo_discussions => |*thread| thread.view = .edit, 788 else => unreachable, 789 } 790 return route; 791 } 792 793 // build a route showing a thread's whole description. 794 pub fn repoThreadDescriptionRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8) ?RoutablePage { 795 if (selected.len == 0) return null; 796 var route = repoThreadRoute(kind, identity, "", selected) orelse return null; 797 switch (route) { 798 .repo_issues => |*thread| thread.view = .description, 799 .repo_patches => |*thread| thread.view = .description, 800 .repo_discussions => |*thread| thread.view = .description, 801 else => unreachable, 802 } 803 return route; 804 } 805 806 // build a `.repo_issues` route showing the conflicts list, rooted at the 807 // issue with hex event id `start` ("" = the first window). 808 pub fn repoIssuesConflictsRoute(identity: []const u8, start: []const u8) ?RoutablePage { 809 return .{ .repo_issues = .{ 810 .name = Array(repo_route_max_len).from(identity) orelse return null, 811 .selected = Array(evt.event_id_size * 2).from(start) orelse return null, 812 .view = .conflicts, 813 } }; 814 } 815 816 // build a `.repo_issues` route showing the conflict resolve form for the 817 // issue with hex event id `selected`; `theirs` is the comma-separated list 818 // of fields prefilled from their side. 819 pub fn repoIssuesResolveRoute(identity: []const u8, selected: []const u8, theirs: []const u8) ?RoutablePage { 820 if (selected.len == 0) return null; 821 return .{ .repo_issues = .{ 822 .name = Array(repo_route_max_len).from(identity) orelse return null, 823 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 824 .theirs = Array(theirs_route_max_len).from(theirs) orelse return null, 825 .view = .resolve, 826 } }; 827 } 828 829 pub fn repoPatchesConflictsRoute(identity: []const u8, start: []const u8) ?RoutablePage { 830 return .{ .repo_patches = .{ 831 .name = Array(repo_route_max_len).from(identity) orelse return null, 832 .selected = Array(evt.event_id_size * 2).from(start) orelse return null, 833 .view = .conflicts, 834 } }; 835 } 836 837 pub fn repoPatchesResolveRoute(identity: []const u8, selected: []const u8, theirs: []const u8) ?RoutablePage { 838 if (selected.len == 0) return null; 839 return .{ .repo_patches = .{ 840 .name = Array(repo_route_max_len).from(identity) orelse return null, 841 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 842 .theirs = Array(theirs_route_max_len).from(theirs) orelse return null, 843 .view = .resolve, 844 } }; 845 } 846 847 // an inline, owned array of data. keeping it in the route (rather than a 848 // borrowed slice) makes RoutablePage a plain value: it can be copied, stored 849 // in history, and serialized without any arena tracking. 850 pub fn Array(comptime max_len: usize) type { 851 return struct { 852 bytes: [max_len]u8 = undefined, 853 len: u16 = 0, 854 855 pub fn from(s: []const u8) ?Array(max_len) { 856 if (s.len > max_len) return null; 857 var name = Array(max_len){ .len = @intCast(s.len) }; 858 @memcpy(name.bytes[0..s.len], s); 859 return name; 860 } 861 862 pub fn slice(self: *const Array(max_len)) []const u8 { 863 return self.bytes[0..self.len]; 864 } 865 866 // round-trip as a json string instead of an array of numbers. 867 pub fn jsonStringify(self: *const Array(max_len), jw: anytype) !void { 868 try jw.write(self.slice()); 869 } 870 871 pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !Array(max_len) { 872 const s = try std.json.innerParse([]const u8, allocator, source, options); 873 // ValueTooLong is std.json's error for an over-long field. 874 return from(s) orelse error.ValueTooLong; 875 } 876 }; 877 } 878 879 pub fn toUrl(self: RoutablePage, arena: *std.heap.ArenaAllocator) ![]const u8 { 880 return switch (self) { 881 .home_users => |start| if (start == 0) @as([]const u8, "/users") else try std.fmt.allocPrint(arena.allocator(), "/users/" ++ start_seg ++ "{d}", .{start}), 882 .home_repos => |start| if (start == 0) @as([]const u8, "/repos") else try std.fmt.allocPrint(arena.allocator(), "/repos/" ++ start_seg ++ "{d}", .{start}), 883 .home_settings => "/settings", 884 .home_auth => "/auth", 885 .user_repos => |u| if (u.start == 0) 886 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/repos", .{u.name.slice()}) 887 else 888 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/repos/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 889 .user_forks => |u| if (u.start == 0) 890 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/forks", .{u.name.slice()}) 891 else 892 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/forks/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 893 .user_settings => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/settings", .{name.slice()}), 894 .user_auth => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/auth", .{name.slice()}), 895 .repo_files => |f| blk: { 896 const prefix = try repoUrlPrefix(arena, f.name.slice()); 897 if (f.ref_kind == null and f.patchrev_id.len == 0 and f.line == 0) break :blk if (prefix.len == 0) "/" else prefix; 898 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 899 try out.writer.print("{s}/" ++ files_seg, .{prefix}); 900 if (f.ref_kind) |kind| if (f.ref_value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), f.ref_value.slice() }); 901 if (f.patchrev_id.len != 0) try out.writer.print("/patchrev:{s}", .{f.patchrev_id.slice()}); 902 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 903 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 904 break :blk out.written(); 905 }, 906 .repo_commits => |c| blk: { 907 const prefix = try repoUrlPrefix(arena, c.name.slice()); 908 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 909 try out.writer.print("{s}/" ++ commits_seg, .{prefix}); 910 if (c.ref_or_oid) |kind| if (c.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), c.value.slice() }); 911 if (c.base_oid.len != 0) try out.writer.print("/" ++ base_seg ++ "{s}", .{c.base_oid.slice()}); 912 switch (c.content) { 913 .diff => |d| { 914 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 915 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 916 }, 917 .message => try out.writer.print("/" ++ message_seg, .{}), 918 } 919 break :blk out.written(); 920 }, 921 .repo_diff => |d| blk: { 922 const prefix = try repoUrlPrefix(arena, d.name.slice()); 923 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 924 try out.writer.print("{s}/diff", .{prefix}); 925 if (d.ref_or_oid) |kind| if (d.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), d.value.slice() }); 926 if (d.patchrev_id.len != 0) try out.writer.print("/patchrev:{s}", .{d.patchrev_id.slice()}); 927 if (d.base_oid.len != 0) try out.writer.print("/" ++ base_seg ++ "{s}", .{d.base_oid.slice()}); 928 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 929 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 930 break :blk out.written(); 931 }, 932 .repo_refs => |r| blk: { 933 const prefix = try repoUrlPrefix(arena, r.name.slice()); 934 break :blk if (r.from.len == 0) 935 try std.fmt.allocPrint(arena.allocator(), "{s}/refs", .{prefix}) 936 else 937 try std.fmt.allocPrint(arena.allocator(), "{s}/refs/{s}:{s}", .{ prefix, @tagName(r.kind), r.from.slice() }); 938 }, 939 .repo_issues => |i| blk: { 940 const prefix = try repoUrlPrefix(arena, i.name.slice()); 941 if (i.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/edit", .{ prefix, i.selected.slice() }); 942 if (i.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/description", .{ prefix, i.selected.slice() }); 943 if (i.view == .resolve) break :blk if (i.theirs.len == 0) 944 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/resolve", .{ prefix, i.selected.slice() }) 945 else 946 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, i.selected.slice(), i.theirs.slice() }); 947 if (i.view == .new_comment) break :blk if (i.comment.len == 0) 948 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/new", .{ prefix, i.selected.slice() }) 949 else 950 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, i.selected.slice(), i.comment.slice() }); 951 if (i.view == .edit_comment) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, i.selected.slice(), i.comment.slice() }); 952 if (i.view == .remove) break :blk if (i.comment.len == 0) 953 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/remove", .{ prefix, i.selected.slice() }) 954 else 955 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, i.selected.slice(), i.comment.slice() }); 956 if (i.view == .conflicts) break :blk if (i.selected.len == 0) 957 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/conflicts", .{prefix}) 958 else 959 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/" ++ start_seg ++ "{s}/conflicts", .{ prefix, i.selected.slice() }); 960 if (i.comment.len != 0) break :blk if (i.comments_start == 0) 961 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, i.selected.slice(), i.comment.slice() }) 962 else 963 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, i.selected.slice(), i.comment.slice(), i.comments_start }); 964 if (i.selected.len != 0) break :blk if (i.comments_start == 0) 965 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}", .{ prefix, i.selected.slice() }) 966 else 967 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, i.selected.slice(), i.comments_start }); 968 break :blk if (i.tag.len == 0) 969 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}", .{ prefix, @tagName(i.view) }) 970 else 971 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(i.view), i.tag.slice() }); 972 }, 973 .repo_patches => |p| blk: { 974 const prefix = try repoUrlPrefix(arena, p.name.slice()); 975 if (p.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/edit", .{ prefix, p.selected.slice() }); 976 if (p.view == .publish) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/publish", .{ prefix, p.selected.slice() }); 977 if (p.view == .merge) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/merge", .{ prefix, p.selected.slice() }); 978 if (p.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/description", .{ prefix, p.selected.slice() }); 979 if (p.view == .resolve) break :blk if (p.theirs.len == 0) 980 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/resolve", .{ prefix, p.selected.slice() }) 981 else 982 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, p.selected.slice(), p.theirs.slice() }); 983 if (p.view == .new_comment) break :blk if (p.comment.len == 0) 984 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/new", .{ prefix, p.selected.slice() }) 985 else 986 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, p.selected.slice(), p.comment.slice() }); 987 if (p.view == .edit_comment) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, p.selected.slice(), p.comment.slice() }); 988 if (p.view == .remove) break :blk if (p.comment.len == 0) 989 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/remove", .{ prefix, p.selected.slice() }) 990 else 991 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, p.selected.slice(), p.comment.slice() }); 992 if (p.view == .conflicts) break :blk if (p.selected.len == 0) 993 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/conflicts", .{prefix}) 994 else 995 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/" ++ start_seg ++ "{s}/conflicts", .{ prefix, p.selected.slice() }); 996 if (p.comment.len != 0) break :blk if (p.comments_start == 0) 997 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, p.selected.slice(), p.comment.slice() }) 998 else 999 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, p.selected.slice(), p.comment.slice(), p.comments_start }); 1000 if (p.selected.len != 0) break :blk if (p.comments_start == 0) 1001 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}", .{ prefix, p.selected.slice() }) 1002 else 1003 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, p.selected.slice(), p.comments_start }); 1004 break :blk if (p.tag.len == 0) 1005 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/{s}", .{ prefix, @tagName(p.view) }) 1006 else 1007 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(p.view), p.tag.slice() }); 1008 }, 1009 .repo_discussions => |t| blk: { 1010 const prefix = try repoUrlPrefix(arena, t.name.slice()); 1011 if (t.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/edit", .{ prefix, t.selected.slice() }); 1012 if (t.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/description", .{ prefix, t.selected.slice() }); 1013 if (t.view == .new_comment) break :blk if (t.comment.len == 0) 1014 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/new", .{ prefix, t.selected.slice() }) 1015 else 1016 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, t.selected.slice(), t.comment.slice() }); 1017 if (t.view == .edit_comment) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, t.selected.slice(), t.comment.slice() }); 1018 if (t.view == .remove) break :blk if (t.comment.len == 0) 1019 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/remove", .{ prefix, t.selected.slice() }) 1020 else 1021 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, t.selected.slice(), t.comment.slice() }); 1022 if (t.comment.len != 0) break :blk if (t.comments_start == 0) 1023 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, t.selected.slice(), t.comment.slice() }) 1024 else 1025 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, t.selected.slice(), t.comment.slice(), t.comments_start }); 1026 if (t.selected.len != 0) break :blk if (t.comments_start == 0) 1027 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}", .{ prefix, t.selected.slice() }) 1028 else 1029 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, t.selected.slice(), t.comments_start }); 1030 break :blk if (t.tag.len == 0) 1031 try std.fmt.allocPrint(arena.allocator(), "{s}/discussions/{s}", .{ prefix, @tagName(t.view) }) 1032 else 1033 try std.fmt.allocPrint(arena.allocator(), "{s}/discussions/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(t.view), t.tag.slice() }); 1034 }, 1035 .repo_events => |e| blk: { 1036 const prefix = try repoUrlPrefix(arena, e.name.slice()); 1037 break :blk if (e.kind) |kind| 1038 try std.fmt.allocPrint(arena.allocator(), "{s}/event:{s}/kind:{s}", .{ prefix, e.selected.slice(), @tagName(kind) }) 1039 else 1040 try std.fmt.allocPrint(arena.allocator(), "{s}/events/{s}", .{ prefix, @tagName(e.view) }); 1041 }, 1042 .repo_settings => |name| try std.fmt.allocPrint(arena.allocator(), "{s}/settings", .{try repoUrlPrefix(arena, name.slice())}), 1043 .repo_auth => |name| try std.fmt.allocPrint(arena.allocator(), "{s}/auth", .{try repoUrlPrefix(arena, name.slice())}), 1044 .fork_patch => |f| try std.fmt.allocPrint(arena.allocator(), fork_segment ++ "{s}/" ++ patch_seg ++ "{s}", .{ f.name.slice(), f.id.slice() }), 1045 .fork_diff => |d| blk: { 1046 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1047 try out.writer.print(fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/diff", .{ d.fork.name.slice(), d.fork.id.slice() }); 1048 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 1049 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 1050 break :blk out.written(); 1051 }, 1052 .fork_files => |f| blk: { 1053 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1054 try out.writer.print(fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/" ++ files_seg, .{ f.fork.name.slice(), f.fork.id.slice() }); 1055 if (f.oid.len != 0) try out.writer.print("/object:{s}", .{f.oid.slice()}); 1056 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 1057 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 1058 break :blk out.written(); 1059 }, 1060 .fork_commits => |c| blk: { 1061 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1062 try out.writer.print(fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/" ++ commits_seg, .{ c.fork.name.slice(), c.fork.id.slice() }); 1063 if (c.oid.len != 0) try out.writer.print("/object:{s}", .{c.oid.slice()}); 1064 switch (c.content) { 1065 .diff => |d| { 1066 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 1067 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 1068 }, 1069 .message => try out.writer.print("/" ++ message_seg, .{}), 1070 } 1071 break :blk out.written(); 1072 }, 1073 .fork_settings => |f| try std.fmt.allocPrint(arena.allocator(), fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/settings", .{ f.name.slice(), f.id.slice() }), 1074 .fork_auth => |f| try std.fmt.allocPrint(arena.allocator(), fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/auth", .{ f.name.slice(), f.id.slice() }), 1075 }; 1076 } 1077 1078 pub fn fromUrl(path: []const u8) ?RoutablePage { 1079 if (std.mem.eql(u8, path, "/")) return default; 1080 if (path.len < 2 or path[0] != '/') return null; 1081 var segments = std.mem.splitScalar(u8, path[1..], '/'); 1082 const first = segments.next() orelse return null; 1083 if (std.mem.eql(u8, first, "users")) return .{ .home_users = listStart(&segments) orelse return null }; 1084 if (std.mem.eql(u8, first, "repos")) return .{ .home_repos = listStart(&segments) orelse return null }; 1085 if (std.mem.eql(u8, first, "settings")) return if (segments.next() == null) .home_settings else null; 1086 if (std.mem.eql(u8, first, "auth")) return if (segments.next() == null) .home_auth else null; 1087 // "user/<name>[/repos[/start:<n>]|/settings|/auth]" 1088 if (std.mem.eql(u8, first, "user")) { 1089 const name = segments.next() orelse return null; 1090 if (name.len == 0) return null; 1091 const parsed = Array(evt.User.name_max_len).from(name) orelse return null; // name too long 1092 const sub = segments.next() orelse return .{ .user_repos = .{ .name = parsed } }; 1093 if (std.mem.eql(u8, sub, "repos")) return .{ .user_repos = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 1094 if (std.mem.eql(u8, sub, "forks")) return .{ .user_forks = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 1095 if (std.mem.eql(u8, sub, "settings")) return if (segments.next() == null) .{ .user_settings = parsed } else null; 1096 if (std.mem.eql(u8, sub, "auth")) return if (segments.next() == null) .{ .user_auth = parsed } else null; 1097 return null; // unknown sub-path 1098 } 1099 // "fork/<username>/<reponame>/patch:<id>[/files|/commits]"; the 1100 // patch branch is implicit, while object ids and view-specific params 1101 // use the same tails as the repo files and commits routes. 1102 if (std.mem.eql(u8, first, "fork")) { 1103 const rest = segments.rest(); 1104 const owner = segments.next() orelse return null; 1105 const repo_name = segments.next() orelse return null; 1106 if (owner.len == 0 or repo_name.len == 0) return null; 1107 const identity = rest[0 .. owner.len + 1 + repo_name.len]; 1108 const patch = segments.next() orelse return null; 1109 if (!std.mem.startsWith(u8, patch, patch_seg)) return null; 1110 const id = patch[patch_seg.len..]; 1111 if (id.len != evt.event_id_size * 2) return null; 1112 const tab = segments.next() orelse return forkPatchRoute(identity, id); 1113 var params = Params{}; 1114 if (std.mem.eql(u8, tab, "diff")) { 1115 params.scanPairs(&segments) catch return null; 1116 if (!params.only(&.{.start})) return null; 1117 return forkDiffRoute(identity, id, params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1118 } 1119 if (std.mem.eql(u8, tab, files_seg)) { 1120 params.scanPairs(&segments) catch return null; 1121 if (!params.only(&.{ .line, .object })) return null; 1122 const line = params.line() orelse return null; 1123 const ref = params.ref() catch return null; 1124 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1125 const file_path = pathValue(segments.rest()) orelse return null; 1126 return forkFilesRoute(identity, id, oid, file_path, line); 1127 } 1128 if (std.mem.eql(u8, tab, commits_seg)) { 1129 params.scanPairs(&segments) catch return null; 1130 if (!params.only(&.{ .start, .object })) return null; 1131 const start = params.start() orelse return null; 1132 const ref = params.ref() catch return null; 1133 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1134 if (std.mem.eql(u8, segments.rest(), message_seg)) 1135 return if (start == 0 and oid.len != 0) forkCommitMessageRoute(identity, id, oid) else null; 1136 const file_path = pathValue(segments.rest()) orelse return null; 1137 return forkCommitsRoute(identity, id, oid, start, file_path); 1138 } 1139 if (std.mem.eql(u8, tab, "settings")) return if (segments.next() == null) forkSettingsRoute(identity, id) else null; 1140 if (std.mem.eql(u8, tab, "auth")) return if (segments.next() == null) forkAuthRoute(identity, id) else null; 1141 return null; 1142 } 1143 // "repo/<username>/<reponame>[/<tab tail>]"; the bare pair is the 1144 // files root 1145 if (std.mem.eql(u8, first, "repo")) { 1146 const rest = segments.rest(); // "username/reponame[/...]" 1147 const owner = segments.next() orelse return null; 1148 const repo_name = segments.next() orelse return null; 1149 // reject an empty username or reponame 1150 if (owner.len == 0 or repo_name.len == 0) return null; 1151 const pair = rest[0 .. owner.len + 1 + repo_name.len]; // "username/reponame" 1152 if (segments.peek() != null) { 1153 return repoSubRoute(pair, segments.rest()); 1154 } 1155 return repoFilesRoute(pair, null, "", "", 0); 1156 } 1157 return null; 1158 } 1159 1160 // parse a local-mode url, which elides the "/repo/<owner>/<name>" prefix: 1161 // "/" is the files root and the sub-paths follow directly. 1162 pub fn fromUrlLocal(path: []const u8) ?RoutablePage { 1163 if (std.mem.eql(u8, path, "/")) return repoFilesRoute("", null, "", "", 0); 1164 if (path.len < 2 or path[0] != '/') return null; 1165 return repoSubRoute("", path[1..]); 1166 } 1167 1168 // the url prefix for a repo route's identity: "/repo/<identity>", or "" 1169 // when the identity is elided. 1170 fn repoUrlPrefix(arena: *std.heap.ArenaAllocator, identity: []const u8) ![]const u8 { 1171 if (identity.len == 0) return ""; 1172 return std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}", .{identity}); 1173 } 1174 1175 // true when the page builds at its content height on the web, letting the 1176 // browser scroll the whole page, rather than being pinned to the viewport 1177 pub fn fullHeight(self: RoutablePage) bool { 1178 return switch (self) { 1179 .repo_issues => |i| i.view == .resolve, 1180 .repo_patches => |p| p.view == .resolve, 1181 else => false, 1182 }; 1183 } 1184 1185 // the "owner/name" a repo route carries, null for any other route. the 1186 // result borrows the route, so it must outlive the slice. 1187 pub fn repoIdentity(self: *const RoutablePage) ?[]const u8 { 1188 return switch (self.*) { 1189 .repo_files => |*f| f.name.slice(), 1190 .repo_commits => |*c| c.name.slice(), 1191 .repo_diff => |*d| d.name.slice(), 1192 .repo_refs => |*r| r.name.slice(), 1193 .repo_issues => |*i| i.name.slice(), 1194 .repo_patches => |*p| p.name.slice(), 1195 .repo_discussions => |*t| t.name.slice(), 1196 .repo_events => |*e| e.name.slice(), 1197 .repo_settings, .repo_auth => |*name| name.slice(), 1198 else => null, 1199 }; 1200 } 1201 1202 pub fn forkRoute(self: *const RoutablePage) ?*const ForkRoute { 1203 return switch (self.*) { 1204 .fork_patch => |*f| f, 1205 .fork_diff => |*d| &d.fork, 1206 .fork_files => |*f| &f.fork, 1207 .fork_commits => |*f| &f.fork, 1208 .fork_settings => |*f| f, 1209 .fork_auth => |*f| f, 1210 else => null, 1211 }; 1212 } 1213 1214 // the route this route's page lands on, with no tab or params: the users 1215 // list, a user's repos, or a repo's files root. 1216 pub fn pageRoot(self: RoutablePage) RoutablePage { 1217 return switch (self.parent()) { 1218 .home => .default, 1219 .user => switch (self) { 1220 .user_repos => |u| .{ .user_repos = .{ .name = u.name } }, 1221 .user_forks => |u| .{ .user_repos = .{ .name = u.name } }, 1222 .user_settings, .user_auth => |name| .{ .user_repos = .{ .name = name } }, 1223 else => self, 1224 }, 1225 // every repo route stores the same identity, so it always fits 1226 .repo => repoFilesRoute(self.repoIdentity() orelse return self, null, "", "", 0) orelse self, 1227 .fork => blk: { 1228 const f = self.forkRoute() orelse break :blk self; 1229 break :blk forkPatchRoute(f.name.slice(), f.id.slice()) orelse self; 1230 }, 1231 }; 1232 } 1233 1234 pub fn parent(self: RoutablePage) PageKind { 1235 return switch (self) { 1236 .home_users, .home_repos, .home_settings, .home_auth => .home, 1237 .user_repos, .user_forks, .user_settings, .user_auth => .user, 1238 .repo_files, .repo_commits, .repo_diff, .repo_refs, .repo_issues, .repo_patches, .repo_discussions, .repo_events, .repo_settings, .repo_auth => .repo, 1239 .fork_patch, .fork_diff, .fork_files, .fork_commits, .fork_settings, .fork_auth => .fork, 1240 }; 1241 } 1242 1243 // the dir a trailing "path:<dir>" names: the raw remainder ("" when 1244 // absent, null when malformed) 1245 fn pathValue(rest: []const u8) ?[]const u8 { 1246 if (rest.len == 0) return ""; 1247 if (!std.mem.startsWith(u8, rest, path_seg)) return null; 1248 const dir = rest[path_seg.len..]; 1249 return if (dir.len == 0) null else dir; 1250 } 1251 1252 // a list route's tail: at most a "start:<n>" param, null on anything else 1253 fn listStart(segments: *std.mem.SplitIterator(u8, .scalar)) ?usize { 1254 var params = Params{}; 1255 const word = params.scanTail(segments) catch return null; 1256 if (word != null or !params.only(&.{.start})) return null; 1257 return params.start(); 1258 } 1259 1260 // parse the sub-path after a repo route's identity ("" for a local 1261 // route): a tab keyword followed by "key:value" params in any order, plus 1262 // the files tab's trailing "path:<dir>". 1263 fn repoSubRoute(pair: []const u8, sub: []const u8) ?RoutablePage { 1264 var segments = std.mem.splitScalar(u8, sub, '/'); 1265 const tab = segments.next() orelse return null; 1266 var params = Params{}; 1267 if (std.mem.eql(u8, tab, "settings")) return if (segments.next() == null) .{ .repo_settings = Array(repo_route_max_len).from(pair) orelse return null } else null; 1268 if (std.mem.eql(u8, tab, "auth")) return if (segments.next() == null) .{ .repo_auth = Array(repo_route_max_len).from(pair) orelse return null } else null; 1269 if (std.mem.eql(u8, tab, "refs")) { 1270 const word = params.scanTail(&segments) catch return null; 1271 if (word != null or !params.only(&.{ .branch, .tag })) return null; 1272 const ref = (params.ref() catch return null) orelse return repoRefsRoute(pair, .branch, ""); 1273 const kind = std.meta.stringToEnum(RefKind, @tagName(ref.kind)) orelse return null; 1274 return repoRefsRoute(pair, kind, ref.value); 1275 } 1276 if (std.mem.startsWith(u8, tab, issue_seg)) { 1277 const issue_id = tab[issue_seg.len..]; 1278 if (issue_id.len == 0) return null; 1279 if (segments.peek()) |tail| { 1280 if (std.mem.startsWith(u8, tail, comment_seg)) { 1281 _ = segments.next(); 1282 const comment_id = tail[comment_seg.len..]; 1283 if (comment_id.len == 0) return null; 1284 const word = params.scanTail(&segments) catch return null; 1285 if (word) |last| { 1286 if (!params.only(&.{})) return null; 1287 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.issue, pair, issue_id, comment_id); 1288 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.issue, pair, issue_id, comment_id); 1289 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.issue, pair, issue_id, comment_id); 1290 return null; 1291 } 1292 if (!params.only(&.{.start})) return null; 1293 return repoThreadCommentRoute(.issue, pair, issue_id, comment_id, params.start() orelse return null); 1294 } 1295 } 1296 const word = params.scanTail(&segments) catch return null; 1297 if (word) |tail| { 1298 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.issue, pair, issue_id, "") else null; 1299 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.issue, pair, issue_id) else null; 1300 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.issue, pair, issue_id, "") else null; 1301 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.issue, pair, issue_id) else null; 1302 if (std.mem.eql(u8, tail, "resolve")) { 1303 if (!params.only(&.{.theirs})) return null; 1304 return repoIssuesResolveRoute(pair, issue_id, params.values.get(.theirs) orelse ""); 1305 } 1306 return null; 1307 } 1308 if (!params.only(&.{.start})) return null; 1309 return repoThreadCommentsRoute(.issue, pair, issue_id, params.start() orelse return null); 1310 } 1311 if (std.mem.startsWith(u8, tab, patch_seg)) { 1312 const patch_id = tab[patch_seg.len..]; 1313 if (patch_id.len == 0) return null; 1314 if (segments.peek()) |tail| { 1315 if (std.mem.startsWith(u8, tail, comment_seg)) { 1316 _ = segments.next(); 1317 const comment_id = tail[comment_seg.len..]; 1318 if (comment_id.len == 0) return null; 1319 const word = params.scanTail(&segments) catch return null; 1320 if (word) |last| { 1321 if (!params.only(&.{})) return null; 1322 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.patch, pair, patch_id, comment_id); 1323 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.patch, pair, patch_id, comment_id); 1324 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.patch, pair, patch_id, comment_id); 1325 return null; 1326 } 1327 if (!params.only(&.{.start})) return null; 1328 return repoThreadCommentRoute(.patch, pair, patch_id, comment_id, params.start() orelse return null); 1329 } 1330 } 1331 const word = params.scanTail(&segments) catch return null; 1332 if (word) |tail| { 1333 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.patch, pair, patch_id, "") else null; 1334 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.patch, pair, patch_id) else null; 1335 if (std.mem.eql(u8, tail, "publish")) return if (params.only(&.{})) repoPatchPublishRoute(pair, patch_id) else null; 1336 if (std.mem.eql(u8, tail, "merge")) return if (params.only(&.{})) repoPatchMergeRoute(pair, patch_id) else null; 1337 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.patch, pair, patch_id, "") else null; 1338 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.patch, pair, patch_id) else null; 1339 if (std.mem.eql(u8, tail, "resolve")) { 1340 if (!params.only(&.{.theirs})) return null; 1341 return repoPatchesResolveRoute(pair, patch_id, params.values.get(.theirs) orelse ""); 1342 } 1343 return null; 1344 } 1345 if (!params.only(&.{.start})) return null; 1346 return repoThreadCommentsRoute(.patch, pair, patch_id, params.start() orelse return null); 1347 } 1348 if (std.mem.startsWith(u8, tab, discuss_seg)) { 1349 const discussion_id = tab[discuss_seg.len..]; 1350 if (discussion_id.len == 0) return null; 1351 if (segments.peek()) |tail| { 1352 if (std.mem.startsWith(u8, tail, comment_seg)) { 1353 _ = segments.next(); 1354 const comment_id = tail[comment_seg.len..]; 1355 if (comment_id.len == 0) return null; 1356 const word = params.scanTail(&segments) catch return null; 1357 if (word) |last| { 1358 if (!params.only(&.{})) return null; 1359 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.discuss, pair, discussion_id, comment_id); 1360 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.discuss, pair, discussion_id, comment_id); 1361 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.discuss, pair, discussion_id, comment_id); 1362 return null; 1363 } 1364 if (!params.only(&.{.start})) return null; 1365 return repoThreadCommentRoute(.discuss, pair, discussion_id, comment_id, params.start() orelse return null); 1366 } 1367 } 1368 const word = params.scanTail(&segments) catch return null; 1369 if (word) |tail| { 1370 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.discuss, pair, discussion_id, "") else null; 1371 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.discuss, pair, discussion_id) else null; 1372 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.discuss, pair, discussion_id, "") else null; 1373 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.discuss, pair, discussion_id) else null; 1374 return null; 1375 } 1376 if (!params.only(&.{.start})) return null; 1377 return repoThreadCommentsRoute(.discuss, pair, discussion_id, params.start() orelse return null); 1378 } 1379 if (std.mem.eql(u8, tab, "issues")) { 1380 // the word is a list view; a filter with neither is never emitted. 1381 const word = params.scanTail(&segments) catch return null; 1382 const tag_value = params.values.get(.tag) orelse ""; 1383 const w = word orelse return if (params.only(&.{})) repoIssuesRoute(pair, .open, "", "") else null; 1384 // the conflicts list windows by an issue id, not a number, so it 1385 // reads the start param raw 1386 if (std.mem.eql(u8, w, "conflicts")) { 1387 if (!params.only(&.{.start})) return null; 1388 return repoIssuesConflictsRoute(pair, params.values.get(.start) orelse ""); 1389 } 1390 if (!params.only(&.{.tag})) return null; 1391 if (std.meta.stringToEnum(evt.Issue.Status, w)) |status| return repoIssuesRoute(pair, status, tag_value, ""); 1392 if (std.mem.eql(u8, w, "tags")) return repoThreadTagsRoute(.issue, pair, tag_value); 1393 if (std.mem.eql(u8, w, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.issue, pair) else null; 1394 return null; 1395 } 1396 if (std.mem.eql(u8, tab, "patches")) { 1397 const word = params.scanTail(&segments) catch return null; 1398 const tag_value = params.values.get(.tag) orelse ""; 1399 const view = word orelse return if (params.only(&.{})) repoPatchesRoute(pair, .open, "", "") else null; 1400 if (std.mem.eql(u8, view, "conflicts")) { 1401 if (!params.only(&.{.start})) return null; 1402 return repoPatchesConflictsRoute(pair, params.values.get(.start) orelse ""); 1403 } 1404 if (!params.only(&.{.tag})) return null; 1405 if (std.meta.stringToEnum(evt.Patch.StatusKind, view)) |status| return repoPatchesRoute(pair, status, tag_value, ""); 1406 if (std.mem.eql(u8, view, "tags")) return repoThreadTagsRoute(.patch, pair, tag_value); 1407 if (std.mem.eql(u8, view, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.patch, pair) else null; 1408 if (std.mem.eql(u8, view, "drafts")) return if (tag_value.len == 0) repoPatchesDraftsRoute(pair) else null; 1409 return null; 1410 } 1411 if (std.mem.eql(u8, tab, "discussions")) { 1412 const word = params.scanTail(&segments) catch return null; 1413 const tag_value = params.values.get(.tag) orelse ""; 1414 const view = word orelse return if (params.only(&.{})) repoDiscussionsRoute(pair, "", "") else null; 1415 if (!params.only(&.{.tag})) return null; 1416 if (std.mem.eql(u8, view, "recent")) return repoDiscussionsRoute(pair, tag_value, ""); 1417 if (std.mem.eql(u8, view, "tags")) return repoThreadTagsRoute(.discuss, pair, tag_value); 1418 if (std.mem.eql(u8, view, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.discuss, pair) else null; 1419 return null; 1420 } 1421 if (std.mem.eql(u8, tab, "events")) { 1422 const view = if (segments.next()) |word| 1423 std.meta.stringToEnum(EventsView, word) orelse return null 1424 else 1425 .active; 1426 return if (segments.next() == null) repoEventsRoute(pair, view, null, "") else null; 1427 } 1428 if (std.mem.startsWith(u8, tab, "event:")) { 1429 const event_id = tab["event:".len..]; 1430 const kind_segment = segments.next() orelse return null; 1431 if (!std.mem.startsWith(u8, kind_segment, "kind:")) return null; 1432 const kind = std.meta.stringToEnum(evt.EventKind, kind_segment["kind:".len..]) orelse return null; 1433 if (event_id.len == 0 or segments.next() != null) return null; 1434 return repoEventsRoute(pair, .active, kind, event_id); 1435 } 1436 if (std.mem.eql(u8, tab, files_seg)) { 1437 params.scanPairs(&segments) catch return null; 1438 if (params.values.get(.patchrev)) |id| { 1439 if (!params.only(&.{ .line, .patchrev })) return null; 1440 return repoPatchRevFilesRoute(pair, id, pathValue(segments.rest()) orelse return null, params.line() orelse return null); 1441 } 1442 if (!params.only(&.{ .line, .branch, .tag, .object })) return null; 1443 const line = params.line() orelse return null; 1444 const dir = pathValue(segments.rest()) orelse return null; 1445 const ref = (params.ref() catch return null) orelse 1446 return if (dir.len == 0) repoFilesRoute(pair, null, "", "", line) else null; 1447 return repoFilesRoute(pair, ref.kind, ref.value, dir, line); 1448 } 1449 if (std.mem.eql(u8, tab, commits_seg)) { 1450 params.scanPairs(&segments) catch return null; 1451 if (!params.only(&.{ .start, .branch, .tag, .object, .base })) return null; 1452 const base_oid = params.values.get(.base) orelse ""; 1453 const start = params.start() orelse return null; 1454 const ref = (params.ref() catch return null) orelse { 1455 const file = pathValue(segments.rest()) orelse return null; 1456 return if (file.len == 0) repoCommitsRoute(pair, null, "", start, "", base_oid) else null; 1457 }; 1458 // the message page has no diff window or file filter 1459 if (std.mem.eql(u8, segments.rest(), message_seg)) 1460 return if (start == 0) repoCommitMessageRoute(pair, ref.kind, ref.value, base_oid) else null; 1461 const file = pathValue(segments.rest()) orelse return null; 1462 return repoCommitsRoute(pair, ref.kind, ref.value, start, file, base_oid); 1463 } 1464 if (std.mem.eql(u8, tab, "diff")) { 1465 params.scanPairs(&segments) catch return null; 1466 if (params.values.get(.patchrev)) |id| { 1467 if (!params.only(&.{ .start, .patchrev })) return null; 1468 return repoPatchRevDiffRoute(pair, id, params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1469 } 1470 if (!params.only(&.{ .start, .branch, .tag, .object, .base })) return null; 1471 const base_oid = params.values.get(.base) orelse return null; 1472 const start = params.start() orelse return null; 1473 const ref = params.ref() catch return null; 1474 const file = pathValue(segments.rest()) orelse return null; 1475 return repoDiffRoute(pair, if (ref) |r| r.kind else null, if (ref) |r| r.value else "", start, file, base_oid); 1476 } 1477 return null; // unknown sub-path 1478 } 1479 1480 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 1481 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1482 return switch (a) { 1483 .home_users => |a_start| a_start == b.home_users, 1484 .home_repos => |a_start| a_start == b.home_repos, 1485 .user_repos => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_repos.name.slice()) and a_u.start == b.user_repos.start, 1486 .user_forks => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_forks.name.slice()) and a_u.start == b.user_forks.start, 1487 .user_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.user_settings.slice()), 1488 .user_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.user_auth.slice()), 1489 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 1490 a_f.ref_kind == b.repo_files.ref_kind and 1491 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 1492 std.mem.eql(u8, a_f.patchrev_id.slice(), b.repo_files.patchrev_id.slice()) and 1493 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 1494 a_f.line == b.repo_files.line, 1495 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 1496 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 1497 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 1498 std.mem.eql(u8, a_c.base_oid.slice(), b.repo_commits.base_oid.slice()) and 1499 switch (a_c.content) { 1500 .diff => |a_d| switch (b.repo_commits.content) { 1501 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1502 .message => false, 1503 }, 1504 .message => std.meta.activeTag(b.repo_commits.content) == .message, 1505 }, 1506 .repo_diff => |a_d| std.mem.eql(u8, a_d.name.slice(), b.repo_diff.name.slice()) and 1507 a_d.ref_or_oid == b.repo_diff.ref_or_oid and 1508 std.mem.eql(u8, a_d.value.slice(), b.repo_diff.value.slice()) and 1509 std.mem.eql(u8, a_d.base_oid.slice(), b.repo_diff.base_oid.slice()) and 1510 std.mem.eql(u8, a_d.patchrev_id.slice(), b.repo_diff.patchrev_id.slice()) and 1511 a_d.start == b.repo_diff.start and std.mem.eql(u8, a_d.path.slice(), b.repo_diff.path.slice()), 1512 .repo_refs => |a_r| std.mem.eql(u8, a_r.name.slice(), b.repo_refs.name.slice()) and a_r.kind == b.repo_refs.kind and std.mem.eql(u8, a_r.from.slice(), b.repo_refs.from.slice()), 1513 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 1514 std.mem.eql(u8, a_i.tag.slice(), b.repo_issues.tag.slice()) and 1515 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 1516 std.mem.eql(u8, a_i.theirs.slice(), b.repo_issues.theirs.slice()) and 1517 a_i.view == b.repo_issues.view and 1518 a_i.comments_start == b.repo_issues.comments_start and 1519 std.mem.eql(u8, a_i.comment.slice(), b.repo_issues.comment.slice()), 1520 .repo_discussions => |a_t| std.mem.eql(u8, a_t.name.slice(), b.repo_discussions.name.slice()) and 1521 std.mem.eql(u8, a_t.tag.slice(), b.repo_discussions.tag.slice()) and 1522 std.mem.eql(u8, a_t.selected.slice(), b.repo_discussions.selected.slice()) and 1523 a_t.view == b.repo_discussions.view and 1524 a_t.comments_start == b.repo_discussions.comments_start and 1525 std.mem.eql(u8, a_t.comment.slice(), b.repo_discussions.comment.slice()), 1526 .repo_patches => |a_p| std.mem.eql(u8, a_p.name.slice(), b.repo_patches.name.slice()) and 1527 std.mem.eql(u8, a_p.tag.slice(), b.repo_patches.tag.slice()) and 1528 std.mem.eql(u8, a_p.selected.slice(), b.repo_patches.selected.slice()) and 1529 std.mem.eql(u8, a_p.theirs.slice(), b.repo_patches.theirs.slice()) and 1530 a_p.view == b.repo_patches.view and 1531 a_p.comments_start == b.repo_patches.comments_start and 1532 std.mem.eql(u8, a_p.comment.slice(), b.repo_patches.comment.slice()), 1533 .repo_events => |a_e| std.mem.eql(u8, a_e.name.slice(), b.repo_events.name.slice()) and 1534 a_e.view == b.repo_events.view and 1535 a_e.kind == b.repo_events.kind and 1536 std.mem.eql(u8, a_e.selected.slice(), b.repo_events.selected.slice()), 1537 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 1538 .repo_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_auth.slice()), 1539 .fork_patch => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_patch.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_patch.id.slice()), 1540 .fork_diff => |a_d| std.mem.eql(u8, a_d.fork.name.slice(), b.fork_diff.fork.name.slice()) and 1541 std.mem.eql(u8, a_d.fork.id.slice(), b.fork_diff.fork.id.slice()) and 1542 a_d.start == b.fork_diff.start and std.mem.eql(u8, a_d.path.slice(), b.fork_diff.path.slice()), 1543 .fork_files => |a_f| std.mem.eql(u8, a_f.fork.name.slice(), b.fork_files.fork.name.slice()) and 1544 std.mem.eql(u8, a_f.fork.id.slice(), b.fork_files.fork.id.slice()) and 1545 std.mem.eql(u8, a_f.oid.slice(), b.fork_files.oid.slice()) and 1546 std.mem.eql(u8, a_f.path.slice(), b.fork_files.path.slice()) and 1547 a_f.line == b.fork_files.line, 1548 .fork_commits => |a_c| std.mem.eql(u8, a_c.fork.name.slice(), b.fork_commits.fork.name.slice()) and 1549 std.mem.eql(u8, a_c.fork.id.slice(), b.fork_commits.fork.id.slice()) and 1550 std.mem.eql(u8, a_c.oid.slice(), b.fork_commits.oid.slice()) and 1551 switch (a_c.content) { 1552 .diff => |a_d| switch (b.fork_commits.content) { 1553 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1554 .message => false, 1555 }, 1556 .message => std.meta.activeTag(b.fork_commits.content) == .message, 1557 }, 1558 .fork_settings => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_settings.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_settings.id.slice()), 1559 .fork_auth => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_auth.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_auth.id.slice()), 1560 else => true, 1561 }; 1562 } 1563 1564 // true when following a link from `b` to `a` (both repo routes) should reload 1565 // rather than stay in page 1566 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 1567 if (a.parent() != .repo or b.parent() != .repo) return false; 1568 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 1569 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 1570 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 1571 return !a.eql(b); 1572 } 1573 1574 pub fn forkPageChanged(a: RoutablePage, b: RoutablePage) bool { 1575 return a.parent() == .fork and b.parent() == .fork and !a.eql(b); 1576 } 1577 1578 // true when `a` and `b` are the same user paginated to a different repos 1579 // window. switching between a user's tabs is in-page, so 1580 // only a changed `start` on the repos list navigates. 1581 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 1582 return switch (a) { 1583 .user_repos => |aa| switch (b) { 1584 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1585 else => false, 1586 }, 1587 .user_forks => |aa| switch (b) { 1588 .user_forks => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1589 else => false, 1590 }, 1591 else => false, 1592 }; 1593 } 1594 1595 // true when `a` and `b` are the same home list tab paginated to a different 1596 // window. switching between the users/repos tabs is in-page (the home page 1597 // holds both lists), so only a changed `after` on the same tab navigates. 1598 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 1599 return switch (a) { 1600 .home_users => |aa| switch (b) { 1601 .home_users => |bb| aa != bb, 1602 else => false, 1603 }, 1604 .home_repos => |aa| switch (b) { 1605 .home_repos => |bb| aa != bb, 1606 else => false, 1607 }, 1608 else => false, 1609 }; 1610 } 1611 }; 1612 1613 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 1614 fn isUnreserved(c: u8) bool { 1615 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 1616 } 1617 1618 // percent-encode a ref name for use as a single url path segment. 1619 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 1620 var out: std.Io.Writer.Allocating = .init(aa); 1621 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 1622 return out.written(); 1623 } 1624 1625 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 1626 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1627 return struct { 1628 pub const hex_len = hash.hexLen(repo_opts.hash); 1629 1630 // the concrete ref/oid (a null request resolves to the default branch) 1631 ref_or_oid: RoutablePage.RefOrOid, 1632 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 1633 // the route layer — which splits on '/' — stores them encoded). duped 1634 // into `aa`. 1635 value: []const u8, 1636 // the commit oid it points at 1637 oid: [hex_len]u8, 1638 1639 const Self = @This(); 1640 1641 // resolve a requested ref/oid (null = the repo's default branch) to a 1642 // concrete ref/oid plus the commit oid it points at. `requested_value` is 1643 // the url-encoded form (as it appears in the route). null when it doesn't 1644 // resolve (an unknown branch/tag, or a malformed/unknown oid). 1645 pub fn init( 1646 repo: *rp.Repo(repo_kind, repo_opts), 1647 io: std.Io, 1648 aa: std.mem.Allocator, 1649 requested_ref_or_oid: ?RoutablePage.RefOrOid, 1650 requested_value: []const u8, 1651 ) !?Self { 1652 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 1653 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 1654 var value: []const u8 = if (requested_ref_or_oid == null) 1655 requested_value 1656 else 1657 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 1658 // no ref named: fall back to HEAD's branch (or its oid when detached). 1659 if (requested_ref_or_oid == null) { 1660 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1661 if (repo.head(io, &head_buf)) |head| switch (head) { 1662 .ref => |r| { 1663 ref_or_oid = .branch; 1664 value = r.name; 1665 }, 1666 .oid => |o| { 1667 ref_or_oid = .object; 1668 value = o; 1669 }, 1670 } else |_| {} 1671 } 1672 1673 var oid: [hex_len]u8 = undefined; 1674 switch (ref_or_oid) { 1675 .object => { 1676 if (value.len != hex_len) return null; 1677 @memcpy(&oid, value); 1678 }, 1679 .branch, .tag => { 1680 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 1681 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 1682 }, 1683 } 1684 // store url-encoded so the route layer can hold the value verbatim. 1685 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 1686 } 1687 }; 1688 } 1689 1690 // where a repo page reads its on-disk repo from 1691 pub const RepoSource = struct { 1692 path: []const u8, 1693 repo_kind: rp.RepoKind, 1694 // the user's global git config, so local mode picks up their identity and 1695 // their signing and ssh settings. null on the server paths, which must not 1696 // read whoever runs the server. 1697 global_config_path: ?[]const u8 = null, 1698 1699 pub fn localInitOpts(self: RepoSource) rp.InitOpts { 1700 return .{ .path = self.path, .global_config_path = self.global_config_path }; 1701 } 1702 1703 pub fn hasBranch(self: RepoSource, io: std.Io, allocator: std.mem.Allocator, branch: []const u8) !bool { 1704 if (!xit.ref.validateName(branch)) return false; 1705 return switch (self.repo_kind) { 1706 inline else => |repo_kind| blk: { 1707 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 1708 defer any_repo.deinit(io, allocator); 1709 break :blk switch (any_repo) { 1710 inline else => |*repo| (try repo.readRef(io, .{ .kind = .head, .name = branch })) != null, 1711 }; 1712 }, 1713 }; 1714 } 1715 }; 1716 1717 // events created in local mode have no account behind them, so the commit 1718 // author comes from the repo's config, which the global config feeds into. 1719 pub const local_author_fallback = evt.CommitAuthor{ .name = "haxy", .email = "user@haxy" }; 1720 1721 pub fn localAuthor( 1722 src: RepoSource, 1723 io: std.Io, 1724 allocator: std.mem.Allocator, 1725 arena: *std.heap.ArenaAllocator, 1726 ) !evt.CommitAuthor { 1727 switch (src.repo_kind) { 1728 inline else => |repo_kind| { 1729 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()) catch return local_author_fallback; 1730 defer any_repo.deinit(io, allocator); 1731 1732 switch (any_repo) { 1733 inline else => |*repo| { 1734 var config = try repo.listConfig(io, allocator); 1735 defer config.deinit(); 1736 1737 const user = config.sections.get("user") orelse return local_author_fallback; 1738 const name = user.get("name") orelse return local_author_fallback; 1739 const email = user.get("email") orelse return local_author_fallback; 1740 return .{ 1741 .name = try arena.allocator().dupe(u8, name), 1742 .email = try arena.allocator().dupe(u8, email), 1743 }; 1744 }, 1745 } 1746 }, 1747 } 1748 } 1749 1750 // per-connection mutable state. each SSH session / web session / local TUI 1751 // run gets its own. 1752 pub const Session = struct { 1753 data: Data = .{}, 1754 // session-lifetime allocations: login/user_id, persisted prefs, queued 1755 // actions. lives as long as the connection. 1756 arena: *std.heap.ArenaAllocator, 1757 // current page's allocations: page data and the page-scoped strings widgets 1758 // build (form actions, links). owned by Nav, swapped on each navigation, so 1759 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 1760 // where a render is one-shot, this points at the same arena as `arena`. 1761 page_arena: *std.heap.ArenaAllocator, 1762 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 1763 // filesystem io and the path to <server>/repos, for opening on-disk repos 1764 // during page construction. both null on wasm, which has no filesystem and 1765 // rebuilds pages from the serialized snapshot rather than from disk. 1766 io: ?std.Io = null, 1767 admin_repo: ?*rp.Repo(.xit, evt.admin_repo_opts) = null, 1768 repos_dir: ?[]const u8 = null, 1769 // the single on-disk repo this session views, when running in local mode 1770 // (haxy invoked with no arguments inside a repo). null on the server paths, 1771 // which resolve repos from the admin db instead. 1772 local: ?RepoSource = null, 1773 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 1774 // focus id -> the live TextInput, refreshed each frame by the views that own 1775 // inputs. web/wasm form handling looks widgets up here by focus id. 1776 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput) = .empty, 1777 back: enum { unavailable, available, requested } = .unavailable, 1778 refresh_requested: bool = false, // set by input (ctrl+r) 1779 // a requested forward navigation. set this (via navigate) to move to a new 1780 // page; Nav.sync builds it and then copies it into current_page, clearing 1781 // this back to null. setting current_page directly only updates the url and 1782 // does not navigate. 1783 next_page: ?RoutablePage = null, 1784 is_terminal: bool = false, // true on remote SSH and local TUI 1785 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 1786 // url. null on the web itself (no footer there). 1787 web_port: ?u16 = null, 1788 // a host operation requested by a widget 1789 host_request: ?HostRequest = null, 1790 quit_requested: bool = false, 1791 1792 pub const HostRequest = union(enum) { 1793 show_copyable_text: []const u8, 1794 sync_events, 1795 }; 1796 1797 pub const FormFeedback = union(enum) { 1798 pub const LoginFailure = enum { unknown_user, wrong_password }; 1799 pub const ThreadFailure = enum { required_title }; 1800 pub const PatchFailure = enum { 1801 required_title, 1802 invalid_target_branch, 1803 invalid_source_branch, 1804 same_branch, 1805 unrelated_branches, 1806 1807 pub fn fromError(err: anyerror) ?PatchFailure { 1808 return switch (err) { 1809 error.InvalidTargetBranch => .invalid_target_branch, 1810 error.InvalidSourceBranch => .invalid_source_branch, 1811 error.SameBranch => .same_branch, 1812 error.UnrelatedBranches => .unrelated_branches, 1813 else => null, 1814 }; 1815 } 1816 }; 1817 1818 login: struct { 1819 failure: LoginFailure, 1820 username: []const u8, 1821 }, 1822 issue: struct { 1823 failure: ThreadFailure, 1824 fields: ?struct { 1825 title: []const u8, 1826 tags: []const u8, 1827 description: []const u8, 1828 } = null, 1829 }, 1830 patch: struct { 1831 failure: PatchFailure, 1832 fields: ?struct { 1833 title: []const u8, 1834 tags: []const u8, 1835 description: []const u8, 1836 target_branch: []const u8, 1837 source_branch: []const u8 = "", 1838 source_kind: Repo.Patches.Source.Kind = .fork, 1839 } = null, 1840 }, 1841 discussion: struct { 1842 failure: ThreadFailure, 1843 fields: ?struct { 1844 title: []const u8, 1845 tags: []const u8, 1846 description: []const u8, 1847 } = null, 1848 }, 1849 }; 1850 1851 const Self = @This(); 1852 1853 // serializable data sent down to web client 1854 pub const Data = struct { 1855 user_id: ?[]const u8 = null, 1856 // the logged-in user's name, for the views that show it 1857 user_name: ?[]const u8 = null, 1858 // a failed form retained long enough to render it for correction 1859 form_feedback: ?FormFeedback = null, 1860 // a transient local event-sync error 1861 sync_failure: ?[]const u8 = null, 1862 current_page: RoutablePage = .default, 1863 // whether to render the ANSI art backdrop 1864 enable_ansi: bool = true, 1865 // the selected ANSI art. the server serializes one image for WASM so 1866 // the browser does not need the whole build-generated collection. 1867 ansi_art: []const u8 = "", 1868 // the bound git-service ports. absent in local mode, where remote urls 1869 // are not shown. 1870 git_http_port: ?u16 = null, 1871 git_ssh_port: ?u16 = null, 1872 git_ssh_prefix: []const u8 = "", 1873 // whether this session views a local repo or the server. unlike `local` 1874 // (the host-side filesystem source) this travels in the snapshot, so 1875 // the wasm side also parses elided link urls and hides the multi-user 1876 // chrome. 1877 host_kind: evt.HostKind = .server, 1878 }; 1879 1880 // a user-initiated state change. widgets enqueue these on the session during 1881 // input instead of mutating state or touching the DB themselves; the host 1882 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 1883 // side effects out of the widget tree and gives every render path one place to 1884 // turn a UI action into a state change + event. 1885 pub const Action = union(enum) { 1886 toggle_ansi, 1887 }; 1888 1889 pub fn init( 1890 arena: *std.heap.ArenaAllocator, 1891 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1892 data: Data, 1893 ) !Self { 1894 var session = Self{ 1895 .data = data, 1896 .arena = arena, 1897 // until a host swaps in a page-scoped arena (Nav does this), page 1898 // allocations land in the session arena. on the web path that's the 1899 // intended behavior, since the whole arena is per-request. 1900 .page_arena = arena, 1901 .admin_repo = repo, 1902 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 1903 }; 1904 try session.loadUser(); 1905 return session; 1906 } 1907 1908 // load the logged-in user's name and persisted preferences from the db 1909 pub fn loadUser(self: *Self) !void { 1910 const user_id = self.data.user_id orelse return; 1911 const moment = self.haxy_moment orelse return; 1912 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.arena, user_id)) |user| { 1913 self.data.user_name = user.event.name; 1914 self.data.enable_ansi = user.event.enable_ansi; 1915 } 1916 } 1917 1918 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 1919 const bytes = self.data.user_id orelse return null; 1920 if (bytes.len != evt.event_id_size) return null; 1921 var id: [evt.event_id_size]u8 = undefined; 1922 @memcpy(&id, bytes); 1923 return id; 1924 } 1925 1926 pub fn formFeedback(self: *const Self, comptime tag: std.meta.Tag(FormFeedback)) ?@FieldType(FormFeedback, @tagName(tag)) { 1927 const feedback = self.data.form_feedback orelse return null; 1928 if (std.meta.activeTag(feedback) != tag) return null; 1929 return @field(feedback, @tagName(tag)); 1930 } 1931 1932 pub fn clearFormFeedback(self: *Self) void { 1933 self.data.form_feedback = null; 1934 } 1935 1936 // the commit author for an event this session creates, or null when it may 1937 // not create one 1938 pub fn eventAuthor(self: *Self) !?evt.CommitAuthor { 1939 if (self.data.host_kind == .local) { 1940 const src = self.local orelse return local_author_fallback; 1941 const io = self.io orelse return local_author_fallback; 1942 return try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 1943 } 1944 const user_id = self.data.user_id orelse return null; 1945 const moment = self.haxy_moment orelse return null; 1946 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.page_arena, user_id)) orelse return null; 1947 return .{ .name = user.event.name, .email = user.event.email }; 1948 } 1949 1950 // queue an action for the host to drain this frame. 1951 pub fn push(self: *Self, action: Action) !void { 1952 try self.pending.append(self.arena.allocator(), action); 1953 } 1954 1955 // apply a single action's in-memory effect only (no persistence). 1956 fn apply(self: *Self, action: Action) void { 1957 switch (action) { 1958 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 1959 } 1960 } 1961 1962 // drain the session's queued actions by applying them to the session. 1963 // used on the wasm path, which has no repo to persist to. 1964 pub fn applyPending(self: *Self) void { 1965 for (self.pending.items) |action| self.apply(action); 1966 self.pending.clearRetainingCapacity(); 1967 } 1968 1969 // drain the session's queued actions by applying them to the session and 1970 // writing them to the db 1971 pub fn applyAndWritePending( 1972 self: *Self, 1973 io: std.Io, 1974 allocator: std.mem.Allocator, 1975 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1976 ) !void { 1977 defer self.pending.clearRetainingCapacity(); 1978 for (self.pending.items) |action| { 1979 self.apply(action); 1980 switch (action) { 1981 .toggle_ansi => if (self.data.user_id) |user_id| { 1982 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 1983 }, 1984 } 1985 } 1986 } 1987 1988 // reload the moment from the admin repo and re-read user preferences 1989 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 1990 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 1991 self.haxy_moment = moment; 1992 1993 const user_id = self.data.user_id orelse return; 1994 var arena = std.heap.ArenaAllocator.init(allocator); 1995 defer arena.deinit(); 1996 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, user_id)) |user| { 1997 self.data.enable_ansi = user.event.enable_ansi; 1998 } 1999 } 2000 2001 // request a forward navigation to `route`; the host consumes next_page 2002 // (Nav.sync on the terminal, the wasm tick on the web). 2003 pub fn navigate(self: *Session, route: RoutablePage) !void { 2004 self.clearFormFeedback(); 2005 self.next_page = route; 2006 } 2007 }; 2008 2009 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2010 var nav = try Nav.init(allocator, session); 2011 defer nav.deinit(allocator); 2012 2013 var terminal = try term.Terminal.init(io, allocator); 2014 var terminal_live = true; 2015 defer if (terminal_live) terminal.deinit(io); 2016 2017 // set term as active so it will be properly cooked 2018 // when a panic/segfault happens 2019 term.setActive(&terminal); 2020 defer term.setActive(null); 2021 2022 while (!terminal.shouldQuit()) { 2023 const grid_changed = try terminal.render(&nav.root); 2024 2025 // process any inputs. 2026 // 2027 // if the grid didn't change, then first do a blocking 2028 // read, so the thread will sleep until further input. 2029 // after that, all remaining reads are non-blocking so 2030 // we can process the rest of the queued inputs. 2031 // 2032 // if the grid *did* change, then only do non-blocking 2033 // reads. we do not want to sleep the thread because 2034 // there may be an animation that requires more looping. 2035 var blocking = !grid_changed; 2036 while (try terminal.readKey(io, blocking)) |key| { 2037 blocking = false; 2038 try inputKey(allocator, &nav.root, key, session); 2039 } 2040 2041 if (session.host_request) |request| { 2042 session.host_request = null; 2043 switch (request) { 2044 .show_copyable_text => |copyable_text| { 2045 term.setActive(null); 2046 terminal.deinit(io); 2047 terminal_live = false; 2048 showCopyableText(io, copyable_text) catch |show_err| { 2049 terminal = try term.Terminal.init(io, allocator); 2050 terminal_live = true; 2051 term.setActive(&terminal); 2052 return show_err; 2053 }; 2054 terminal = try term.Terminal.init(io, allocator); 2055 terminal_live = true; 2056 term.setActive(&terminal); 2057 }, 2058 .sync_events => { 2059 try nav.root.build(allocator, .{ 2060 .min_size = .{ .width = null, .height = null }, 2061 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2062 }, nav.root.getFocus()); 2063 _ = try terminal.render(&nav.root); 2064 try Repo.Events.performSync(allocator, session); 2065 }, 2066 } 2067 } 2068 2069 // persist queued actions when there's a repo; local mode has none, 2070 // so just apply in-memory 2071 if (repo_maybe) |repo| { 2072 try session.applyAndWritePending(io, allocator, repo); 2073 // reload so the next navigation builds its page from the current moment 2074 try session.reloadMoment(allocator, repo); 2075 } else { 2076 session.applyPending(); 2077 } 2078 2079 // reconcile navigation: forward to a new page, or back on escape. 2080 try nav.sync(allocator, session); 2081 2082 // the quit button (on the quit tab) asks the host to tear down. 2083 if (session.quit_requested) terminal.requestQuit(); 2084 2085 try nav.root.build(allocator, .{ 2086 .min_size = .{ .width = null, .height = null }, 2087 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2088 }, nav.root.getFocus()); 2089 } 2090 } 2091 2092 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2093 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2094 null 2095 else 2096 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2097 defer if (tty) |file| file.close(io); 2098 2099 const input = if (tty) |file| file else std.Io.File.stdin(); 2100 const output = if (tty) |file| file else std.Io.File.stdout(); 2101 2102 var write_buf: [1024]u8 = undefined; 2103 var writer = output.writer(io, &write_buf); 2104 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}); 2105 try writer.interface.flush(); 2106 2107 var read_buf: [1]u8 = undefined; 2108 var reader = input.reader(io, &read_buf); 2109 while (true) { 2110 const byte = try reader.interface.takeByte(); 2111 if (byte == '\r' or byte == '\n') return; 2112 } 2113 } 2114 2115 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2116 const root_focus = root.getFocus(); 2117 const focused_before = root_focus.grandchild_id; 2118 var activate_in_page_link = false; 2119 switch (key) { 2120 // request a navigation pop; the host's Nav.sync goes back a page, or 2121 // quits when there's no history left. 2122 .escape => { 2123 session.back = .requested; 2124 return; 2125 }, 2126 .ctrl => |letter| switch (letter) { 2127 'r' => { 2128 session.refresh_requested = true; 2129 return; 2130 }, 2131 else => try root.input(allocator, key, root_focus), 2132 }, 2133 .enter => { 2134 if (root_focus.grandchild_id) |gid| { 2135 // follow a cross-page link 2136 if (crossPageLink(root_focus, gid, session.data)) |route| { 2137 return session.navigate(route); 2138 } 2139 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2140 } 2141 activate_in_page_link = true; 2142 try root.input(allocator, key, root_focus); 2143 }, 2144 .mouse => |mouse| { 2145 if (mouse.action == .press and mouse.action.press == .left) { 2146 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2147 const focus_id = hit.id; 2148 if (hit.kind == .background) { 2149 root_focus.setFocus(focus_id); 2150 return; 2151 } 2152 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2153 if (session.back == .available) session.back = .requested; 2154 return; 2155 } 2156 // follow a cross-page link 2157 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2158 return session.navigate(route); 2159 } 2160 if (rawLink(root_focus, focus_id)) |url| { 2161 if (!mouse.ctrl) try requestRawLink(session, url); 2162 return; 2163 } 2164 root_focus.setFocus(focus_id); 2165 activate_in_page_link = true; 2166 } 2167 // forward the press into the widget tree so buttons (and any 2168 // future click-aware widgets) can react. widgets that don't 2169 // care about presses ignore it. 2170 try root.input(allocator, key, root_focus); 2171 } else { 2172 try root.input(allocator, key, root_focus); 2173 } 2174 }, 2175 else => try root.input(allocator, key, root_focus), 2176 } 2177 2178 const focused_after = root_focus.grandchild_id; 2179 if (!activate_in_page_link and focused_before == focused_after) return; 2180 const focus_id = focused_after orelse return; 2181 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2182 } 2183 2184 // a link to bytes the server serves directly rather than to a page route, so 2185 // it never resolves to a RoutablePage 2186 pub const raw_link_prefix = "ax:"; 2187 2188 // a route already loaded into the current page; focusing it changes the url 2189 // without rebuilding the page 2190 pub const in_page_link_prefix = "ai:"; 2191 2192 // an in-page tab's base route, or the exact current route when it is selected 2193 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2194 const target = if (selected) session.data.current_page else route; 2195 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2196 } 2197 2198 // a button the web renderer covers with a file picker that posts the chosen 2199 // file to the url after the prefix 2200 pub const file_input_prefix = "file:"; 2201 2202 // a submit button that overrides its enclosing form's action 2203 pub const submit_action_prefix = "submit:"; 2204 2205 // the url an `ax:` link points at, or null for anything else. a host that 2206 // suppresses the anchor's own navigation needs this to follow the link. 2207 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2208 const child = root_focus.children.get(focus_id) orelse return null; 2209 const custom = switch (child.focus.kind) { 2210 .custom => |c| c, 2211 else => return null, 2212 }; 2213 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2214 return custom[raw_link_prefix.len..]; 2215 } 2216 2217 fn requestRawLink(session: *Session, url: []const u8) !void { 2218 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2219 } 2220 2221 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2222 return if (session.web_port) |port| 2223 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2224 else 2225 path; 2226 } 2227 2228 // if the focus target at focus_id is an `a:` link that should navigate (a 2229 // different parent page, or a different files directory within the repo page), 2230 // return its route; otherwise null. lets a host turn a click / enter on such a 2231 // link into a navigation rather than a focus change. 2232 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2233 const current = data.current_page; 2234 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2235 // a link to a different parent page always navigates; within a page, a 2236 // files-directory / commits-page / list-window change navigates while tab 2237 // links stay in-page. 2238 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; 2239 return null; 2240 } 2241 2242 // the route carried by an `ai:` focus target, or null for any other target 2243 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2244 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2245 } 2246 2247 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2248 const child = root_focus.children.get(focus_id) orelse return null; 2249 const custom = switch (child.focus.kind) { 2250 .custom => |c| c, 2251 else => return null, 2252 }; 2253 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2254 const path = custom[prefix.len..]; 2255 // local sessions build (and parse) links with the repo identity elided 2256 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2257 } 2258 2259 // a display author, resolved against the admin db at read time 2260 pub const Author = union(enum) { 2261 unknown, 2262 email: []const u8, // an email no user matches 2263 user_name: []const u8, 2264 2265 // the commit author line's email, resolved like initFromEmail 2266 pub fn init( 2267 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2268 arena: *std.heap.ArenaAllocator, 2269 author_line: []const u8, 2270 ) !Author { 2271 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2272 } 2273 2274 // from a record's stored author email: it resolves to the user currently 2275 // holding it, else stays the email. the result is allocated in `arena`, 2276 // so the email may borrow transient memory. 2277 pub fn initFromEmail( 2278 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2279 arena: *std.heap.ArenaAllocator, 2280 author_email: ?[]const u8, 2281 ) !Author { 2282 const email = author_email orelse return .unknown; 2283 if (haxy_moment) |moment| { 2284 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2285 return .{ .user_name = user.event.name }; 2286 } 2287 } 2288 return .{ .email = try arena.allocator().dupe(u8, email) }; 2289 } 2290 }; 2291 2292 // the "a:" link to user `name`'s page. 2293 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2294 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2295 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2296 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2297 } 2298 2299 // a focusable " author " box showing `author`, linking to their user page 2300 // when it is a known user 2301 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2302 const text = switch (author) { 2303 .unknown => "", 2304 .email, .user_name => |t| t, 2305 }; 2306 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2307 errdefer tb.deinit(allocator); 2308 tb.getFocus().mode = .all; 2309 switch (author) { 2310 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2311 else => {}, 2312 } 2313 return tb; 2314 } 2315 2316 // native-TUI navigation 2317 pub const Nav = struct { 2318 root: Widget, 2319 // backs the current page (its Page data and the page-scoped strings its 2320 // widgets build). owned here, swapped on each navigation. session.page_arena 2321 // tracks whichever of these is current so widgets allocate into it. 2322 arena: *std.heap.ArenaAllocator, 2323 history: std.ArrayList(Entry), 2324 2325 // each retained page keeps its own arena, freed when the page leaves the 2326 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2327 // long-lived session from accumulating every page it ever visited. 2328 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2329 2330 // cap on retained back-history so the chain can't grow memory without bound 2331 const max_history: usize = 16; 2332 2333 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2334 session.back = .unavailable; 2335 const arena = try allocator.create(std.heap.ArenaAllocator); 2336 arena.* = std.heap.ArenaAllocator.init(allocator); 2337 errdefer { 2338 arena.deinit(); 2339 allocator.destroy(arena); 2340 } 2341 2342 session.page_arena = arena; 2343 const route = session.data.current_page; 2344 2345 const page = try arena.allocator().create(Page); 2346 page.* = try Page.init(arena, session, route); 2347 return .{ 2348 .root = try initRoot(allocator, page, session), 2349 .arena = arena, 2350 .history = .empty, 2351 }; 2352 } 2353 2354 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2355 self.root.deinit(allocator); 2356 freeArena(allocator, self.arena); 2357 for (self.history.items) |*entry| { 2358 entry.root.deinit(allocator); 2359 freeArena(allocator, entry.arena); 2360 } 2361 self.history.deinit(allocator); 2362 } 2363 2364 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2365 arena.deinit(); 2366 allocator.destroy(arena); 2367 } 2368 2369 // reconcile the displayed root with the session's navigation state. a 2370 // forward nav (current_page moved to a different parent page) pushes the 2371 // current root and builds the new page in a fresh arena; a back request 2372 // frees the current page and restores the previous one. when escape is 2373 // pressed with no history left we switch to the quit tab instead of quitting. 2374 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2375 // refresh: rebuild the current page in place from a current moment 2376 if (session.refresh_requested) { 2377 session.refresh_requested = false; 2378 if (session.haxy_moment != null or session.local != null) { 2379 const arena = try allocator.create(std.heap.ArenaAllocator); 2380 arena.* = std.heap.ArenaAllocator.init(allocator); 2381 errdefer freeArena(allocator, arena); 2382 2383 session.page_arena = arena; 2384 2385 const route = session.data.current_page; 2386 const page = try arena.allocator().create(Page); 2387 page.* = try Page.init(arena, session, route); 2388 const new_root = try initRoot(allocator, page, session); 2389 2390 self.root.deinit(allocator); 2391 freeArena(allocator, self.arena); 2392 self.root = new_root; 2393 self.arena = arena; 2394 } 2395 return; 2396 } 2397 2398 if (session.back == .requested) { 2399 if (self.history.pop()) |entry| { 2400 session.back = if (self.history.items.len == 0) .unavailable else .available; 2401 self.root.deinit(allocator); 2402 freeArena(allocator, self.arena); 2403 self.root = entry.root; 2404 self.arena = entry.arena; 2405 session.page_arena = entry.arena; 2406 session.data.current_page = entry.route; 2407 chooseAnsiArtForNavigation(session); 2408 return; 2409 } 2410 session.back = .unavailable; 2411 // nothing to go back to; switch to the quit confirmation 2412 const root_focus = self.root.getFocus(); 2413 var iter = root_focus.children.iterator(); 2414 while (iter.next()) |entry| { 2415 switch (entry.value_ptr.focus.kind) { 2416 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2417 root_focus.setFocus(entry.key_ptr.*); 2418 // a Stack only builds its selected child, so the quit 2419 // button isn't in the focus tree until a build runs with 2420 // the quit tab selected. build once, then send a 2421 // synthetic arrow_down to drop focus from the tab onto 2422 // the button so it's ready to confirm. 2423 try self.root.build(allocator, .{ 2424 .min_size = .{ .width = null, .height = 40 }, 2425 .max_size = .{ .width = 80, .height = null }, 2426 }, root_focus); 2427 try self.root.input(allocator, .arrow_down, root_focus); 2428 break; 2429 }, 2430 else => {}, 2431 } 2432 } 2433 return; 2434 } 2435 2436 // forward navigation: navigate() set next_page to the page to move to (a 2437 // cross-page link or tab change crossing pages) 2438 if (session.next_page) |route| { 2439 session.next_page = null; 2440 if (session.haxy_moment == null and session.local == null) return; 2441 const previous_route = session.data.current_page; 2442 // the page we navigated to becomes the current page 2443 session.data.current_page = route; 2444 session.back = .available; 2445 2446 const arena = try allocator.create(std.heap.ArenaAllocator); 2447 arena.* = std.heap.ArenaAllocator.init(allocator); 2448 errdefer freeArena(allocator, arena); 2449 2450 session.page_arena = arena; 2451 2452 const page = try arena.allocator().create(Page); 2453 page.* = try Page.init(arena, session, route); 2454 const new_root = try initRoot(allocator, page, session); 2455 2456 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2457 // drop the oldest entry (freeing its widget tree and arena) once over cap 2458 if (self.history.items.len > max_history) { 2459 var oldest = self.history.orderedRemove(0); 2460 oldest.root.deinit(allocator); 2461 freeArena(allocator, oldest.arena); 2462 } 2463 self.root = new_root; 2464 self.arena = arena; 2465 } 2466 } 2467 }; 2468 2469 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2470 const page_widget: Widget = switch (page.*) { 2471 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2472 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2473 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2474 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2475 }; 2476 2477 chooseAnsiArtForNavigation(session); 2478 2479 // on the TUI/SSH, the page sits above a one-row footer showing the url 2480 var root = if (session.is_terminal) blk: { 2481 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2482 errdefer box.deinit(allocator); 2483 const bg_id = bg_blk: { 2484 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2485 errdefer background.deinit(allocator); 2486 const id = background.getFocus().id; 2487 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2488 break :bg_blk id; 2489 }; 2490 { 2491 var footer = try widget.Footer.init(allocator, session); 2492 errdefer footer.deinit(allocator); 2493 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2494 } 2495 box.getFocus().child_id = bg_id; 2496 break :blk Widget{ .box = box }; 2497 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2498 errdefer root.deinit(allocator); 2499 2500 // input-owning views build their TextInputs in init, so reset their 2501 // registrations here 2502 session.text_inputs.clearRetainingCapacity(); 2503 2504 try root.build(allocator, .{ 2505 .min_size = .{ .width = null, .height = 40 }, 2506 .max_size = .{ .width = 100, .height = null }, 2507 }, root.getFocus()); 2508 2509 return root; 2510 } 2511 2512 // native and server-side sessions choose from the embedded collection. WASM 2513 // keeps the art serialized by the server instead. 2514 fn chooseAnsiArtForNavigation(session: *Session) void { 2515 if (comptime ansi_arts.len == 0) return; 2516 const io = session.io orelse return; 2517 var random: usize = undefined; 2518 io.random(std.mem.asBytes(&random)); 2519 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2520 } 2521