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