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