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