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