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