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