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