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