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