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