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