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