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