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