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