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