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