╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
517 else 518 null, 519 }; 520 } 521 522 pub fn commitsRoute(self: RepoLocation, ref_or_oid: ?RefOrOid, value: []const u8, base_oid: []const u8) ?RoutablePage { 523 return switch (self) { 524 .repo => |repo_identity| repoCommitsRoute(repo_identity, ref_or_oid, value, base_oid), 525 .fork => |f| if (ref_or_oid == null or (ref_or_oid == .branch and std.mem.eql(u8, value, "patch"))) 526 forkCommitsRoute(f.identity, f.id, "") 527 else if (ref_or_oid == .object) 528 forkCommitsRoute(f.identity, f.id, value) 529 else 530 null, 531 }; 532 } 533 534 // commit `oid`'s diff against `base_oid` (all zeros for a root commit) 535 pub fn commitDiffRoute(self: RepoLocation, oid: []const u8, base_oid: []const u8) ?RoutablePage { 536 return switch (self) { 537 .repo => |repo_identity| repoDiffRoute(repo_identity, .object, oid, 0, "", base_oid), 538 .fork => |f| forkDiffRoute(f.identity, f.id, oid, base_oid, 0, ""), 539 }; 540 } 541 542 // fork commits routes carry no base, so only a repo can list a merge's commits 543 pub fn commitsMergeRoute(self: RepoLocation, oid: []const u8) ?RoutablePage { 544 return switch (self) { 545 .repo => |repo_identity| repoCommitsMergeRoute(repo_identity, oid), 546 .fork => null, 547 }; 548 } 549 550 pub fn commitMessageRoute(self: RepoLocation, ref_or_oid: RefOrOid, value: []const u8, base_oid: []const u8) ?RoutablePage { 551 return switch (self) { 552 .repo => |repo_identity| repoCommitMessageRoute(repo_identity, ref_or_oid, value, base_oid), 553 .fork => |f| if (ref_or_oid == .object) forkCommitMessageRoute(f.identity, f.id, value) else null, 554 }; 555 } 556 }; 557 558 fn initForkRoute(identity: []const u8, id: []const u8) ?ForkRoute { 559 if (id.len != evt.event_id_size * 2) return null; 560 return .{ 561 .name = Array(repo_identity_max_len).from(identity) orelse return null, 562 .id = Array(evt.event_id_size * 2).from(id) orelse return null, 563 }; 564 } 565 566 pub fn forkPatchRoute(identity: []const u8, id: []const u8) ?RoutablePage { 567 return .{ .fork_patch = initForkRoute(identity, id) orelse return null }; 568 } 569 570 // the whole patch's diff when `oid` is "", else that commit's against `base_oid` 571 pub fn forkDiffRoute(identity: []const u8, id: []const u8, oid: []const u8, base_oid: []const u8, start: usize, path: []const u8) ?RoutablePage { 572 if ((oid.len == 0) != (base_oid.len == 0)) return null; 573 return .{ .fork_diff = .{ 574 .fork = initForkRoute(identity, id) orelse return null, 575 .oid = Array(ref_route_max_len).from(oid) orelse return null, 576 .base_oid = Array(ref_route_max_len).from(base_oid) orelse return null, 577 .start = start, 578 .path = Array(repo_route_max_len).from(path) orelse return null, 579 } }; 580 } 581 582 pub fn forkFilesRoute(identity: []const u8, id: []const u8, oid: []const u8, path: []const u8, line: usize) ?RoutablePage { 583 return .{ .fork_files = .{ 584 .fork = initForkRoute(identity, id) orelse return null, 585 .oid = Array(ref_route_max_len).from(oid) orelse return null, 586 .path = Array(repo_route_max_len).from(path) orelse return null, 587 .line = line, 588 } }; 589 } 590 591 pub fn forkCommitsRoute(identity: []const u8, id: []const u8, oid: []const u8) ?RoutablePage { 592 return .{ .fork_commits = .{ 593 .fork = initForkRoute(identity, id) orelse return null, 594 .oid = Array(ref_route_max_len).from(oid) orelse return null, 595 } }; 596 } 597 598 pub fn forkCommitMessageRoute(identity: []const u8, id: []const u8, oid: []const u8) ?RoutablePage { 599 if (oid.len == 0) return null; 600 return .{ .fork_commits = .{ 601 .fork = initForkRoute(identity, id) orelse return null, 602 .oid = Array(ref_route_max_len).from(oid) orelse return null, 603 .message = true, 604 } }; 605 } 606 607 pub fn forkRepoNewRoute(identity: []const u8, id: []const u8) ?RoutablePage { 608 return .{ .fork_repo_new = initForkRoute(identity, id) orelse return null }; 609 } 610 611 pub fn forkUserNewRoute(identity: []const u8, id: []const u8) ?RoutablePage { 612 return .{ .fork_user_new = initForkRoute(identity, id) orelse return null }; 613 } 614 615 pub fn forkUserRoute(identity: []const u8, id: []const u8) ?RoutablePage { 616 return .{ .fork_user = .{ .fork = initForkRoute(identity, id) orelse return null } }; 617 } 618 619 // the tab a user tab route names, the default for any other route 620 pub fn userTab(self: RoutablePage) UserTab { 621 return switch (self) { 622 .home_user => |tab| tab, 623 inline .user_user, .repo_user, .fork_user => |u| u.tab, 624 else => .{}, 625 }; 626 } 627 628 // a user tab route showing `tab` instead 629 pub fn withUserTab(self: RoutablePage, tab: UserTab) RoutablePage { 630 var route = self; 631 switch (route) { 632 .home_user => |*t| t.* = tab, 633 inline .user_user, .repo_user, .fork_user => |*u| u.tab = tab, 634 else => unreachable, 635 } 636 return route; 637 } 638 639 // build a `.repo_files` route (a null ref_kind = the bare default-branch 640 // root). null if the result doesn't fit the inline name. 641 pub fn repoFilesRoute(identity: []const u8, ref_kind: ?RefOrOid, ref_value: []const u8, dir: []const u8, line: usize) ?RoutablePage { 642 if (ref_kind == null and (ref_value.len != 0 or dir.len != 0)) return null; 643 if (ref_kind != null and ref_value.len == 0 and dir.len != 0) return null; 644 return .{ .repo_files = .{ 645 .name = Array(repo_identity_max_len).from(identity) orelse return null, 646 .ref_kind = ref_kind, 647 .ref_value = Array(ref_route_max_len).from(ref_value) orelse return null, 648 .path = Array(repo_route_max_len).from(dir) orelse return null, 649 .line = line, 650 } }; 651 } 652 653 // carry a search term on a files route, url-encoding it into the route. 654 // null when it doesn't fit. 655 pub fn withFind(self: RoutablePage, file_name: []const u8) ?RoutablePage { 656 const find = encodeParam(find_route_max_len, file_name) orelse return null; 657 var route = self; 658 switch (route) { 659 .repo_files => |*f| f.find = find, 660 .fork_files => |*f| f.find = find, 661 else => return null, 662 } 663 return route; 664 } 665 666 // carry a search query on a searchable route, url-encoding it 667 // into the route. 668 pub fn withSearch(self: RoutablePage, query: []const u8) ?RoutablePage { 669 const search = encodeParam(search_route_max_len, query) orelse return null; 670 return withEncodedSearch(self, search.slice()); 671 } 672 673 // carry an already-encoded search param, as a url hands it over 674 fn withEncodedSearch(self: ?RoutablePage, value: []const u8) ?RoutablePage { 675 var route = self orelse return null; 676 const search = Array(search_route_max_len).from(value) orelse return null; 677 switch (route) { 678 .home_users => |*u| u.search = search, 679 .user_repos => |*u| u.search = search, 680 .repo_commits => |*c| c.search = search, 681 .repo_refs => |*r| r.search = search, 682 .repo_issues => |*i| i.search = search, 683 .repo_patches => |*p| p.search = search, 684 .repo_discussions => |*d| d.search = search, 685 else => return null, 686 } 687 return route; 688 } 689 690 // root a commits route's list at `oid` rather than the ref's own commit. 691 pub fn withFrom(self: RoutablePage, oid: []const u8) ?RoutablePage { 692 var route = self; 693 switch (route) { 694 .repo_commits => |*c| c.from = Array(ref_route_max_len).from(oid) orelse return null, 695 else => return null, 696 } 697 return route; 698 } 699 700 fn encodeParam(comptime max_len: usize, value: []const u8) ?Array(max_len) { 701 var encoded: Array(max_len) = .{}; 702 var writer: std.Io.Writer = .fixed(&encoded.bytes); 703 std.Uri.Component.percentEncode(&writer, value, isUnreserved) catch return null; 704 encoded.len = @intCast(writer.buffered().len); 705 return encoded; 706 } 707 708 pub fn repoPatchRevFilesRoute(identity: []const u8, id: []const u8, path: []const u8, line: usize) ?RoutablePage { 709 _ = evt.parseEventId(id) catch return null; 710 return .{ .repo_files = .{ 711 .name = Array(repo_identity_max_len).from(identity) orelse return null, 712 .patchrev_id = Array(evt.event_id_size * 2).from(id) orelse return null, 713 .path = Array(repo_route_max_len).from(path) orelse return null, 714 .line = line, 715 } }; 716 } 717 718 // build a `.repo_commits` route (a null ref_or_oid = the default branch). 719 // always carries the `commits` marker so the bare route doesn't collide 720 // with the files root. 721 pub fn repoCommitsRoute(identity: []const u8, ref_or_oid: ?RefOrOid, value: []const u8, base_oid: []const u8) ?RoutablePage { 722 if (ref_or_oid == null and value.len != 0) return null; 723 return .{ .repo_commits = .{ 724 .name = Array(repo_identity_max_len).from(identity) orelse return null, 725 .ref_or_oid = ref_or_oid, 726 .value = Array(ref_route_max_len).from(value) orelse return null, 727 .base_oid = Array(ref_route_max_len).from(base_oid) orelse return null, 728 } }; 729 } 730 731 pub fn repoCommitsMergeRoute(identity: []const u8, oid: []const u8) ?RoutablePage { 732 return .{ .repo_commits = .{ 733 .name = Array(repo_identity_max_len).from(identity) orelse return null, 734 .merge = Array(ref_route_max_len).from(oid) orelse return null, 735 } }; 736 } 737 738 pub fn repoDiffRoute(identity: []const u8, ref_or_oid: ?RefOrOid, value: []const u8, start: usize, path: []const u8, base_oid: []const u8) ?RoutablePage { 739 if (base_oid.len == 0 or (ref_or_oid == null and value.len != 0)) return null; 740 return .{ .repo_diff = .{ 741 .name = Array(repo_identity_max_len).from(identity) orelse return null, 742 .ref_or_oid = ref_or_oid, 743 .value = Array(ref_route_max_len).from(value) orelse return null, 744 .base_oid = Array(ref_route_max_len).from(base_oid) orelse return null, 745 .start = start, 746 .path = Array(repo_route_max_len).from(path) orelse return null, 747 } }; 748 } 749 750 pub fn repoPatchRevDiffRoute(identity: []const u8, id: []const u8, start: usize, path: []const u8) ?RoutablePage { 751 _ = evt.parseEventId(id) catch return null; 752 return .{ .repo_diff = .{ 753 .name = Array(repo_identity_max_len).from(identity) orelse return null, 754 .patchrev_id = Array(evt.event_id_size * 2).from(id) orelse return null, 755 .start = start, 756 .path = Array(repo_route_max_len).from(path) orelse return null, 757 } }; 758 } 759 760 // build a `.repo_commits` route showing the ref/oid's commit message in 761 // full, which is the only content that page renders. 762 pub fn repoCommitMessageRoute(identity: []const u8, ref_or_oid: RefOrOid, value: []const u8, base_oid: []const u8) ?RoutablePage { 763 if (value.len == 0) return null; 764 return .{ .repo_commits = .{ 765 .name = Array(repo_identity_max_len).from(identity) orelse return null, 766 .ref_or_oid = ref_or_oid, 767 .value = Array(ref_route_max_len).from(value) orelse return null, 768 .base_oid = Array(ref_route_max_len).from(base_oid) orelse return null, 769 .message = true, 770 } }; 771 } 772 773 // build a `.repo_refs` route windowing `kind`'s column from the 774 // url-encoded ref name `from` ("" = the first window). 775 pub fn repoRefsRoute(identity: []const u8, kind: RefKind, from: []const u8) ?RoutablePage { 776 return .{ .repo_refs = .{ 777 .name = Array(repo_route_max_len).from(identity) orelse return null, 778 .kind = kind, 779 .from = Array(ref_route_max_len).from(from) orelse return null, 780 } }; 781 } 782 783 // build the events list or a window rooted at one event. 784 pub fn repoEventsRoute(identity: []const u8, view: EventsView, kind: ?evt.EventKind, selected: []const u8, moment: ?u64) ?RoutablePage { 785 if ((kind == null) != (selected.len == 0)) return null; 786 return .{ .repo_events = .{ 787 .name = Array(repo_identity_max_len).from(identity) orelse return null, 788 .view = view, 789 .kind = kind, 790 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 791 .moment = moment, 792 } }; 793 } 794 795 // build a `.repo_issues` route for "owner:name" (identity) showing 796 // `status`'s list, rooted at the issue with hex event id `selected` ("" = 797 // the first window). 798 pub fn repoIssuesRoute(identity: []const u8, status: evt.Issue.Status, selected: []const u8) ?RoutablePage { 799 return .{ .repo_issues = .{ 800 .name = Array(repo_route_max_len).from(identity) orelse return null, 801 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 802 .view = switch (status) { 803 .open => .open, 804 .closed => .closed, 805 }, 806 } }; 807 } 808 809 pub fn repoPatchesRoute(identity: []const u8, status: evt.Patch.StatusKind, selected: []const u8) ?RoutablePage { 810 return .{ .repo_patches = .{ 811 .name = Array(repo_route_max_len).from(identity) orelse return null, 812 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 813 .view = switch (status) { 814 .open => .open, 815 .closed => .closed, 816 .merged => .merged, 817 }, 818 } }; 819 } 820 821 pub fn repoPatchesDraftsRoute(identity: []const u8) ?RoutablePage { 822 return .{ .repo_patches = .{ 823 .name = Array(repo_route_max_len).from(identity) orelse return null, 824 .view = .drafts, 825 } }; 826 } 827 828 pub fn repoPatchPublishRoute(identity: []const u8, selected: []const u8) ?RoutablePage { 829 if (selected.len == 0) return null; 830 var route = repoPatchesRoute(identity, .open, selected) orelse return null; 831 switch (route) { 832 .repo_patches => |*patch| patch.view = .publish, 833 else => unreachable, 834 } 835 return route; 836 } 837 838 pub fn repoPatchMergeRoute(identity: []const u8, selected: []const u8) ?RoutablePage { 839 if (selected.len == 0) return null; 840 var route = repoPatchesRoute(identity, .open, selected) orelse return null; 841 switch (route) { 842 .repo_patches => |*patch| patch.view = .merge, 843 else => unreachable, 844 } 845 return route; 846 } 847 848 pub fn repoDiscussionsRoute(identity: []const u8, selected: []const u8) ?RoutablePage { 849 return .{ .repo_discussions = .{ 850 .name = Array(repo_route_max_len).from(identity) orelse return null, 851 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 852 } }; 853 } 854 855 // build the default list route for a thread event kind. 856 pub fn repoThreadRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8) ?RoutablePage { 857 return switch (kind) { 858 .issue => repoIssuesRoute(identity, .open, selected), 859 .patch => repoPatchesRoute(identity, .open, selected), 860 .discuss => repoDiscussionsRoute(identity, selected), 861 else => null, 862 }; 863 } 864 865 // build a selected thread route at one of its comment windows. 866 pub fn repoThreadCommentsRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8, start: usize) ?RoutablePage { 867 var route = repoThreadRoute(kind, identity, selected) orelse return null; 868 switch (route) { 869 .repo_issues => |*thread| thread.comments_start = start, 870 .repo_patches => |*thread| thread.comments_start = start, 871 .repo_discussions => |*thread| thread.comments_start = start, 872 else => unreachable, 873 } 874 return route; 875 } 876 877 // build a comment permalink route at one of its immediate-reply windows. 878 pub fn repoThreadCommentRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8, start: usize) ?RoutablePage { 879 if (thread_id.len == 0 or comment_id.len == 0) return null; 880 var route = repoThreadCommentsRoute(kind, identity, thread_id, start) orelse return null; 881 const comment = Array(evt.event_id_size * 2).from(comment_id) orelse return null; 882 switch (route) { 883 .repo_issues => |*thread| thread.comment = comment, 884 .repo_patches => |*thread| thread.comment = comment, 885 .repo_discussions => |*thread| thread.comment = comment, 886 else => unreachable, 887 } 888 return route; 889 } 890 891 // build the form route for replying to a thread or one of its comments. 892 pub fn repoThreadCommentNewRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, parent_id: []const u8) ?RoutablePage { 893 if (thread_id.len == 0) return null; 894 var route = repoThreadCommentsRoute(kind, identity, thread_id, 0) orelse return null; 895 const parent_event_id = Array(evt.event_id_size * 2).from(parent_id) orelse return null; 896 switch (route) { 897 .repo_issues => |*thread| { 898 thread.comment = parent_event_id; 899 thread.view = .new_comment; 900 }, 901 .repo_patches => |*thread| { 902 thread.comment = parent_event_id; 903 thread.view = .new_comment; 904 }, 905 .repo_discussions => |*thread| { 906 thread.comment = parent_event_id; 907 thread.view = .new_comment; 908 }, 909 else => unreachable, 910 } 911 return route; 912 } 913 914 // build the form route for editing a comment. 915 pub fn repoThreadCommentEditRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?RoutablePage { 916 var route = repoThreadCommentRoute(kind, identity, thread_id, comment_id, 0) orelse return null; 917 switch (route) { 918 .repo_issues => |*thread| thread.view = .edit_comment, 919 .repo_patches => |*thread| thread.view = .edit_comment, 920 .repo_discussions => |*thread| thread.view = .edit_comment, 921 else => unreachable, 922 } 923 return route; 924 } 925 926 // build the confirmation route for removing a thread or comment. 927 pub fn repoThreadRemoveRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?RoutablePage { 928 var route = if (comment_id.len == 0) 929 repoThreadCommentsRoute(kind, identity, thread_id, 0) orelse return null 930 else 931 repoThreadCommentRoute(kind, identity, thread_id, comment_id, 0) orelse return null; 932 switch (route) { 933 .repo_issues => |*thread| thread.view = .remove, 934 .repo_patches => |*thread| thread.view = .remove, 935 .repo_discussions => |*thread| thread.view = .remove, 936 else => unreachable, 937 } 938 return route; 939 } 940 941 // build a thread labels route. 942 pub fn repoThreadLabelsRoute(kind: evt.EventKind, identity: []const u8) ?RoutablePage { 943 var route = repoThreadRoute(kind, identity, "") orelse return null; 944 switch (route) { 945 .repo_issues => |*thread| thread.view = .labels, 946 .repo_patches => |*thread| thread.view = .labels, 947 .repo_discussions => |*thread| thread.view = .labels, 948 else => unreachable, 949 } 950 return route; 951 } 952 953 // build a new-thread form route. 954 pub fn repoThreadNewRoute(kind: evt.EventKind, identity: []const u8) ?RoutablePage { 955 var route = repoThreadRoute(kind, identity, "") orelse return null; 956 switch (route) { 957 .repo_issues => |*thread| thread.view = .new, 958 .repo_patches => |*thread| thread.view = .new, 959 .repo_discussions => |*thread| thread.view = .new, 960 else => unreachable, 961 } 962 return route; 963 } 964 965 // build the edit form route for a thread. 966 pub fn repoThreadEditRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8) ?RoutablePage { 967 if (selected.len == 0) return null; 968 var route = repoThreadRoute(kind, identity, selected) orelse return null; 969 switch (route) { 970 .repo_issues => |*thread| thread.view = .edit, 971 .repo_patches => |*thread| thread.view = .edit, 972 .repo_discussions => |*thread| thread.view = .edit, 973 else => unreachable, 974 } 975 return route; 976 } 977 978 // build a route showing a thread's whole description. 979 pub fn repoThreadDescriptionRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8) ?RoutablePage { 980 if (selected.len == 0) return null; 981 var route = repoThreadRoute(kind, identity, selected) orelse return null; 982 switch (route) { 983 .repo_issues => |*thread| thread.view = .description, 984 .repo_patches => |*thread| thread.view = .description, 985 .repo_discussions => |*thread| thread.view = .description, 986 else => unreachable, 987 } 988 return route; 989 } 990 991 // build a `.repo_issues` route showing the conflicts list, rooted at the 992 // issue with hex event id `start` ("" = the first window). 993 pub fn repoIssuesConflictsRoute(identity: []const u8, start: []const u8) ?RoutablePage { 994 return .{ .repo_issues = .{ 995 .name = Array(repo_route_max_len).from(identity) orelse return null, 996 .selected = Array(evt.event_id_size * 2).from(start) orelse return null, 997 .view = .conflicts, 998 } }; 999 } 1000 1001 // build a `.repo_issues` route showing the conflict resolve form for the 1002 // issue with hex event id `selected`; `theirs` is the comma-separated list 1003 // of fields prefilled from their side. 1004 pub fn repoIssuesResolveRoute(identity: []const u8, selected: []const u8, theirs: []const u8) ?RoutablePage { 1005 if (selected.len == 0) return null; 1006 return .{ .repo_issues = .{ 1007 .name = Array(repo_route_max_len).from(identity) orelse return null, 1008 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 1009 .theirs = Array(theirs_route_max_len).from(theirs) orelse return null, 1010 .view = .resolve, 1011 } }; 1012 } 1013 1014 pub fn repoPatchesConflictsRoute(identity: []const u8, start: []const u8) ?RoutablePage { 1015 return .{ .repo_patches = .{ 1016 .name = Array(repo_route_max_len).from(identity) orelse return null, 1017 .selected = Array(evt.event_id_size * 2).from(start) orelse return null, 1018 .view = .conflicts, 1019 } }; 1020 } 1021 1022 pub fn repoPatchesResolveRoute(identity: []const u8, selected: []const u8, theirs: []const u8) ?RoutablePage { 1023 if (selected.len == 0) return null; 1024 return .{ .repo_patches = .{ 1025 .name = Array(repo_route_max_len).from(identity) orelse return null, 1026 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 1027 .theirs = Array(theirs_route_max_len).from(theirs) orelse return null, 1028 .view = .resolve, 1029 } }; 1030 } 1031 1032 // an inline, owned array of data. keeping it in the route (rather than a 1033 // borrowed slice) makes RoutablePage a plain value: it can be copied, stored 1034 // in history, and serialized without any arena tracking. 1035 pub fn Array(comptime max_len: usize) type { 1036 return struct { 1037 bytes: [max_len]u8 = undefined, 1038 len: u16 = 0, 1039 1040 pub fn from(s: []const u8) ?Array(max_len) { 1041 if (s.len > max_len) return null; 1042 var name = Array(max_len){ .len = @intCast(s.len) }; 1043 @memcpy(name.bytes[0..s.len], s); 1044 return name; 1045 } 1046 1047 pub fn slice(self: *const Array(max_len)) []const u8 { 1048 return self.bytes[0..self.len]; 1049 } 1050 1051 // round-trip as a json string instead of an array of numbers. 1052 pub fn jsonStringify(self: *const Array(max_len), jw: anytype) !void { 1053 try jw.write(self.slice()); 1054 } 1055 1056 pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !Array(max_len) { 1057 const s = try std.json.innerParse([]const u8, allocator, source, options); 1058 // ValueTooLong is std.json's error for an over-long field. 1059 return from(s) orelse error.ValueTooLong; 1060 } 1061 }; 1062 } 1063 1064 pub fn toUrl(self: RoutablePage, arena: *std.heap.ArenaAllocator) ![]const u8 { 1065 return switch (self) { 1066 .home_about => "/", 1067 .home_users => |u| blk: { 1068 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1069 try out.writer.writeAll("/" ++ home_segment ++ "/users"); 1070 try writeListWindow(&out.writer, u.search.slice(), u.start); 1071 break :blk out.written(); 1072 }, 1073 .home_repo_new => "/" ++ home_segment ++ "/repo/new", 1074 .home_user_new => "/" ++ home_segment ++ "/user/new", 1075 .home_user => |tab| try userTabUrl(arena, "/" ++ home_segment, tab), 1076 .not_found => "/" ++ home_segment ++ "/not-found", 1077 .user_repos => |u| blk: { 1078 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1079 try out.writer.print("/{s}/repos", .{u.name.slice()}); 1080 try writeListWindow(&out.writer, u.search.slice(), u.start); 1081 break :blk out.written(); 1082 }, 1083 .user_forks => |u| if (u.start == 0) 1084 try arena.allocator().print("/{s}/forks", .{u.name.slice()}) 1085 else 1086 try arena.allocator().print("/{s}/forks/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 1087 .user_repo_new => |name| try arena.allocator().print("/{s}/repo/new", .{name.slice()}), 1088 .user_user_new => |name| try arena.allocator().print("/{s}/user/new", .{name.slice()}), 1089 .user_user => |u| try userTabUrl(arena, try arena.allocator().print("/{s}", .{u.name.slice()}), u.tab), 1090 .repo_files => |f| blk: { 1091 const prefix = try repoUrlPrefix(arena, f.name.slice()); 1092 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; 1093 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1094 try out.writer.print("{s}/" ++ files_seg, .{prefix}); 1095 if (f.ref_kind) |kind| if (f.ref_value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), f.ref_value.slice() }); 1096 if (f.patchrev_id.len != 0) try out.writer.print("/patchrev:{s}", .{f.patchrev_id.slice()}); 1097 if (f.find.len != 0) try out.writer.print("/" ++ find_seg ++ "{s}", .{f.find.slice()}); 1098 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 1099 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 1100 break :blk out.written(); 1101 }, 1102 .repo_commits => |c| blk: { 1103 const prefix = try repoUrlPrefix(arena, c.name.slice()); 1104 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1105 try out.writer.print("{s}/" ++ commits_seg, .{prefix}); 1106 if (c.ref_or_oid) |kind| if (c.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), c.value.slice() }); 1107 if (c.base_oid.len != 0) try out.writer.print("/" ++ base_seg ++ "{s}", .{c.base_oid.slice()}); 1108 if (c.search.len != 0) try out.writer.print("/" ++ search_seg ++ "{s}", .{c.search.slice()}); 1109 if (c.from.len != 0) try out.writer.print("/" ++ from_seg ++ "{s}", .{c.from.slice()}); 1110 if (c.merge.len != 0) try out.writer.print("/" ++ merge_seg ++ "{s}", .{c.merge.slice()}); 1111 if (c.message) try out.writer.print("/" ++ message_seg, .{}); 1112 break :blk out.written(); 1113 }, 1114 .repo_diff => |d| blk: { 1115 const prefix = try repoUrlPrefix(arena, d.name.slice()); 1116 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1117 try out.writer.print("{s}/diff", .{prefix}); 1118 if (d.ref_or_oid) |kind| if (d.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), d.value.slice() }); 1119 if (d.patchrev_id.len != 0) try out.writer.print("/patchrev:{s}", .{d.patchrev_id.slice()}); 1120 if (d.base_oid.len != 0) try out.writer.print("/" ++ base_seg ++ "{s}", .{d.base_oid.slice()}); 1121 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 1122 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 1123 break :blk out.written(); 1124 }, 1125 .repo_refs => |r| blk: { 1126 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1127 try out.writer.print("{s}/refs", .{try repoUrlPrefix(arena, r.name.slice())}); 1128 if (r.from.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(r.kind), r.from.slice() }); 1129 if (r.search.len != 0) try out.writer.print("/" ++ search_seg ++ "{s}", .{r.search.slice()}); 1130 break :blk out.written(); 1131 }, 1132 .repo_issues => |i| blk: { 1133 const prefix = try repoUrlPrefix(arena, i.name.slice()); 1134 if (i.view == .edit) break :blk try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/edit", .{ prefix, i.selected.slice() }); 1135 if (i.view == .description) break :blk try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/description", .{ prefix, i.selected.slice() }); 1136 if (i.view == .resolve) break :blk if (i.theirs.len == 0) 1137 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/resolve", .{ prefix, i.selected.slice() }) 1138 else 1139 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, i.selected.slice(), i.theirs.slice() }); 1140 if (i.view == .new_comment) break :blk if (i.comment.len == 0) 1141 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/new", .{ prefix, i.selected.slice() }) 1142 else 1143 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, i.selected.slice(), i.comment.slice() }); 1144 if (i.view == .edit_comment) break :blk try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, i.selected.slice(), i.comment.slice() }); 1145 if (i.view == .remove) break :blk if (i.comment.len == 0) 1146 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/remove", .{ prefix, i.selected.slice() }) 1147 else 1148 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, i.selected.slice(), i.comment.slice() }); 1149 if (i.view == .conflicts) break :blk if (i.selected.len == 0) 1150 try arena.allocator().print("{s}/issues/conflicts", .{prefix}) 1151 else 1152 try arena.allocator().print("{s}/issues/" ++ start_seg ++ "{s}/conflicts", .{ prefix, i.selected.slice() }); 1153 if (i.comment.len != 0) break :blk if (i.comments_start == 0) 1154 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, i.selected.slice(), i.comment.slice() }) 1155 else 1156 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, i.selected.slice(), i.comment.slice(), i.comments_start }); 1157 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1158 // the list the thread is selected in stays filtered, so paging 1159 // and reloading keep the query 1160 if (i.selected.len != 0) { 1161 try out.writer.print("{s}/" ++ issue_seg ++ "{s}", .{ prefix, i.selected.slice() }); 1162 if (i.search.len != 0) try out.writer.print("/" ++ search_seg ++ "{s}", .{i.search.slice()}); 1163 if (i.comments_start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{i.comments_start}); 1164 break :blk out.written(); 1165 } 1166 try out.writer.print("{s}/issues/{s}", .{ prefix, @tagName(i.view) }); 1167 if (i.search.len != 0) try out.writer.print("/" ++ search_seg ++ "{s}", .{i.search.slice()}); 1168 break :blk out.written(); 1169 }, 1170 .repo_patches => |p| blk: { 1171 const prefix = try repoUrlPrefix(arena, p.name.slice()); 1172 if (p.view == .edit) break :blk try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/edit", .{ prefix, p.selected.slice() }); 1173 if (p.view == .publish) break :blk try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/publish", .{ prefix, p.selected.slice() }); 1174 if (p.view == .merge) break :blk try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/merge", .{ prefix, p.selected.slice() }); 1175 if (p.view == .description) break :blk try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/description", .{ prefix, p.selected.slice() }); 1176 if (p.view == .resolve) break :blk if (p.theirs.len == 0) 1177 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/resolve", .{ prefix, p.selected.slice() }) 1178 else 1179 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, p.selected.slice(), p.theirs.slice() }); 1180 if (p.view == .new_comment) break :blk if (p.comment.len == 0) 1181 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/new", .{ prefix, p.selected.slice() }) 1182 else 1183 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, p.selected.slice(), p.comment.slice() }); 1184 if (p.view == .edit_comment) break :blk try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, p.selected.slice(), p.comment.slice() }); 1185 if (p.view == .remove) break :blk if (p.comment.len == 0) 1186 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/remove", .{ prefix, p.selected.slice() }) 1187 else 1188 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, p.selected.slice(), p.comment.slice() }); 1189 if (p.view == .conflicts) break :blk if (p.selected.len == 0) 1190 try arena.allocator().print("{s}/patches/conflicts", .{prefix}) 1191 else 1192 try arena.allocator().print("{s}/patches/" ++ start_seg ++ "{s}/conflicts", .{ prefix, p.selected.slice() }); 1193 if (p.comment.len != 0) break :blk if (p.comments_start == 0) 1194 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, p.selected.slice(), p.comment.slice() }) 1195 else 1196 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, p.selected.slice(), p.comment.slice(), p.comments_start }); 1197 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1198 // the list the thread is selected in stays filtered, so paging 1199 // and reloading keep the query 1200 if (p.selected.len != 0) { 1201 try out.writer.print("{s}/" ++ patch_seg ++ "{s}", .{ prefix, p.selected.slice() }); 1202 if (p.search.len != 0) try out.writer.print("/" ++ search_seg ++ "{s}", .{p.search.slice()}); 1203 if (p.comments_start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{p.comments_start}); 1204 break :blk out.written(); 1205 } 1206 try out.writer.print("{s}/patches/{s}", .{ prefix, @tagName(p.view) }); 1207 if (p.search.len != 0) try out.writer.print("/" ++ search_seg ++ "{s}", .{p.search.slice()}); 1208 break :blk out.written(); 1209 }, 1210 .repo_discussions => |t| blk: { 1211 const prefix = try repoUrlPrefix(arena, t.name.slice()); 1212 if (t.view == .edit) break :blk try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/edit", .{ prefix, t.selected.slice() }); 1213 if (t.view == .description) break :blk try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/description", .{ prefix, t.selected.slice() }); 1214 if (t.view == .new_comment) break :blk if (t.comment.len == 0) 1215 try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/new", .{ prefix, t.selected.slice() }) 1216 else 1217 try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, t.selected.slice(), t.comment.slice() }); 1218 if (t.view == .edit_comment) break :blk try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, t.selected.slice(), t.comment.slice() }); 1219 if (t.view == .remove) break :blk if (t.comment.len == 0) 1220 try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/remove", .{ prefix, t.selected.slice() }) 1221 else 1222 try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, t.selected.slice(), t.comment.slice() }); 1223 if (t.comment.len != 0) break :blk if (t.comments_start == 0) 1224 try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, t.selected.slice(), t.comment.slice() }) 1225 else 1226 try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, t.selected.slice(), t.comment.slice(), t.comments_start }); 1227 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1228 // the list the thread is selected in stays filtered, so paging 1229 // and reloading keep the query 1230 if (t.selected.len != 0) { 1231 try out.writer.print("{s}/" ++ discuss_seg ++ "{s}", .{ prefix, t.selected.slice() }); 1232 if (t.search.len != 0) try out.writer.print("/" ++ search_seg ++ "{s}", .{t.search.slice()}); 1233 if (t.comments_start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{t.comments_start}); 1234 break :blk out.written(); 1235 } 1236 try out.writer.print("{s}/discussions/{s}", .{ prefix, @tagName(t.view) }); 1237 if (t.search.len != 0) try out.writer.print("/" ++ search_seg ++ "{s}", .{t.search.slice()}); 1238 break :blk out.written(); 1239 }, 1240 .repo_undo => |u| blk: { 1241 const prefix = try repoUrlPrefix(arena, u.name.slice()); 1242 break :blk if (u.clear) 1243 try arena.allocator().print("{s}/undo/clear", .{prefix}) 1244 else if (u.index) |index| 1245 try arena.allocator().print("{s}/undo/{d}", .{ prefix, index }) 1246 else 1247 try arena.allocator().print("{s}/undo", .{prefix}); 1248 }, 1249 .repo_events => |e| blk: { 1250 const prefix = try repoUrlPrefix(arena, e.name.slice()); 1251 const moment = if (e.moment) |value| try arena.allocator().print("/moment:{d}", .{value}) else ""; 1252 break :blk if (e.kind) |kind| 1253 try arena.allocator().print("{s}/event:{s}/kind:{s}{s}", .{ prefix, e.selected.slice(), @tagName(kind), moment }) 1254 else 1255 try arena.allocator().print("{s}/events/{s}{s}", .{ prefix, @tagName(e.view), moment }); 1256 }, 1257 .repo_repo_new => |name| try arena.allocator().print("{s}/repo/new", .{try repoUrlPrefix(arena, name.slice())}), 1258 .repo_user_new => |name| try arena.allocator().print("{s}/user/new", .{try repoUrlPrefix(arena, name.slice())}), 1259 .repo_settings => |name| try arena.allocator().print("{s}/repo/settings", .{try repoUrlPrefix(arena, name.slice())}), 1260 .repo_roles => |r| blk: { 1261 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1262 try out.writer.print("{s}/repo/roles", .{try repoUrlPrefix(arena, r.name.slice())}); 1263 if (r.user.len != 0) try out.writer.print("/" ++ user_seg ++ "{s}", .{r.user.slice()}); 1264 if (r.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{r.start}); 1265 break :blk out.written(); 1266 }, 1267 .repo_user => |u| try userTabUrl(arena, try repoUrlPrefix(arena, u.name.slice()), u.tab), 1268 .fork_patch => |f| try arena.allocator().print("/{s}+{s}", .{ f.name.slice(), f.id.slice() }), 1269 .fork_diff => |d| blk: { 1270 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1271 try out.writer.print("/{s}+{s}/diff", .{ d.fork.name.slice(), d.fork.id.slice() }); 1272 if (d.oid.len != 0) try out.writer.print("/object:{s}/" ++ base_seg ++ "{s}", .{ d.oid.slice(), d.base_oid.slice() }); 1273 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 1274 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 1275 break :blk out.written(); 1276 }, 1277 .fork_files => |f| blk: { 1278 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1279 try out.writer.print("/{s}+{s}/" ++ files_seg, .{ f.fork.name.slice(), f.fork.id.slice() }); 1280 if (f.oid.len != 0) try out.writer.print("/object:{s}", .{f.oid.slice()}); 1281 if (f.find.len != 0) try out.writer.print("/" ++ find_seg ++ "{s}", .{f.find.slice()}); 1282 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 1283 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 1284 break :blk out.written(); 1285 }, 1286 .fork_commits => |c| blk: { 1287 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1288 try out.writer.print("/{s}+{s}/" ++ commits_seg, .{ c.fork.name.slice(), c.fork.id.slice() }); 1289 if (c.oid.len != 0) try out.writer.print("/object:{s}", .{c.oid.slice()}); 1290 if (c.message) try out.writer.print("/" ++ message_seg, .{}); 1291 break :blk out.written(); 1292 }, 1293 .fork_repo_new => |f| try arena.allocator().print("/{s}+{s}/repo/new", .{ f.name.slice(), f.id.slice() }), 1294 .fork_user_new => |f| try arena.allocator().print("/{s}+{s}/user/new", .{ f.name.slice(), f.id.slice() }), 1295 .fork_user => |u| try userTabUrl(arena, try arena.allocator().print("/{s}+{s}", .{ u.fork.name.slice(), u.fork.id.slice() }), u.tab), 1296 }; 1297 } 1298 1299 // a user tab's url under its page's `prefix` 1300 fn userTabUrl(arena: *std.heap.ArenaAllocator, prefix: []const u8, tab: UserTab) ![]const u8 { 1301 return switch (tab.view) { 1302 .settings => try arena.allocator().print("{s}/user/settings", .{prefix}), 1303 .keys => try arena.allocator().print("{s}/user/keys", .{prefix}), 1304 .notifications => if (tab.start == 0) 1305 try arena.allocator().print("{s}/user/notifications", .{prefix}) 1306 else 1307 try arena.allocator().print("{s}/user/notifications/" ++ start_seg ++ "{d}", .{ prefix, tab.start }), 1308 }; 1309 } 1310 1311 pub fn fromUrl(path: []const u8) ?RoutablePage { 1312 if (std.mem.eql(u8, path, "/")) return default; 1313 if (path.len < 2 or path[0] != '/') return null; 1314 var segments = std.mem.splitScalar(u8, path[1..], '/'); 1315 const first = segments.next() orelse return null; 1316 // "<forker>:<reponame>+<patch id>[/files|/commits]"; the patch 1317 // branch is implicit, while object ids and view-specific params 1318 // use the same tails as the repo files and commits routes. 1319 if (std.mem.indexOfScalar(u8, first, '+')) |plus| { 1320 const identity = first[0..plus]; 1321 _ = RepoIdentity.parse(identity) orelse return null; 1322 const id = first[plus + 1 ..]; 1323 if (id.len != evt.event_id_size * 2) return null; 1324 for (id) |c| if (!std.ascii.isHex(c)) return null; 1325 const tab = segments.next() orelse return forkPatchRoute(identity, id); 1326 var params = Params{}; 1327 if (std.mem.eql(u8, tab, "diff")) { 1328 params.scanPairs(&segments) catch return null; 1329 if (!params.only(&.{ .start, .object, .base })) return null; 1330 const ref = params.ref() catch return null; 1331 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1332 return forkDiffRoute(identity, id, oid, params.values.get(.base) orelse "", params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1333 } 1334 if (std.mem.eql(u8, tab, files_seg)) { 1335 params.scanPairs(&segments) catch return null; 1336 if (!params.only(&.{ .line, .object, .find })) return null; 1337 const line = params.line() orelse return null; 1338 const ref = params.ref() catch return null; 1339 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1340 const file_path = pathValue(segments.rest()) orelse return null; 1341 var route = forkFilesRoute(identity, id, oid, file_path, line) orelse return null; 1342 if (params.values.get(.find)) |find| route.fork_files.find = Array(find_route_max_len).from(find) orelse return null; 1343 return route; 1344 } 1345 if (std.mem.eql(u8, tab, commits_seg)) { 1346 params.scanPairs(&segments) catch return null; 1347 if (!params.only(&.{.object})) return null; 1348 const ref = params.ref() catch return null; 1349 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1350 if (std.mem.eql(u8, segments.rest(), message_seg)) 1351 return if (oid.len != 0) forkCommitMessageRoute(identity, id, oid) else null; 1352 return if (segments.rest().len == 0) forkCommitsRoute(identity, id, oid) else null; 1353 } 1354 if (std.mem.eql(u8, tab, "repo")) return if (isNewTail(&segments)) forkRepoNewRoute(identity, id) else null; 1355 if (std.mem.eql(u8, tab, "user")) return switch (userTail(&segments) orelse return null) { 1356 .new => forkUserNewRoute(identity, id), 1357 .tab => |user_tab| (forkUserRoute(identity, id) orelse return null).withUserTab(user_tab), 1358 }; 1359 return null; 1360 } 1361 // "<username>:<reponame>[/<tab tail>]"; the bare pair is the files root 1362 if (std.mem.indexOfScalar(u8, first, ':') != null) { 1363 _ = RepoIdentity.parse(first) orelse return null; 1364 if (segments.peek() != null) { 1365 return repoSubRoute(first, segments.rest()); 1366 } 1367 return repoFilesRoute(first, null, "", "", 0); 1368 } 1369 // "home[/users[/search:<s>][/start:<n>]|/repo/new|/user/<tail>|/not-found]" 1370 if (std.mem.eql(u8, first, home_segment)) { 1371 const sub = segments.next() orelse return default; 1372 if (std.mem.eql(u8, sub, "users")) { 1373 const params = listParams(&segments, &.{ .start, .search }) orelse return null; 1374 return withEncodedSearch(.{ .home_users = .{ .start = params.start() orelse return null } }, params.values.get(.search) orelse ""); 1375 } 1376 if (std.mem.eql(u8, sub, "repo")) return if (isNewTail(&segments)) .home_repo_new else null; 1377 if (std.mem.eql(u8, sub, "user")) return switch (userTail(&segments) orelse return null) { 1378 .new => .home_user_new, 1379 .tab => |user_tab| .{ .home_user = user_tab }, 1380 }; 1381 if (std.mem.eql(u8, sub, "not-found")) return if (segments.peek() == null) .not_found else null; 1382 return null; 1383 } 1384 // "<username>[/repos[/search:<s>][/start:<n>]|/forks|/repo/new|/user/<tail>]" 1385 evt.User.validateName(first) catch return null; 1386 const parsed = Array(evt.User.name_max_len).from(first) orelse return null; 1387 const sub = segments.next() orelse return .{ .user_repos = .{ .name = parsed } }; 1388 if (std.mem.eql(u8, sub, "repos")) { 1389 const params = listParams(&segments, &.{ .start, .search }) orelse return null; 1390 return withEncodedSearch(.{ .user_repos = .{ .name = parsed, .start = params.start() orelse return null } }, params.values.get(.search) orelse ""); 1391 } 1392 if (std.mem.eql(u8, sub, "forks")) return .{ .user_forks = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 1393 if (std.mem.eql(u8, sub, "repo")) return if (isNewTail(&segments)) .{ .user_repo_new = parsed } else null; 1394 if (std.mem.eql(u8, sub, "user")) return switch (userTail(&segments) orelse return null) { 1395 .new => .{ .user_user_new = parsed }, 1396 .tab => |user_tab| .{ .user_user = .{ .name = parsed, .tab = user_tab } }, 1397 }; 1398 return null; // unknown sub-path 1399 } 1400 1401 // parse a local-mode url, which elides the "/<owner>:<name>" prefix: 1402 // "/" is the files root and the sub-paths follow directly. 1403 pub fn fromUrlLocal(path: []const u8) ?RoutablePage { 1404 if (std.mem.eql(u8, path, "/")) return repoFilesRoute("", null, "", "", 0); 1405 if (path.len < 2 or path[0] != '/') return null; 1406 return repoSubRoute("", path[1..]); 1407 } 1408 1409 // the url prefix for a repo route's identity: "/<identity>", or "" 1410 // when the identity is elided. 1411 fn repoUrlPrefix(arena: *std.heap.ArenaAllocator, identity: []const u8) ![]const u8 { 1412 if (identity.len == 0) return ""; 1413 return arena.allocator().print("/{s}", .{identity}); 1414 } 1415 1416 // true when the page builds at its content height on the web, letting the 1417 // browser scroll the whole page, rather than being pinned to the viewport 1418 pub fn fullHeight(self: RoutablePage) bool { 1419 return switch (self) { 1420 .repo_issues => |i| i.view == .resolve, 1421 .repo_patches => |p| p.view == .resolve, 1422 else => false, 1423 }; 1424 } 1425 1426 // the "owner:name" a repo route carries, null for any other route. the 1427 // result borrows the route, so it must outlive the slice. 1428 pub fn repoIdentity(self: *const RoutablePage) ?[]const u8 { 1429 return switch (self.*) { 1430 .repo_files => |*f| f.name.slice(), 1431 .repo_commits => |*c| c.name.slice(), 1432 .repo_diff => |*d| d.name.slice(), 1433 .repo_refs => |*r| r.name.slice(), 1434 .repo_issues => |*i| i.name.slice(), 1435 .repo_patches => |*p| p.name.slice(), 1436 .repo_discussions => |*t| t.name.slice(), 1437 .repo_events => |*e| e.name.slice(), 1438 .repo_undo => |*u| u.name.slice(), 1439 .repo_roles => |*r| r.name.slice(), 1440 .repo_user => |*u| u.name.slice(), 1441 .repo_settings, .repo_repo_new, .repo_user_new => |*name| name.slice(), 1442 else => null, 1443 }; 1444 } 1445 1446 pub fn forkRoute(self: *const RoutablePage) ?*const ForkRoute { 1447 return switch (self.*) { 1448 .fork_patch => |*f| f, 1449 .fork_diff => |*d| &d.fork, 1450 .fork_files => |*f| &f.fork, 1451 .fork_commits => |*f| &f.fork, 1452 .fork_repo_new => |*f| f, 1453 .fork_user_new => |*f| f, 1454 .fork_user => |*u| &u.fork, 1455 else => null, 1456 }; 1457 } 1458 1459 // the route this route's page lands on, with no tab or params: the users 1460 // list, a user's repos, or a repo's files root. 1461 pub fn pageRoot(self: RoutablePage) RoutablePage { 1462 return switch (self.parent()) { 1463 .home => .default, 1464 .user => switch (self) { 1465 .user_repos => |u| .{ .user_repos = .{ .name = u.name } }, 1466 .user_forks => |u| .{ .user_repos = .{ .name = u.name } }, 1467 .user_repo_new, .user_user_new => |name| .{ .user_repos = .{ .name = name } }, 1468 .user_user => |u| .{ .user_repos = .{ .name = u.name } }, 1469 else => self, 1470 }, 1471 // every repo route stores the same identity, so it always fits 1472 .repo => repoFilesRoute(self.repoIdentity() orelse return self, null, "", "", 0) orelse self, 1473 .fork => blk: { 1474 const f = self.forkRoute() orelse break :blk self; 1475 break :blk forkPatchRoute(f.name.slice(), f.id.slice()) orelse self; 1476 }, 1477 .not_found => self, 1478 }; 1479 } 1480 1481 pub fn parent(self: RoutablePage) PageKind { 1482 return switch (self) { 1483 .home_about, .home_users, .home_repo_new, .home_user_new, .home_user => .home, 1484 .user_repos, .user_forks, .user_repo_new, .user_user_new, .user_user => .user, 1485 .repo_files, .repo_commits, .repo_diff, .repo_refs, .repo_issues, .repo_patches, .repo_discussions, .repo_events, .repo_undo, .repo_settings, .repo_roles, .repo_repo_new, .repo_user_new, .repo_user => .repo, 1486 .fork_patch, .fork_diff, .fork_files, .fork_commits, .fork_repo_new, .fork_user_new, .fork_user => .fork, 1487 .not_found => .not_found, 1488 }; 1489 } 1490 1491 // the dir a trailing "path:<dir>" names: the raw remainder ("" when 1492 // absent, null when malformed) 1493 fn pathValue(rest: []const u8) ?[]const u8 { 1494 if (rest.len == 0) return ""; 1495 if (!std.mem.startsWith(u8, rest, path_seg)) return null; 1496 const dir = rest[path_seg.len..]; 1497 return if (dir.len == 0) null else dir; 1498 } 1499 1500 // a list route's tail: at most a "start:<n>" param, null on anything else 1501 // whether the rest of `segments` is exactly "new" 1502 fn isNewTail(segments: *std.mem.SplitIterator(u8, .scalar)) bool { 1503 const next = segments.next() orelse return false; 1504 return std.mem.eql(u8, next, "new") and segments.next() == null; 1505 } 1506 1507 // the tail after a page's "user": none or "notifications" with an optional 1508 // "start:<n>", "settings", "keys", or "new" 1509 fn userTail(segments: *std.mem.SplitIterator(u8, .scalar)) ?union(enum) { new, tab: UserTab } { 1510 const word = segments.next() orelse return .{ .tab = .{} }; 1511 if (std.mem.eql(u8, word, "new")) return if (segments.next() == null) .new else null; 1512 return switch (std.meta.stringToEnum(UserView, word) orelse return null) { 1513 inline .settings, .keys => |view| if (segments.next() == null) .{ .tab = .{ .view = view } } else null, 1514 .notifications => .{ .tab = .{ .start = listStart(segments) orelse return null } }, 1515 }; 1516 } 1517 1518 fn listStart(segments: *std.mem.SplitIterator(u8, .scalar)) ?usize { 1519 const params = listParams(segments, &.{.start}) orelse return null; 1520 return params.start(); 1521 } 1522 1523 // a list route's tail of `allowed` params, null on anything else 1524 fn listParams(segments: *std.mem.SplitIterator(u8, .scalar), allowed: []const Params.ParamKey) ?Params { 1525 var params = Params{}; 1526 const word = params.scanTail(segments) catch return null; 1527 if (word != null or !params.only(allowed)) return null; 1528 return params; 1529 } 1530 1531 // a searchable list url's search and window segments, each optional 1532 fn writeListWindow(writer: *std.Io.Writer, search: []const u8, start: usize) !void { 1533 if (search.len != 0) try writer.print("/" ++ search_seg ++ "{s}", .{search}); 1534 if (start != 0) try writer.print("/" ++ start_seg ++ "{d}", .{start}); 1535 } 1536 1537 // parse the sub-path after a repo route's identity ("" for a local 1538 // route): a tab keyword followed by "key:value" params in any order, plus 1539 // the files tab's trailing "path:<dir>". 1540 fn repoSubRoute(pair: []const u8, sub: []const u8) ?RoutablePage { 1541 var segments = std.mem.splitScalar(u8, sub, '/'); 1542 const tab = segments.next() orelse return null; 1543 var params = Params{}; 1544 if (std.mem.eql(u8, tab, "repo")) { 1545 const word = segments.peek() orelse return null; 1546 if (std.mem.eql(u8, word, "settings")) { 1547 _ = segments.next(); 1548 return if (segments.next() == null) repoSettingsRoute(pair) else null; 1549 } 1550 if (std.mem.eql(u8, word, "roles")) { 1551 _ = segments.next(); 1552 const roles_params = listParams(&segments, &.{ .user, .start }) orelse return null; 1553 var route = repoRolesRoute(pair, roles_params.start() orelse return null) orelse return null; 1554 if (roles_params.values.get(.user)) |user| route.repo_roles.user = Array(evt.User.name_max_len).from(user) orelse return null; 1555 return route; 1556 } 1557 return if (isNewTail(&segments)) .{ .repo_repo_new = Array(repo_route_max_len).from(pair) orelse return null } else null; 1558 } 1559 if (std.mem.eql(u8, tab, "user")) return switch (userTail(&segments) orelse return null) { 1560 .new => .{ .repo_user_new = Array(repo_route_max_len).from(pair) orelse return null }, 1561 .tab => |user_tab| .{ .repo_user = .{ .name = Array(repo_route_max_len).from(pair) orelse return null, .tab = user_tab } }, 1562 }; 1563 if (std.mem.eql(u8, tab, "refs")) { 1564 const word = params.scanTail(&segments) catch return null; 1565 if (word != null or !params.only(&.{ .branch, .tag, .search })) return null; 1566 const ref = params.ref() catch return null; 1567 const kind: RefKind = if (ref) |r| std.meta.stringToEnum(RefKind, @tagName(r.kind)) orelse return null else .branch; 1568 const from = if (ref) |r| r.value else ""; 1569 return withEncodedSearch(repoRefsRoute(pair, kind, from), params.values.get(.search) orelse ""); 1570 } 1571 if (std.mem.startsWith(u8, tab, issue_seg)) { 1572 const issue_id = tab[issue_seg.len..]; 1573 if (issue_id.len == 0) return null; 1574 if (segments.peek()) |tail| { 1575 if (std.mem.startsWith(u8, tail, comment_seg)) { 1576 _ = segments.next(); 1577 const comment_id = tail[comment_seg.len..]; 1578 if (comment_id.len == 0) return null; 1579 const word = params.scanTail(&segments) catch return null; 1580 if (word) |last| { 1581 if (!params.only(&.{})) return null; 1582 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.issue, pair, issue_id, comment_id); 1583 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.issue, pair, issue_id, comment_id); 1584 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.issue, pair, issue_id, comment_id); 1585 return null; 1586 } 1587 if (!params.only(&.{.start})) return null; 1588 return repoThreadCommentRoute(.issue, pair, issue_id, comment_id, params.start() orelse return null); 1589 } 1590 } 1591 const word = params.scanTail(&segments) catch return null; 1592 if (word) |tail| { 1593 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.issue, pair, issue_id, "") else null; 1594 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.issue, pair, issue_id) else null; 1595 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.issue, pair, issue_id, "") else null; 1596 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.issue, pair, issue_id) else null; 1597 if (std.mem.eql(u8, tail, "resolve")) { 1598 if (!params.only(&.{.theirs})) return null; 1599 return repoIssuesResolveRoute(pair, issue_id, params.values.get(.theirs) orelse ""); 1600 } 1601 return null; 1602 } 1603 if (!params.only(&.{ .start, .search })) return null; 1604 return withEncodedSearch(repoThreadCommentsRoute(.issue, pair, issue_id, params.start() orelse return null), params.values.get(.search) orelse ""); 1605 } 1606 if (std.mem.startsWith(u8, tab, patch_seg)) { 1607 const patch_id = tab[patch_seg.len..]; 1608 if (patch_id.len == 0) return null; 1609 if (segments.peek()) |tail| { 1610 if (std.mem.startsWith(u8, tail, comment_seg)) { 1611 _ = segments.next(); 1612 const comment_id = tail[comment_seg.len..]; 1613 if (comment_id.len == 0) return null; 1614 const word = params.scanTail(&segments) catch return null; 1615 if (word) |last| { 1616 if (!params.only(&.{})) return null; 1617 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.patch, pair, patch_id, comment_id); 1618 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.patch, pair, patch_id, comment_id); 1619 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.patch, pair, patch_id, comment_id); 1620 return null; 1621 } 1622 if (!params.only(&.{.start})) return null; 1623 return repoThreadCommentRoute(.patch, pair, patch_id, comment_id, params.start() orelse return null); 1624 } 1625 } 1626 const word = params.scanTail(&segments) catch return null; 1627 if (word) |tail| { 1628 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.patch, pair, patch_id, "") else null; 1629 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.patch, pair, patch_id) else null; 1630 if (std.mem.eql(u8, tail, "publish")) return if (params.only(&.{})) repoPatchPublishRoute(pair, patch_id) else null; 1631 if (std.mem.eql(u8, tail, "merge")) return if (params.only(&.{})) repoPatchMergeRoute(pair, patch_id) else null; 1632 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.patch, pair, patch_id, "") else null; 1633 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.patch, pair, patch_id) else null; 1634 if (std.mem.eql(u8, tail, "resolve")) { 1635 if (!params.only(&.{.theirs})) return null; 1636 return repoPatchesResolveRoute(pair, patch_id, params.values.get(.theirs) orelse ""); 1637 } 1638 return null; 1639 } 1640 if (!params.only(&.{ .start, .search })) return null; 1641 return withEncodedSearch(repoThreadCommentsRoute(.patch, pair, patch_id, params.start() orelse return null), params.values.get(.search) orelse ""); 1642 } 1643 if (std.mem.startsWith(u8, tab, discuss_seg)) { 1644 const discussion_id = tab[discuss_seg.len..]; 1645 if (discussion_id.len == 0) return null; 1646 if (segments.peek()) |tail| { 1647 if (std.mem.startsWith(u8, tail, comment_seg)) { 1648 _ = segments.next(); 1649 const comment_id = tail[comment_seg.len..]; 1650 if (comment_id.len == 0) return null; 1651 const word = params.scanTail(&segments) catch return null; 1652 if (word) |last| { 1653 if (!params.only(&.{})) return null; 1654 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.discuss, pair, discussion_id, comment_id); 1655 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.discuss, pair, discussion_id, comment_id); 1656 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.discuss, pair, discussion_id, comment_id); 1657 return null; 1658 } 1659 if (!params.only(&.{.start})) return null; 1660 return repoThreadCommentRoute(.discuss, pair, discussion_id, comment_id, params.start() orelse return null); 1661 } 1662 } 1663 const word = params.scanTail(&segments) catch return null; 1664 if (word) |tail| { 1665 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.discuss, pair, discussion_id, "") else null; 1666 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.discuss, pair, discussion_id) else null; 1667 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.discuss, pair, discussion_id, "") else null; 1668 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.discuss, pair, discussion_id) else null; 1669 return null; 1670 } 1671 if (!params.only(&.{ .start, .search })) return null; 1672 return withEncodedSearch(repoThreadCommentsRoute(.discuss, pair, discussion_id, params.start() orelse return null), params.values.get(.search) orelse ""); 1673 } 1674 if (std.mem.eql(u8, tab, "issues")) { 1675 // the word is a list view; a filter with neither is never emitted. 1676 const word = params.scanTail(&segments) catch return null; 1677 const w = word orelse return if (params.only(&.{})) repoIssuesRoute(pair, .open, "") else null; 1678 // the conflicts list windows by an issue id, not a number, so it 1679 // reads the start param raw 1680 if (std.mem.eql(u8, w, "conflicts")) { 1681 if (!params.only(&.{.start})) return null; 1682 return repoIssuesConflictsRoute(pair, params.values.get(.start) orelse ""); 1683 } 1684 if (!params.only(&.{.search})) return null; 1685 const search_value = params.values.get(.search) orelse ""; 1686 if (std.meta.stringToEnum(evt.Issue.Status, w)) |status| return withEncodedSearch(repoIssuesRoute(pair, status, ""), search_value); 1687 if (std.mem.eql(u8, w, "labels")) return withEncodedSearch(repoThreadLabelsRoute(.issue, pair), search_value); 1688 if (std.mem.eql(u8, w, "new")) return if (search_value.len == 0) repoThreadNewRoute(.issue, pair) else null; 1689 return null; 1690 } 1691 if (std.mem.eql(u8, tab, "patches")) { 1692 const word = params.scanTail(&segments) catch return null; 1693 const view = word orelse return if (params.only(&.{})) repoPatchesRoute(pair, .open, "") else null; 1694 if (std.mem.eql(u8, view, "conflicts")) { 1695 if (!params.only(&.{.start})) return null; 1696 return repoPatchesConflictsRoute(pair, params.values.get(.start) orelse ""); 1697 } 1698 if (!params.only(&.{.search})) return null; 1699 const search_value = params.values.get(.search) orelse ""; 1700 if (std.meta.stringToEnum(evt.Patch.StatusKind, view)) |status| return withEncodedSearch(repoPatchesRoute(pair, status, ""), search_value); 1701 if (std.mem.eql(u8, view, "labels")) return withEncodedSearch(repoThreadLabelsRoute(.patch, pair), search_value); 1702 if (std.mem.eql(u8, view, "new")) return if (search_value.len == 0) repoThreadNewRoute(.patch, pair) else null; 1703 if (std.mem.eql(u8, view, "drafts")) return if (search_value.len == 0) repoPatchesDraftsRoute(pair) else null; 1704 return null; 1705 } 1706 if (std.mem.eql(u8, tab, "discussions")) { 1707 const word = params.scanTail(&segments) catch return null; 1708 const view = word orelse return if (params.only(&.{})) repoDiscussionsRoute(pair, "") else null; 1709 if (!params.only(&.{.search})) return null; 1710 const search_value = params.values.get(.search) orelse ""; 1711 if (std.mem.eql(u8, view, "recent")) return withEncodedSearch(repoDiscussionsRoute(pair, ""), search_value); 1712 if (std.mem.eql(u8, view, "labels")) return withEncodedSearch(repoThreadLabelsRoute(.discuss, pair), search_value); 1713 if (std.mem.eql(u8, view, "new")) return if (search_value.len == 0) repoThreadNewRoute(.discuss, pair) else null; 1714 return null; 1715 } 1716 if (std.mem.eql(u8, tab, "undo")) { 1717 const word = segments.next(); 1718 if (segments.next() != null) return null; 1719 const value = word orelse return repoUndoRoute(pair, null); 1720 if (std.mem.eql(u8, value, "clear")) return repoUndoClearRoute(pair); 1721 return repoUndoRoute(pair, std.fmt.parseInt(u64, value, 10) catch return null); 1722 } 1723 if (std.mem.eql(u8, tab, "events")) { 1724 const word = params.scanTail(&segments) catch return null; 1725 if (!params.only(&.{.moment})) return null; 1726 const view = if (word) |value| std.meta.stringToEnum(EventsView, value) orelse return null else .active; 1727 return repoEventsRoute(pair, view, null, "", params.moment() catch return null); 1728 } 1729 if (std.mem.startsWith(u8, tab, "event:")) { 1730 const event_id = tab["event:".len..]; 1731 const kind_segment = segments.next() orelse return null; 1732 if (!std.mem.startsWith(u8, kind_segment, "kind:")) return null; 1733 const kind = std.meta.stringToEnum(evt.EventKind, kind_segment["kind:".len..]) orelse return null; 1734 if (event_id.len == 0) return null; 1735 params.scanPairs(&segments) catch return null; 1736 if (!params.only(&.{.moment}) or segments.next() != null) return null; 1737 return repoEventsRoute(pair, .active, kind, event_id, params.moment() catch return null); 1738 } 1739 if (std.mem.eql(u8, tab, files_seg)) { 1740 params.scanPairs(&segments) catch return null; 1741 if (params.values.get(.patchrev)) |id| { 1742 if (!params.only(&.{ .line, .patchrev })) return null; 1743 return repoPatchRevFilesRoute(pair, id, pathValue(segments.rest()) orelse return null, params.line() orelse return null); 1744 } 1745 if (!params.only(&.{ .line, .branch, .tag, .object, .find })) return null; 1746 const line = params.line() orelse return null; 1747 const dir = pathValue(segments.rest()) orelse return null; 1748 const find = params.values.get(.find) orelse ""; 1749 const ref = params.ref() catch return null; 1750 // a find route's `path` names the selected result, so unlike a 1751 // directory path it doesn't need a ref beside it 1752 var route = if (ref) |r| 1753 repoFilesRoute(pair, r.kind, r.value, dir, line) orelse return null 1754 else if (dir.len == 0 or find.len != 0) 1755 repoFilesRoute(pair, null, "", "", line) orelse return null 1756 else 1757 return null; 1758 if (find.len != 0) { 1759 route.repo_files.find = Array(find_route_max_len).from(find) orelse return null; 1760 route.repo_files.path = Array(repo_route_max_len).from(dir) orelse return null; 1761 } 1762 return route; 1763 } 1764 if (std.mem.eql(u8, tab, commits_seg)) { 1765 params.scanPairs(&segments) catch return null; 1766 if (params.values.get(.merge)) |oid| { 1767 if (!params.only(&.{.merge}) or segments.next() != null) return null; 1768 return repoCommitsMergeRoute(pair, oid); 1769 } 1770 if (!params.only(&.{ .branch, .tag, .object, .base, .search, .from })) return null; 1771 const base_oid = params.values.get(.base) orelse ""; 1772 const search = params.values.get(.search) orelse ""; 1773 const from = params.values.get(.from) orelse ""; 1774 const ref = (params.ref() catch return null) orelse { 1775 if (search.len != 0 or from.len != 0 or segments.rest().len != 0) return null; 1776 return repoCommitsRoute(pair, null, "", base_oid); 1777 }; 1778 // the index covers branch and tag tips, so only they can be 1779 // searched or paged by commit; an object roots the list itself 1780 if ((search.len != 0 or from.len != 0) and ref.kind == .object) return null; 1781 var route = if (std.mem.eql(u8, segments.rest(), message_seg)) 1782 repoCommitMessageRoute(pair, ref.kind, ref.value, base_oid) orelse return null 1783 else if (segments.rest().len == 0) 1784 repoCommitsRoute(pair, ref.kind, ref.value, base_oid) orelse return null 1785 else 1786 return null; 1787 if (search.len != 0) route.repo_commits.search = Array(search_route_max_len).from(search) orelse return null; 1788 if (from.len != 0) route.repo_commits.from = Array(ref_route_max_len).from(from) orelse return null; 1789 return route; 1790 } 1791 if (std.mem.eql(u8, tab, "diff")) { 1792 params.scanPairs(&segments) catch return null; 1793 if (params.values.get(.patchrev)) |id| { 1794 if (!params.only(&.{ .start, .patchrev })) return null; 1795 return repoPatchRevDiffRoute(pair, id, params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1796 } 1797 if (!params.only(&.{ .start, .branch, .tag, .object, .base })) return null; 1798 const base_oid = params.values.get(.base) orelse return null; 1799 const start = params.start() orelse return null; 1800 const ref = params.ref() catch return null; 1801 const file = pathValue(segments.rest()) orelse return null; 1802 return repoDiffRoute(pair, if (ref) |r| r.kind else null, if (ref) |r| r.value else "", start, file, base_oid); 1803 } 1804 return null; // unknown sub-path 1805 } 1806 1807 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 1808 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1809 return switch (a) { 1810 .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()), 1811 .home_user => |a_t| std.meta.eql(a_t, b.home_user), 1812 .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()), 1813 .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, 1814 .user_repo_new => |a_name| std.mem.eql(u8, a_name.slice(), b.user_repo_new.slice()), 1815 .user_user_new => |a_name| std.mem.eql(u8, a_name.slice(), b.user_user_new.slice()), 1816 .user_user => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_user.name.slice()) and std.meta.eql(a_u.tab, b.user_user.tab), 1817 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 1818 a_f.ref_kind == b.repo_files.ref_kind and 1819 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 1820 std.mem.eql(u8, a_f.patchrev_id.slice(), b.repo_files.patchrev_id.slice()) and 1821 std.mem.eql(u8, a_f.find.slice(), b.repo_files.find.slice()) and 1822 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 1823 a_f.line == b.repo_files.line, 1824 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 1825 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 1826 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 1827 std.mem.eql(u8, a_c.base_oid.slice(), b.repo_commits.base_oid.slice()) and 1828 std.mem.eql(u8, a_c.search.slice(), b.repo_commits.search.slice()) and 1829 std.mem.eql(u8, a_c.from.slice(), b.repo_commits.from.slice()) and 1830 std.mem.eql(u8, a_c.merge.slice(), b.repo_commits.merge.slice()) and 1831 a_c.message == b.repo_commits.message, 1832 .repo_diff => |a_d| std.mem.eql(u8, a_d.name.slice(), b.repo_diff.name.slice()) and 1833 a_d.ref_or_oid == b.repo_diff.ref_or_oid and 1834 std.mem.eql(u8, a_d.value.slice(), b.repo_diff.value.slice()) and 1835 std.mem.eql(u8, a_d.base_oid.slice(), b.repo_diff.base_oid.slice()) and 1836 std.mem.eql(u8, a_d.patchrev_id.slice(), b.repo_diff.patchrev_id.slice()) and 1837 a_d.start == b.repo_diff.start and std.mem.eql(u8, a_d.path.slice(), b.repo_diff.path.slice()), 1838 .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()), 1839 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 1840 std.mem.eql(u8, a_i.search.slice(), b.repo_issues.search.slice()) and 1841 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 1842 std.mem.eql(u8, a_i.theirs.slice(), b.repo_issues.theirs.slice()) and 1843 a_i.view == b.repo_issues.view and 1844 a_i.comments_start == b.repo_issues.comments_start and 1845 std.mem.eql(u8, a_i.comment.slice(), b.repo_issues.comment.slice()), 1846 .repo_discussions => |a_t| std.mem.eql(u8, a_t.name.slice(), b.repo_discussions.name.slice()) and 1847 std.mem.eql(u8, a_t.search.slice(), b.repo_discussions.search.slice()) and 1848 std.mem.eql(u8, a_t.selected.slice(), b.repo_discussions.selected.slice()) and 1849 a_t.view == b.repo_discussions.view and 1850 a_t.comments_start == b.repo_discussions.comments_start and 1851 std.mem.eql(u8, a_t.comment.slice(), b.repo_discussions.comment.slice()), 1852 .repo_patches => |a_p| std.mem.eql(u8, a_p.name.slice(), b.repo_patches.name.slice()) and 1853 std.mem.eql(u8, a_p.search.slice(), b.repo_patches.search.slice()) and 1854 std.mem.eql(u8, a_p.selected.slice(), b.repo_patches.selected.slice()) and 1855 std.mem.eql(u8, a_p.theirs.slice(), b.repo_patches.theirs.slice()) and 1856 a_p.view == b.repo_patches.view and 1857 a_p.comments_start == b.repo_patches.comments_start and 1858 std.mem.eql(u8, a_p.comment.slice(), b.repo_patches.comment.slice()), 1859 .repo_undo => |a_u| std.mem.eql(u8, a_u.name.slice(), b.repo_undo.name.slice()) and 1860 a_u.index == b.repo_undo.index and 1861 a_u.clear == b.repo_undo.clear, 1862 .repo_events => |a_e| std.mem.eql(u8, a_e.name.slice(), b.repo_events.name.slice()) and 1863 a_e.view == b.repo_events.view and 1864 a_e.moment == b.repo_events.moment and 1865 a_e.kind == b.repo_events.kind and 1866 std.mem.eql(u8, a_e.selected.slice(), b.repo_events.selected.slice()), 1867 .repo_repo_new => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_repo_new.slice()), 1868 .repo_user_new => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_user_new.slice()), 1869 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 1870 .repo_roles => |a_r| std.mem.eql(u8, a_r.name.slice(), b.repo_roles.name.slice()) and 1871 a_r.start == b.repo_roles.start and 1872 std.mem.eql(u8, a_r.user.slice(), b.repo_roles.user.slice()), 1873 .repo_user => |a_u| std.mem.eql(u8, a_u.name.slice(), b.repo_user.name.slice()) and std.meta.eql(a_u.tab, b.repo_user.tab), 1874 .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()), 1875 .fork_diff => |a_d| std.mem.eql(u8, a_d.fork.name.slice(), b.fork_diff.fork.name.slice()) and 1876 std.mem.eql(u8, a_d.fork.id.slice(), b.fork_diff.fork.id.slice()) and 1877 std.mem.eql(u8, a_d.oid.slice(), b.fork_diff.oid.slice()) and 1878 std.mem.eql(u8, a_d.base_oid.slice(), b.fork_diff.base_oid.slice()) and 1879 a_d.start == b.fork_diff.start and std.mem.eql(u8, a_d.path.slice(), b.fork_diff.path.slice()), 1880 .fork_files => |a_f| std.mem.eql(u8, a_f.fork.name.slice(), b.fork_files.fork.name.slice()) and 1881 std.mem.eql(u8, a_f.fork.id.slice(), b.fork_files.fork.id.slice()) and 1882 std.mem.eql(u8, a_f.oid.slice(), b.fork_files.oid.slice()) and 1883 std.mem.eql(u8, a_f.find.slice(), b.fork_files.find.slice()) and 1884 std.mem.eql(u8, a_f.path.slice(), b.fork_files.path.slice()) and 1885 a_f.line == b.fork_files.line, 1886 .fork_commits => |a_c| std.mem.eql(u8, a_c.fork.name.slice(), b.fork_commits.fork.name.slice()) and 1887 std.mem.eql(u8, a_c.fork.id.slice(), b.fork_commits.fork.id.slice()) and 1888 std.mem.eql(u8, a_c.oid.slice(), b.fork_commits.oid.slice()) and 1889 a_c.message == b.fork_commits.message, 1890 .fork_repo_new => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_repo_new.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_repo_new.id.slice()), 1891 .fork_user_new => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_user_new.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_user_new.id.slice()), 1892 .fork_user => |a_u| std.mem.eql(u8, a_u.fork.name.slice(), b.fork_user.fork.name.slice()) and 1893 std.mem.eql(u8, a_u.fork.id.slice(), b.fork_user.fork.id.slice()) and 1894 std.meta.eql(a_u.tab, b.fork_user.tab), 1895 else => true, 1896 }; 1897 } 1898 1899 // true when following a link from `b` to `a` (both repo routes) should reload 1900 // rather than stay in page 1901 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 1902 if (a.parent() != .repo or b.parent() != .repo) return false; 1903 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 1904 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 1905 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 1906 return !a.eql(b); 1907 } 1908 1909 pub fn forkPageChanged(a: RoutablePage, b: RoutablePage) bool { 1910 return a.parent() == .fork and b.parent() == .fork and !a.eql(b); 1911 } 1912 1913 // true when `a` and `b` are the same user paginated to a different repos 1914 // window or search. switching between a user's tabs is in-page, so only a 1915 // changed `start` or search on the same list navigates. 1916 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 1917 return switch (a) { 1918 .user_repos => |aa| switch (b) { 1919 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and !a.eql(b), 1920 else => false, 1921 }, 1922 .user_forks => |aa| switch (b) { 1923 .user_forks => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1924 else => false, 1925 }, 1926 .user_user => |aa| switch (b) { 1927 .user_user => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and !a.eql(b), 1928 else => false, 1929 }, 1930 else => false, 1931 }; 1932 } 1933 1934 // true when `a` and `b` are the same home list tab paginated to a different 1935 // window or search. switching tabs is in-page, so only a changed `start` or 1936 // search on the same tab navigates. 1937 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 1938 return switch (a) { 1939 .home_users => switch (b) { 1940 .home_users => !a.eql(b), 1941 else => false, 1942 }, 1943 .home_user => switch (b) { 1944 .home_user => !a.eql(b), 1945 else => false, 1946 }, 1947 else => false, 1948 }; 1949 } 1950 }; 1951 1952 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 1953 fn isUnreserved(c: u8) bool { 1954 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 1955 } 1956 1957 // percent-encode a ref name for use as a single url path segment. 1958 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 1959 var out: std.Io.Writer.Allocating = .init(aa); 1960 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 1961 return out.written(); 1962 } 1963 1964 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 1965 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1966 return struct { 1967 pub const hex_len = hash.hexLen(repo_opts.hash); 1968 1969 // the concrete ref/oid (a null request resolves to the default branch) 1970 ref_or_oid: RoutablePage.RefOrOid, 1971 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 1972 // the route layer — which splits on '/' — stores them encoded). duped 1973 // into `aa`. 1974 value: []const u8, 1975 // the commit oid it points at 1976 oid: [hex_len]u8, 1977 1978 const Self = @This(); 1979 1980 // resolve a requested ref/oid (null = the repo's default branch) to a 1981 // concrete ref/oid plus the commit oid it points at. `requested_value` is 1982 // the url-encoded form (as it appears in the route). null when it doesn't 1983 // resolve (an unknown branch/tag, or a malformed/unknown oid). 1984 pub fn init( 1985 repo: *rp.Repo(repo_kind, repo_opts), 1986 io: std.Io, 1987 aa: std.mem.Allocator, 1988 requested_ref_or_oid: ?RoutablePage.RefOrOid, 1989 requested_value: []const u8, 1990 ) !?Self { 1991 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 1992 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 1993 var value: []const u8 = if (requested_ref_or_oid == null) 1994 requested_value 1995 else 1996 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 1997 // no ref named: fall back to HEAD's branch (or its oid when detached). 1998 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1999 if (requested_ref_or_oid == null) { 2000 if (repo.head(io, &head_buf)) |head| switch (head) { 2001 .ref => |r| { 2002 ref_or_oid = .branch; 2003 value = r.name; 2004 }, 2005 .oid => |o| { 2006 ref_or_oid = .object; 2007 value = o; 2008 }, 2009 } else |_| {} 2010 } 2011 2012 var oid: [hex_len]u8 = undefined; 2013 switch (ref_or_oid) { 2014 .object => { 2015 if (value.len != hex_len) return null; 2016 @memcpy(&oid, value); 2017 }, 2018 .branch, .tag => { 2019 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 2020 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 2021 }, 2022 } 2023 // store url-encoded so the route layer can hold the value verbatim. 2024 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 2025 } 2026 }; 2027 } 2028 2029 // who a repo page is built for, which picks the actions it shows 2030 pub const Viewer = struct { 2031 role: evt.Repo.Role, 2032 email: []const u8, 2033 2034 // the session's viewer of a repo it holds `role` in, or null when logged 2035 // out. local mode owns everything. 2036 pub fn init(session: *Session, arena: *std.heap.ArenaAllocator, role: evt.Repo.Role) !?Viewer { 2037 if (session.data.host_kind == .local) return .{ .role = .owner, .email = "" }; 2038 const user_id = session.userId() orelse return null; 2039 const moment = session.haxy_moment orelse unreachable; 2040 const user = (try activeUser(moment, arena, user_id)) orelse return null; 2041 return .{ .role = role, .email = user.event.email }; 2042 } 2043 }; 2044 2045 // a repo a page shows, and what its viewer may do there 2046 pub const RepoHandle = struct { 2047 location: RoutablePage.RepoLocation, 2048 viewer: ?Viewer = null, 2049 2050 pub fn dupe(self: RepoHandle, allocator: std.mem.Allocator) !RepoHandle { 2051 var copy = self; 2052 copy.location = try self.location.dupe(allocator); 2053 return copy; 2054 } 2055 2056 // pushing, and seeing the events and undo tabs. local mode is always owner. 2057 pub fn canWrite(self: RepoHandle) bool { 2058 const viewer = self.viewer orelse return false; 2059 return viewer.role.atLeast(.write); 2060 } 2061 2062 pub fn canUndo(self: RepoHandle) bool { 2063 const viewer = self.viewer orelse return false; 2064 return viewer.role == .owner; 2065 } 2066 }; 2067 2068 // where a repo page reads its on-disk repo from 2069 pub const RepoSource = struct { 2070 path: []const u8, 2071 repo_kind: rp.RepoKind, 2072 // the user's global git config, so local mode picks up their identity and 2073 // their signing and ssh settings. null on the server paths, which must not 2074 // read whoever runs the server. 2075 global_config_path: ?[]const u8 = null, 2076 2077 pub fn localInitOpts(self: RepoSource) rp.InitOpts { 2078 return .{ .path = self.path, .global_config_path = self.global_config_path }; 2079 } 2080 2081 pub fn hasBranch(self: RepoSource, io: std.Io, allocator: std.mem.Allocator, branch: []const u8) !bool { 2082 if (!xit.ref.validateName(branch)) return false; 2083 return switch (self.repo_kind) { 2084 inline else => |repo_kind| blk: { 2085 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 2086 defer any_repo.deinit(io, allocator); 2087 break :blk switch (any_repo) { 2088 inline else => |*repo| (try repo.readRef(io, .{ .kind = .head, .name = branch })) != null, 2089 }; 2090 }, 2091 }; 2092 } 2093 2094 // whether the actor may change the event `id` names 2095 pub fn canModify( 2096 self: RepoSource, 2097 io: std.Io, 2098 allocator: std.mem.Allocator, 2099 actor: Actor, 2100 kind: evt.EventKind, 2101 id: *const [evt.event_id_size]u8, 2102 ) !bool { 2103 // a role that may change an event with no author needs no read 2104 if (actor.role.canModify(actor.author.email, null)) return true; 2105 2106 var arena = std.heap.ArenaAllocator.init(allocator); 2107 defer arena.deinit(); 2108 return switch (self.repo_kind) { 2109 inline else => |repo_kind| blk: { 2110 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 2111 defer any_repo.deinit(io, allocator); 2112 break :blk switch (any_repo) { 2113 inline else => |*repo| actor.role.canModify(actor.author.email, try evt.readAuthorEmail(repo_kind, repo.self_repo_opts, io, allocator, &arena, repo, kind, id)), 2114 }; 2115 }, 2116 }; 2117 } 2118 }; 2119 2120 // who a write acts as and the role they hold in its repo 2121 // the thread button that changes the viewer's notifications, named by its label 2122 pub const NotifyAction = enum { subscribe, mute }; 2123 2124 pub const Actor = struct { 2125 author: evt.CommitAuthor, 2126 // local mode has no accounts, so it has no user id 2127 user_id: ?[evt.event_id_size]u8 = null, 2128 role: evt.Repo.Role, 2129 // the repo owner's id, which local mode lacks 2130 repo_user_id: ?[evt.event_id_size]u8 = null, 2131 }; 2132 2133 pub const Authorization = union(enum) { 2134 actor: Actor, 2135 login_required, 2136 repo_not_found, 2137 forbidden, 2138 }; 2139 2140 // the account `user_id` names, or null once it has been removed. a user id is 2141 // checked here wherever one enters, so the role lookups can trust theirs. 2142 pub fn activeUser( 2143 moment: evt.AdminDB.HashMap(.read_only), 2144 arena: *std.heap.ArenaAllocator, 2145 user_id: [evt.event_id_size]u8, 2146 ) !?evt.User.Record { 2147 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id)) orelse return null; 2148 return if (user.removed) null else user; 2149 } 2150 2151 // what the typed assignee resolves to 2152 pub const AssigneeLookup = union(enum) { 2153 email: []const u8, 2154 failure: Session.FormFeedback.AssignmentFailure, 2155 }; 2156 2157 // the email `text` assigns: itself when it holds an @, else the email of the 2158 // user it names. a null moment is local mode, which has no users to look up. 2159 pub fn assigneeEmail(moment_maybe: ?evt.AdminDB.HashMap(.read_only), arena: *std.heap.ArenaAllocator, text: []const u8) !AssigneeLookup { 2160 const trimmed = std.mem.trim(u8, text, " \t\r\n"); 2161 if (std.mem.indexOfScalar(u8, trimmed, '@') != null) return .{ .email = trimmed }; 2162 const moment = moment_maybe orelse return .{ .failure = .not_an_email }; 2163 const user_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, trimmed)) orelse return .{ .failure = .not_found }; 2164 const user = (try activeUser(moment, arena, user_id)) orelse return .{ .failure = .not_found }; 2165 return .{ .email = user.event.email }; 2166 } 2167 2168 // whether the user may write to the repo `identity` names with at least 2169 // `min_role`, and to the `thread_kind` tab when there is one 2170 pub fn authorizeUser( 2171 io: std.Io, 2172 moment: evt.AdminDB.HashMap(.read_only), 2173 arena: *std.heap.ArenaAllocator, 2174 users_dir: []const u8, 2175 user_id: [evt.event_id_size]u8, 2176 identity: []const u8, 2177 min_role: evt.Repo.Role, 2178 thread_kind: ?evt.EventKind, 2179 ) !Authorization { 2180 const user = (try activeUser(moment, arena, user_id)) orelse return .login_required; 2181 2182 const owner_repo = evt.parseOwnerRepoPath(identity) orelse return .repo_not_found; 2183 const repo = (try evt.readRepoByOwnerAndName(io, arena.child_allocator, arena, moment, users_dir, owner_repo.owner, owner_repo.name, user_id)) orelse return .repo_not_found; 2184 // a repo the user can't read doesn't exist to them 2185 const role = repo.role orelse return .repo_not_found; 2186 if (!role.atLeast(min_role)) return .forbidden; 2187 if (thread_kind) |kind| { 2188 // a tab that is off doesn't exist 2189 const tab_role = repo.repo.event.threadRole(kind) orelse return .repo_not_found; 2190 if (!role.atLeast(tab_role)) return .forbidden; 2191 } 2192 return .{ .actor = .{ 2193 .author = .{ .name = user.event.name, .email = user.event.email }, 2194 .user_id = user_id, 2195 .role = role, 2196 .repo_user_id = try evt.parseEventId(&repo.repo.event.user_id), 2197 } }; 2198 } 2199 2200 // events created in local mode have no account behind them, so the commit 2201 // author comes from the repo's config, which the global config feeds into. 2202 pub const local_author_fallback = evt.CommitAuthor{ .name = "haxy", .email = "user@haxy" }; 2203 2204 pub fn localAuthor( 2205 src: RepoSource, 2206 io: std.Io, 2207 allocator: std.mem.Allocator, 2208 arena: *std.heap.ArenaAllocator, 2209 ) !evt.CommitAuthor { 2210 switch (src.repo_kind) { 2211 inline else => |repo_kind| { 2212 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()) catch return local_author_fallback; 2213 defer any_repo.deinit(io, allocator); 2214 2215 switch (any_repo) { 2216 inline else => |*repo| { 2217 var config = try repo.listConfig(io, allocator); 2218 defer config.deinit(); 2219 2220 const user = config.sections.get("user") orelse return local_author_fallback; 2221 const name = user.get("name") orelse return local_author_fallback; 2222 const email = user.get("email") orelse return local_author_fallback; 2223 return .{ 2224 .name = try arena.allocator().dupe(u8, name), 2225 .email = try arena.allocator().dupe(u8, email), 2226 }; 2227 }, 2228 } 2229 }, 2230 } 2231 } 2232 2233 // per-connection mutable state. each SSH session / web session / local TUI 2234 // run gets its own. 2235 pub const Session = struct { 2236 data: Data = .{}, 2237 // session-lifetime allocations: login/user_id, persisted prefs, queued 2238 // actions. lives as long as the connection. 2239 arena: *std.heap.ArenaAllocator, 2240 // current page's allocations: page data and the page-scoped strings widgets 2241 // build (form actions, links). owned by Nav, swapped on each navigation, so 2242 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 2243 // where a render is one-shot, this points at the same arena as `arena`. 2244 page_arena: *std.heap.ArenaAllocator, 2245 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 2246 // filesystem io and the path to <server>/repos, for opening on-disk repos 2247 // during page construction. both null on wasm, which has no filesystem and 2248 // rebuilds pages from the serialized snapshot rather than from disk. 2249 io: ?std.Io = null, 2250 admin_repo: ?*rp.Repo(.xit, evt.admin_repo_opts) = null, 2251 users_dir: ?[]const u8 = null, 2252 // the notify worker's queue, null where no worker runs 2253 dirty_repos: ?*notify.DirtyRepos = null, 2254 // the single on-disk repo this session views, when running in local mode 2255 // (haxy invoked with no arguments inside a repo). null on the server paths, 2256 // which resolve repos from the admin db instead. 2257 local: ?RepoSource = null, 2258 // focus id -> the live TextInput, refreshed each frame by the views that own 2259 // inputs. web/wasm form handling looks widgets up here by focus id. 2260 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput) = .empty, 2261 back: enum { unavailable, available, requested } = .unavailable, 2262 refresh_requested: bool = false, // set by input (ctrl+r) 2263 // a requested forward navigation. set this (via navigate) to move to a new 2264 // page; Nav.sync builds it and then copies it into current_page, clearing 2265 // this back to null. setting current_page directly only updates the url and 2266 // does not navigate. 2267 next_page: ?RoutablePage = null, 2268 is_terminal: bool = false, // true on remote SSH and local TUI 2269 color: bool = true, // false when NO_COLOR is set, which also hides the art 2270 // the terminal's reported default background, which the art blends toward 2271 terminal_background: ?Grid.Color.Rgb = null, 2272 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 2273 // url. null on the web itself (no footer there). 2274 web_port: ?u16 = null, 2275 // a host operation requested by a widget 2276 host_request: ?HostRequest = null, 2277 quit_requested: bool = false, 2278 2279 pub const HostRequest = union(enum) { 2280 show_copyable_text: []const u8, 2281 sync_events, 2282 undo: RoutablePage.RepoUndoRoute, 2283 }; 2284 2285 pub const FormFeedback = union(enum) { 2286 pub const LoginFailure = enum { unknown_user, wrong_password }; 2287 pub const ThreadFailure = enum { required_title }; 2288 pub const PatchFailure = enum { 2289 required_title, 2290 invalid_target_branch, 2291 invalid_source_branch, 2292 same_branch, 2293 unrelated_branches, 2294 2295 pub fn fromError(err: anyerror) ?PatchFailure { 2296 return switch (err) { 2297 error.InvalidTargetBranch => .invalid_target_branch, 2298 error.InvalidSourceBranch => .invalid_source_branch, 2299 error.SameBranch => .same_branch, 2300 error.UnrelatedBranches => .unrelated_branches, 2301 else => null, 2302 }; 2303 } 2304 }; 2305 2306 pub const RepoFailure = enum { 2307 required_name, 2308 invalid_name, 2309 name_taken, 2310 2311 pub fn fromError(err: anyerror) ?RepoFailure { 2312 return switch (err) { 2313 error.NameEmpty => .required_name, 2314 error.NameTooLong, error.InvalidName => .invalid_name, 2315 error.NameTaken => .name_taken, 2316 else => null, 2317 }; 2318 } 2319 }; 2320 2321 pub const RolesFailure = enum { not_found, already_added }; 2322 2323 pub const AssignmentFailure = enum { not_found, not_an_email, already_assigned }; 2324 2325 pub const KeyFailure = enum { 2326 invalid, 2327 unsupported, 2328 already_added, 2329 2330 pub fn fromError(err: anyerror) ?KeyFailure { 2331 return switch (err) { 2332 error.InvalidKey => .invalid, 2333 error.UnsupportedKey => .unsupported, 2334 error.KeyAdded => .already_added, 2335 else => null, 2336 }; 2337 } 2338 }; 2339 2340 pub const UserFailure = enum { 2341 required_name, 2342 invalid_name, 2343 name_taken, 2344 required_email, 2345 email_taken, 2346 required_password, 2347 password_mismatch, 2348 wrong_password, 2349 2350 pub fn fromError(err: anyerror) ?UserFailure { 2351 return switch (err) { 2352 error.NameEmpty => .required_name, 2353 error.NameTooShort, error.NameTooLong, error.InvalidName => .invalid_name, 2354 error.NameTaken => .name_taken, 2355 error.EmailEmpty => .required_email, 2356 error.EmailTaken => .email_taken, 2357 error.PasswordEmpty => .required_password, 2358 error.PasswordMismatch => .password_mismatch, 2359 error.WrongPassword => .wrong_password, 2360 else => null, 2361 }; 2362 } 2363 }; 2364 2365 login: struct { 2366 failure: LoginFailure, 2367 username: []const u8, 2368 }, 2369 user: struct { 2370 failure: UserFailure, 2371 // passwords are never retained 2372 fields: struct { 2373 name: []const u8, 2374 email: []const u8, 2375 }, 2376 }, 2377 user_settings: struct { 2378 failure: UserFailure, 2379 // passwords are never retained 2380 fields: struct { email: []const u8 }, 2381 }, 2382 user_keys: struct { 2383 failure: KeyFailure, 2384 key: []const u8, 2385 }, 2386 repo: struct { 2387 failure: RepoFailure, 2388 fields: struct { 2389 name: []const u8, 2390 description: []const u8, 2391 hash: hash.HashKind, 2392 access: evt.Repo.Access, 2393 }, 2394 }, 2395 repo_settings: struct { 2396 failure: RepoFailure, 2397 fields: struct { 2398 name: []const u8, 2399 description: []const u8, 2400 access: evt.Repo.Access, 2401 discuss_role: ?evt.Repo.Role, 2402 issue_role: ?evt.Repo.Role, 2403 patch_role: ?evt.Repo.Role, 2404 }, 2405 }, 2406 repo_roles: struct { 2407 failure: RolesFailure, 2408 name: []const u8, 2409 }, 2410 assignment: struct { 2411 failure: AssignmentFailure, 2412 text: []const u8, 2413 }, 2414 issue: struct { 2415 failure: ThreadFailure, 2416 fields: ?struct { 2417 title: []const u8, 2418 labels: []const u8, 2419 description: []const u8, 2420 } = null, 2421 }, 2422 patch: struct { 2423 failure: PatchFailure, 2424 fields: ?struct { 2425 title: []const u8, 2426 labels: []const u8, 2427 description: []const u8, 2428 target_branch: []const u8, 2429 source_branch: []const u8 = "", 2430 source_kind: Repo.Patches.Source.Kind = .fork, 2431 } = null, 2432 }, 2433 discussion: struct { 2434 failure: ThreadFailure, 2435 fields: ?struct { 2436 title: []const u8, 2437 labels: []const u8, 2438 description: []const u8, 2439 } = null, 2440 }, 2441 }; 2442 2443 const Self = @This(); 2444 2445 // serializable data sent down to web client 2446 pub const Data = struct { 2447 // the server's reference time for relative timestamps on this page. 2448 now: i64 = 0, 2449 user_id: ?[]const u8 = null, 2450 // the logged-in user's name and email, for the views that show them 2451 user_name: ?[]const u8 = null, 2452 user_email: ?[]const u8 = null, 2453 // the logged-in user's unread notifications, refreshed on each page build 2454 unread_notifs: u64 = 0, 2455 // a failed form retained long enough to render it for correction 2456 form_feedback: ?FormFeedback = null, 2457 // a transient local event-sync error 2458 sync_failure: ?[]const u8 = null, 2459 // a failed terminal undo retained on the current tab 2460 undo_failure: ?[]const u8 = null, 2461 current_page: RoutablePage = .default, 2462 // the selected ANSI art. the server serializes one image for WASM so 2463 // the browser does not need the whole build-generated collection. 2464 ansi_art: []const u8 = "", 2465 // the bound git-service ports. absent in local mode, where remote urls 2466 // are not shown. 2467 git_http_port: ?u16 = null, 2468 git_ssh_port: ?u16 = null, 2469 git_ssh_prefix: []const u8 = "", 2470 // whether this session views a local repo or the server. unlike `local` 2471 // (the host-side filesystem source) this travels in the snapshot, so 2472 // the wasm side also parses elided link urls and hides the multi-user 2473 // chrome. 2474 host_kind: evt.HostKind = .server, 2475 }; 2476 2477 pub fn init( 2478 arena: *std.heap.ArenaAllocator, 2479 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2480 data: Data, 2481 ) !Self { 2482 var session = Self{ 2483 .data = data, 2484 .arena = arena, 2485 // until a host swaps in a page-scoped arena (Nav does this), page 2486 // allocations land in the session arena. on the web path that's the 2487 // intended behavior, since the whole arena is per-request. 2488 .page_arena = arena, 2489 .admin_repo = repo, 2490 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 2491 }; 2492 try session.loadUser(); 2493 return session; 2494 } 2495 2496 // web views share the page's reference time; terminals use the live clock. 2497 pub fn nowSeconds(self: *const Self) !i64 { 2498 if (builtin.target.cpu.arch == .wasm32 or !self.is_terminal) return self.data.now; 2499 const io = self.io orelse return error.NoClock; 2500 return std.math.cast(i64, std.Io.Timestamp.now(io, .real).toSeconds()) orelse error.TimestampOutOfRange; 2501 } 2502 2503 // load the logged-in user's name and persisted preferences from the db 2504 pub fn loadUser(self: *Self) !void { 2505 const user_id = self.userId() orelse return; 2506 const moment = self.haxy_moment orelse return; 2507 const user = (try activeUser(moment, self.arena, user_id)) orelse return self.logOut(); 2508 self.data.user_name = user.event.name; 2509 self.data.user_email = user.event.email; 2510 } 2511 2512 // forget the logged-in user, on a logout or once their account is removed 2513 pub fn logOut(self: *Self) void { 2514 self.data.user_id = null; 2515 self.data.user_name = null; 2516 self.data.user_email = null; 2517 } 2518 2519 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 2520 const bytes = self.data.user_id orelse return null; 2521 if (bytes.len != evt.event_id_size) return null; 2522 var id: [evt.event_id_size]u8 = undefined; 2523 @memcpy(&id, bytes); 2524 return id; 2525 } 2526 2527 pub fn formFeedback(self: *const Self, comptime tag: std.meta.Tag(FormFeedback)) ?@FieldType(FormFeedback, @tagName(tag)) { 2528 const feedback = self.data.form_feedback orelse return null; 2529 if (std.meta.activeTag(feedback) != tag) return null; 2530 return @field(feedback, @tagName(tag)); 2531 } 2532 2533 pub fn clearFormFeedback(self: *Self) void { 2534 self.data.form_feedback = null; 2535 } 2536 2537 // the actor for a write to the repo `identity` names, or null when the 2538 // session may not write there with at least `min_role` or in the 2539 // `thread_kind` tab. local mode always writes. 2540 pub fn authorize(self: *Self, identity: []const u8, min_role: evt.Repo.Role, thread_kind: ?evt.EventKind) !?Actor { 2541 if (self.data.host_kind == .local) { 2542 const author = blk: { 2543 const src = self.local orelse break :blk local_author_fallback; 2544 const io = self.io orelse break :blk local_author_fallback; 2545 break :blk try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 2546 }; 2547 return .{ .author = author, .role = .owner }; 2548 } 2549 const user_id = self.userId() orelse return null; 2550 // the session's moment predates this input, so read the roles afresh 2551 const admin_repo = self.admin_repo orelse return null; 2552 const io = self.io orelse return null; 2553 const users_dir = self.users_dir orelse return null; 2554 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2555 return switch (try authorizeUser(io, moment, self.page_arena, users_dir, user_id, identity, min_role, thread_kind)) { 2556 .actor => |actor| actor, 2557 .login_required, .repo_not_found, .forbidden => null, 2558 }; 2559 } 2560 2561 // where this session's event writes go. a server session always has a users dir and an admin repo 2562 pub fn eventHost(self: *const Self) evt.Host { 2563 return switch (self.data.host_kind) { 2564 .local => .local, 2565 .server => .{ .server = .{ 2566 .users_dir = self.users_dir orelse unreachable, 2567 .dirty_repos = self.dirty_repos, 2568 .admin_repo_path = (self.admin_repo orelse unreachable).core.work_path, 2569 } }, 2570 }; 2571 } 2572 2573 // reload the moment from the admin repo and log out a removed user 2574 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 2575 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 2576 self.haxy_moment = moment; 2577 2578 const user_id = self.userId() orelse return; 2579 var arena = std.heap.ArenaAllocator.init(allocator); 2580 defer arena.deinit(); 2581 if (try activeUser(moment, &arena, user_id) == null) self.logOut(); 2582 } 2583 2584 // request a forward navigation to `route`; the host consumes next_page 2585 // (Nav.sync on the terminal, the wasm tick on the web). 2586 pub fn navigate(self: *Session, route: RoutablePage) !void { 2587 self.clearFormFeedback(); 2588 self.next_page = route; 2589 } 2590 }; 2591 2592 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2593 var nav = try Nav.init(allocator, session); 2594 defer nav.deinit(allocator); 2595 2596 var terminal = try term.Terminal.init(io, allocator); 2597 terminal.render_state.no_color = !session.color; 2598 var terminal_live = true; 2599 defer if (terminal_live) terminal.deinit(io); 2600 2601 // set term as active so it will be properly cooked 2602 // when a panic/segfault happens 2603 term.setActive(&terminal); 2604 defer term.setActive(null); 2605 2606 try terminal.queryBackground(); 2607 2608 while (!terminal.shouldQuit()) { 2609 const grid_changed = try terminal.render(&nav.root); 2610 2611 // process any inputs. 2612 // 2613 // if the grid didn't change, then first do a blocking 2614 // read, so the thread will sleep until further input. 2615 // after that, all remaining reads are non-blocking so 2616 // we can process the rest of the queued inputs. 2617 // 2618 // if the grid *did* change, then only do non-blocking 2619 // reads. we do not want to sleep the thread because 2620 // there may be an animation that requires more looping. 2621 var blocking = !grid_changed; 2622 while (try terminal.readKey(io, blocking)) |key| { 2623 blocking = false; 2624 try inputKey(allocator, &nav.root, key, session); 2625 } 2626 2627 if (session.host_request) |request| { 2628 session.host_request = null; 2629 switch (request) { 2630 .show_copyable_text => |copyable_text| { 2631 term.setActive(null); 2632 terminal.deinit(io); 2633 terminal_live = false; 2634 showCopyableText(io, copyable_text) catch |show_err| { 2635 terminal = try term.Terminal.init(io, allocator); 2636 terminal_live = true; 2637 term.setActive(&terminal); 2638 return show_err; 2639 }; 2640 terminal = try term.Terminal.init(io, allocator); 2641 terminal.render_state.no_color = !session.color; 2642 terminal_live = true; 2643 term.setActive(&terminal); 2644 }, 2645 .undo => |target| Repo.Undo.handleRequest(allocator, session, target), 2646 .sync_events => { 2647 try nav.root.build(allocator, .{ 2648 .min_size = .{ .width = null, .height = null }, 2649 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2650 }, nav.root.getFocus()); 2651 _ = try terminal.render(&nav.root); 2652 try Repo.Events.performSync(allocator, session); 2653 }, 2654 } 2655 } 2656 2657 // reload so the next navigation builds its page from the current moment 2658 if (repo_maybe) |repo| try session.reloadMoment(allocator, repo); 2659 2660 // reconcile navigation: forward to a new page, or back on escape. 2661 // the terminal background can change at any time, so recheck it on each page 2662 if (try nav.sync(allocator, session)) try terminal.queryBackground(); 2663 2664 // the quit button (on the quit tab) asks the host to tear down. 2665 if (session.quit_requested) terminal.requestQuit(); 2666 2667 try nav.root.build(allocator, .{ 2668 .min_size = .{ .width = null, .height = null }, 2669 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2670 }, nav.root.getFocus()); 2671 } 2672 } 2673 2674 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2675 const tty: ?std.Io.File = if (builtin.target.os.tag == .windows) 2676 null 2677 else 2678 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2679 defer if (tty) |file| file.close(io); 2680 2681 const input = if (tty) |file| file else std.Io.File.stdin(); 2682 const output = if (tty) |file| file else std.Io.File.stdout(); 2683 2684 var write_buf: [1024]u8 = undefined; 2685 var writer = output.writer(io, &write_buf); 2686 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}); 2687 try writer.interface.flush(); 2688 2689 var read_buf: [1]u8 = undefined; 2690 var reader = input.reader(io, &read_buf); 2691 while (true) { 2692 const byte = try reader.interface.takeByte(); 2693 if (byte == '\r' or byte == '\n') return; 2694 } 2695 } 2696 2697 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2698 const root_focus = root.getFocus(); 2699 const focused_before = root_focus.grandchild_id; 2700 var activate_in_page_link = false; 2701 switch (key) { 2702 // request a navigation pop; the host's Nav.sync goes back a page, or 2703 // quits when there's no history left. 2704 .escape => { 2705 session.back = .requested; 2706 return; 2707 }, 2708 .ctrl => |letter| switch (letter) { 2709 'r' => { 2710 session.refresh_requested = true; 2711 return; 2712 }, 2713 else => try root.input(allocator, key, root_focus), 2714 }, 2715 .enter => { 2716 if (root_focus.grandchild_id) |gid| { 2717 // follow a cross-page link 2718 if (crossPageLink(root_focus, gid, session.data)) |route| { 2719 return session.navigate(route); 2720 } 2721 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2722 } 2723 activate_in_page_link = true; 2724 try root.input(allocator, key, root_focus); 2725 }, 2726 .mouse => |mouse| { 2727 if (mouse.button == .left) { 2728 if (cellPageLink(root, mouse.x, mouse.y, session.data)) |route| return session.navigate(route); 2729 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2730 const focus_id = hit.id; 2731 if (hit.kind == .background) { 2732 root_focus.setFocus(focus_id); 2733 return; 2734 } 2735 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2736 if (session.back == .available) session.back = .requested; 2737 return; 2738 } 2739 // follow a cross-page link 2740 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2741 return session.navigate(route); 2742 } 2743 if (currentPageLink(root_focus, focus_id, session.data)) return; 2744 if (rawLink(root_focus, focus_id)) |url| { 2745 if (!mouse.ctrl) try requestRawLink(session, url); 2746 return; 2747 } 2748 root_focus.setFocus(focus_id); 2749 activate_in_page_link = true; 2750 } 2751 // forward the press into the widget tree so buttons (and any 2752 // future click-aware widgets) can react. widgets that don't 2753 // care about presses ignore it. 2754 try root.input(allocator, key, root_focus); 2755 } else { 2756 try root.input(allocator, key, root_focus); 2757 } 2758 }, 2759 .event => |event| switch (event) { 2760 .background => |rgb| session.terminal_background = rgb, 2761 .resize => try root.input(allocator, key, root_focus), 2762 }, 2763 // a scroll tick goes to the pane under the pointer, focusing it first. 2764 // from outside any pane (the tabs, a sub-header) or from a text input 2765 // it moves like the arrows instead, so scrolling up from a search box 2766 // reaches the header and stops there. 2767 .scroll_up, .scroll_down => |position| { 2768 if (focusedPane(root_focus)) |pane| if (scrollUnder(root_focus, position)) |under| { 2769 if (under != pane) root_focus.setFocus(under); 2770 }; 2771 try root.input(allocator, key, root_focus); 2772 }, 2773 else => try root.input(allocator, key, root_focus), 2774 } 2775 2776 const focused_after = root_focus.grandchild_id; 2777 if (!activate_in_page_link and focused_before == focused_after) return; 2778 const focus_id = focused_after orelse return; 2779 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2780 } 2781 2782 // the innermost vertically scrolling widget laid out under `position` 2783 fn scrollUnder(root_focus: *const Focus, position: xitui.input.Position) ?usize { 2784 var found: ?usize = null; 2785 var found_area: usize = std.math.maxInt(usize); 2786 var iter = root_focus.children.iterator(); 2787 while (iter.next()) |entry| { 2788 const info = entry.value_ptr.focus.scroll orelse continue; 2789 if (info.direction == .horiz) continue; 2790 const r = entry.value_ptr.rect; 2791 if (position.x < r.x or position.y < r.y or position.x >= r.x + r.size.width or position.y >= r.y + r.size.height) continue; 2792 // a nested scroll lies within its parent, so the smallest is innermost 2793 const area = r.size.width * r.size.height; 2794 if (area < found_area) { 2795 found = entry.key_ptr.*; 2796 found_area = area; 2797 } 2798 } 2799 return found; 2800 } 2801 2802 // the innermost vertically scrolling widget the focus lies in, unless the 2803 // focused widget is a text input 2804 fn focusedPane(root_focus: *const Focus) ?usize { 2805 var current = root_focus.grandchild_id orelse return null; 2806 switch ((root_focus.children.get(current) orelse return null).focus.kind) { 2807 .text_input, .text_input_password, .text_area => return null, 2808 else => {}, 2809 } 2810 while (root_focus.children.get(current)) |child| { 2811 if (child.focus.scroll) |info| if (info.direction != .horiz) return current; 2812 current = child.parent_id; 2813 } 2814 return null; 2815 } 2816 2817 // a link to bytes the server serves directly rather than to a page route, so 2818 // it never resolves to a RoutablePage 2819 pub const raw_link_prefix = "ax:"; 2820 2821 // a route already loaded into the current page; focusing it changes the url 2822 // without rebuilding the page 2823 pub const in_page_link_prefix = "ai:"; 2824 2825 // an in-page tab's base route, or the exact current route when it is selected 2826 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2827 const target = if (selected) session.data.current_page else route; 2828 return session.page_arena.allocator().print("{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2829 } 2830 2831 // a button the web renderer covers with a file picker that posts the chosen 2832 // file to the url after the prefix 2833 pub const file_input_prefix = "file:"; 2834 2835 // a submit button that overrides its enclosing form's action 2836 pub const submit_action_prefix = "submit:"; 2837 2838 // one option of a radio field, as `radio:<name>=<value>` 2839 pub const radio_prefix = "radio:"; 2840 2841 // the url an `ax:` link points at, or null for anything else. a host that 2842 // suppresses the anchor's own navigation needs this to follow the link. 2843 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2844 const child = root_focus.children.get(focus_id) orelse return null; 2845 const custom = switch (child.focus.kind) { 2846 .custom => |c| c, 2847 else => return null, 2848 }; 2849 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2850 return custom[raw_link_prefix.len..]; 2851 } 2852 2853 fn requestRawLink(session: *Session, url: []const u8) !void { 2854 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2855 } 2856 2857 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2858 // an absolute url is already complete 2859 if (!std.mem.startsWith(u8, path, "/")) return path; 2860 return if (session.web_port) |port| 2861 try allocator.print("http://localhost:{d}{s}", .{ port, path }) 2862 else 2863 path; 2864 } 2865 2866 // if the focus target at focus_id is an `a:` link that should navigate (a 2867 // different parent page, or a different files directory within the repo page), 2868 // return its route; otherwise null. lets a host turn a click / enter on such a 2869 // link into a navigation rather than a focus change. 2870 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2871 const current = data.current_page; 2872 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2873 // a link to a different parent page always navigates; within a page, a 2874 // files-directory / commits-page / list-window change navigates while tab 2875 // links stay in-page. 2876 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; 2877 return null; 2878 } 2879 2880 // the page the clicked cell links to, from a link inside a text box's content 2881 fn cellPageLink(root: *Widget, x: usize, y: usize, data: Session.Data) ?RoutablePage { 2882 const grid = root.getGrid() orelse return null; 2883 const cell = grid.cell(x, y) catch return null; 2884 const link = cell.link orelse return null; 2885 if (link.len == 0 or link[0] != '/') return null; 2886 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(link) else RoutablePage.fromUrl(link); 2887 } 2888 2889 // whether the `a:` focus target links to the page already shown, which a click 2890 // leaves alone rather than focusing 2891 pub fn currentPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) bool { 2892 const route = pageLink(root_focus, focus_id, data, "a:") orelse return false; 2893 return route.eql(data.current_page); 2894 } 2895 2896 // the route carried by an `ai:` focus target, or null for any other target 2897 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2898 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2899 } 2900 2901 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2902 const child = root_focus.children.get(focus_id) orelse return null; 2903 const custom = switch (child.focus.kind) { 2904 .custom => |c| c, 2905 else => return null, 2906 }; 2907 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2908 const path = custom[prefix.len..]; 2909 // local sessions build (and parse) links with the repo identity elided 2910 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2911 } 2912 2913 // an assignee as the detail view shows it: the stored email, and the name of 2914 // the user holding it on this instance when there is one 2915 pub const Assignee = struct { 2916 email: []const u8, 2917 name: ?[]const u8 = null, 2918 2919 pub fn display(self: Assignee) []const u8 { 2920 return self.name orelse self.email; 2921 } 2922 }; 2923 2924 // a display author, resolved against the admin db at read time 2925 pub const Author = union(enum) { 2926 unknown, 2927 email: []const u8, // an email no user matches 2928 user_name: []const u8, 2929 2930 // the commit author line's email, resolved like initFromEmail 2931 pub fn init( 2932 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2933 arena: *std.heap.ArenaAllocator, 2934 author_line: []const u8, 2935 ) !Author { 2936 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2937 } 2938 2939 // from a record's stored author email: it resolves to the user currently 2940 // holding it, else stays the email. the result is allocated in `arena`, 2941 // so the email may borrow transient memory. 2942 pub fn initFromEmail( 2943 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2944 arena: *std.heap.ArenaAllocator, 2945 author_email: ?[]const u8, 2946 ) !Author { 2947 const email = author_email orelse return .unknown; 2948 if (haxy_moment) |moment| { 2949 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2950 return .{ .user_name = user.name }; 2951 } 2952 } 2953 return .{ .email = try arena.allocator().dupe(u8, email) }; 2954 } 2955 }; 2956 2957 // the "a:" link to user `name`'s page. 2958 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2959 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2960 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2961 return page_arena.allocator().print("a:{s}", .{url}); 2962 } 2963 2964 // a focusable " author " box showing `author`, linking to their user page 2965 // when it is a known user 2966 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2967 const text = switch (author) { 2968 .unknown => "", 2969 .email, .user_name => |t| t, 2970 }; 2971 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 2972 errdefer tb.deinit(allocator); 2973 tb.getFocus().mode = .all; 2974 switch (author) { 2975 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2976 else => {}, 2977 } 2978 return tb; 2979 } 2980 2981 // native-TUI navigation 2982 pub const Nav = struct { 2983 root: Widget, 2984 // backs the current page (its Page data and the page-scoped strings its 2985 // widgets build). owned here, swapped on each navigation. session.page_arena 2986 // tracks whichever of these is current so widgets allocate into it. 2987 arena: *std.heap.ArenaAllocator, 2988 // the login the current root was built for 2989 user_id: ?[evt.event_id_size]u8, 2990 history: std.ArrayList(Entry), 2991 2992 // each retained page keeps its own arena, freed when the page leaves the 2993 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2994 // long-lived session from accumulating every page it ever visited. 2995 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2996 2997 // cap on retained back-history so the chain can't grow memory without bound 2998 const max_history: usize = 16; 2999 3000 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 3001 session.back = .unavailable; 3002 const arena = try allocator.create(std.heap.ArenaAllocator); 3003 arena.* = std.heap.ArenaAllocator.init(allocator); 3004 errdefer { 3005 arena.deinit(); 3006 allocator.destroy(arena); 3007 } 3008 3009 return .{ 3010 .root = try initCurrentRoot(allocator, arena, session), 3011 .arena = arena, 3012 .user_id = session.userId(), 3013 .history = .empty, 3014 }; 3015 } 3016 3017 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3018 self.root.deinit(allocator); 3019 freeArena(allocator, self.arena); 3020 for (self.history.items) |*entry| { 3021 entry.root.deinit(allocator); 3022 freeArena(allocator, entry.arena); 3023 } 3024 self.history.deinit(allocator); 3025 } 3026 3027 // rebuild the current page in place from a current moment 3028 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3029 if (session.haxy_moment == null and session.local == null) return; 3030 const arena = try allocator.create(std.heap.ArenaAllocator); 3031 arena.* = std.heap.ArenaAllocator.init(allocator); 3032 errdefer freeArena(allocator, arena); 3033 3034 const new_root = try initCurrentRoot(allocator, arena, session); 3035 3036 self.root.deinit(allocator); 3037 freeArena(allocator, self.arena); 3038 self.root = new_root; 3039 self.arena = arena; 3040 self.user_id = session.userId(); 3041 } 3042 3043 // build the current page's root in `arena`, or the not-found page's when 3044 // the route names nothing 3045 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3046 session.page_arena = arena; 3047 const page = try arena.allocator().create(Page); 3048 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3049 error.NotFound => blk: { 3050 session.data.current_page = .not_found; 3051 break :blk .not_found; 3052 }, 3053 else => |e| return e, 3054 }; 3055 return initRoot(allocator, page, session); 3056 } 3057 3058 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3059 arena.deinit(); 3060 allocator.destroy(arena); 3061 } 3062 3063 // reconcile the displayed root with the session's navigation state. a 3064 // forward nav (current_page moved to a different parent page) pushes the 3065 // current root and builds the new page in a fresh arena; a back request 3066 // frees the current page and restores the previous one. when escape is 3067 // pressed with no history left we switch to the quit tab instead of quitting. 3068 // returns whether the root was replaced. 3069 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3070 // refresh: rebuild the current page in place from a current moment 3071 if (session.refresh_requested) { 3072 session.refresh_requested = false; 3073 try self.rebuild(allocator, session); 3074 return true; 3075 } 3076 3077 if (session.back == .requested) { 3078 if (self.history.pop()) |entry| { 3079 session.back = if (self.history.items.len == 0) .unavailable else .available; 3080 self.root.deinit(allocator); 3081 freeArena(allocator, self.arena); 3082 self.root = entry.root; 3083 self.arena = entry.arena; 3084 self.user_id = entry.user_id; 3085 session.page_arena = entry.arena; 3086 session.data.current_page = entry.route; 3087 // a page built under another login is rebuilt rather than restored 3088 if (!std.meta.eql(entry.user_id, session.userId())) { 3089 try self.rebuild(allocator, session); 3090 return true; 3091 } 3092 chooseAnsiArtForNavigation(session); 3093 return true; 3094 } 3095 session.back = .unavailable; 3096 // nothing to go back to; switch to the quit confirmation 3097 const root_focus = self.root.getFocus(); 3098 var iter = root_focus.children.iterator(); 3099 while (iter.next()) |entry| { 3100 switch (entry.value_ptr.focus.kind) { 3101 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3102 root_focus.setFocus(entry.key_ptr.*); 3103 // a Stack only builds its selected child, so the quit 3104 // button isn't in the focus tree until a build runs with 3105 // the quit tab selected. build once, then send a 3106 // synthetic arrow_down to drop focus from the tab onto 3107 // the button so it's ready to confirm. 3108 try self.root.build(allocator, .{ 3109 .min_size = .{ .width = null, .height = 40 }, 3110 .max_size = .{ .width = 80, .height = null }, 3111 }, root_focus); 3112 try self.root.input(allocator, .arrow_down, root_focus); 3113 break; 3114 }, 3115 else => {}, 3116 } 3117 } 3118 return false; 3119 } 3120 3121 // forward navigation: navigate() set next_page to the page to move to (a 3122 // cross-page link or tab change crossing pages) 3123 if (session.next_page) |route| { 3124 session.next_page = null; 3125 if (session.haxy_moment == null and session.local == null) return false; 3126 const previous_route = session.data.current_page; 3127 // the page we navigated to becomes the current page 3128 session.data.current_page = route; 3129 session.back = .available; 3130 3131 const arena = try allocator.create(std.heap.ArenaAllocator); 3132 arena.* = std.heap.ArenaAllocator.init(allocator); 3133 errdefer freeArena(allocator, arena); 3134 3135 const new_root = try initCurrentRoot(allocator, arena, session); 3136 3137 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3138 // drop the oldest entry (freeing its widget tree and arena) once over cap 3139 if (self.history.items.len > max_history) { 3140 var oldest = self.history.orderedRemove(0); 3141 oldest.root.deinit(allocator); 3142 freeArena(allocator, oldest.arena); 3143 } 3144 self.root = new_root; 3145 self.arena = arena; 3146 self.user_id = session.userId(); 3147 return true; 3148 } 3149 return false; 3150 } 3151 }; 3152 3153 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3154 const page_widget: Widget = switch (page.*) { 3155 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3156 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3157 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3158 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3159 .not_found => .{ .not_found = try .init(allocator, session) }, 3160 }; 3161 3162 chooseAnsiArtForNavigation(session); 3163 3164 // on the TUI/SSH, the page sits above a one-row footer showing its web 3165 // url, when there's a web UI to point at 3166 var root = if (session.is_terminal) blk: { 3167 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3168 errdefer box.deinit(allocator); 3169 const bg_id = bg_blk: { 3170 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3171 errdefer background.deinit(allocator); 3172 const id = background.getFocus().id; 3173 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3174 break :bg_blk id; 3175 }; 3176 if (session.web_port != null) { 3177 var footer = try widget.Footer.init(allocator, session); 3178 errdefer footer.deinit(allocator); 3179 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3180 } 3181 box.getFocus().child_id = bg_id; 3182 break :blk Widget{ .box = box }; 3183 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3184 errdefer root.deinit(allocator); 3185 3186 // input-owning views build their TextInputs in init, so reset their 3187 // registrations here 3188 session.text_inputs.clearRetainingCapacity(); 3189 3190 try root.build(allocator, .{ 3191 .min_size = .{ .width = null, .height = 40 }, 3192 .max_size = .{ .width = 100, .height = null }, 3193 }, root.getFocus()); 3194 3195 return root; 3196 } 3197 3198 // native and server-side sessions choose from the embedded collection. WASM 3199 // keeps the art serialized by the server instead. 3200 fn chooseAnsiArtForNavigation(session: *Session) void { 3201 if (comptime ansi_art.art.len == 0) return; 3202 if (session.data.current_page == .not_found) { 3203 session.data.ansi_art = ansi_art.not_found; 3204 return; 3205 } 3206 const io = session.io orelse return; 3207 var random: usize = undefined; 3208 io.random(std.mem.asBytes(&random)); 3209 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3210 } 3211