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