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