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