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