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