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