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