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