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