╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
246 } 247 }; 248 249 const user_segment = "/user/"; 250 const repo_segment = "/repo/"; 251 const fork_segment = "/fork/"; 252 const files_seg = "files"; 253 const commits_seg = "commits"; 254 // ends a commits route naming its commit's full message 255 const message_seg = "message"; 256 // the `Params` key spellings the url emitters use 257 const tag_filter_seg = @tagName(Params.ParamKey.tag) ++ ":"; 258 const start_seg = @tagName(Params.ParamKey.start) ++ ":"; 259 const line_seg = @tagName(Params.ParamKey.line) ++ ":"; 260 const theirs_seg = @tagName(Params.ParamKey.theirs) ++ ":"; 261 const issue_seg = "issue:"; 262 const patch_seg = "patch:"; 263 const discuss_seg = "discuss:"; 264 const comment_seg = "comment:"; 265 // marks a files or commits route's trailing path: the value is the raw 266 // remainder of the url, so it must come last 267 const path_seg = "path:"; 268 269 // a repo route is "username/reponame", optionally followed by the files 270 // tab's "/files/<refkind>:<refvalue>[/line:<n>][/path:<dir>]"; the bare 271 // pair means the default branch's files root. a local session's routes 272 // elide the identity: "" at the bare root, else starting at the tail's "/". 273 pub const repo_route_max_len = 1024; 274 pub const repo_identity_max_len = evt.User.name_max_len + 1 + evt.Repo.name_max_len; 275 276 // true when a repo route's stored string elides its identity (local mode). 277 // unambiguous because a full string always starts with its owner segment. 278 fn identityElided(s: []const u8) bool { 279 return s.len == 0 or s[0] == '/'; 280 } 281 282 // a url-encoded tag can grow to three bytes per source byte. 283 pub const issue_tag_route_max_len = evt.Issue.tag_max_len * 3; 284 pub const patch_tag_route_max_len = evt.Patch.tag_max_len * 3; 285 pub const discussion_tag_route_max_len = evt.Discussion.tag_max_len * 3; 286 287 // caps the resolve view's theirs: field list (a longer one doesn't route). 288 pub const theirs_route_max_len = 512; 289 290 // caps a url-encoded ref name in a route (a longer name doesn't route). 291 pub const ref_route_max_len = 512; 292 293 // the identity stored by every repo route. Local routes elide it. 294 pub const RepoIdentity = struct { 295 identity: []const u8, // "owner/name" 296 owner: []const u8, 297 name: []const u8, 298 299 pub fn parse(s: []const u8) ?RepoIdentity { 300 var result: RepoIdentity = .{ 301 .identity = "", 302 .owner = "", 303 .name = "", 304 }; 305 if (identityElided(s)) { 306 return result; 307 } else { 308 var id_segments = std.mem.splitScalar(u8, s, '/'); 309 const owner = id_segments.next() orelse return null; 310 const name = id_segments.next() orelse return null; 311 if (owner.len == 0 or name.len == 0) return null; 312 result.identity = s[0 .. owner.len + 1 + name.len]; 313 result.owner = owner; 314 result.name = name; 315 } 316 return result; 317 } 318 }; 319 320 pub const RepoFilesRoute = struct { 321 name: Array(repo_identity_max_len), 322 ref_kind: ?RefOrOid = null, 323 ref_value: Array(ref_route_max_len) = .{}, 324 path: Array(repo_route_max_len) = .{}, 325 // the selected file's first visible line (1-based; 0 = unset). 326 line: usize = 0, 327 }; 328 329 pub const RepoCommitsRoute = struct { 330 name: Array(repo_identity_max_len), 331 ref_or_oid: ?RefOrOid = null, 332 value: Array(ref_route_max_len) = .{}, 333 // what the pane shows for the commit the log walks from. 334 content: Content = .{ .diff = .{} }, 335 336 // a diff window and the message are alternatives, so the window's 337 // params can't be named alongside the message. 338 pub const Content = union(enum) { 339 diff: struct { 340 // the hunk index the window starts at. 341 start: usize = 0, 342 // the file the diff is filtered to ("" = every file). 343 path: Array(repo_route_max_len) = .{}, 344 }, 345 message, 346 }; 347 }; 348 349 pub const RepoEventsRoute = struct { 350 name: Array(repo_identity_max_len), 351 view: EventsView = .active, 352 kind: ?evt.EventKind = null, 353 selected: Array(evt.event_id_size * 2) = .{}, 354 }; 355 356 pub const ForkRoute = struct { 357 name: Array(repo_identity_max_len), 358 id: Array(evt.event_id_size * 2), 359 }; 360 361 pub const ForkFilesRoute = struct { 362 fork: ForkRoute, 363 oid: Array(ref_route_max_len) = .{}, 364 path: Array(repo_route_max_len) = .{}, 365 line: usize = 0, 366 }; 367 368 pub const ForkCommitsRoute = struct { 369 fork: ForkRoute, 370 oid: Array(ref_route_max_len) = .{}, 371 content: RepoCommitsRoute.Content = .{ .diff = .{} }, 372 }; 373 374 // where the shared files and commits views are mounted. fork routes always 375 // read their implicit `patch` branch unless they name an object directly. 376 pub const RepoLocation = union(enum) { 377 repo: []const u8, 378 fork: struct { 379 identity: []const u8, 380 id: []const u8, 381 target_branch: []const u8, 382 }, 383 384 pub fn dupe(self: RepoLocation, allocator: std.mem.Allocator) !RepoLocation { 385 return switch (self) { 386 .repo => |identity_value| .{ .repo = try allocator.dupe(u8, identity_value) }, 387 .fork => |f| .{ .fork = .{ 388 .identity = try allocator.dupe(u8, f.identity), 389 .id = try allocator.dupe(u8, f.id), 390 .target_branch = try allocator.dupe(u8, f.target_branch), 391 } }, 392 }; 393 } 394 395 pub fn filesRoute(self: RepoLocation, ref_or_oid: RefOrOid, value: []const u8, path: []const u8, line: usize) ?RoutablePage { 396 return switch (self) { 397 .repo => |repo_identity| repoFilesRoute(repo_identity, ref_or_oid, value, path, line), 398 .fork => |f| if (ref_or_oid == .branch and std.mem.eql(u8, value, "patch")) 399 forkFilesRoute(f.identity, f.id, "", path, line) 400 else if (ref_or_oid == .object) 401 forkFilesRoute(f.identity, f.id, value, path, line) 402 else 403 null, 404 }; 405 } 406 407 pub fn commitsRoute(self: RepoLocation, ref_or_oid: ?RefOrOid, value: []const u8, start: usize, path: []const u8) ?RoutablePage { 408 return switch (self) { 409 .repo => |repo_identity| repoCommitsRoute(repo_identity, ref_or_oid, value, start, path), 410 .fork => |f| if (ref_or_oid == null or (ref_or_oid == .branch and std.mem.eql(u8, value, "patch"))) 411 forkCommitsRoute(f.identity, f.id, "", start, path) 412 else if (ref_or_oid == .object) 413 forkCommitsRoute(f.identity, f.id, value, start, path) 414 else 415 null, 416 }; 417 } 418 419 pub fn commitMessageRoute(self: RepoLocation, ref_or_oid: RefOrOid, value: []const u8) ?RoutablePage { 420 return switch (self) { 421 .repo => |repo_identity| repoCommitMessageRoute(repo_identity, ref_or_oid, value), 422 .fork => |f| if (ref_or_oid == .object) forkCommitMessageRoute(f.identity, f.id, value) else null, 423 }; 424 } 425 }; 426 427 fn initForkRoute(identity: []const u8, id: []const u8) ?ForkRoute { 428 if (id.len != evt.event_id_size * 2) return null; 429 return .{ 430 .name = Array(repo_identity_max_len).from(identity) orelse return null, 431 .id = Array(evt.event_id_size * 2).from(id) orelse return null, 432 }; 433 } 434 435 pub fn forkPatchRoute(identity: []const u8, id: []const u8) ?RoutablePage { 436 return .{ .fork_patch = initForkRoute(identity, id) orelse return null }; 437 } 438 439 pub fn forkFilesRoute(identity: []const u8, id: []const u8, oid: []const u8, path: []const u8, line: usize) ?RoutablePage { 440 return .{ .fork_files = .{ 441 .fork = initForkRoute(identity, id) orelse return null, 442 .oid = Array(ref_route_max_len).from(oid) orelse return null, 443 .path = Array(repo_route_max_len).from(path) orelse return null, 444 .line = line, 445 } }; 446 } 447 448 pub fn forkCommitsRoute(identity: []const u8, id: []const u8, oid: []const u8, start: usize, path: []const u8) ?RoutablePage { 449 return .{ .fork_commits = .{ 450 .fork = initForkRoute(identity, id) orelse return null, 451 .oid = Array(ref_route_max_len).from(oid) orelse return null, 452 .content = .{ .diff = .{ 453 .start = start, 454 .path = Array(repo_route_max_len).from(path) orelse return null, 455 } }, 456 } }; 457 } 458 459 pub fn forkCommitMessageRoute(identity: []const u8, id: []const u8, oid: []const u8) ?RoutablePage { 460 if (oid.len == 0) return null; 461 return .{ .fork_commits = .{ 462 .fork = initForkRoute(identity, id) orelse return null, 463 .oid = Array(ref_route_max_len).from(oid) orelse return null, 464 .content = .message, 465 } }; 466 } 467 468 pub fn forkSettingsRoute(identity: []const u8, id: []const u8) ?RoutablePage { 469 return .{ .fork_settings = initForkRoute(identity, id) orelse return null }; 470 } 471 472 pub fn forkAuthRoute(identity: []const u8, id: []const u8) ?RoutablePage { 473 return .{ .fork_auth = initForkRoute(identity, id) orelse return null }; 474 } 475 476 // build a `.repo_files` route (a null ref_kind = the bare default-branch 477 // root). null if the result doesn't fit the inline name. 478 pub fn repoFilesRoute(identity: []const u8, ref_kind: ?RefOrOid, ref_value: []const u8, dir: []const u8, line: usize) ?RoutablePage { 479 if (ref_kind == null and (ref_value.len != 0 or dir.len != 0)) return null; 480 if (ref_kind != null and ref_value.len == 0 and dir.len != 0) return null; 481 return .{ .repo_files = .{ 482 .name = Array(repo_identity_max_len).from(identity) orelse return null, 483 .ref_kind = ref_kind, 484 .ref_value = Array(ref_route_max_len).from(ref_value) orelse return null, 485 .path = Array(repo_route_max_len).from(dir) orelse return null, 486 .line = line, 487 } }; 488 } 489 490 // build a `.repo_commits` route (a null ref_or_oid = the default branch). 491 // always carries the `commits` marker so the bare route doesn't collide 492 // with the files root. a non-empty `path` filters the diff pane to that 493 // file and requires a ref. 494 pub fn repoCommitsRoute(identity: []const u8, ref_or_oid: ?RefOrOid, value: []const u8, start: usize, path: []const u8) ?RoutablePage { 495 if (ref_or_oid == null and (value.len != 0 or path.len != 0)) return null; 496 return .{ .repo_commits = .{ 497 .name = Array(repo_identity_max_len).from(identity) orelse return null, 498 .ref_or_oid = ref_or_oid, 499 .value = Array(ref_route_max_len).from(value) orelse return null, 500 .content = .{ .diff = .{ 501 .start = start, 502 .path = Array(repo_route_max_len).from(path) orelse return null, 503 } }, 504 } }; 505 } 506 507 // build a `.repo_commits` route showing the ref/oid's commit message in 508 // full, which is the only content that page renders. 509 pub fn repoCommitMessageRoute(identity: []const u8, ref_or_oid: RefOrOid, value: []const u8) ?RoutablePage { 510 if (value.len == 0) return null; 511 return .{ .repo_commits = .{ 512 .name = Array(repo_identity_max_len).from(identity) orelse return null, 513 .ref_or_oid = ref_or_oid, 514 .value = Array(ref_route_max_len).from(value) orelse return null, 515 .content = .message, 516 } }; 517 } 518 519 // build a `.repo_refs` route windowing `kind`'s column from the 520 // url-encoded ref name `from` ("" = the first window). 521 pub fn repoRefsRoute(identity: []const u8, kind: RefKind, from: []const u8) ?RoutablePage { 522 return .{ .repo_refs = .{ 523 .name = Array(repo_route_max_len).from(identity) orelse return null, 524 .kind = kind, 525 .from = Array(ref_route_max_len).from(from) orelse return null, 526 } }; 527 } 528 529 // build the events list or a window rooted at one event. 530 pub fn repoEventsRoute(identity: []const u8, view: EventsView, kind: ?evt.EventKind, selected: []const u8) ?RoutablePage { 531 if ((kind == null) != (selected.len == 0)) return null; 532 return .{ .repo_events = .{ 533 .name = Array(repo_identity_max_len).from(identity) orelse return null, 534 .view = view, 535 .kind = kind, 536 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 537 } }; 538 } 539 540 // build a `.repo_issues` route for "owner/name" (identity) showing 541 // `status`'s list, filtered to the url-encoded `tag` ("" = unfiltered) and 542 // rooted at the issue with hex event id `selected` ("" = the first window). 543 pub fn repoIssuesRoute(identity: []const u8, status: evt.Issue.Status, tag: []const u8, selected: []const u8) ?RoutablePage { 544 return .{ .repo_issues = .{ 545 .name = Array(repo_route_max_len).from(identity) orelse return null, 546 .tag = Array(issue_tag_route_max_len).from(tag) orelse return null, 547 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 548 .view = switch (status) { 549 .open => .open, 550 .closed => .closed, 551 }, 552 } }; 553 } 554 555 pub fn repoPatchesRoute(identity: []const u8, status: evt.Patch.Status, tag: []const u8, selected: []const u8) ?RoutablePage { 556 return .{ .repo_patches = .{ 557 .name = Array(repo_route_max_len).from(identity) orelse return null, 558 .tag = Array(patch_tag_route_max_len).from(tag) orelse return null, 559 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 560 .view = switch (status) { 561 .open => .open, 562 .closed => .closed, 563 .merged => .merged, 564 }, 565 } }; 566 } 567 568 pub fn repoPatchesDraftsRoute(identity: []const u8) ?RoutablePage { 569 return .{ .repo_patches = .{ 570 .name = Array(repo_route_max_len).from(identity) orelse return null, 571 .view = .drafts, 572 } }; 573 } 574 575 pub fn repoPatchPublishRoute(identity: []const u8, selected: []const u8) ?RoutablePage { 576 if (selected.len == 0) return null; 577 var route = repoPatchesRoute(identity, .open, "", selected) orelse return null; 578 switch (route) { 579 .repo_patches => |*patch| patch.view = .publish, 580 else => unreachable, 581 } 582 return route; 583 } 584 585 pub fn repoDiscussionsRoute(identity: []const u8, tag: []const u8, selected: []const u8) ?RoutablePage { 586 return .{ .repo_discussions = .{ 587 .name = Array(repo_route_max_len).from(identity) orelse return null, 588 .tag = Array(discussion_tag_route_max_len).from(tag) orelse return null, 589 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 590 } }; 591 } 592 593 // build the default list route for a thread event kind. 594 pub fn repoThreadRoute(kind: evt.EventKind, identity: []const u8, tag: []const u8, selected: []const u8) ?RoutablePage { 595 return switch (kind) { 596 .issue => repoIssuesRoute(identity, .open, tag, selected), 597 .patch => repoPatchesRoute(identity, .open, tag, selected), 598 .discuss => repoDiscussionsRoute(identity, tag, selected), 599 else => null, 600 }; 601 } 602 603 // build a selected thread route at one of its comment windows. 604 pub fn repoThreadCommentsRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8, start: usize) ?RoutablePage { 605 var route = repoThreadRoute(kind, identity, "", selected) orelse return null; 606 switch (route) { 607 .repo_issues => |*thread| thread.comments_start = start, 608 .repo_patches => |*thread| thread.comments_start = start, 609 .repo_discussions => |*thread| thread.comments_start = start, 610 else => unreachable, 611 } 612 return route; 613 } 614 615 // build a comment permalink route at one of its immediate-reply windows. 616 pub fn repoThreadCommentRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8, start: usize) ?RoutablePage { 617 if (thread_id.len == 0 or comment_id.len == 0) return null; 618 var route = repoThreadCommentsRoute(kind, identity, thread_id, start) orelse return null; 619 const comment = Array(evt.event_id_size * 2).from(comment_id) orelse return null; 620 switch (route) { 621 .repo_issues => |*thread| thread.comment = comment, 622 .repo_patches => |*thread| thread.comment = comment, 623 .repo_discussions => |*thread| thread.comment = comment, 624 else => unreachable, 625 } 626 return route; 627 } 628 629 // build the form route for replying to a thread or one of its comments. 630 pub fn repoThreadCommentNewRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, parent_id: []const u8) ?RoutablePage { 631 if (thread_id.len == 0) return null; 632 var route = repoThreadCommentsRoute(kind, identity, thread_id, 0) orelse return null; 633 const parent_event_id = Array(evt.event_id_size * 2).from(parent_id) orelse return null; 634 switch (route) { 635 .repo_issues => |*thread| { 636 thread.comment = parent_event_id; 637 thread.view = .new_comment; 638 }, 639 .repo_patches => |*thread| { 640 thread.comment = parent_event_id; 641 thread.view = .new_comment; 642 }, 643 .repo_discussions => |*thread| { 644 thread.comment = parent_event_id; 645 thread.view = .new_comment; 646 }, 647 else => unreachable, 648 } 649 return route; 650 } 651 652 // build the form route for editing a comment. 653 pub fn repoThreadCommentEditRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?RoutablePage { 654 var route = repoThreadCommentRoute(kind, identity, thread_id, comment_id, 0) orelse return null; 655 switch (route) { 656 .repo_issues => |*thread| thread.view = .edit_comment, 657 .repo_patches => |*thread| thread.view = .edit_comment, 658 .repo_discussions => |*thread| thread.view = .edit_comment, 659 else => unreachable, 660 } 661 return route; 662 } 663 664 // build the confirmation route for removing a thread or comment. 665 pub fn repoThreadRemoveRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?RoutablePage { 666 var route = if (comment_id.len == 0) 667 repoThreadCommentsRoute(kind, identity, thread_id, 0) orelse return null 668 else 669 repoThreadCommentRoute(kind, identity, thread_id, comment_id, 0) orelse return null; 670 switch (route) { 671 .repo_issues => |*thread| thread.view = .remove, 672 .repo_patches => |*thread| thread.view = .remove, 673 .repo_discussions => |*thread| thread.view = .remove, 674 else => unreachable, 675 } 676 return route; 677 } 678 679 // build a thread tags route, keeping its url-encoded tag filter. 680 pub fn repoThreadTagsRoute(kind: evt.EventKind, identity: []const u8, tag: []const u8) ?RoutablePage { 681 var route = repoThreadRoute(kind, identity, tag, "") orelse return null; 682 switch (route) { 683 .repo_issues => |*thread| thread.view = .tags, 684 .repo_patches => |*thread| thread.view = .tags, 685 .repo_discussions => |*thread| thread.view = .tags, 686 else => unreachable, 687 } 688 return route; 689 } 690 691 // build a new-thread form route. 692 pub fn repoThreadNewRoute(kind: evt.EventKind, identity: []const u8) ?RoutablePage { 693 var route = repoThreadRoute(kind, identity, "", "") orelse return null; 694 switch (route) { 695 .repo_issues => |*thread| thread.view = .new, 696 .repo_patches => |*thread| thread.view = .new, 697 .repo_discussions => |*thread| thread.view = .new, 698 else => unreachable, 699 } 700 return route; 701 } 702 703 // build the edit form route for a thread. 704 pub fn repoThreadEditRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8) ?RoutablePage { 705 if (selected.len == 0) return null; 706 var route = repoThreadRoute(kind, identity, "", selected) orelse return null; 707 switch (route) { 708 .repo_issues => |*thread| thread.view = .edit, 709 .repo_patches => |*thread| thread.view = .edit, 710 .repo_discussions => |*thread| thread.view = .edit, 711 else => unreachable, 712 } 713 return route; 714 } 715 716 // build a route showing a thread's whole description. 717 pub fn repoThreadDescriptionRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8) ?RoutablePage { 718 if (selected.len == 0) return null; 719 var route = repoThreadRoute(kind, identity, "", selected) orelse return null; 720 switch (route) { 721 .repo_issues => |*thread| thread.view = .description, 722 .repo_patches => |*thread| thread.view = .description, 723 .repo_discussions => |*thread| thread.view = .description, 724 else => unreachable, 725 } 726 return route; 727 } 728 729 // build a `.repo_issues` route showing the conflicts list, rooted at the 730 // issue with hex event id `start` ("" = the first window). 731 pub fn repoIssuesConflictsRoute(identity: []const u8, start: []const u8) ?RoutablePage { 732 return .{ .repo_issues = .{ 733 .name = Array(repo_route_max_len).from(identity) orelse return null, 734 .selected = Array(evt.event_id_size * 2).from(start) orelse return null, 735 .view = .conflicts, 736 } }; 737 } 738 739 // build a `.repo_issues` route showing the conflict resolve form for the 740 // issue with hex event id `selected`; `theirs` is the comma-separated list 741 // of fields prefilled from their side. 742 pub fn repoIssuesResolveRoute(identity: []const u8, selected: []const u8, theirs: []const u8) ?RoutablePage { 743 if (selected.len == 0) return null; 744 return .{ .repo_issues = .{ 745 .name = Array(repo_route_max_len).from(identity) orelse return null, 746 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 747 .theirs = Array(theirs_route_max_len).from(theirs) orelse return null, 748 .view = .resolve, 749 } }; 750 } 751 752 pub fn repoPatchesConflictsRoute(identity: []const u8, start: []const u8) ?RoutablePage { 753 return .{ .repo_patches = .{ 754 .name = Array(repo_route_max_len).from(identity) orelse return null, 755 .selected = Array(evt.event_id_size * 2).from(start) orelse return null, 756 .view = .conflicts, 757 } }; 758 } 759 760 pub fn repoPatchesResolveRoute(identity: []const u8, selected: []const u8, theirs: []const u8) ?RoutablePage { 761 if (selected.len == 0) return null; 762 return .{ .repo_patches = .{ 763 .name = Array(repo_route_max_len).from(identity) orelse return null, 764 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 765 .theirs = Array(theirs_route_max_len).from(theirs) orelse return null, 766 .view = .resolve, 767 } }; 768 } 769 770 // an inline, owned array of data. keeping it in the route (rather than a 771 // borrowed slice) makes RoutablePage a plain value: it can be copied, stored 772 // in history, and serialized without any arena tracking. 773 pub fn Array(comptime max_len: usize) type { 774 return struct { 775 bytes: [max_len]u8 = undefined, 776 len: u16 = 0, 777 778 pub fn from(s: []const u8) ?Array(max_len) { 779 if (s.len > max_len) return null; 780 var name = Array(max_len){ .len = @intCast(s.len) }; 781 @memcpy(name.bytes[0..s.len], s); 782 return name; 783 } 784 785 pub fn slice(self: *const Array(max_len)) []const u8 { 786 return self.bytes[0..self.len]; 787 } 788 789 // round-trip as a json string instead of an array of numbers. 790 pub fn jsonStringify(self: *const Array(max_len), jw: anytype) !void { 791 try jw.write(self.slice()); 792 } 793 794 pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !Array(max_len) { 795 const s = try std.json.innerParse([]const u8, allocator, source, options); 796 // ValueTooLong is std.json's error for an over-long field. 797 return from(s) orelse error.ValueTooLong; 798 } 799 }; 800 } 801 802 pub fn toUrl(self: RoutablePage, arena: *std.heap.ArenaAllocator) ![]const u8 { 803 return switch (self) { 804 .home_users => |start| if (start == 0) @as([]const u8, "/users") else try std.fmt.allocPrint(arena.allocator(), "/users/" ++ start_seg ++ "{d}", .{start}), 805 .home_repos => |start| if (start == 0) @as([]const u8, "/repos") else try std.fmt.allocPrint(arena.allocator(), "/repos/" ++ start_seg ++ "{d}", .{start}), 806 .home_settings => "/settings", 807 .home_auth => "/auth", 808 .user_repos => |u| if (u.start == 0) 809 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/repos", .{u.name.slice()}) 810 else 811 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/repos/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 812 .user_forks => |u| if (u.start == 0) 813 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/forks", .{u.name.slice()}) 814 else 815 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/forks/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 816 .user_settings => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/settings", .{name.slice()}), 817 .user_auth => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/auth", .{name.slice()}), 818 .repo_files => |f| blk: { 819 const prefix = try repoUrlPrefix(arena, f.name.slice()); 820 if (f.ref_kind == null and f.line == 0) break :blk if (prefix.len == 0) "/" else prefix; 821 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 822 try out.writer.print("{s}/" ++ files_seg, .{prefix}); 823 if (f.ref_kind) |kind| if (f.ref_value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), f.ref_value.slice() }); 824 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 825 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 826 break :blk out.written(); 827 }, 828 .repo_commits => |c| blk: { 829 const prefix = try repoUrlPrefix(arena, c.name.slice()); 830 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 831 try out.writer.print("{s}/" ++ commits_seg, .{prefix}); 832 if (c.ref_or_oid) |kind| if (c.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), c.value.slice() }); 833 switch (c.content) { 834 .diff => |d| { 835 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 836 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 837 }, 838 .message => try out.writer.print("/" ++ message_seg, .{}), 839 } 840 break :blk out.written(); 841 }, 842 .repo_refs => |r| blk: { 843 const prefix = try repoUrlPrefix(arena, r.name.slice()); 844 break :blk if (r.from.len == 0) 845 try std.fmt.allocPrint(arena.allocator(), "{s}/refs", .{prefix}) 846 else 847 try std.fmt.allocPrint(arena.allocator(), "{s}/refs/{s}:{s}", .{ prefix, @tagName(r.kind), r.from.slice() }); 848 }, 849 .repo_issues => |i| blk: { 850 const prefix = try repoUrlPrefix(arena, i.name.slice()); 851 if (i.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/edit", .{ prefix, i.selected.slice() }); 852 if (i.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/description", .{ prefix, i.selected.slice() }); 853 if (i.view == .resolve) break :blk if (i.theirs.len == 0) 854 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/resolve", .{ prefix, i.selected.slice() }) 855 else 856 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, i.selected.slice(), i.theirs.slice() }); 857 if (i.view == .new_comment) break :blk if (i.comment.len == 0) 858 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/new", .{ prefix, i.selected.slice() }) 859 else 860 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, i.selected.slice(), i.comment.slice() }); 861 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() }); 862 if (i.view == .remove) break :blk if (i.comment.len == 0) 863 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/remove", .{ prefix, i.selected.slice() }) 864 else 865 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, i.selected.slice(), i.comment.slice() }); 866 if (i.view == .conflicts) break :blk if (i.selected.len == 0) 867 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/conflicts", .{prefix}) 868 else 869 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/" ++ start_seg ++ "{s}/conflicts", .{ prefix, i.selected.slice() }); 870 if (i.comment.len != 0) break :blk if (i.comments_start == 0) 871 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, i.selected.slice(), i.comment.slice() }) 872 else 873 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 }); 874 if (i.selected.len != 0) break :blk if (i.comments_start == 0) 875 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}", .{ prefix, i.selected.slice() }) 876 else 877 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, i.selected.slice(), i.comments_start }); 878 break :blk if (i.tag.len == 0) 879 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}", .{ prefix, @tagName(i.view) }) 880 else 881 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(i.view), i.tag.slice() }); 882 }, 883 .repo_patches => |p| blk: { 884 const prefix = try repoUrlPrefix(arena, p.name.slice()); 885 if (p.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/edit", .{ prefix, p.selected.slice() }); 886 if (p.view == .publish) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/publish", .{ prefix, p.selected.slice() }); 887 if (p.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/description", .{ prefix, p.selected.slice() }); 888 if (p.view == .resolve) break :blk if (p.theirs.len == 0) 889 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/resolve", .{ prefix, p.selected.slice() }) 890 else 891 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, p.selected.slice(), p.theirs.slice() }); 892 if (p.view == .new_comment) break :blk if (p.comment.len == 0) 893 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/new", .{ prefix, p.selected.slice() }) 894 else 895 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, p.selected.slice(), p.comment.slice() }); 896 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() }); 897 if (p.view == .remove) break :blk if (p.comment.len == 0) 898 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/remove", .{ prefix, p.selected.slice() }) 899 else 900 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, p.selected.slice(), p.comment.slice() }); 901 if (p.view == .conflicts) break :blk if (p.selected.len == 0) 902 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/conflicts", .{prefix}) 903 else 904 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/" ++ start_seg ++ "{s}/conflicts", .{ prefix, p.selected.slice() }); 905 if (p.comment.len != 0) break :blk if (p.comments_start == 0) 906 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, p.selected.slice(), p.comment.slice() }) 907 else 908 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 }); 909 if (p.selected.len != 0) break :blk if (p.comments_start == 0) 910 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}", .{ prefix, p.selected.slice() }) 911 else 912 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, p.selected.slice(), p.comments_start }); 913 break :blk if (p.tag.len == 0) 914 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/{s}", .{ prefix, @tagName(p.view) }) 915 else 916 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(p.view), p.tag.slice() }); 917 }, 918 .repo_discussions => |t| blk: { 919 const prefix = try repoUrlPrefix(arena, t.name.slice()); 920 if (t.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/edit", .{ prefix, t.selected.slice() }); 921 if (t.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/description", .{ prefix, t.selected.slice() }); 922 if (t.view == .new_comment) break :blk if (t.comment.len == 0) 923 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/new", .{ prefix, t.selected.slice() }) 924 else 925 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, t.selected.slice(), t.comment.slice() }); 926 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() }); 927 if (t.view == .remove) break :blk if (t.comment.len == 0) 928 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/remove", .{ prefix, t.selected.slice() }) 929 else 930 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, t.selected.slice(), t.comment.slice() }); 931 if (t.comment.len != 0) break :blk if (t.comments_start == 0) 932 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, t.selected.slice(), t.comment.slice() }) 933 else 934 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 }); 935 if (t.selected.len != 0) break :blk if (t.comments_start == 0) 936 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}", .{ prefix, t.selected.slice() }) 937 else 938 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, t.selected.slice(), t.comments_start }); 939 break :blk if (t.tag.len == 0) 940 try std.fmt.allocPrint(arena.allocator(), "{s}/discussions/{s}", .{ prefix, @tagName(t.view) }) 941 else 942 try std.fmt.allocPrint(arena.allocator(), "{s}/discussions/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(t.view), t.tag.slice() }); 943 }, 944 .repo_events => |e| blk: { 945 const prefix = try repoUrlPrefix(arena, e.name.slice()); 946 break :blk if (e.kind) |kind| 947 try std.fmt.allocPrint(arena.allocator(), "{s}/event:{s}/kind:{s}", .{ prefix, e.selected.slice(), @tagName(kind) }) 948 else 949 try std.fmt.allocPrint(arena.allocator(), "{s}/events/{s}", .{ prefix, @tagName(e.view) }); 950 }, 951 .repo_settings => |name| try std.fmt.allocPrint(arena.allocator(), "{s}/settings", .{try repoUrlPrefix(arena, name.slice())}), 952 .repo_auth => |name| try std.fmt.allocPrint(arena.allocator(), "{s}/auth", .{try repoUrlPrefix(arena, name.slice())}), 953 .fork_patch => |f| try std.fmt.allocPrint(arena.allocator(), fork_segment ++ "{s}/" ++ patch_seg ++ "{s}", .{ f.name.slice(), f.id.slice() }), 954 .fork_files => |f| blk: { 955 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 956 try out.writer.print(fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/" ++ files_seg, .{ f.fork.name.slice(), f.fork.id.slice() }); 957 if (f.oid.len != 0) try out.writer.print("/object:{s}", .{f.oid.slice()}); 958 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 959 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 960 break :blk out.written(); 961 }, 962 .fork_commits => |c| blk: { 963 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 964 try out.writer.print(fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/" ++ commits_seg, .{ c.fork.name.slice(), c.fork.id.slice() }); 965 if (c.oid.len != 0) try out.writer.print("/object:{s}", .{c.oid.slice()}); 966 switch (c.content) { 967 .diff => |d| { 968 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 969 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 970 }, 971 .message => try out.writer.print("/" ++ message_seg, .{}), 972 } 973 break :blk out.written(); 974 }, 975 .fork_settings => |f| try std.fmt.allocPrint(arena.allocator(), fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/settings", .{ f.name.slice(), f.id.slice() }), 976 .fork_auth => |f| try std.fmt.allocPrint(arena.allocator(), fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/auth", .{ f.name.slice(), f.id.slice() }), 977 }; 978 } 979 980 pub fn fromUrl(path: []const u8) ?RoutablePage { 981 if (std.mem.eql(u8, path, "/")) return default; 982 if (path.len < 2 or path[0] != '/') return null; 983 var segments = std.mem.splitScalar(u8, path[1..], '/'); 984 const first = segments.next() orelse return null; 985 if (std.mem.eql(u8, first, "users")) return .{ .home_users = listStart(&segments) orelse return null }; 986 if (std.mem.eql(u8, first, "repos")) return .{ .home_repos = listStart(&segments) orelse return null }; 987 if (std.mem.eql(u8, first, "settings")) return if (segments.next() == null) .home_settings else null; 988 if (std.mem.eql(u8, first, "auth")) return if (segments.next() == null) .home_auth else null; 989 // "user/<name>[/repos[/start:<n>]|/settings|/auth]" 990 if (std.mem.eql(u8, first, "user")) { 991 const name = segments.next() orelse return null; 992 if (name.len == 0) return null; 993 const parsed = Array(evt.User.name_max_len).from(name) orelse return null; // name too long 994 const sub = segments.next() orelse return .{ .user_repos = .{ .name = parsed } }; 995 if (std.mem.eql(u8, sub, "repos")) return .{ .user_repos = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 996 if (std.mem.eql(u8, sub, "forks")) return .{ .user_forks = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 997 if (std.mem.eql(u8, sub, "settings")) return if (segments.next() == null) .{ .user_settings = parsed } else null; 998 if (std.mem.eql(u8, sub, "auth")) return if (segments.next() == null) .{ .user_auth = parsed } else null; 999 return null; // unknown sub-path 1000 } 1001 // "fork/<username>/<reponame>/patch:<id>[/files|/commits]"; the 1002 // patch branch is implicit, while object ids and view-specific params 1003 // use the same tails as the repo files and commits routes. 1004 if (std.mem.eql(u8, first, "fork")) { 1005 const rest = segments.rest(); 1006 const owner = segments.next() orelse return null; 1007 const repo_name = segments.next() orelse return null; 1008 if (owner.len == 0 or repo_name.len == 0) return null; 1009 const identity = rest[0 .. owner.len + 1 + repo_name.len]; 1010 const patch = segments.next() orelse return null; 1011 if (!std.mem.startsWith(u8, patch, patch_seg)) return null; 1012 const id = patch[patch_seg.len..]; 1013 if (id.len != evt.event_id_size * 2) return null; 1014 const tab = segments.next() orelse return forkPatchRoute(identity, id); 1015 var params = Params{}; 1016 if (std.mem.eql(u8, tab, files_seg)) { 1017 params.scanPairs(&segments) catch return null; 1018 if (!params.only(&.{ .line, .object })) return null; 1019 const line = params.line() orelse return null; 1020 const ref = params.ref() catch return null; 1021 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1022 const file_path = pathValue(segments.rest()) orelse return null; 1023 return forkFilesRoute(identity, id, oid, file_path, line); 1024 } 1025 if (std.mem.eql(u8, tab, commits_seg)) { 1026 params.scanPairs(&segments) catch return null; 1027 if (!params.only(&.{ .start, .object })) return null; 1028 const start = params.start() orelse return null; 1029 const ref = params.ref() catch return null; 1030 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1031 if (std.mem.eql(u8, segments.rest(), message_seg)) 1032 return if (start == 0 and oid.len != 0) forkCommitMessageRoute(identity, id, oid) else null; 1033 const file_path = pathValue(segments.rest()) orelse return null; 1034 return forkCommitsRoute(identity, id, oid, start, file_path); 1035 } 1036 if (std.mem.eql(u8, tab, "settings")) return if (segments.next() == null) forkSettingsRoute(identity, id) else null; 1037 if (std.mem.eql(u8, tab, "auth")) return if (segments.next() == null) forkAuthRoute(identity, id) else null; 1038 return null; 1039 } 1040 // "repo/<username>/<reponame>[/<tab tail>]"; the bare pair is the 1041 // files root 1042 if (std.mem.eql(u8, first, "repo")) { 1043 const rest = segments.rest(); // "username/reponame[/...]" 1044 const owner = segments.next() orelse return null; 1045 const repo_name = segments.next() orelse return null; 1046 // reject an empty username or reponame 1047 if (owner.len == 0 or repo_name.len == 0) return null; 1048 const pair = rest[0 .. owner.len + 1 + repo_name.len]; // "username/reponame" 1049 if (segments.peek() != null) { 1050 return repoSubRoute(pair, segments.rest()); 1051 } 1052 return repoFilesRoute(pair, null, "", "", 0); 1053 } 1054 return null; 1055 } 1056 1057 // parse a local-mode url, which elides the "/repo/<owner>/<name>" prefix: 1058 // "/" is the files root and the sub-paths follow directly. 1059 pub fn fromUrlLocal(path: []const u8) ?RoutablePage { 1060 if (std.mem.eql(u8, path, "/")) return repoFilesRoute("", null, "", "", 0); 1061 if (path.len < 2 or path[0] != '/') return null; 1062 return repoSubRoute("", path[1..]); 1063 } 1064 1065 // the url prefix for a repo route's identity: "/repo/<identity>", or "" 1066 // when the identity is elided. 1067 fn repoUrlPrefix(arena: *std.heap.ArenaAllocator, identity: []const u8) ![]const u8 { 1068 if (identity.len == 0) return ""; 1069 return std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}", .{identity}); 1070 } 1071 1072 // true when the page builds at its content height on the web, letting the 1073 // browser scroll the whole page, rather than being pinned to the viewport 1074 pub fn fullHeight(self: RoutablePage) bool { 1075 return switch (self) { 1076 .repo_issues => |i| i.view == .resolve, 1077 .repo_patches => |p| p.view == .resolve, 1078 else => false, 1079 }; 1080 } 1081 1082 // the "owner/name" a repo route carries, null for any other route. the 1083 // result borrows the route, so it must outlive the slice. 1084 pub fn repoIdentity(self: *const RoutablePage) ?[]const u8 { 1085 return switch (self.*) { 1086 .repo_files => |*f| f.name.slice(), 1087 .repo_commits => |*c| c.name.slice(), 1088 .repo_refs => |*r| r.name.slice(), 1089 .repo_issues => |*i| i.name.slice(), 1090 .repo_patches => |*p| p.name.slice(), 1091 .repo_discussions => |*t| t.name.slice(), 1092 .repo_events => |*e| e.name.slice(), 1093 .repo_settings, .repo_auth => |*name| name.slice(), 1094 else => null, 1095 }; 1096 } 1097 1098 pub fn forkRoute(self: *const RoutablePage) ?*const ForkRoute { 1099 return switch (self.*) { 1100 .fork_patch => |*f| f, 1101 .fork_files => |*f| &f.fork, 1102 .fork_commits => |*f| &f.fork, 1103 .fork_settings => |*f| f, 1104 .fork_auth => |*f| f, 1105 else => null, 1106 }; 1107 } 1108 1109 // the route this route's page lands on, with no tab or params: the users 1110 // list, a user's repos, or a repo's files root. 1111 pub fn pageRoot(self: RoutablePage) RoutablePage { 1112 return switch (self.parent()) { 1113 .home => .default, 1114 .user => switch (self) { 1115 .user_repos => |u| .{ .user_repos = .{ .name = u.name } }, 1116 .user_forks => |u| .{ .user_repos = .{ .name = u.name } }, 1117 .user_settings, .user_auth => |name| .{ .user_repos = .{ .name = name } }, 1118 else => self, 1119 }, 1120 // every repo route stores the same identity, so it always fits 1121 .repo => repoFilesRoute(self.repoIdentity() orelse return self, null, "", "", 0) orelse self, 1122 .fork => blk: { 1123 const f = self.forkRoute() orelse break :blk self; 1124 break :blk forkPatchRoute(f.name.slice(), f.id.slice()) orelse self; 1125 }, 1126 }; 1127 } 1128 1129 pub fn parent(self: RoutablePage) PageKind { 1130 return switch (self) { 1131 .home_users, .home_repos, .home_settings, .home_auth => .home, 1132 .user_repos, .user_forks, .user_settings, .user_auth => .user, 1133 .repo_files, .repo_commits, .repo_refs, .repo_issues, .repo_patches, .repo_discussions, .repo_events, .repo_settings, .repo_auth => .repo, 1134 .fork_patch, .fork_files, .fork_commits, .fork_settings, .fork_auth => .fork, 1135 }; 1136 } 1137 1138 // the dir a trailing "path:<dir>" names: the raw remainder ("" when 1139 // absent, null when malformed) 1140 fn pathValue(rest: []const u8) ?[]const u8 { 1141 if (rest.len == 0) return ""; 1142 if (!std.mem.startsWith(u8, rest, path_seg)) return null; 1143 const dir = rest[path_seg.len..]; 1144 return if (dir.len == 0) null else dir; 1145 } 1146 1147 // a list route's tail: at most a "start:<n>" param, null on anything else 1148 fn listStart(segments: *std.mem.SplitIterator(u8, .scalar)) ?usize { 1149 var params = Params{}; 1150 const word = params.scanTail(segments) catch return null; 1151 if (word != null or !params.only(&.{.start})) return null; 1152 return params.start(); 1153 } 1154 1155 // parse the sub-path after a repo route's identity ("" for a local 1156 // route): a tab keyword followed by "key:value" params in any order, plus 1157 // the files tab's trailing "path:<dir>". 1158 fn repoSubRoute(pair: []const u8, sub: []const u8) ?RoutablePage { 1159 var segments = std.mem.splitScalar(u8, sub, '/'); 1160 const tab = segments.next() orelse return null; 1161 var params = Params{}; 1162 if (std.mem.eql(u8, tab, "settings")) return if (segments.next() == null) .{ .repo_settings = Array(repo_route_max_len).from(pair) orelse return null } else null; 1163 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; 1164 if (std.mem.eql(u8, tab, "refs")) { 1165 const word = params.scanTail(&segments) catch return null; 1166 if (word != null or !params.only(&.{ .branch, .tag })) return null; 1167 const ref = (params.ref() catch return null) orelse return repoRefsRoute(pair, .branch, ""); 1168 const kind = std.meta.stringToEnum(RefKind, @tagName(ref.kind)) orelse return null; 1169 return repoRefsRoute(pair, kind, ref.value); 1170 } 1171 if (std.mem.startsWith(u8, tab, issue_seg)) { 1172 const issue_id = tab[issue_seg.len..]; 1173 if (issue_id.len == 0) return null; 1174 if (segments.peek()) |tail| { 1175 if (std.mem.startsWith(u8, tail, comment_seg)) { 1176 _ = segments.next(); 1177 const comment_id = tail[comment_seg.len..]; 1178 if (comment_id.len == 0) return null; 1179 const word = params.scanTail(&segments) catch return null; 1180 if (word) |last| { 1181 if (!params.only(&.{})) return null; 1182 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.issue, pair, issue_id, comment_id); 1183 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.issue, pair, issue_id, comment_id); 1184 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.issue, pair, issue_id, comment_id); 1185 return null; 1186 } 1187 if (!params.only(&.{.start})) return null; 1188 return repoThreadCommentRoute(.issue, pair, issue_id, comment_id, params.start() orelse return null); 1189 } 1190 } 1191 const word = params.scanTail(&segments) catch return null; 1192 if (word) |tail| { 1193 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.issue, pair, issue_id, "") else null; 1194 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.issue, pair, issue_id) else null; 1195 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.issue, pair, issue_id, "") else null; 1196 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.issue, pair, issue_id) else null; 1197 if (std.mem.eql(u8, tail, "resolve")) { 1198 if (!params.only(&.{.theirs})) return null; 1199 return repoIssuesResolveRoute(pair, issue_id, params.values.get(.theirs) orelse ""); 1200 } 1201 return null; 1202 } 1203 if (!params.only(&.{.start})) return null; 1204 return repoThreadCommentsRoute(.issue, pair, issue_id, params.start() orelse return null); 1205 } 1206 if (std.mem.startsWith(u8, tab, patch_seg)) { 1207 const patch_id = tab[patch_seg.len..]; 1208 if (patch_id.len == 0) return null; 1209 if (segments.peek()) |tail| { 1210 if (std.mem.startsWith(u8, tail, comment_seg)) { 1211 _ = segments.next(); 1212 const comment_id = tail[comment_seg.len..]; 1213 if (comment_id.len == 0) return null; 1214 const word = params.scanTail(&segments) catch return null; 1215 if (word) |last| { 1216 if (!params.only(&.{})) return null; 1217 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.patch, pair, patch_id, comment_id); 1218 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.patch, pair, patch_id, comment_id); 1219 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.patch, pair, patch_id, comment_id); 1220 return null; 1221 } 1222 if (!params.only(&.{.start})) return null; 1223 return repoThreadCommentRoute(.patch, pair, patch_id, comment_id, params.start() orelse return null); 1224 } 1225 } 1226 const word = params.scanTail(&segments) catch return null; 1227 if (word) |tail| { 1228 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.patch, pair, patch_id, "") else null; 1229 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.patch, pair, patch_id) else null; 1230 if (std.mem.eql(u8, tail, "publish")) return if (params.only(&.{})) repoPatchPublishRoute(pair, patch_id) else null; 1231 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.patch, pair, patch_id, "") else null; 1232 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.patch, pair, patch_id) else null; 1233 if (std.mem.eql(u8, tail, "resolve")) { 1234 if (!params.only(&.{.theirs})) return null; 1235 return repoPatchesResolveRoute(pair, patch_id, params.values.get(.theirs) orelse ""); 1236 } 1237 return null; 1238 } 1239 if (!params.only(&.{.start})) return null; 1240 return repoThreadCommentsRoute(.patch, pair, patch_id, params.start() orelse return null); 1241 } 1242 if (std.mem.startsWith(u8, tab, discuss_seg)) { 1243 const discussion_id = tab[discuss_seg.len..]; 1244 if (discussion_id.len == 0) return null; 1245 if (segments.peek()) |tail| { 1246 if (std.mem.startsWith(u8, tail, comment_seg)) { 1247 _ = segments.next(); 1248 const comment_id = tail[comment_seg.len..]; 1249 if (comment_id.len == 0) return null; 1250 const word = params.scanTail(&segments) catch return null; 1251 if (word) |last| { 1252 if (!params.only(&.{})) return null; 1253 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.discuss, pair, discussion_id, comment_id); 1254 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.discuss, pair, discussion_id, comment_id); 1255 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.discuss, pair, discussion_id, comment_id); 1256 return null; 1257 } 1258 if (!params.only(&.{.start})) return null; 1259 return repoThreadCommentRoute(.discuss, pair, discussion_id, comment_id, params.start() orelse return null); 1260 } 1261 } 1262 const word = params.scanTail(&segments) catch return null; 1263 if (word) |tail| { 1264 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.discuss, pair, discussion_id, "") else null; 1265 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.discuss, pair, discussion_id) else null; 1266 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.discuss, pair, discussion_id, "") else null; 1267 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.discuss, pair, discussion_id) else null; 1268 return null; 1269 } 1270 if (!params.only(&.{.start})) return null; 1271 return repoThreadCommentsRoute(.discuss, pair, discussion_id, params.start() orelse return null); 1272 } 1273 if (std.mem.eql(u8, tab, "issues")) { 1274 // the word is a list view; a filter with neither is never emitted. 1275 const word = params.scanTail(&segments) catch return null; 1276 const tag_value = params.values.get(.tag) orelse ""; 1277 const w = word orelse return if (params.only(&.{})) repoIssuesRoute(pair, .open, "", "") else null; 1278 // the conflicts list windows by an issue id, not a number, so it 1279 // reads the start param raw 1280 if (std.mem.eql(u8, w, "conflicts")) { 1281 if (!params.only(&.{.start})) return null; 1282 return repoIssuesConflictsRoute(pair, params.values.get(.start) orelse ""); 1283 } 1284 if (!params.only(&.{.tag})) return null; 1285 if (std.meta.stringToEnum(evt.Issue.Status, w)) |status| return repoIssuesRoute(pair, status, tag_value, ""); 1286 if (std.mem.eql(u8, w, "tags")) return repoThreadTagsRoute(.issue, pair, tag_value); 1287 if (std.mem.eql(u8, w, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.issue, pair) else null; 1288 return null; 1289 } 1290 if (std.mem.eql(u8, tab, "patches")) { 1291 const word = params.scanTail(&segments) catch return null; 1292 const tag_value = params.values.get(.tag) orelse ""; 1293 const view = word orelse return if (params.only(&.{})) repoPatchesRoute(pair, .open, "", "") else null; 1294 if (std.mem.eql(u8, view, "conflicts")) { 1295 if (!params.only(&.{.start})) return null; 1296 return repoPatchesConflictsRoute(pair, params.values.get(.start) orelse ""); 1297 } 1298 if (!params.only(&.{.tag})) return null; 1299 if (std.meta.stringToEnum(evt.Patch.Status, view)) |status| return repoPatchesRoute(pair, status, tag_value, ""); 1300 if (std.mem.eql(u8, view, "tags")) return repoThreadTagsRoute(.patch, pair, tag_value); 1301 if (std.mem.eql(u8, view, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.patch, pair) else null; 1302 if (std.mem.eql(u8, view, "drafts")) return if (tag_value.len == 0) repoPatchesDraftsRoute(pair) else null; 1303 return null; 1304 } 1305 if (std.mem.eql(u8, tab, "discussions")) { 1306 const word = params.scanTail(&segments) catch return null; 1307 const tag_value = params.values.get(.tag) orelse ""; 1308 const view = word orelse return if (params.only(&.{})) repoDiscussionsRoute(pair, "", "") else null; 1309 if (!params.only(&.{.tag})) return null; 1310 if (std.mem.eql(u8, view, "recent")) return repoDiscussionsRoute(pair, tag_value, ""); 1311 if (std.mem.eql(u8, view, "tags")) return repoThreadTagsRoute(.discuss, pair, tag_value); 1312 if (std.mem.eql(u8, view, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.discuss, pair) else null; 1313 return null; 1314 } 1315 if (std.mem.eql(u8, tab, "events")) { 1316 const view = if (segments.next()) |word| 1317 std.meta.stringToEnum(EventsView, word) orelse return null 1318 else 1319 .active; 1320 return if (segments.next() == null) repoEventsRoute(pair, view, null, "") else null; 1321 } 1322 if (std.mem.startsWith(u8, tab, "event:")) { 1323 const event_id = tab["event:".len..]; 1324 const kind_segment = segments.next() orelse return null; 1325 if (!std.mem.startsWith(u8, kind_segment, "kind:")) return null; 1326 const kind = std.meta.stringToEnum(evt.EventKind, kind_segment["kind:".len..]) orelse return null; 1327 if (event_id.len == 0 or segments.next() != null) return null; 1328 return repoEventsRoute(pair, .active, kind, event_id); 1329 } 1330 if (std.mem.eql(u8, tab, files_seg)) { 1331 params.scanPairs(&segments) catch return null; 1332 if (!params.only(&.{ .line, .branch, .tag, .object })) return null; 1333 const line = params.line() orelse return null; 1334 const dir = pathValue(segments.rest()) orelse return null; 1335 const ref = (params.ref() catch return null) orelse 1336 return if (dir.len == 0) repoFilesRoute(pair, null, "", "", line) else null; 1337 return repoFilesRoute(pair, ref.kind, ref.value, dir, line); 1338 } 1339 if (std.mem.eql(u8, tab, commits_seg)) { 1340 params.scanPairs(&segments) catch return null; 1341 if (!params.only(&.{ .start, .branch, .tag, .object })) return null; 1342 const start = params.start() orelse return null; 1343 const ref = (params.ref() catch return null) orelse { 1344 const file = pathValue(segments.rest()) orelse return null; 1345 return if (file.len == 0) repoCommitsRoute(pair, null, "", start, "") else null; 1346 }; 1347 // the message page names its commit and nothing else 1348 if (std.mem.eql(u8, segments.rest(), message_seg)) 1349 return if (start == 0) repoCommitMessageRoute(pair, ref.kind, ref.value) else null; 1350 const file = pathValue(segments.rest()) orelse return null; 1351 return repoCommitsRoute(pair, ref.kind, ref.value, start, file); 1352 } 1353 return null; // unknown sub-path 1354 } 1355 1356 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 1357 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1358 return switch (a) { 1359 .home_users => |a_start| a_start == b.home_users, 1360 .home_repos => |a_start| a_start == b.home_repos, 1361 .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, 1362 .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, 1363 .user_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.user_settings.slice()), 1364 .user_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.user_auth.slice()), 1365 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 1366 a_f.ref_kind == b.repo_files.ref_kind and 1367 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 1368 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 1369 a_f.line == b.repo_files.line, 1370 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 1371 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 1372 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 1373 switch (a_c.content) { 1374 .diff => |a_d| switch (b.repo_commits.content) { 1375 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1376 .message => false, 1377 }, 1378 .message => std.meta.activeTag(b.repo_commits.content) == .message, 1379 }, 1380 .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()), 1381 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 1382 std.mem.eql(u8, a_i.tag.slice(), b.repo_issues.tag.slice()) and 1383 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 1384 std.mem.eql(u8, a_i.theirs.slice(), b.repo_issues.theirs.slice()) and 1385 a_i.view == b.repo_issues.view and 1386 a_i.comments_start == b.repo_issues.comments_start and 1387 std.mem.eql(u8, a_i.comment.slice(), b.repo_issues.comment.slice()), 1388 .repo_discussions => |a_t| std.mem.eql(u8, a_t.name.slice(), b.repo_discussions.name.slice()) and 1389 std.mem.eql(u8, a_t.tag.slice(), b.repo_discussions.tag.slice()) and 1390 std.mem.eql(u8, a_t.selected.slice(), b.repo_discussions.selected.slice()) and 1391 a_t.view == b.repo_discussions.view and 1392 a_t.comments_start == b.repo_discussions.comments_start and 1393 std.mem.eql(u8, a_t.comment.slice(), b.repo_discussions.comment.slice()), 1394 .repo_patches => |a_p| std.mem.eql(u8, a_p.name.slice(), b.repo_patches.name.slice()) and 1395 std.mem.eql(u8, a_p.tag.slice(), b.repo_patches.tag.slice()) and 1396 std.mem.eql(u8, a_p.selected.slice(), b.repo_patches.selected.slice()) and 1397 std.mem.eql(u8, a_p.theirs.slice(), b.repo_patches.theirs.slice()) and 1398 a_p.view == b.repo_patches.view and 1399 a_p.comments_start == b.repo_patches.comments_start and 1400 std.mem.eql(u8, a_p.comment.slice(), b.repo_patches.comment.slice()), 1401 .repo_events => |a_e| std.mem.eql(u8, a_e.name.slice(), b.repo_events.name.slice()) and 1402 a_e.view == b.repo_events.view and 1403 a_e.kind == b.repo_events.kind and 1404 std.mem.eql(u8, a_e.selected.slice(), b.repo_events.selected.slice()), 1405 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 1406 .repo_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_auth.slice()), 1407 .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()), 1408 .fork_files => |a_f| std.mem.eql(u8, a_f.fork.name.slice(), b.fork_files.fork.name.slice()) and 1409 std.mem.eql(u8, a_f.fork.id.slice(), b.fork_files.fork.id.slice()) and 1410 std.mem.eql(u8, a_f.oid.slice(), b.fork_files.oid.slice()) and 1411 std.mem.eql(u8, a_f.path.slice(), b.fork_files.path.slice()) and 1412 a_f.line == b.fork_files.line, 1413 .fork_commits => |a_c| std.mem.eql(u8, a_c.fork.name.slice(), b.fork_commits.fork.name.slice()) and 1414 std.mem.eql(u8, a_c.fork.id.slice(), b.fork_commits.fork.id.slice()) and 1415 std.mem.eql(u8, a_c.oid.slice(), b.fork_commits.oid.slice()) and 1416 switch (a_c.content) { 1417 .diff => |a_d| switch (b.fork_commits.content) { 1418 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1419 .message => false, 1420 }, 1421 .message => std.meta.activeTag(b.fork_commits.content) == .message, 1422 }, 1423 .fork_settings => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_settings.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_settings.id.slice()), 1424 .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()), 1425 else => true, 1426 }; 1427 } 1428 1429 // true when following a link from `b` to `a` (both repo routes) should reload 1430 // rather than stay in page 1431 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 1432 if (a.parent() != .repo or b.parent() != .repo) return false; 1433 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 1434 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 1435 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 1436 return !a.eql(b); 1437 } 1438 1439 pub fn forkPageChanged(a: RoutablePage, b: RoutablePage) bool { 1440 return a.parent() == .fork and b.parent() == .fork and !a.eql(b); 1441 } 1442 1443 // true when `a` and `b` are the same user paginated to a different repos 1444 // window. switching between a user's tabs is in-page, so 1445 // only a changed `start` on the repos list navigates. 1446 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 1447 return switch (a) { 1448 .user_repos => |aa| switch (b) { 1449 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1450 else => false, 1451 }, 1452 .user_forks => |aa| switch (b) { 1453 .user_forks => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1454 else => false, 1455 }, 1456 else => false, 1457 }; 1458 } 1459 1460 // true when `a` and `b` are the same home list tab paginated to a different 1461 // window. switching between the users/repos tabs is in-page (the home page 1462 // holds both lists), so only a changed `after` on the same tab navigates. 1463 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 1464 return switch (a) { 1465 .home_users => |aa| switch (b) { 1466 .home_users => |bb| aa != bb, 1467 else => false, 1468 }, 1469 .home_repos => |aa| switch (b) { 1470 .home_repos => |bb| aa != bb, 1471 else => false, 1472 }, 1473 else => false, 1474 }; 1475 } 1476 }; 1477 1478 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 1479 fn isUnreserved(c: u8) bool { 1480 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 1481 } 1482 1483 // percent-encode a ref name for use as a single url path segment. 1484 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 1485 var out: std.Io.Writer.Allocating = .init(aa); 1486 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 1487 return out.written(); 1488 } 1489 1490 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 1491 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1492 return struct { 1493 pub const hex_len = hash.hexLen(repo_opts.hash); 1494 1495 // the concrete ref/oid (a null request resolves to the default branch) 1496 ref_or_oid: RoutablePage.RefOrOid, 1497 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 1498 // the route layer — which splits on '/' — stores them encoded). duped 1499 // into `aa`. 1500 value: []const u8, 1501 // the commit oid it points at 1502 oid: [hex_len]u8, 1503 1504 const Self = @This(); 1505 1506 // resolve a requested ref/oid (null = the repo's default branch) to a 1507 // concrete ref/oid plus the commit oid it points at. `requested_value` is 1508 // the url-encoded form (as it appears in the route). null when it doesn't 1509 // resolve (an unknown branch/tag, or a malformed/unknown oid). 1510 pub fn init( 1511 repo: *rp.Repo(repo_kind, repo_opts), 1512 io: std.Io, 1513 aa: std.mem.Allocator, 1514 requested_ref_or_oid: ?RoutablePage.RefOrOid, 1515 requested_value: []const u8, 1516 ) !?Self { 1517 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 1518 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 1519 var value: []const u8 = if (requested_ref_or_oid == null) 1520 requested_value 1521 else 1522 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 1523 // no ref named: fall back to HEAD's branch (or its oid when detached). 1524 if (requested_ref_or_oid == null) { 1525 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1526 if (repo.head(io, &head_buf)) |head| switch (head) { 1527 .ref => |r| { 1528 ref_or_oid = .branch; 1529 value = r.name; 1530 }, 1531 .oid => |o| { 1532 ref_or_oid = .object; 1533 value = o; 1534 }, 1535 } else |_| {} 1536 } 1537 1538 var oid: [hex_len]u8 = undefined; 1539 switch (ref_or_oid) { 1540 .object => { 1541 if (value.len != hex_len) return null; 1542 @memcpy(&oid, value); 1543 }, 1544 .branch, .tag => { 1545 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 1546 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 1547 }, 1548 } 1549 // store url-encoded so the route layer can hold the value verbatim. 1550 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 1551 } 1552 }; 1553 } 1554 1555 // where a repo page reads its on-disk repo from 1556 pub const RepoSource = struct { 1557 path: []const u8, 1558 repo_kind: rp.RepoKind, 1559 // the user's global git config, so local mode picks up their identity and 1560 // their signing and ssh settings. null on the server paths, which must not 1561 // read whoever runs the server. 1562 global_config_path: ?[]const u8 = null, 1563 1564 pub fn localInitOpts(self: RepoSource) rp.InitOpts { 1565 return .{ .path = self.path, .global_config_path = self.global_config_path }; 1566 } 1567 }; 1568 1569 // events created in local mode have no account behind them, so the commit 1570 // author comes from the repo's config, which the global config feeds into. 1571 pub const local_author_fallback = evt.CommitAuthor{ .name = "haxy", .email = "user@haxy" }; 1572 1573 pub fn localAuthor( 1574 src: RepoSource, 1575 io: std.Io, 1576 allocator: std.mem.Allocator, 1577 arena: *std.heap.ArenaAllocator, 1578 ) !evt.CommitAuthor { 1579 switch (src.repo_kind) { 1580 inline else => |repo_kind| { 1581 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()) catch return local_author_fallback; 1582 defer any_repo.deinit(io, allocator); 1583 1584 switch (any_repo) { 1585 inline else => |*repo| { 1586 var config = try repo.listConfig(io, allocator); 1587 defer config.deinit(); 1588 1589 const user = config.sections.get("user") orelse return local_author_fallback; 1590 const name = user.get("name") orelse return local_author_fallback; 1591 const email = user.get("email") orelse return local_author_fallback; 1592 return .{ 1593 .name = try arena.allocator().dupe(u8, name), 1594 .email = try arena.allocator().dupe(u8, email), 1595 }; 1596 }, 1597 } 1598 }, 1599 } 1600 } 1601 1602 // per-connection mutable state. each SSH session / web session / local TUI 1603 // run gets its own. 1604 pub const Session = struct { 1605 data: Data = .{}, 1606 // session-lifetime allocations: login/user_id, persisted prefs, queued 1607 // actions. lives as long as the connection. 1608 arena: *std.heap.ArenaAllocator, 1609 // current page's allocations: page data and the page-scoped strings widgets 1610 // build (form actions, links). owned by Nav, swapped on each navigation, so 1611 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 1612 // where a render is one-shot, this points at the same arena as `arena`. 1613 page_arena: *std.heap.ArenaAllocator, 1614 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 1615 // filesystem io and the path to <server>/repos, for opening on-disk repos 1616 // during page construction. both null on wasm, which has no filesystem and 1617 // rebuilds pages from the serialized snapshot rather than from disk. 1618 io: ?std.Io = null, 1619 admin_repo: ?*rp.Repo(.xit, evt.admin_repo_opts) = null, 1620 repos_dir: ?[]const u8 = null, 1621 // the single on-disk repo this session views, when running in local mode 1622 // (haxy invoked with no arguments inside a repo). null on the server paths, 1623 // which resolve repos from the admin db instead. 1624 local: ?RepoSource = null, 1625 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 1626 // focus id -> the live TextInput, refreshed each frame by the views that own 1627 // inputs. web/wasm form handling looks widgets up here by focus id. 1628 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput) = .empty, 1629 nav_back: bool = false, // set by input (escape) to request the native TUI pop a page; see Nav 1630 refresh_requested: bool = false, // set by input (ctrl+r) 1631 // a requested forward navigation. set this (via navigate) to move to a new 1632 // page; Nav.sync builds it and then copies it into current_page, clearing 1633 // this back to null. setting current_page directly only updates the url and 1634 // does not navigate. 1635 next_page: ?RoutablePage = null, 1636 is_terminal: bool = false, // true on remote SSH and local TUI 1637 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 1638 // url. null on the web itself (no footer there). 1639 web_port: ?u16 = null, 1640 // a host operation requested by a widget 1641 host_request: ?HostRequest = null, 1642 quit_requested: bool = false, 1643 1644 pub const HostRequest = union(enum) { 1645 show_copyable_text: []const u8, 1646 sync_events, 1647 }; 1648 1649 const Self = @This(); 1650 1651 // serializable data sent down to web client 1652 pub const Data = struct { 1653 user_id: ?[]const u8 = null, 1654 // the logged-in user's name, for the views that show it 1655 user_name: ?[]const u8 = null, 1656 // a transient outcome to surface from the last /login POST attempt 1657 login_failure: ?Home.Auth.Login.Failure = null, 1658 // a transient local event-sync error 1659 sync_failure: ?[]const u8 = null, 1660 current_page: RoutablePage = .default, 1661 // whether to render the ANSI art backdrop 1662 enable_ansi: bool = true, 1663 // the selected ANSI art. the server serializes one image for WASM so 1664 // the browser does not need the whole build-generated collection. 1665 ansi_art: []const u8 = "", 1666 // the bound git-service ports. absent in local mode, where remote urls 1667 // are not shown. 1668 git_http_port: ?u16 = null, 1669 git_ssh_port: ?u16 = null, 1670 git_ssh_prefix: []const u8 = "", 1671 // true when this session views a single local repo. unlike `local` 1672 // (the host-side filesystem source) this travels in the snapshot, so 1673 // the wasm side also parses elided link urls and hides the multi-user 1674 // chrome. 1675 is_local: bool = false, 1676 }; 1677 1678 // a user-initiated state change. widgets enqueue these on the session during 1679 // input instead of mutating state or touching the DB themselves; the host 1680 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 1681 // side effects out of the widget tree and gives every render path one place to 1682 // turn a UI action into a state change + event. 1683 pub const Action = union(enum) { 1684 toggle_ansi, 1685 }; 1686 1687 pub fn init( 1688 arena: *std.heap.ArenaAllocator, 1689 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1690 data: Data, 1691 ) !Self { 1692 var session = Self{ 1693 .data = data, 1694 .arena = arena, 1695 // until a host swaps in a page-scoped arena (Nav does this), page 1696 // allocations land in the session arena. on the web path that's the 1697 // intended behavior, since the whole arena is per-request. 1698 .page_arena = arena, 1699 .admin_repo = repo, 1700 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 1701 }; 1702 try session.loadUser(); 1703 return session; 1704 } 1705 1706 // load the logged-in user's name and persisted preferences from the db 1707 pub fn loadUser(self: *Self) !void { 1708 const user_id = self.data.user_id orelse return; 1709 const moment = self.haxy_moment orelse return; 1710 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.arena, user_id)) |user| { 1711 self.data.user_name = user.event.name; 1712 self.data.enable_ansi = user.event.enable_ansi; 1713 } 1714 } 1715 1716 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 1717 const bytes = self.data.user_id orelse return null; 1718 if (bytes.len != evt.event_id_size) return null; 1719 var id: [evt.event_id_size]u8 = undefined; 1720 @memcpy(&id, bytes); 1721 return id; 1722 } 1723 1724 // the commit author for an event this session creates, or null when it may 1725 // not create one 1726 pub fn eventAuthor(self: *Self) !?evt.CommitAuthor { 1727 if (self.data.is_local) { 1728 const src = self.local orelse return local_author_fallback; 1729 const io = self.io orelse return local_author_fallback; 1730 return try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 1731 } 1732 const user_id = self.data.user_id orelse return null; 1733 const moment = self.haxy_moment orelse return null; 1734 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.page_arena, user_id)) orelse return null; 1735 return .{ .name = user.event.name, .email = user.event.email }; 1736 } 1737 1738 // queue an action for the host to drain this frame. 1739 pub fn push(self: *Self, action: Action) !void { 1740 try self.pending.append(self.arena.allocator(), action); 1741 } 1742 1743 // apply a single action's in-memory effect only (no persistence). 1744 fn apply(self: *Self, action: Action) void { 1745 switch (action) { 1746 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 1747 } 1748 } 1749 1750 // drain the session's queued actions by applying them to the session. 1751 // used on the wasm path, which has no repo to persist to. 1752 pub fn applyPending(self: *Self) void { 1753 for (self.pending.items) |action| self.apply(action); 1754 self.pending.clearRetainingCapacity(); 1755 } 1756 1757 // drain the session's queued actions by applying them to the session and 1758 // writing them to the db 1759 pub fn applyAndWritePending( 1760 self: *Self, 1761 io: std.Io, 1762 allocator: std.mem.Allocator, 1763 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1764 ) !void { 1765 defer self.pending.clearRetainingCapacity(); 1766 for (self.pending.items) |action| { 1767 self.apply(action); 1768 switch (action) { 1769 .toggle_ansi => if (self.data.user_id) |user_id| { 1770 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 1771 }, 1772 } 1773 } 1774 } 1775 1776 // reload the moment from the admin repo and re-read user preferences 1777 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 1778 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 1779 self.haxy_moment = moment; 1780 1781 const user_id = self.data.user_id orelse return; 1782 var arena = std.heap.ArenaAllocator.init(allocator); 1783 defer arena.deinit(); 1784 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, user_id)) |user| { 1785 self.data.enable_ansi = user.event.enable_ansi; 1786 } 1787 } 1788 1789 // request a forward navigation to `route`; the host consumes next_page 1790 // (Nav.sync on the terminal, the wasm tick on the web). 1791 pub fn navigate(self: *Session, route: RoutablePage) !void { 1792 self.next_page = route; 1793 } 1794 }; 1795 1796 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 1797 var nav = try Nav.init(allocator, session); 1798 defer nav.deinit(allocator); 1799 1800 var terminal = try term.Terminal.init(io, allocator); 1801 var terminal_live = true; 1802 defer if (terminal_live) terminal.deinit(io); 1803 1804 // set term as active so it will be properly cooked 1805 // when a panic/segfault happens 1806 term.setActive(&terminal); 1807 defer term.setActive(null); 1808 1809 while (!terminal.shouldQuit()) { 1810 const grid_changed = try terminal.render(&nav.root); 1811 1812 // process any inputs. 1813 // 1814 // if the grid didn't change, then first do a blocking 1815 // read, so the thread will sleep until further input. 1816 // after that, all remaining reads are non-blocking so 1817 // we can process the rest of the queued inputs. 1818 // 1819 // if the grid *did* change, then only do non-blocking 1820 // reads. we do not want to sleep the thread because 1821 // there may be an animation that requires more looping. 1822 var blocking = !grid_changed; 1823 while (try terminal.readKey(io, blocking)) |key| { 1824 blocking = false; 1825 try inputKey(allocator, &nav.root, key, session); 1826 } 1827 1828 if (session.host_request) |request| { 1829 session.host_request = null; 1830 switch (request) { 1831 .show_copyable_text => |copyable_text| { 1832 term.setActive(null); 1833 terminal.deinit(io); 1834 terminal_live = false; 1835 showCopyableText(io, copyable_text) catch |show_err| { 1836 terminal = try term.Terminal.init(io, allocator); 1837 terminal_live = true; 1838 term.setActive(&terminal); 1839 return show_err; 1840 }; 1841 terminal = try term.Terminal.init(io, allocator); 1842 terminal_live = true; 1843 term.setActive(&terminal); 1844 }, 1845 .sync_events => { 1846 try nav.root.build(allocator, .{ 1847 .min_size = .{ .width = null, .height = null }, 1848 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 1849 }, nav.root.getFocus()); 1850 _ = try terminal.render(&nav.root); 1851 try Repo.Events.performSync(allocator, session); 1852 }, 1853 } 1854 } 1855 1856 // persist queued actions when there's a repo; local mode has none, 1857 // so just apply in-memory 1858 if (repo_maybe) |repo| { 1859 try session.applyAndWritePending(io, allocator, repo); 1860 // reload so the next navigation builds its page from the current moment 1861 try session.reloadMoment(allocator, repo); 1862 } else { 1863 session.applyPending(); 1864 } 1865 1866 // reconcile navigation: forward to a new page, or back on escape. 1867 try nav.sync(allocator, session); 1868 1869 // the quit button (on the quit tab) asks the host to tear down. 1870 if (session.quit_requested) terminal.requestQuit(); 1871 1872 try nav.root.build(allocator, .{ 1873 .min_size = .{ .width = null, .height = null }, 1874 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 1875 }, nav.root.getFocus()); 1876 } 1877 } 1878 1879 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 1880 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 1881 null 1882 else 1883 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 1884 defer if (tty) |file| file.close(io); 1885 1886 const input = if (tty) |file| file else std.Io.File.stdin(); 1887 const output = if (tty) |file| file else std.Io.File.stdout(); 1888 1889 var write_buf: [1024]u8 = undefined; 1890 var writer = output.writer(io, &write_buf); 1891 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}); 1892 try writer.interface.flush(); 1893 1894 var read_buf: [1]u8 = undefined; 1895 var reader = input.reader(io, &read_buf); 1896 while (true) { 1897 const byte = try reader.interface.takeByte(); 1898 if (byte == '\r' or byte == '\n') return; 1899 } 1900 } 1901 1902 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 1903 const root_focus = root.getFocus(); 1904 const focused_before = root_focus.grandchild_id; 1905 var activate_in_page_link = false; 1906 switch (key) { 1907 // request a navigation pop; the host's Nav.sync goes back a page, or 1908 // quits when there's no history left. 1909 .escape => { 1910 session.nav_back = true; 1911 return; 1912 }, 1913 .ctrl => |letter| switch (letter) { 1914 'r' => { 1915 session.refresh_requested = true; 1916 return; 1917 }, 1918 else => try root.input(allocator, key, root_focus), 1919 }, 1920 .enter => { 1921 if (root_focus.grandchild_id) |gid| { 1922 // follow a cross-page link 1923 if (crossPageLink(root_focus, gid, session.data)) |route| { 1924 return session.navigate(route); 1925 } 1926 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 1927 } 1928 activate_in_page_link = true; 1929 try root.input(allocator, key, root_focus); 1930 }, 1931 .mouse => |mouse| { 1932 if (mouse.action == .press and mouse.action.press == .left) { 1933 var clicked: ?usize = null; 1934 var iter = root_focus.children.iterator(); 1935 while (iter.next()) |entry| { 1936 const child = entry.value_ptr.*; 1937 if (!child.focus.focusable) continue; 1938 const r = child.rect; 1939 if (mouse.x >= r.x and mouse.y >= r.y and 1940 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 1941 { 1942 clicked = entry.key_ptr.*; 1943 break; 1944 } 1945 } 1946 if (clicked) |focus_id| { 1947 // follow a cross-page link 1948 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 1949 return session.navigate(route); 1950 } 1951 if (rawLink(root_focus, focus_id)) |url| { 1952 if (!mouse.ctrl) try requestRawLink(session, url); 1953 return; 1954 } 1955 root_focus.setFocus(focus_id); 1956 activate_in_page_link = true; 1957 } 1958 // forward the press into the widget tree so buttons (and any 1959 // future click-aware widgets) can react. widgets that don't 1960 // care about presses ignore it. 1961 try root.input(allocator, key, root_focus); 1962 } else { 1963 try root.input(allocator, key, root_focus); 1964 } 1965 }, 1966 else => try root.input(allocator, key, root_focus), 1967 } 1968 1969 const focused_after = root_focus.grandchild_id; 1970 if (!activate_in_page_link and focused_before == focused_after) return; 1971 const focus_id = focused_after orelse return; 1972 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 1973 } 1974 1975 // a link to bytes the server serves directly rather than to a page route, so 1976 // it never resolves to a RoutablePage 1977 pub const raw_link_prefix = "ax:"; 1978 1979 // a route already loaded into the current page; focusing it changes the url 1980 // without rebuilding the page 1981 pub const in_page_link_prefix = "ai:"; 1982 1983 // an in-page tab's base route, or the exact current route when it is selected 1984 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 1985 const target = if (selected) session.data.current_page else route; 1986 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 1987 } 1988 1989 // a button the web renderer covers with a file picker that posts the chosen 1990 // file to the url after the prefix 1991 pub const file_input_prefix = "file:"; 1992 1993 // a submit button that overrides its enclosing form's action 1994 pub const submit_action_prefix = "submit:"; 1995 1996 // the url an `ax:` link points at, or null for anything else. a host that 1997 // suppresses the anchor's own navigation needs this to follow the link. 1998 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 1999 const child = root_focus.children.get(focus_id) orelse return null; 2000 const custom = switch (child.focus.kind) { 2001 .custom => |c| c, 2002 else => return null, 2003 }; 2004 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2005 return custom[raw_link_prefix.len..]; 2006 } 2007 2008 fn requestRawLink(session: *Session, url: []const u8) !void { 2009 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2010 } 2011 2012 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2013 return if (session.web_port) |port| 2014 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2015 else 2016 path; 2017 } 2018 2019 // if the focus target at focus_id is an `a:` link that should navigate (a 2020 // different parent page, or a different files directory within the repo page), 2021 // return its route; otherwise null. lets a host turn a click / enter on such a 2022 // link into a navigation rather than a focus change. 2023 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2024 const current = data.current_page; 2025 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2026 // a link to a different parent page always navigates; within a page, a 2027 // files-directory / commits-page / list-window change navigates while tab 2028 // links stay in-page. 2029 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; 2030 return null; 2031 } 2032 2033 // the route carried by an `ai:` focus target, or null for any other target 2034 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2035 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2036 } 2037 2038 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2039 const child = root_focus.children.get(focus_id) orelse return null; 2040 const custom = switch (child.focus.kind) { 2041 .custom => |c| c, 2042 else => return null, 2043 }; 2044 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2045 const path = custom[prefix.len..]; 2046 // local sessions build (and parse) links with the repo identity elided 2047 return if (data.is_local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2048 } 2049 2050 // a display author, resolved against the admin db at read time 2051 pub const Author = union(enum) { 2052 unknown, 2053 email: []const u8, // an email no user matches 2054 user_name: []const u8, 2055 2056 // the commit author line's email, resolved like initFromEmail 2057 pub fn init( 2058 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2059 arena: *std.heap.ArenaAllocator, 2060 author_line: []const u8, 2061 ) !Author { 2062 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2063 } 2064 2065 // from a record's stored author email: it resolves to the user currently 2066 // holding it, else stays the email. the result is allocated in `arena`, 2067 // so the email may borrow transient memory. 2068 pub fn initFromEmail( 2069 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2070 arena: *std.heap.ArenaAllocator, 2071 author_email: ?[]const u8, 2072 ) !Author { 2073 const email = author_email orelse return .unknown; 2074 if (haxy_moment) |moment| { 2075 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2076 return .{ .user_name = user.event.name }; 2077 } 2078 } 2079 return .{ .email = try arena.allocator().dupe(u8, email) }; 2080 } 2081 }; 2082 2083 // the "a:" link to user `name`'s page. 2084 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2085 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2086 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2087 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2088 } 2089 2090 // a focusable " author " box showing `author`, linking to their user page 2091 // when it is a known user 2092 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2093 const text = switch (author) { 2094 .unknown => "", 2095 .email, .user_name => |t| t, 2096 }; 2097 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2098 errdefer tb.deinit(allocator); 2099 tb.getFocus().focusable = true; 2100 switch (author) { 2101 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2102 else => {}, 2103 } 2104 return tb; 2105 } 2106 2107 // native-TUI navigation 2108 pub const Nav = struct { 2109 root: Widget, 2110 route: RoutablePage, 2111 // backs the current page (its Page data and the page-scoped strings its 2112 // widgets build). owned here, swapped on each navigation. session.page_arena 2113 // tracks whichever of these is current so widgets allocate into it. 2114 arena: *std.heap.ArenaAllocator, 2115 history: std.ArrayList(Entry), 2116 2117 // each retained page keeps its own arena, freed when the page leaves the 2118 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2119 // long-lived session from accumulating every page it ever visited. 2120 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2121 2122 // cap on retained back-history so the chain can't grow memory without bound 2123 const max_history: usize = 16; 2124 2125 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2126 const arena = try allocator.create(std.heap.ArenaAllocator); 2127 arena.* = std.heap.ArenaAllocator.init(allocator); 2128 errdefer { 2129 arena.deinit(); 2130 allocator.destroy(arena); 2131 } 2132 2133 session.page_arena = arena; 2134 const route = session.data.current_page; 2135 2136 const page = try arena.allocator().create(Page); 2137 page.* = try Page.init(arena, session, route); 2138 return .{ 2139 .root = try initRoot(allocator, page, session), 2140 .route = route, 2141 .arena = arena, 2142 .history = .empty, 2143 }; 2144 } 2145 2146 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2147 self.root.deinit(allocator); 2148 freeArena(allocator, self.arena); 2149 for (self.history.items) |*entry| { 2150 entry.root.deinit(allocator); 2151 freeArena(allocator, entry.arena); 2152 } 2153 self.history.deinit(allocator); 2154 } 2155 2156 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2157 arena.deinit(); 2158 allocator.destroy(arena); 2159 } 2160 2161 // reconcile the displayed root with the session's navigation state. a 2162 // forward nav (current_page moved to a different parent page) pushes the 2163 // current root and builds the new page in a fresh arena; a back request 2164 // frees the current page and restores the previous one. when escape is 2165 // pressed with no history left we switch to the quit tab instead of quitting. 2166 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2167 // refresh: rebuild the current page in place from a current moment 2168 if (session.refresh_requested) { 2169 session.refresh_requested = false; 2170 if (session.haxy_moment != null or session.local != null) { 2171 const arena = try allocator.create(std.heap.ArenaAllocator); 2172 arena.* = std.heap.ArenaAllocator.init(allocator); 2173 errdefer freeArena(allocator, arena); 2174 2175 session.page_arena = arena; 2176 2177 const route = session.data.current_page; 2178 const page = try arena.allocator().create(Page); 2179 page.* = try Page.init(arena, session, route); 2180 const new_root = try initRoot(allocator, page, session); 2181 2182 self.root.deinit(allocator); 2183 freeArena(allocator, self.arena); 2184 self.root = new_root; 2185 self.route = route; 2186 self.arena = arena; 2187 } 2188 return; 2189 } 2190 2191 if (session.nav_back) { 2192 session.nav_back = false; 2193 if (self.history.pop()) |entry| { 2194 self.root.deinit(allocator); 2195 freeArena(allocator, self.arena); 2196 self.root = entry.root; 2197 self.route = entry.route; 2198 self.arena = entry.arena; 2199 session.page_arena = entry.arena; 2200 session.data.current_page = entry.route; 2201 chooseAnsiArtForNavigation(session); 2202 return; 2203 } 2204 // nothing to go back to; switch to the quit confirmation 2205 const root_focus = self.root.getFocus(); 2206 var iter = root_focus.children.iterator(); 2207 while (iter.next()) |entry| { 2208 switch (entry.value_ptr.focus.kind) { 2209 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2210 root_focus.setFocus(entry.key_ptr.*); 2211 // a Stack only builds its selected child, so the quit 2212 // button isn't in the focus tree until a build runs with 2213 // the quit tab selected. build once, then send a 2214 // synthetic arrow_down to drop focus from the tab onto 2215 // the button so it's ready to confirm. 2216 try self.root.build(allocator, .{ 2217 .min_size = .{ .width = null, .height = 40 }, 2218 .max_size = .{ .width = 80, .height = null }, 2219 }, root_focus); 2220 try self.root.input(allocator, .arrow_down, root_focus); 2221 break; 2222 }, 2223 else => {}, 2224 } 2225 } 2226 return; 2227 } 2228 2229 // forward navigation: navigate() set next_page to the page to move to (a 2230 // cross-page link or tab change crossing pages) 2231 if (session.next_page) |route| { 2232 session.next_page = null; 2233 if (session.haxy_moment == null and session.local == null) return; 2234 // the page we navigated to becomes the current page 2235 session.data.current_page = route; 2236 2237 const arena = try allocator.create(std.heap.ArenaAllocator); 2238 arena.* = std.heap.ArenaAllocator.init(allocator); 2239 errdefer freeArena(allocator, arena); 2240 2241 session.page_arena = arena; 2242 2243 const page = try arena.allocator().create(Page); 2244 page.* = try Page.init(arena, session, route); 2245 const new_root = try initRoot(allocator, page, session); 2246 2247 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 2248 // drop the oldest entry (freeing its widget tree and arena) once over cap 2249 if (self.history.items.len > max_history) { 2250 var oldest = self.history.orderedRemove(0); 2251 oldest.root.deinit(allocator); 2252 freeArena(allocator, oldest.arena); 2253 } 2254 self.root = new_root; 2255 self.route = route; 2256 self.arena = arena; 2257 } 2258 } 2259 }; 2260 2261 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2262 const page_widget: Widget = switch (page.*) { 2263 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2264 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2265 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2266 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2267 }; 2268 2269 chooseAnsiArtForNavigation(session); 2270 2271 // on the TUI/SSH, the page sits above a one-row footer showing the url 2272 var root = if (session.is_terminal) blk: { 2273 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2274 errdefer box.deinit(allocator); 2275 const bg_id = bg_blk: { 2276 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2277 errdefer background.deinit(allocator); 2278 const id = background.getFocus().id; 2279 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2280 break :bg_blk id; 2281 }; 2282 { 2283 var footer = try widget.Footer.init(allocator, session); 2284 errdefer footer.deinit(allocator); 2285 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2286 } 2287 box.getFocus().child_id = bg_id; 2288 break :blk Widget{ .box = box }; 2289 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2290 errdefer root.deinit(allocator); 2291 2292 // input-owning views build their TextInputs in init, so reset their 2293 // registrations here 2294 session.text_inputs.clearRetainingCapacity(); 2295 2296 try root.build(allocator, .{ 2297 .min_size = .{ .width = null, .height = 40 }, 2298 .max_size = .{ .width = 100, .height = null }, 2299 }, root.getFocus()); 2300 2301 return root; 2302 } 2303 2304 // native and server-side sessions choose from the embedded collection. WASM 2305 // keeps the art serialized by the server instead. 2306 fn chooseAnsiArtForNavigation(session: *Session) void { 2307 if (comptime ansi_arts.len == 0) return; 2308 const io = session.io orelse return; 2309 var random: usize = undefined; 2310 io.random(std.mem.asBytes(&random)); 2311 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2312 } 2313