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