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