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