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