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