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