╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const xit = @import("xit"); 3 const rp = xit.repo; 4 const rf = xit.ref; 5 const hash = xit.hash; 6 const xitui = xit.xitui; 7 const term = xitui.terminal; 8 const wgt = xitui.widget; 9 const layout = xitui.layout; 10 const Key = xitui.input.Key; 11 const Grid = xitui.grid.Grid; 12 const Focus = xitui.focus.Focus; 13 const evt = @import("./event.zig"); 14 15 pub const Home = @import("./ui/Home.zig"); 16 pub const User = @import("./ui/User.zig"); 17 pub const Repo = @import("./ui/Repo.zig"); 18 pub const Title = @import("./ui/Title.zig"); 19 pub const SubTitle = @import("./ui/SubTitle.zig"); 20 pub const Quit = @import("./ui/Quit.zig"); 21 pub const Unauthorized = @import("./ui/Unauthorized.zig"); 22 23 pub const PageKind = enum { 24 home, 25 user, 26 repo, 27 }; 28 29 pub const Page = union(PageKind) { 30 home: Home, 31 user: User, 32 repo: Repo, 33 34 pub fn init(arena: *std.heap.ArenaAllocator, session: *Session, route: RoutablePage) !Page { 35 // the repo page can build without a moment in local mode; the home and 36 // user pages always read from the admin db. 37 return switch (route.parent()) { 38 .home => .{ .home = try Home.init(arena, session.haxy_moment orelse return error.NoMoment, switch (route) { 39 .home_users => |start| start, 40 else => 0, 41 }, switch (route) { 42 .home_repos => |start| start, 43 else => 0, 44 }) }, 45 .user => blk: { 46 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 47 break :blk switch (route) { 48 .user_repos => |u| .{ .user = try User.init(arena, haxy_moment, u.name, u.start) }, 49 .user_settings, .user_auth => |name| .{ .user = try User.init(arena, haxy_moment, name, 0) }, 50 else => return error.UnexpectedRoute, 51 }; 52 }, 53 .repo => switch (route) { 54 .repo_files, .repo_commits, .repo_refs, .repo_issues, .repo_settings, .repo_auth => .{ .repo = try Repo.init(arena, session, route) }, 55 else => return error.UnexpectedRoute, 56 }, 57 }; 58 } 59 }; 60 61 // what the server hands to the client (and what main_wasm parses on _start). 62 // keeps Page free of any per-request session state. 63 pub const Snapshot = struct { 64 page: Page, 65 session: Session.Data = .{}, 66 }; 67 68 // a top-level "page" the user can navigate to. the user_* variants are the 69 // tabs of a user page; they each carry the user's name and all map to the 70 // .user parent page, so switching between them stays on that page (and just 71 // updates the url) rather than navigating away. 72 pub const RoutablePage = union(enum) { 73 home_users: usize, // 0 = first page 74 home_repos: usize, // 0 = first page 75 home_settings, 76 home_auth, 77 user_repos: struct { name: Array(evt.User.name_max_len), start: usize = 0 }, 78 user_settings: Array(evt.User.name_max_len), 79 user_auth: Array(evt.User.name_max_len), 80 repo_files: RepoFilesRoute, 81 repo_commits: RepoCommitsRoute, 82 repo_refs: struct { 83 name: Array(repo_route_max_len), 84 kind: RefKind = .branch, 85 // the url-encoded ref name `kind`'s column windows from ("" = the 86 // first window; the other column always shows its first window). 87 from: Array(ref_route_max_len) = .{}, 88 }, 89 repo_issues: struct { 90 name: Array(repo_route_max_len), 91 // the tag the list is filtered to, url-encoded ("" = unfiltered). 92 tag: Array(issue_tag_route_max_len) = .{}, 93 selected: Array(evt.event_id_size * 2) = .{}, 94 // the view the url names; a route with `selected` derives its view 95 // from that issue's status instead. 96 view: IssuesView = .open, 97 }, 98 repo_settings: Array(repo_route_max_len), 99 repo_auth: Array(repo_route_max_len), 100 101 pub const default: RoutablePage = .{ .home_users = 0 }; 102 103 pub const RefKind = enum { branch, tag }; 104 105 pub const IssuesView = enum { open, closed, tags, new, edit, description }; 106 107 pub const RefOrOid = enum { 108 branch, 109 tag, 110 object, 111 }; 112 113 // the "key:value" segments of a url tail, collected in any order 114 const Params = struct { 115 const ParamKey = enum { start, line, branch, tag, object }; 116 117 // a branch:/tag:/object: param; the value stays url-encoded 118 const RefParam = struct { kind: RefOrOid, value: []const u8 }; 119 120 values: std.enums.EnumArray(ParamKey, ?[]const u8) = .initFill(null), 121 122 // consume the leading "key:value" segments, stopping at the first 123 // segment that isn't one 124 fn scanPairs(self: *Params, segments: *std.mem.SplitIterator(u8, .scalar)) error{BadUrl}!void { 125 while (segments.peek()) |seg| { 126 const colon = std.mem.indexOfScalar(u8, seg, ':') orelse return; 127 const key = std.meta.stringToEnum(ParamKey, seg[0..colon]) orelse return; 128 const value = seg[colon + 1 ..]; 129 if (value.len == 0) return error.BadUrl; 130 if (self.values.get(key) != null) return error.BadUrl; 131 self.values.set(key, value); 132 _ = segments.next(); 133 } 134 } 135 136 // consume every remaining segment: pairs plus at most one bare word, 137 // which is returned 138 fn scanTail(self: *Params, segments: *std.mem.SplitIterator(u8, .scalar)) error{BadUrl}!?[]const u8 { 139 var word: ?[]const u8 = null; 140 while (true) { 141 try self.scanPairs(segments); 142 const seg = segments.next() orelse return word; 143 if (seg.len == 0 or word != null) return error.BadUrl; 144 word = seg; 145 } 146 } 147 148 // true when every set param is one of `allowed` 149 fn only(self: Params, allowed: []const ParamKey) bool { 150 for (std.enums.values(ParamKey)) |key| { 151 if (self.values.get(key) == null) continue; 152 if (std.mem.indexOfScalar(ParamKey, allowed, key) == null) return false; 153 } 154 return true; 155 } 156 157 // the "start:<n>" window param (0 when absent), null when malformed 158 fn start(self: Params) ?usize { 159 const value = self.values.get(.start) orelse return 0; 160 return std.fmt.parseInt(usize, value, 10) catch null; 161 } 162 163 // the "line:<n>" first-line param (0 when absent), null when malformed 164 fn line(self: Params) ?usize { 165 const value = self.values.get(.line) orelse return 0; 166 return std.fmt.parseInt(usize, value, 10) catch null; 167 } 168 169 // the single branch:/tag:/object: param, null when none is set 170 fn ref(self: Params) error{BadUrl}!?RefParam { 171 var result: ?RefParam = null; 172 inline for (@typeInfo(RefOrOid).@"enum".fields) |field| { 173 if (self.values.get(@field(ParamKey, field.name))) |value| { 174 if (result != null) return error.BadUrl; 175 result = .{ .kind = @field(RefOrOid, field.name), .value = value }; 176 } 177 } 178 return result; 179 } 180 }; 181 182 const user_segment = "/user/"; 183 const repo_segment = "/repo/"; 184 const files_seg = "files"; 185 const commits_seg = "commits"; 186 // ends a commits route naming its commit's full message 187 const message_seg = "message"; 188 // the `Params` key spellings the url emitters use 189 const tag_filter_seg = @tagName(Params.ParamKey.tag) ++ ":"; 190 const start_seg = @tagName(Params.ParamKey.start) ++ ":"; 191 const line_seg = @tagName(Params.ParamKey.line) ++ ":"; 192 // marks a files or commits route's trailing path: the value is the raw 193 // remainder of the url, so it must come last 194 const path_seg = "path:"; 195 196 // a repo route is "username/reponame", optionally followed by the files 197 // tab's "/files/<refkind>:<refvalue>[/line:<n>][/path:<dir>]"; the bare 198 // pair means the default branch's files root. a local session's routes 199 // elide the identity: "" at the bare root, else starting at the tail's "/". 200 pub const repo_route_max_len = 1024; 201 pub const repo_identity_max_len = evt.User.name_max_len + 1 + evt.Repo.name_max_len; 202 203 // true when a repo route's stored string elides its identity (local mode). 204 // unambiguous because a full string always starts with its owner segment. 205 fn identityElided(s: []const u8) bool { 206 return s.len == 0 or s[0] == '/'; 207 } 208 209 // a url-encoded tag can grow to three bytes per source byte. 210 pub const issue_tag_route_max_len = evt.Issue.tag_max_len * 3; 211 212 // caps a url-encoded ref name in a route (a longer name doesn't route). 213 pub const ref_route_max_len = 512; 214 215 // the identity stored by every repo route. Local routes elide it. 216 pub const RepoIdentity = struct { 217 identity: []const u8, // "owner/name" 218 owner: []const u8, 219 name: []const u8, 220 221 pub fn parse(s: []const u8) ?RepoIdentity { 222 var result: RepoIdentity = .{ 223 .identity = "", 224 .owner = "", 225 .name = "", 226 }; 227 if (identityElided(s)) { 228 return result; 229 } else { 230 var id_segments = std.mem.splitScalar(u8, s, '/'); 231 const owner = id_segments.next() orelse return null; 232 const name = id_segments.next() orelse return null; 233 if (owner.len == 0 or name.len == 0) return null; 234 result.identity = s[0 .. owner.len + 1 + name.len]; 235 result.owner = owner; 236 result.name = name; 237 } 238 return result; 239 } 240 }; 241 242 pub const RepoFilesRoute = struct { 243 name: Array(repo_identity_max_len), 244 ref_kind: ?RefOrOid = null, 245 ref_value: Array(ref_route_max_len) = .{}, 246 path: Array(repo_route_max_len) = .{}, 247 // the selected file's first visible line (1-based; 0 = unset). 248 line: usize = 0, 249 }; 250 251 pub const RepoCommitsRoute = struct { 252 name: Array(repo_identity_max_len), 253 ref_or_oid: ?RefOrOid = null, 254 value: Array(ref_route_max_len) = .{}, 255 // what the pane shows for the commit the log walks from. 256 content: Content = .{ .diff = .{} }, 257 258 // a diff window and the message are alternatives, so the window's 259 // params can't be named alongside the message. 260 pub const Content = union(enum) { 261 diff: struct { 262 // the hunk index the window starts at. 263 start: usize = 0, 264 // the file the diff is filtered to ("" = every file). 265 path: Array(repo_route_max_len) = .{}, 266 }, 267 message, 268 }; 269 }; 270 271 // build a `.repo_files` route (a null ref_kind = the bare default-branch 272 // root). null if the result doesn't fit the inline name. 273 pub fn repoFilesRoute(identity: []const u8, ref_kind: ?RefOrOid, ref_value: []const u8, dir: []const u8, line: usize) ?RoutablePage { 274 if (ref_kind == null and (ref_value.len != 0 or dir.len != 0)) return null; 275 if (ref_kind != null and ref_value.len == 0 and dir.len != 0) return null; 276 return .{ .repo_files = .{ 277 .name = Array(repo_identity_max_len).from(identity) orelse return null, 278 .ref_kind = ref_kind, 279 .ref_value = Array(ref_route_max_len).from(ref_value) orelse return null, 280 .path = Array(repo_route_max_len).from(dir) orelse return null, 281 .line = line, 282 } }; 283 } 284 285 // build a `.repo_commits` route (a null ref_or_oid = the default branch). 286 // always carries the `commits` marker so the bare route doesn't collide 287 // with the files root. a non-empty `path` filters the diff pane to that 288 // file and requires a ref. 289 pub fn repoCommitsRoute(identity: []const u8, ref_or_oid: ?RefOrOid, value: []const u8, start: usize, path: []const u8) ?RoutablePage { 290 if (ref_or_oid == null and (value.len != 0 or path.len != 0)) return null; 291 return .{ .repo_commits = .{ 292 .name = Array(repo_identity_max_len).from(identity) orelse return null, 293 .ref_or_oid = ref_or_oid, 294 .value = Array(ref_route_max_len).from(value) orelse return null, 295 .content = .{ .diff = .{ 296 .start = start, 297 .path = Array(repo_route_max_len).from(path) orelse return null, 298 } }, 299 } }; 300 } 301 302 // build a `.repo_commits` route showing the ref/oid's commit message in 303 // full, which is the only content that page renders. 304 pub fn repoCommitMessageRoute(identity: []const u8, ref_or_oid: RefOrOid, value: []const u8) ?RoutablePage { 305 if (value.len == 0) return null; 306 return .{ .repo_commits = .{ 307 .name = Array(repo_identity_max_len).from(identity) orelse return null, 308 .ref_or_oid = ref_or_oid, 309 .value = Array(ref_route_max_len).from(value) orelse return null, 310 .content = .message, 311 } }; 312 } 313 314 // build a `.repo_refs` route windowing `kind`'s column from the 315 // url-encoded ref name `from` ("" = the first window). 316 pub fn repoRefsRoute(identity: []const u8, kind: RefKind, from: []const u8) ?RoutablePage { 317 return .{ .repo_refs = .{ 318 .name = Array(repo_route_max_len).from(identity) orelse return null, 319 .kind = kind, 320 .from = Array(ref_route_max_len).from(from) orelse return null, 321 } }; 322 } 323 324 // build a `.repo_issues` route for "owner/name" (identity) showing 325 // `status`'s list, filtered to the url-encoded `tag` ("" = unfiltered) and 326 // rooted at the issue with hex event id `selected` ("" = the first window). 327 pub fn repoIssuesRoute(identity: []const u8, status: evt.Issue.Status, tag: []const u8, selected: []const u8) ?RoutablePage { 328 return .{ .repo_issues = .{ 329 .name = Array(repo_route_max_len).from(identity) orelse return null, 330 .tag = Array(issue_tag_route_max_len).from(tag) orelse return null, 331 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 332 .view = switch (status) { 333 .open => .open, 334 .closed => .closed, 335 }, 336 } }; 337 } 338 339 // build a `.repo_issues` route showing tags view, keeping the url-encoded 340 // `tag` filter ("" = unfiltered). 341 pub fn repoIssuesTagsRoute(identity: []const u8, tag: []const u8) ?RoutablePage { 342 return .{ .repo_issues = .{ 343 .name = Array(repo_route_max_len).from(identity) orelse return null, 344 .tag = Array(issue_tag_route_max_len).from(tag) orelse return null, 345 .view = .tags, 346 } }; 347 } 348 349 // build a `.repo_issues` route showing new-issue form. 350 pub fn repoIssuesNewRoute(identity: []const u8) ?RoutablePage { 351 return .{ .repo_issues = .{ 352 .name = Array(repo_route_max_len).from(identity) orelse return null, 353 .view = .new, 354 } }; 355 } 356 357 // build a `.repo_issues` route showing the edit form for the issue with 358 // hex event id `selected`. 359 pub fn repoIssuesEditRoute(identity: []const u8, selected: []const u8) ?RoutablePage { 360 if (selected.len == 0) return null; 361 return .{ .repo_issues = .{ 362 .name = Array(repo_route_max_len).from(identity) orelse return null, 363 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 364 .view = .edit, 365 } }; 366 } 367 368 // build a `.repo_issues` route showing the whole description of the issue 369 // with hex event id `selected`. 370 pub fn repoIssuesDescriptionRoute(identity: []const u8, selected: []const u8) ?RoutablePage { 371 if (selected.len == 0) return null; 372 return .{ .repo_issues = .{ 373 .name = Array(repo_route_max_len).from(identity) orelse return null, 374 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 375 .view = .description, 376 } }; 377 } 378 379 // an inline, owned array of data. keeping it in the route (rather than a 380 // borrowed slice) makes RoutablePage a plain value: it can be copied, stored 381 // in history, and serialized without any arena tracking. 382 pub fn Array(comptime max_len: usize) type { 383 return struct { 384 bytes: [max_len]u8 = undefined, 385 len: u16 = 0, 386 387 pub fn from(s: []const u8) ?Array(max_len) { 388 if (s.len > max_len) return null; 389 var name = Array(max_len){ .len = @intCast(s.len) }; 390 @memcpy(name.bytes[0..s.len], s); 391 return name; 392 } 393 394 pub fn slice(self: *const Array(max_len)) []const u8 { 395 return self.bytes[0..self.len]; 396 } 397 398 // round-trip as a json string instead of an array of numbers. 399 pub fn jsonStringify(self: *const Array(max_len), jw: anytype) !void { 400 try jw.write(self.slice()); 401 } 402 403 pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !Array(max_len) { 404 const s = try std.json.innerParse([]const u8, allocator, source, options); 405 // ValueTooLong is std.json's error for an over-long field. 406 return from(s) orelse error.ValueTooLong; 407 } 408 }; 409 } 410 411 pub fn toUrl(self: RoutablePage, arena: *std.heap.ArenaAllocator) ![]const u8 { 412 return switch (self) { 413 .home_users => |start| if (start == 0) @as([]const u8, "/users") else try std.fmt.allocPrint(arena.allocator(), "/users/" ++ start_seg ++ "{d}", .{start}), 414 .home_repos => |start| if (start == 0) @as([]const u8, "/repos") else try std.fmt.allocPrint(arena.allocator(), "/repos/" ++ start_seg ++ "{d}", .{start}), 415 .home_settings => "/settings", 416 .home_auth => "/auth", 417 .user_repos => |u| if (u.start == 0) 418 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/repos", .{u.name.slice()}) 419 else 420 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/repos/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 421 .user_settings => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/settings", .{name.slice()}), 422 .user_auth => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/auth", .{name.slice()}), 423 .repo_files => |f| blk: { 424 const prefix = try repoUrlPrefix(arena, f.name.slice()); 425 if (f.ref_kind == null and f.line == 0) break :blk if (prefix.len == 0) "/" else prefix; 426 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 427 try out.writer.print("{s}/" ++ files_seg, .{prefix}); 428 if (f.ref_kind) |kind| if (f.ref_value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), f.ref_value.slice() }); 429 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 430 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 431 break :blk out.written(); 432 }, 433 .repo_commits => |c| blk: { 434 const prefix = try repoUrlPrefix(arena, c.name.slice()); 435 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 436 try out.writer.print("{s}/" ++ commits_seg, .{prefix}); 437 if (c.ref_or_oid) |kind| if (c.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), c.value.slice() }); 438 switch (c.content) { 439 .diff => |d| { 440 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 441 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 442 }, 443 .message => try out.writer.print("/" ++ message_seg, .{}), 444 } 445 break :blk out.written(); 446 }, 447 .repo_refs => |r| blk: { 448 const prefix = try repoUrlPrefix(arena, r.name.slice()); 449 break :blk if (r.from.len == 0) 450 try std.fmt.allocPrint(arena.allocator(), "{s}/refs", .{prefix}) 451 else 452 try std.fmt.allocPrint(arena.allocator(), "{s}/refs/{s}:{s}", .{ prefix, @tagName(r.kind), r.from.slice() }); 453 }, 454 .repo_issues => |i| blk: { 455 const prefix = try repoUrlPrefix(arena, i.name.slice()); 456 if (i.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}/edit", .{ prefix, i.selected.slice() }); 457 if (i.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}/description", .{ prefix, i.selected.slice() }); 458 // a selected issue's url carries its id instead of a view 459 // name, so it survives status changes 460 const word = if (i.selected.len != 0) i.selected.slice() else @tagName(i.view); 461 break :blk if (i.tag.len == 0) 462 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}", .{ prefix, word }) 463 else 464 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, word, i.tag.slice() }); 465 }, 466 .repo_settings => |name| try std.fmt.allocPrint(arena.allocator(), "{s}/settings", .{try repoUrlPrefix(arena, name.slice())}), 467 .repo_auth => |name| try std.fmt.allocPrint(arena.allocator(), "{s}/auth", .{try repoUrlPrefix(arena, name.slice())}), 468 }; 469 } 470 471 pub fn fromUrl(path: []const u8) ?RoutablePage { 472 if (std.mem.eql(u8, path, "/")) return default; 473 if (path.len < 2 or path[0] != '/') return null; 474 var segments = std.mem.splitScalar(u8, path[1..], '/'); 475 const first = segments.next() orelse return null; 476 if (std.mem.eql(u8, first, "users")) return .{ .home_users = listStart(&segments) orelse return null }; 477 if (std.mem.eql(u8, first, "repos")) return .{ .home_repos = listStart(&segments) orelse return null }; 478 if (std.mem.eql(u8, first, "settings")) return if (segments.next() == null) .home_settings else null; 479 if (std.mem.eql(u8, first, "auth")) return if (segments.next() == null) .home_auth else null; 480 // "user/<name>[/repos[/start:<n>]|/settings|/auth]" 481 if (std.mem.eql(u8, first, "user")) { 482 const name = segments.next() orelse return null; 483 if (name.len == 0) return null; 484 const parsed = Array(evt.User.name_max_len).from(name) orelse return null; // name too long 485 const sub = segments.next() orelse return .{ .user_repos = .{ .name = parsed } }; 486 if (std.mem.eql(u8, sub, "repos")) return .{ .user_repos = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 487 if (std.mem.eql(u8, sub, "settings")) return if (segments.next() == null) .{ .user_settings = parsed } else null; 488 if (std.mem.eql(u8, sub, "auth")) return if (segments.next() == null) .{ .user_auth = parsed } else null; 489 return null; // unknown sub-path 490 } 491 // "repo/<username>/<reponame>[/<tab tail>]"; the bare pair is the 492 // files root 493 if (std.mem.eql(u8, first, "repo")) { 494 const rest = segments.rest(); // "username/reponame[/...]" 495 const owner = segments.next() orelse return null; 496 const repo_name = segments.next() orelse return null; 497 // reject an empty username or reponame 498 if (owner.len == 0 or repo_name.len == 0) return null; 499 const pair = rest[0 .. owner.len + 1 + repo_name.len]; // "username/reponame" 500 if (segments.peek() != null) { 501 return repoSubRoute(pair, segments.rest()); 502 } 503 return repoFilesRoute(pair, null, "", "", 0); 504 } 505 return null; 506 } 507 508 // parse a local-mode url, which elides the "/repo/<owner>/<name>" prefix: 509 // "/" is the files root and the sub-paths follow directly. 510 pub fn fromUrlLocal(path: []const u8) ?RoutablePage { 511 if (std.mem.eql(u8, path, "/")) return repoFilesRoute("", null, "", "", 0); 512 if (path.len < 2 or path[0] != '/') return null; 513 return repoSubRoute("", path[1..]); 514 } 515 516 // the url prefix for a repo route's identity: "/repo/<identity>", or "" 517 // when the identity is elided. 518 fn repoUrlPrefix(arena: *std.heap.ArenaAllocator, identity: []const u8) ![]const u8 { 519 if (identity.len == 0) return ""; 520 return std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}", .{identity}); 521 } 522 523 // the "owner/name" a repo route carries, null for any other route. the 524 // result borrows the route, so it must outlive the slice. 525 pub fn repoIdentity(self: *const RoutablePage) ?[]const u8 { 526 return switch (self.*) { 527 .repo_files => |*f| f.name.slice(), 528 .repo_commits => |*c| c.name.slice(), 529 .repo_refs => |*r| r.name.slice(), 530 .repo_issues => |*i| i.name.slice(), 531 .repo_settings, .repo_auth => |*name| name.slice(), 532 else => null, 533 }; 534 } 535 536 // the route this route's page lands on, with no tab or params: the users 537 // list, a user's repos, or a repo's files root. 538 pub fn pageRoot(self: RoutablePage) RoutablePage { 539 return switch (self.parent()) { 540 .home => .default, 541 .user => switch (self) { 542 .user_repos => |u| .{ .user_repos = .{ .name = u.name } }, 543 .user_settings, .user_auth => |name| .{ .user_repos = .{ .name = name } }, 544 else => self, 545 }, 546 // every repo route stores the same identity, so it always fits 547 .repo => repoFilesRoute(self.repoIdentity() orelse return self, null, "", "", 0) orelse self, 548 }; 549 } 550 551 pub fn parent(self: RoutablePage) PageKind { 552 return switch (self) { 553 .home_users, .home_repos, .home_settings, .home_auth => .home, 554 .user_repos, .user_settings, .user_auth => .user, 555 .repo_files, .repo_commits, .repo_refs, .repo_issues, .repo_settings, .repo_auth => .repo, 556 }; 557 } 558 559 // the dir a trailing "path:<dir>" names: the raw remainder ("" when 560 // absent, null when malformed) 561 fn pathValue(rest: []const u8) ?[]const u8 { 562 if (rest.len == 0) return ""; 563 if (!std.mem.startsWith(u8, rest, path_seg)) return null; 564 const dir = rest[path_seg.len..]; 565 return if (dir.len == 0) null else dir; 566 } 567 568 // a list route's tail: at most a "start:<n>" param, null on anything else 569 fn listStart(segments: *std.mem.SplitIterator(u8, .scalar)) ?usize { 570 var params = Params{}; 571 const word = params.scanTail(segments) catch return null; 572 if (word != null or !params.only(&.{.start})) return null; 573 return params.start(); 574 } 575 576 // parse the sub-path after a repo route's identity ("" for a local 577 // route): a tab keyword followed by "key:value" params in any order, plus 578 // the files tab's trailing "path:<dir>". 579 fn repoSubRoute(pair: []const u8, sub: []const u8) ?RoutablePage { 580 var segments = std.mem.splitScalar(u8, sub, '/'); 581 const tab = segments.next() orelse return null; 582 var params = Params{}; 583 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; 584 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; 585 if (std.mem.eql(u8, tab, "refs")) { 586 const word = params.scanTail(&segments) catch return null; 587 if (word != null or !params.only(&.{ .branch, .tag })) return null; 588 const ref = (params.ref() catch return null) orelse return repoRefsRoute(pair, .branch, ""); 589 const kind = std.meta.stringToEnum(RefKind, @tagName(ref.kind)) orelse return null; 590 return repoRefsRoute(pair, kind, ref.value); 591 } 592 if (std.mem.eql(u8, tab, "issues")) { 593 // "<id>/edit" is the edit form for that issue, with no params; 594 // "<id>/description" is its whole-description page. 595 const after_tab = segments.rest(); 596 if (std.mem.endsWith(u8, after_tab, "/edit")) { 597 const id = after_tab[0 .. after_tab.len - "/edit".len]; 598 if (id.len == 0 or std.mem.indexOfScalar(u8, id, '/') != null) return null; 599 return repoIssuesEditRoute(pair, id); 600 } 601 if (std.mem.endsWith(u8, after_tab, "/description")) { 602 const id = after_tab[0 .. after_tab.len - "/description".len]; 603 if (id.len == 0 or std.mem.indexOfScalar(u8, id, '/') != null) return null; 604 return repoIssuesDescriptionRoute(pair, id); 605 } 606 // the word is a view name or an issue event id (whose status 607 // picks the view); a filter with neither is never emitted. 608 const word = params.scanTail(&segments) catch return null; 609 if (!params.only(&.{.tag})) return null; 610 const tag_value = params.values.get(.tag) orelse ""; 611 const w = word orelse return if (tag_value.len == 0) repoIssuesRoute(pair, .open, "", "") else null; 612 if (std.meta.stringToEnum(evt.Issue.Status, w)) |status| return repoIssuesRoute(pair, status, tag_value, ""); 613 if (std.mem.eql(u8, w, "tags")) return repoIssuesTagsRoute(pair, tag_value); 614 if (std.mem.eql(u8, w, "new")) return if (tag_value.len == 0) repoIssuesNewRoute(pair) else null; 615 return repoIssuesRoute(pair, .open, tag_value, w); 616 } 617 if (std.mem.eql(u8, tab, files_seg)) { 618 params.scanPairs(&segments) catch return null; 619 if (!params.only(&.{ .line, .branch, .tag, .object })) return null; 620 const line = params.line() orelse return null; 621 const dir = pathValue(segments.rest()) orelse return null; 622 const ref = (params.ref() catch return null) orelse 623 return if (dir.len == 0) repoFilesRoute(pair, null, "", "", line) else null; 624 return repoFilesRoute(pair, ref.kind, ref.value, dir, line); 625 } 626 if (std.mem.eql(u8, tab, commits_seg)) { 627 params.scanPairs(&segments) catch return null; 628 if (!params.only(&.{ .start, .branch, .tag, .object })) return null; 629 const start = params.start() orelse return null; 630 const ref = (params.ref() catch return null) orelse { 631 const file = pathValue(segments.rest()) orelse return null; 632 return if (file.len == 0) repoCommitsRoute(pair, null, "", start, "") else null; 633 }; 634 // the message page names its commit and nothing else 635 if (std.mem.eql(u8, segments.rest(), message_seg)) 636 return if (start == 0) repoCommitMessageRoute(pair, ref.kind, ref.value) else null; 637 const file = pathValue(segments.rest()) orelse return null; 638 return repoCommitsRoute(pair, ref.kind, ref.value, start, file); 639 } 640 return null; // unknown sub-path 641 } 642 643 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 644 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 645 return switch (a) { 646 .home_users => |a_start| a_start == b.home_users, 647 .home_repos => |a_start| a_start == b.home_repos, 648 .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, 649 .user_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.user_settings.slice()), 650 .user_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.user_auth.slice()), 651 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 652 a_f.ref_kind == b.repo_files.ref_kind and 653 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 654 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 655 a_f.line == b.repo_files.line, 656 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 657 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 658 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 659 switch (a_c.content) { 660 .diff => |a_d| switch (b.repo_commits.content) { 661 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 662 .message => false, 663 }, 664 .message => std.meta.activeTag(b.repo_commits.content) == .message, 665 }, 666 .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()), 667 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 668 std.mem.eql(u8, a_i.tag.slice(), b.repo_issues.tag.slice()) and 669 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 670 a_i.view == b.repo_issues.view, 671 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 672 .repo_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_auth.slice()), 673 else => true, 674 }; 675 } 676 677 // true when following a link from `b` to `a` (both repo routes) should reload 678 // rather than stay in page 679 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 680 if (a.parent() != .repo or b.parent() != .repo) return false; 681 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 682 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 683 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 684 return !a.eql(b); 685 } 686 687 // true when `a` and `b` are the same user paginated to a different repos 688 // window. switching between a user's tabs is in-page (header-handled), so 689 // only a changed `start` on the repos list navigates. 690 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 691 return switch (a) { 692 .user_repos => |aa| switch (b) { 693 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 694 else => false, 695 }, 696 else => false, 697 }; 698 } 699 700 // true when `a` and `b` are the same home list tab paginated to a different 701 // window. switching between the users/repos tabs is in-page (the home page 702 // holds both lists), so only a changed `after` on the same tab navigates. 703 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 704 return switch (a) { 705 .home_users => |aa| switch (b) { 706 .home_users => |bb| aa != bb, 707 else => false, 708 }, 709 .home_repos => |aa| switch (b) { 710 .home_repos => |bb| aa != bb, 711 else => false, 712 }, 713 else => false, 714 }; 715 } 716 }; 717 718 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 719 fn isUnreserved(c: u8) bool { 720 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 721 } 722 723 // percent-encode a ref name for use as a single url path segment. 724 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 725 var out: std.Io.Writer.Allocating = .init(aa); 726 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 727 return out.written(); 728 } 729 730 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 731 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 732 return struct { 733 pub const hex_len = hash.hexLen(repo_opts.hash); 734 735 // the concrete ref/oid (a null request resolves to the default branch) 736 ref_or_oid: RoutablePage.RefOrOid, 737 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 738 // the route layer — which splits on '/' — stores them encoded). duped 739 // into `aa`. 740 value: []const u8, 741 // the commit oid it points at 742 oid: [hex_len]u8, 743 744 const Self = @This(); 745 746 // resolve a requested ref/oid (null = the repo's default branch) to a 747 // concrete ref/oid plus the commit oid it points at. `requested_value` is 748 // the url-encoded form (as it appears in the route). null when it doesn't 749 // resolve (an unknown branch/tag, or a malformed/unknown oid). 750 pub fn init( 751 repo: *rp.Repo(repo_kind, repo_opts), 752 io: std.Io, 753 aa: std.mem.Allocator, 754 requested_ref_or_oid: ?RoutablePage.RefOrOid, 755 requested_value: []const u8, 756 ) !?Self { 757 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 758 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 759 var value: []const u8 = if (requested_ref_or_oid == null) 760 requested_value 761 else 762 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 763 // no ref named: fall back to HEAD's branch (or its oid when detached). 764 if (requested_ref_or_oid == null) { 765 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 766 if (repo.head(io, &head_buf)) |head| switch (head) { 767 .ref => |r| { 768 ref_or_oid = .branch; 769 value = r.name; 770 }, 771 .oid => |o| { 772 ref_or_oid = .object; 773 value = o; 774 }, 775 } else |_| {} 776 } 777 778 var oid: [hex_len]u8 = undefined; 779 switch (ref_or_oid) { 780 .object => { 781 if (value.len != hex_len) return null; 782 @memcpy(&oid, value); 783 }, 784 .branch, .tag => { 785 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 786 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 787 }, 788 } 789 // store url-encoded so the route layer can hold the value verbatim. 790 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 791 } 792 }; 793 } 794 795 // where a repo page reads its on-disk repo from 796 pub const RepoSource = struct { 797 path: []const u8, 798 repo_kind: rp.RepoKind, 799 }; 800 801 // per-connection mutable state. each SSH session / web session / local TUI 802 // run gets its own. 803 pub const Session = struct { 804 data: Data = .{}, 805 // session-lifetime allocations: login/user_id, persisted prefs, queued 806 // actions. lives as long as the connection. 807 arena: *std.heap.ArenaAllocator, 808 // current page's allocations: page data and the page-scoped strings widgets 809 // build (form actions, links). owned by Nav, swapped on each navigation, so 810 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 811 // where a render is one-shot, this points at the same arena as `arena`. 812 page_arena: *std.heap.ArenaAllocator, 813 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 814 // filesystem io and the path to <server>/repos, for opening on-disk repos 815 // during page construction. both null on wasm, which has no filesystem and 816 // rebuilds pages from the serialized snapshot rather than from disk. 817 io: ?std.Io = null, 818 repos_dir: ?[]const u8 = null, 819 // the single on-disk repo this session views, when running in local mode 820 // (haxy invoked with no arguments inside a repo). null on the server paths, 821 // which resolve repos from the admin db instead. 822 local: ?RepoSource = null, 823 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 824 // focus id -> the live TextInput, refreshed each frame by the views that own 825 // inputs. web/wasm form handling looks widgets up here by focus id. 826 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput(Widget)) = .empty, 827 // focus id -> the page-constant text the web overlay renders into an 828 // input initially, refreshed each frame alongside `text_inputs`. never 829 // the input's live content: the overlay html must not change as the 830 // user types, or the diff in _setOverlay would rebuild the element. 831 input_values: std.AutoHashMapUnmanaged(usize, []const u8) = .empty, 832 nav_back: bool = false, // set by input (escape) to request the native TUI pop a page; see Nav 833 refresh_requested: bool = false, // set by input (ctrl+r) 834 // a requested forward navigation. set this (via navigate) to move to a new 835 // page; Nav.sync builds it and then copies it into current_page, clearing 836 // this back to null. setting current_page directly only updates the url and 837 // does not navigate. 838 next_page: ?RoutablePage = null, 839 is_terminal: bool = false, // true on remote SSH and local TUI 840 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 841 // url. null on the web itself (no footer there). 842 web_port: ?u16 = null, 843 quit_requested: bool = false, 844 845 const Self = @This(); 846 847 // serializable data sent down to web client 848 pub const Data = struct { 849 user_id: ?[]const u8 = null, 850 // the logged-in user's name, for the views that show it 851 user_name: ?[]const u8 = null, 852 // a transient outcome to surface from the last /login POST attempt 853 login_failure: ?Home.Auth.Login.Failure = null, 854 current_page: RoutablePage = .default, 855 // whether to render the ANSI art backdrop 856 enable_ansi: bool = true, 857 // true when this session views a single local repo. unlike `local` 858 // (the host-side filesystem source) this travels in the snapshot, so 859 // the wasm side also parses elided link urls and hides the multi-user 860 // chrome. 861 is_local: bool = false, 862 }; 863 864 // a user-initiated state change. widgets enqueue these on the session during 865 // input instead of mutating state or touching the DB themselves; the host 866 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 867 // side effects out of the widget tree and gives every render path one place to 868 // turn a UI action into a state change + event. 869 pub const Action = union(enum) { 870 toggle_ansi, 871 }; 872 873 pub fn init( 874 arena: *std.heap.ArenaAllocator, 875 repo: *rp.Repo(.xit, evt.admin_repo_opts), 876 data: Data, 877 ) !Self { 878 var session = Self{ 879 .data = data, 880 .arena = arena, 881 // until a host swaps in a page-scoped arena (Nav does this), page 882 // allocations land in the session arena. on the web path that's the 883 // intended behavior, since the whole arena is per-request. 884 .page_arena = arena, 885 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 886 }; 887 try session.loadUser(); 888 return session; 889 } 890 891 // load the logged-in user's name and persisted preferences from the db 892 pub fn loadUser(self: *Self) !void { 893 const user_id = self.data.user_id orelse return; 894 const moment = self.haxy_moment orelse return; 895 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.arena, user_id)) |user| { 896 self.data.user_name = user.event.name; 897 self.data.enable_ansi = user.event.enable_ansi; 898 } 899 } 900 901 // the author for an event this session creates, or null when it may not 902 // create one. local mode has no accounts, so it authors anonymously. 903 pub fn eventAuthorEmail(self: *Self) !?[]const u8 { 904 if (self.data.is_local) return "user@haxy"; // TODO: try the repo's git config user.email first 905 const user_id = self.data.user_id orelse return null; 906 const moment = self.haxy_moment orelse return null; 907 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.page_arena, user_id)) orelse return null; 908 return user.event.email; 909 } 910 911 // queue an action for the host to drain this frame. 912 pub fn push(self: *Self, action: Action) !void { 913 try self.pending.append(self.arena.allocator(), action); 914 } 915 916 // apply a single action's in-memory effect only (no persistence). 917 fn apply(self: *Self, action: Action) void { 918 switch (action) { 919 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 920 } 921 } 922 923 // drain the session's queued actions by applying them to the session. 924 // used on the wasm path, which has no repo to persist to. 925 pub fn applyPending(self: *Self) void { 926 for (self.pending.items) |action| self.apply(action); 927 self.pending.clearRetainingCapacity(); 928 } 929 930 // drain the session's queued actions by applying them to the session and 931 // writing them to the db 932 pub fn applyAndWritePending( 933 self: *Self, 934 io: std.Io, 935 allocator: std.mem.Allocator, 936 repo: *rp.Repo(.xit, evt.admin_repo_opts), 937 ) !void { 938 defer self.pending.clearRetainingCapacity(); 939 for (self.pending.items) |action| { 940 self.apply(action); 941 switch (action) { 942 .toggle_ansi => if (self.data.user_id) |user_id| { 943 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 944 }, 945 } 946 } 947 } 948 949 // reload the moment from the admin repo 950 pub fn reloadMoment(self: *Self, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 951 self.haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo); 952 } 953 954 // request a forward navigation to `route`; the host consumes next_page 955 // (Nav.sync on the terminal, the wasm tick on the web). 956 pub fn navigate(self: *Session, route: RoutablePage) !void { 957 self.next_page = route; 958 } 959 }; 960 961 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 962 var nav = try Nav.init(allocator, session); 963 defer nav.deinit(allocator); 964 965 var terminal = try term.Terminal.init(io, allocator); 966 defer terminal.deinit(io); 967 968 // set term as active so it will be properly cooked 969 // when a panic/segfault happens 970 term.setActive(&terminal); 971 defer term.setActive(null); 972 973 var last_size = layout.Size{ .width = 0, .height = 0 }; 974 var last_grid = try Grid.init(allocator, last_size); 975 defer last_grid.deinit(); 976 977 while (!terminal.shouldQuit()) { 978 const grid_changed = try terminal.render(&nav.root, &last_grid, &last_size); 979 980 // process any inputs. 981 // 982 // if the grid didn't change, then first do a blocking 983 // read, so the thread will sleep until further input. 984 // after that, all remaining reads are non-blocking so 985 // we can process the rest of the queued inputs. 986 // 987 // if the grid *did* change, then only do non-blocking 988 // reads. we do not want to sleep the thread because 989 // there may be an animation that requires more looping. 990 var blocking = !grid_changed; 991 while (try terminal.readKey(io, blocking)) |key| { 992 blocking = false; 993 try inputKey(allocator, &nav.root, key, session); 994 } 995 996 // local run has no repo to persist to, so just apply in-memory 997 session.applyPending(); 998 999 // pick up data written by other handles so the next navigation 1000 // builds its page from a current moment (local mode has no admin repo) 1001 if (repo_maybe) |repo| try session.reloadMoment(repo); 1002 1003 // reconcile navigation: forward to a new page, or back on escape. 1004 try nav.sync(allocator, session); 1005 1006 // the quit button (on the quit tab) asks the host to tear down. 1007 if (session.quit_requested) terminal.requestQuit(); 1008 1009 try nav.root.build(allocator, .{ 1010 .min_size = .{ .width = null, .height = null }, 1011 .max_size = .{ .width = last_size.width, .height = last_size.height }, 1012 }, nav.root.getFocus()); 1013 } 1014 } 1015 1016 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 1017 switch (key) { 1018 // request a navigation pop; the host's Nav.sync goes back a page, or 1019 // quits when there's no history left. 1020 .escape => session.nav_back = true, 1021 .ctrl => |letter| switch (letter) { 1022 'r' => session.refresh_requested = true, 1023 else => try root.input(allocator, key, root.getFocus()), 1024 }, 1025 .enter => { 1026 const root_focus = root.getFocus(); 1027 if (root_focus.grandchild_id) |gid| { 1028 // follow a cross-page link 1029 if (crossPageLink(root_focus, gid, session.data)) |route| { 1030 return session.navigate(route); 1031 } 1032 } 1033 try root.input(allocator, key, root_focus); 1034 }, 1035 .mouse => |mouse| { 1036 if (mouse.action == .press and mouse.action.press == .left) { 1037 const root_focus = root.getFocus(); 1038 var clicked: ?usize = null; 1039 var iter = root_focus.children.iterator(); 1040 while (iter.next()) |entry| { 1041 const child = entry.value_ptr.*; 1042 if (!child.focus.focusable) continue; 1043 const r = child.rect; 1044 if (mouse.x >= r.x and mouse.y >= r.y and 1045 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 1046 { 1047 clicked = entry.key_ptr.*; 1048 break; 1049 } 1050 } 1051 if (clicked) |focus_id| { 1052 // follow a cross-page link 1053 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 1054 return session.navigate(route); 1055 } 1056 root_focus.setFocus(focus_id); 1057 } 1058 // forward the press into the widget tree so buttons (and any 1059 // future click-aware widgets) can react. widgets that don't 1060 // care about presses ignore it. 1061 try root.input(allocator, key, root.getFocus()); 1062 } else { 1063 try root.input(allocator, key, root.getFocus()); 1064 } 1065 }, 1066 else => try root.input(allocator, key, root.getFocus()), 1067 } 1068 } 1069 1070 // if the focus target at focus_id is an `a:` link that should navigate (a 1071 // different parent page, or a different files directory within the repo page), 1072 // return its route; otherwise null. lets a host turn a click / enter on such a 1073 // link into a navigation rather than a focus change. 1074 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 1075 const current = data.current_page; 1076 const child = root_focus.children.get(focus_id) orelse return null; 1077 const custom = switch (child.focus.kind) { 1078 .custom => |c| c, 1079 else => return null, 1080 }; 1081 const a_prefix = "a:"; 1082 if (!std.mem.startsWith(u8, custom, a_prefix)) return null; 1083 const path = custom[a_prefix.len..]; 1084 // local sessions build (and parse) links with the repo identity elided 1085 const route = (if (data.is_local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path)) orelse return null; 1086 // a link to a different parent page always navigates; within a page, a 1087 // files-directory / commits-page / list-window change navigates while tab 1088 // links stay in-page (header-handled). 1089 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 1090 return null; 1091 } 1092 1093 // a display author, resolved against the admin db at read time 1094 pub const Author = union(enum) { 1095 unknown, 1096 email: []const u8, // an email no user matches 1097 user_name: []const u8, 1098 1099 // the commit author line's email, resolved like initFromEmail 1100 pub fn init( 1101 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1102 arena: *std.heap.ArenaAllocator, 1103 author_line: []const u8, 1104 ) !Author { 1105 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 1106 } 1107 1108 // from a record's stored author email: it resolves to the user currently 1109 // holding it, else stays the email. the result is allocated in `arena`, 1110 // so the email may borrow transient memory. 1111 pub fn initFromEmail( 1112 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1113 arena: *std.heap.ArenaAllocator, 1114 author_email: ?[]const u8, 1115 ) !Author { 1116 const email = author_email orelse return .unknown; 1117 if (haxy_moment) |moment| { 1118 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 1119 return .{ .user_name = user.event.name }; 1120 } 1121 } 1122 return .{ .email = try arena.allocator().dupe(u8, email) }; 1123 } 1124 }; 1125 1126 // the "a:" link to user `name`'s page. 1127 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 1128 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 1129 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 1130 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1131 } 1132 1133 // a focusable " author " box showing `author`, linking to their user page 1134 // when it is a known user 1135 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox(Widget) { 1136 const text = switch (author) { 1137 .unknown => "", 1138 .email, .user_name => |t| t, 1139 }; 1140 var tb = try wgt.TextBox(Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 1141 errdefer tb.deinit(allocator); 1142 tb.getFocus().focusable = true; 1143 switch (author) { 1144 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 1145 else => {}, 1146 } 1147 return tb; 1148 } 1149 1150 // native-TUI navigation 1151 pub const Nav = struct { 1152 root: Widget, 1153 route: RoutablePage, 1154 // backs the current page (its Page data and the page-scoped strings its 1155 // widgets build). owned here, swapped on each navigation. session.page_arena 1156 // tracks whichever of these is current so widgets allocate into it. 1157 arena: *std.heap.ArenaAllocator, 1158 history: std.ArrayList(Entry), 1159 1160 // each retained page keeps its own arena, freed when the page leaves the 1161 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 1162 // long-lived session from accumulating every page it ever visited. 1163 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 1164 1165 // cap on retained back-history so the chain can't grow memory without bound 1166 const max_history: usize = 16; 1167 1168 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 1169 const arena = try allocator.create(std.heap.ArenaAllocator); 1170 arena.* = std.heap.ArenaAllocator.init(allocator); 1171 errdefer { 1172 arena.deinit(); 1173 allocator.destroy(arena); 1174 } 1175 1176 session.page_arena = arena; 1177 const route = session.data.current_page; 1178 1179 const page = try arena.allocator().create(Page); 1180 page.* = try Page.init(arena, session, route); 1181 return .{ 1182 .root = try initRoot(allocator, page, session), 1183 .route = route, 1184 .arena = arena, 1185 .history = .empty, 1186 }; 1187 } 1188 1189 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 1190 self.root.deinit(allocator); 1191 freeArena(allocator, self.arena); 1192 for (self.history.items) |*entry| { 1193 entry.root.deinit(allocator); 1194 freeArena(allocator, entry.arena); 1195 } 1196 self.history.deinit(allocator); 1197 } 1198 1199 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 1200 arena.deinit(); 1201 allocator.destroy(arena); 1202 } 1203 1204 // reconcile the displayed root with the session's navigation state. a 1205 // forward nav (current_page moved to a different parent page) pushes the 1206 // current root and builds the new page in a fresh arena; a back request 1207 // frees the current page and restores the previous one. when escape is 1208 // pressed with no history left we switch to the quit tab instead of quitting. 1209 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 1210 // refresh: rebuild the current page in place from a current moment 1211 if (session.refresh_requested) { 1212 session.refresh_requested = false; 1213 if (session.haxy_moment != null or session.local != null) { 1214 const arena = try allocator.create(std.heap.ArenaAllocator); 1215 arena.* = std.heap.ArenaAllocator.init(allocator); 1216 errdefer freeArena(allocator, arena); 1217 1218 session.page_arena = arena; 1219 1220 const page = try arena.allocator().create(Page); 1221 page.* = try Page.init(arena, session, self.route); 1222 const new_root = try initRoot(allocator, page, session); 1223 1224 self.root.deinit(allocator); 1225 freeArena(allocator, self.arena); 1226 self.root = new_root; 1227 self.arena = arena; 1228 } 1229 return; 1230 } 1231 1232 if (session.nav_back) { 1233 session.nav_back = false; 1234 if (self.history.pop()) |entry| { 1235 self.root.deinit(allocator); 1236 freeArena(allocator, self.arena); 1237 self.root = entry.root; 1238 self.route = entry.route; 1239 self.arena = entry.arena; 1240 session.page_arena = entry.arena; 1241 session.data.current_page = entry.route; 1242 return; 1243 } 1244 // nothing to go back to; switch to the quit confirmation 1245 const root_focus = self.root.getFocus(); 1246 var iter = root_focus.children.iterator(); 1247 while (iter.next()) |entry| { 1248 switch (entry.value_ptr.focus.kind) { 1249 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1250 root_focus.setFocus(entry.key_ptr.*); 1251 // a Stack only builds its selected child, so the quit 1252 // button isn't in the focus tree until a build runs with 1253 // the quit tab selected. build once, then send a 1254 // synthetic arrow_down to drop focus from the tab onto 1255 // the button so it's ready to confirm. 1256 try self.root.build(allocator, .{ 1257 .min_size = .{ .width = null, .height = 40 }, 1258 .max_size = .{ .width = 80, .height = null }, 1259 }, root_focus); 1260 try self.root.input(allocator, .arrow_down, root_focus); 1261 break; 1262 }, 1263 else => {}, 1264 } 1265 } 1266 return; 1267 } 1268 1269 // forward navigation: navigate() set next_page to the page to move to (a 1270 // cross-page link or tab change crossing pages) 1271 if (session.next_page) |route| { 1272 session.next_page = null; 1273 if (session.haxy_moment == null and session.local == null) return; 1274 // the page we navigated to becomes the current page 1275 session.data.current_page = route; 1276 1277 const arena = try allocator.create(std.heap.ArenaAllocator); 1278 arena.* = std.heap.ArenaAllocator.init(allocator); 1279 errdefer freeArena(allocator, arena); 1280 1281 session.page_arena = arena; 1282 1283 const page = try arena.allocator().create(Page); 1284 page.* = try Page.init(arena, session, route); 1285 const new_root = try initRoot(allocator, page, session); 1286 1287 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 1288 // drop the oldest entry (freeing its widget tree and arena) once over cap 1289 if (self.history.items.len > max_history) { 1290 var oldest = self.history.orderedRemove(0); 1291 oldest.root.deinit(allocator); 1292 freeArena(allocator, oldest.arena); 1293 } 1294 self.root = new_root; 1295 self.route = route; 1296 self.arena = arena; 1297 } 1298 } 1299 }; 1300 1301 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 1302 const page_widget: Widget = switch (page.*) { 1303 .home => |*p| .{ .home = try .init(allocator, p, session) }, 1304 .user => |*p| .{ .user = try .init(allocator, p, session) }, 1305 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 1306 }; 1307 1308 const demon_art = @embedFile("embed/demon.ans"); 1309 1310 // on the TUI/SSH, the page sits above a one-row footer showing the url 1311 var root = if (session.is_terminal) blk: { 1312 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1313 errdefer box.deinit(allocator); 1314 const bg_id = bg_blk: { 1315 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 1316 errdefer background.deinit(allocator); 1317 const id = background.getFocus().id; 1318 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 1319 break :bg_blk id; 1320 }; 1321 { 1322 var footer = try Footer.init(allocator, session); 1323 errdefer footer.deinit(allocator); 1324 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 1325 } 1326 box.getFocus().child_id = bg_id; 1327 break :blk Widget{ .box = box }; 1328 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 1329 errdefer root.deinit(allocator); 1330 1331 // input-owning views build their TextInputs in init — so reset the 1332 // focus-id -> *TextInput map here 1333 session.text_inputs.clearRetainingCapacity(); 1334 session.input_values.clearRetainingCapacity(); 1335 1336 try root.build(allocator, .{ 1337 .min_size = .{ .width = null, .height = 40 }, 1338 .max_size = .{ .width = 100, .height = null }, 1339 }, root.getFocus()); 1340 1341 return root; 1342 } 1343 1344 pub const Widget = union(enum) { 1345 text: wgt.Text(Widget), 1346 box: wgt.Box(Widget), 1347 text_box: wgt.TextBox(Widget), 1348 text_input: wgt.TextInput(Widget), 1349 scroll: wgt.Scroll(Widget), 1350 stack: wgt.Stack(Widget), 1351 flow_box: FlowBox, 1352 flow_box_scroll: FlowBox.Scroll, 1353 tag_flow: TagFlow, 1354 spacer: Spacer, 1355 center: Center, 1356 background: AnsiBackground, 1357 home: Home.View, 1358 user: User.View, 1359 repo: Repo.View, 1360 quit: Quit.View, 1361 unauthorized: Unauthorized.View, 1362 title: Title.View, 1363 sub_title: SubTitle.View, 1364 home_header: Home.Header.View, 1365 user_header: User.Header.View, 1366 repo_header: Repo.Header.View, 1367 repo_files_header: Repo.Files.Header.View, 1368 repo_commits_header: Repo.Commits.Header.View, 1369 repo_issues_header: Repo.Issues.Header, 1370 repo_files: Repo.Files.View, 1371 repo_commits: Repo.Commits.View, 1372 repo_refs: Repo.Refs.View, 1373 repo_issues: Repo.Issues.View, 1374 home_users: Home.Users.View, 1375 home_repos: Home.Repos.View, 1376 auth_tab: Home.Header.AuthTab.View, 1377 home_settings: Home.Settings.View, 1378 home_auth: Home.Auth.View, 1379 footer: Footer, 1380 1381 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 1382 switch (self.*) { 1383 inline else => |*case| case.deinit(allocator), 1384 } 1385 } 1386 1387 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 1388 switch (self.*) { 1389 inline else => |*case| try case.build(allocator, constraint, root_focus), 1390 } 1391 } 1392 1393 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 1394 switch (self.*) { 1395 inline else => |*case| try case.input(allocator, key, root_focus), 1396 } 1397 } 1398 1399 pub fn clearGrid(self: *Widget) void { 1400 switch (self.*) { 1401 inline else => |*case| case.clearGrid(), 1402 } 1403 } 1404 1405 pub fn getGrid(self: Widget) ?Grid { 1406 switch (self) { 1407 inline else => |*case| return case.getGrid(), 1408 } 1409 } 1410 1411 pub fn getFocus(self: *Widget) *Focus { 1412 switch (self.*) { 1413 inline else => |*case| return case.getFocus(), 1414 } 1415 } 1416 }; 1417 1418 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 1419 // `scroll` to keep it visible. 1420 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 1421 const keys = box.children.keys(); 1422 if (keys.len == 0) return; 1423 const cur_id = box.getFocus().child_id orelse return; 1424 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 1425 const last: isize = @intCast(keys.len - 1); 1426 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 1427 if (next == @as(usize, @intCast(cur))) return; 1428 root_focus.setFocus(keys[next]); 1429 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 1430 } 1431 1432 pub const FlowBox = struct { 1433 focus: *Focus, 1434 grid: ?Grid, 1435 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1436 // backs the per-item strings the text boxes borrow: each box's `content` 1437 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 1438 // wholesale on each setItems so there's no per-string ownership to track. 1439 arena: std.heap.ArenaAllocator, 1440 // column count from the last build — FlowBox.Scroll.input uses it so arrow 1441 // up/down can step by a row's worth of items. 1442 last_cols: usize, 1443 options: Options, 1444 1445 const border_rows: usize = 2; 1446 1447 pub const Options = struct { 1448 cell_width: usize = 40, 1449 cell_height: usize = 3, 1450 }; 1451 1452 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1453 return .{ 1454 .focus = try Focus.create(allocator, .container), 1455 .grid = null, 1456 .text_boxes = .empty, 1457 .arena = std.heap.ArenaAllocator.init(allocator), 1458 .last_cols = 1, 1459 .options = options, 1460 }; 1461 } 1462 1463 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1464 self.focus.destroy(allocator); 1465 if (self.grid) |*grid| { 1466 grid.deinit(); 1467 self.grid = null; 1468 } 1469 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1470 self.text_boxes.deinit(allocator); 1471 self.arena.deinit(); 1472 } 1473 1474 // one flow item: its display text plus an optional link. a non-empty `link` 1475 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1476 // the web renderer turns into an anchor. 1477 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1478 1479 // both the item text and the link are copied into the arena, so the caller's 1480 // slices needn't outlive this call. 1481 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1482 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1483 self.text_boxes.clearAndFree(allocator); 1484 1485 // the old text boxes (and their borrowed content/links) are gone now, so 1486 // the arena that backed those strings can be reclaimed. 1487 _ = self.arena.reset(.retain_capacity); 1488 const aa = self.arena.allocator(); 1489 1490 self.focus.clear(); 1491 self.focus.child_id = null; 1492 1493 for (items) |item| { 1494 const line = try aa.dupe(u8, item.text); 1495 1496 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1497 errdefer text_box.deinit(allocator); 1498 text_box.getFocus().focusable = true; 1499 1500 if (item.link.len > 0) { 1501 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1502 } 1503 1504 try self.text_boxes.append(allocator, text_box); 1505 } 1506 1507 if (self.text_boxes.items.len > 0) { 1508 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1509 } 1510 } 1511 1512 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1513 self.clearGrid(); 1514 self.focus.clear(); 1515 1516 const cell_width = self.options.cell_width; 1517 const max_width = constraint.max_size.width orelse cell_width; 1518 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1519 self.last_cols = cols; 1520 1521 const count = self.text_boxes.items.len; 1522 if (count == 0) return; 1523 const slot_height = self.options.cell_height + border_rows; 1524 if (slot_height == 0) return; 1525 1526 // build at the slot size so every text box fits a single grid cell 1527 for (self.text_boxes.items) |*tb| { 1528 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1529 try tb.build(allocator, .{ 1530 .min_size = .{ .width = cell_width, .height = null }, 1531 .max_size = .{ .width = cell_width, .height = slot_height }, 1532 }, root_focus); 1533 } 1534 1535 const rows = (count + cols - 1) / cols; 1536 const content_height = rows * slot_height; 1537 const total_width = cols * cell_width; 1538 if (total_width == 0 or content_height == 0) return; 1539 1540 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1541 errdefer grid.deinit(); 1542 1543 for (self.text_boxes.items, 0..) |*tb, i| { 1544 const tb_grid = tb.getGrid() orelse continue; 1545 const col = i % cols; 1546 const row = i / cols; 1547 const cell_x = col * cell_width; 1548 const cell_y = row * slot_height; 1549 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1550 try grid.drawGrid(tb_grid, cell_x, cell_y); 1551 } 1552 1553 self.grid = grid; 1554 } 1555 1556 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1557 _ = self; 1558 _ = allocator; 1559 _ = key; 1560 _ = root_focus; 1561 } 1562 1563 pub fn clearGrid(self: *FlowBox) void { 1564 if (self.grid) |*grid| { 1565 grid.deinit(); 1566 self.grid = null; 1567 } 1568 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1569 } 1570 1571 pub fn getGrid(self: FlowBox) ?Grid { 1572 return self.grid; 1573 } 1574 1575 pub fn getFocus(self: *FlowBox) *Focus { 1576 return self.focus; 1577 } 1578 1579 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1580 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1581 const slot_height = self.options.cell_height + border_rows; 1582 const col = index % self.last_cols; 1583 const row = index / self.last_cols; 1584 return .{ 1585 .x = @intCast(col * self.options.cell_width), 1586 .y = @intCast(row * slot_height), 1587 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1588 }; 1589 } 1590 1591 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1592 for (self.text_boxes.items, 0..) |tb, i| { 1593 if (tb.box.focus.id == focus_id) return i; 1594 } 1595 return null; 1596 } 1597 1598 pub const Scroll = struct { 1599 scroll: wgt.Scroll(Widget), 1600 1601 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1602 var layout_inner = try FlowBox.init(allocator, options); 1603 errdefer layout_inner.deinit(allocator); 1604 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1605 errdefer scroll.deinit(allocator); 1606 return .{ .scroll = scroll }; 1607 } 1608 1609 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1610 self.scroll.deinit(allocator); 1611 } 1612 1613 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1614 self.scroll.x = 0; 1615 self.scroll.y = 0; 1616 try self.inner().setItems(allocator, items); 1617 } 1618 1619 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1620 try self.scroll.build(allocator, constraint, root_focus); 1621 } 1622 1623 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1624 _ = allocator; 1625 const in = self.inner(); 1626 const child_id = in.focus.child_id orelse return; 1627 const current_index = in.indexOfFocusId(child_id) orelse return; 1628 const count = in.text_boxes.items.len; 1629 if (count == 0) return; 1630 const cols = in.last_cols; 1631 const slot_height = in.options.cell_height + FlowBox.border_rows; 1632 1633 var index = current_index; 1634 switch (key) { 1635 .arrow_up => index -|= cols, 1636 .arrow_down => if (index + cols < count) { 1637 index += cols; 1638 }, 1639 .arrow_left => index -|= 1, 1640 .arrow_right => if (index + 1 < count) { 1641 index += 1; 1642 }, 1643 .home => index = 0, 1644 .end => index = count - 1, 1645 .page_up => { 1646 if (self.scroll.grid) |grid| if (slot_height > 0) { 1647 const rows_per_page = grid.size.height / slot_height; 1648 index -|= rows_per_page * cols; 1649 }; 1650 }, 1651 .page_down => { 1652 if (self.scroll.grid) |grid| if (slot_height > 0) { 1653 const rows_per_page = grid.size.height / slot_height; 1654 index = @min(index + rows_per_page * cols, count - 1); 1655 }; 1656 }, 1657 // scroll wheel moves the focused cell by a full row so the 1658 // viewport (via scrollToRect) follows in row-sized steps, 1659 // matching how a scroll wheel feels in a grid view. 1660 .mouse => |mouse| switch (mouse.action) { 1661 .scroll => |dir| switch (dir) { 1662 .up => index -|= cols, 1663 .down => if (index + cols < count) { 1664 index += cols; 1665 }, 1666 }, 1667 else => {}, 1668 }, 1669 else => {}, 1670 } 1671 1672 if (index != current_index) { 1673 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1674 if (in.cellRect(index)) |rect| { 1675 self.scroll.scrollToRect(rect); 1676 } 1677 } 1678 } 1679 1680 pub fn clearGrid(self: *Scroll) void { 1681 self.scroll.clearGrid(); 1682 } 1683 1684 pub fn getGrid(self: Scroll) ?Grid { 1685 return self.scroll.getGrid(); 1686 } 1687 1688 pub fn getFocus(self: *Scroll) *Focus { 1689 return self.scroll.getFocus(); 1690 } 1691 1692 pub fn getSelectedIndex(self: Scroll) ?usize { 1693 const in = self.scroll.child.flow_box; 1694 const child_id = in.focus.child_id orelse return null; 1695 return in.indexOfFocusId(child_id); 1696 } 1697 1698 fn inner(self: *Scroll) *FlowBox { 1699 return &self.scroll.child.flow_box; 1700 } 1701 }; 1702 }; 1703 1704 // a left-to-right flow of variable-width focusable items that wraps at the 1705 // available width, like words in a paragraph. unlike FlowBox there is no cell 1706 // grid: each item is as wide as its text. selection movement is driven by the 1707 // owning view via indexOfFocusId/verticalNeighbor. 1708 pub const TagFlow = struct { 1709 focus: *Focus, 1710 grid: ?Grid, 1711 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1712 // last build's item rects (content space), for vertical navigation and 1713 // scroll-into-view. 1714 rects: std.ArrayList(layout.IRect), 1715 // backs the per-item strings the text boxes borrow. 1716 arena: std.heap.ArenaAllocator, 1717 1718 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1719 1720 pub fn init(allocator: std.mem.Allocator) !TagFlow { 1721 return .{ 1722 .focus = try Focus.create(allocator, .container), 1723 .grid = null, 1724 .text_boxes = .empty, 1725 .rects = .empty, 1726 .arena = std.heap.ArenaAllocator.init(allocator), 1727 }; 1728 } 1729 1730 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 1731 self.focus.destroy(allocator); 1732 if (self.grid) |*grid| { 1733 grid.deinit(); 1734 self.grid = null; 1735 } 1736 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1737 self.text_boxes.deinit(allocator); 1738 self.rects.deinit(allocator); 1739 self.arena.deinit(); 1740 } 1741 1742 // both the item text and the link are copied into the arena, so the caller's 1743 // slices needn't outlive this call. 1744 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 1745 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1746 self.text_boxes.clearAndFree(allocator); 1747 1748 _ = self.arena.reset(.retain_capacity); 1749 const aa = self.arena.allocator(); 1750 1751 self.focus.clear(); 1752 self.focus.child_id = null; 1753 1754 for (items) |item| { 1755 const line = try aa.dupe(u8, item.text); 1756 1757 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1758 errdefer text_box.deinit(allocator); 1759 text_box.getFocus().focusable = true; 1760 1761 if (item.link.len > 0) { 1762 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1763 } 1764 1765 try self.text_boxes.append(allocator, text_box); 1766 } 1767 1768 if (self.text_boxes.items.len > 0) { 1769 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1770 } 1771 } 1772 1773 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1774 self.clearGrid(); 1775 self.focus.clear(); 1776 1777 if (self.text_boxes.items.len == 0) return; 1778 const max_width = constraint.max_size.width orelse 80; 1779 if (max_width == 0) return; 1780 1781 // lay the items out left to right, wrapping to a new row when the next 1782 // one wouldn't fit. 1783 self.rects.clearRetainingCapacity(); 1784 var x: usize = 0; 1785 var y: usize = 0; 1786 var row_height: usize = 0; 1787 var total_width: usize = 0; 1788 for (self.text_boxes.items) |*tb| { 1789 try tb.build(allocator, .{ 1790 .min_size = .{ .width = null, .height = null }, 1791 .max_size = .{ .width = max_width, .height = null }, 1792 }, root_focus); 1793 const tb_grid_maybe = tb.getGrid(); 1794 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1795 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1796 if (x > 0 and x + w > max_width) { 1797 x = 0; 1798 y += row_height; 1799 row_height = 0; 1800 } 1801 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 1802 if (h > row_height) row_height = h; 1803 x += w; 1804 if (x > total_width) total_width = x; 1805 } 1806 1807 const total_height = y + row_height; 1808 if (total_width == 0 or total_height == 0) return; 1809 1810 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1811 errdefer grid.deinit(); 1812 1813 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1814 const tb_grid = tb.getGrid() orelse continue; 1815 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1816 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1817 } 1818 1819 self.grid = grid; 1820 } 1821 1822 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1823 _ = self; 1824 _ = allocator; 1825 _ = key; 1826 _ = root_focus; 1827 } 1828 1829 pub fn clearGrid(self: *TagFlow) void { 1830 if (self.grid) |*grid| { 1831 grid.deinit(); 1832 self.grid = null; 1833 } 1834 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1835 } 1836 1837 pub fn getGrid(self: TagFlow) ?Grid { 1838 return self.grid; 1839 } 1840 1841 pub fn getFocus(self: *TagFlow) *Focus { 1842 return self.focus; 1843 } 1844 1845 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1846 for (self.text_boxes.items, 0..) |tb, i| { 1847 if (tb.box.focus.id == focus_id) return i; 1848 } 1849 return null; 1850 } 1851 1852 // the first item of the row below (when `downward`) or above, or null at 1853 // the flow's edge. items are laid out in order, so y is nondecreasing. 1854 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1855 const rects = self.rects.items; 1856 if (index >= rects.len) return null; 1857 const cur_y = rects[index].y; 1858 if (downward) { 1859 for (rects[index + 1 ..], index + 1..) |r, i| { 1860 if (r.y > cur_y) return i; 1861 } 1862 return null; 1863 } 1864 // walk the earlier rows; the last one visited is the row above. 1865 var prev_first: ?usize = null; 1866 var i: usize = 0; 1867 while (i < rects.len and rects[i].y < cur_y) { 1868 prev_first = i; 1869 const y = rects[i].y; 1870 while (i < rects.len and rects[i].y == y) i += 1; 1871 } 1872 return prev_first; 1873 } 1874 }; 1875 1876 // an invisible widget that fills the horizontal space granted by its parent. 1877 // used inside Box(horiz) with a min_size so the box reserves space for the 1878 // children that follow, pushing them to the right. 1879 pub const Spacer = struct { 1880 focus: *Focus, 1881 grid: ?Grid, 1882 1883 pub fn init(allocator: std.mem.Allocator) !Spacer { 1884 return .{ 1885 .focus = try Focus.create(allocator, .container), 1886 .grid = null, 1887 }; 1888 } 1889 1890 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1891 self.focus.destroy(allocator); 1892 if (self.grid) |*grid| { 1893 grid.deinit(); 1894 self.grid = null; 1895 } 1896 } 1897 1898 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1899 _ = root_focus; 1900 self.clearGrid(); 1901 const width = constraint.max_size.width orelse return; 1902 if (width == 0) return; 1903 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1904 } 1905 1906 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1907 _ = self; 1908 _ = allocator; 1909 _ = key; 1910 _ = root_focus; 1911 } 1912 1913 pub fn clearGrid(self: *Spacer) void { 1914 if (self.grid) |*grid| { 1915 grid.deinit(); 1916 self.grid = null; 1917 } 1918 } 1919 1920 pub fn getGrid(self: Spacer) ?Grid { 1921 return self.grid; 1922 } 1923 1924 pub fn getFocus(self: *Spacer) *Focus { 1925 return self.focus; 1926 } 1927 }; 1928 1929 // a one-row, non-focusable status bar showing the current page's url 1930 pub const Footer = struct { 1931 focus: *Focus, 1932 grid: ?Grid, 1933 session: *Session, 1934 arena: std.heap.ArenaAllocator, 1935 1936 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1937 return .{ 1938 .focus = try Focus.create(allocator, .container), 1939 .grid = null, 1940 .session = session, 1941 .arena = std.heap.ArenaAllocator.init(allocator), 1942 }; 1943 } 1944 1945 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1946 self.focus.destroy(allocator); 1947 if (self.grid) |*grid| grid.deinit(); 1948 self.arena.deinit(); 1949 } 1950 1951 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1952 _ = root_focus; 1953 self.clearGrid(); 1954 const width = constraint.max_size.width orelse return; 1955 if (width == 0) return; 1956 1957 _ = self.arena.reset(.retain_capacity); 1958 const aa = self.arena.allocator(); 1959 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1960 const text = if (self.session.web_port) |port| 1961 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1962 else 1963 path; 1964 1965 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1966 errdefer grid.deinit(); 1967 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1968 var i: usize = 0; 1969 while (utf8.nextCodepointSlice()) |ch| { 1970 if (i == width) break; 1971 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1972 i += 1; 1973 } 1974 self.grid = grid; 1975 } 1976 1977 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1978 _ = self; 1979 _ = allocator; 1980 _ = key; 1981 _ = root_focus; 1982 } 1983 1984 pub fn clearGrid(self: *Footer) void { 1985 if (self.grid) |*grid| { 1986 grid.deinit(); 1987 self.grid = null; 1988 } 1989 } 1990 1991 pub fn getGrid(self: Footer) ?Grid { 1992 return self.grid; 1993 } 1994 1995 pub fn getFocus(self: *Footer) *Focus { 1996 return self.focus; 1997 } 1998 }; 1999 2000 // a single-child wrapper that builds the child at its natural size and 2001 // positions its grid in the middle of the area granted by the parent 2002 pub const Center = struct { 2003 focus: *Focus, 2004 grid: ?Grid, 2005 child: *Widget, 2006 2007 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2008 const child = try allocator.create(Widget); 2009 errdefer allocator.destroy(child); 2010 child.* = child_widget; 2011 return .{ 2012 .focus = try Focus.create(allocator, .container), 2013 .grid = null, 2014 .child = child, 2015 }; 2016 } 2017 2018 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2019 self.focus.destroy(allocator); 2020 if (self.grid) |*grid| { 2021 grid.deinit(); 2022 self.grid = null; 2023 } 2024 self.child.deinit(allocator); 2025 allocator.destroy(self.child); 2026 } 2027 2028 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2029 self.clearGrid(); 2030 self.getFocus().clear(); 2031 2032 // build the child without forcing it to fill min_size; it sizes 2033 // itself to its content within the available max. 2034 try self.child.build(allocator, .{ 2035 .min_size = .{ .width = null, .height = null }, 2036 .max_size = constraint.max_size, 2037 }, root_focus); 2038 2039 const child_grid = self.child.getGrid() orelse return; 2040 2041 // prefer max when given; otherwise grow to max(min, child) so that 2042 // when only a min is set (e.g. the wasm path passes viewport rows 2043 // as min_height with no max), we can still vertically center while 2044 // letting taller content extend past the min. 2045 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 2046 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 2047 if (width == 0 or height == 0) return; 2048 2049 const offset_x: usize = (width -| child_grid.size.width) / 2; 2050 const offset_y: usize = (height -| child_grid.size.height) / 2; 2051 2052 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2053 errdefer grid.deinit(); 2054 try grid.drawGrid(child_grid, offset_x, offset_y); 2055 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2056 self.getFocus().child_id = self.child.getFocus().id; 2057 2058 self.grid = grid; 2059 } 2060 2061 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2062 try self.child.input(allocator, key, root_focus); 2063 } 2064 2065 pub fn clearGrid(self: *Center) void { 2066 if (self.grid) |*grid| { 2067 grid.deinit(); 2068 self.grid = null; 2069 } 2070 self.child.clearGrid(); 2071 } 2072 2073 pub fn getGrid(self: Center) ?Grid { 2074 return self.grid; 2075 } 2076 2077 pub fn getFocus(self: *Center) *Focus { 2078 return self.focus; 2079 } 2080 }; 2081 2082 // renders truecolor ANSI art 2083 pub const AnsiArt = struct { 2084 focus: *Focus, 2085 grid: ?Grid, 2086 content: []const u8, 2087 2088 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2089 return .{ 2090 .focus = try Focus.create(allocator, .container), 2091 .grid = null, 2092 .content = content, 2093 }; 2094 } 2095 2096 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2097 self.focus.destroy(allocator); 2098 if (self.grid) |*grid| { 2099 grid.deinit(); 2100 self.grid = null; 2101 } 2102 } 2103 2104 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2105 _ = root_focus; 2106 self.clearGrid(); 2107 2108 // parse into rows of cells, then copy into a rectangular grid 2109 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2110 defer { 2111 for (rows.items) |*row| row.deinit(allocator); 2112 rows.deinit(allocator); 2113 } 2114 var row: std.ArrayList(Grid.Cell) = .empty; 2115 errdefer row.deinit(allocator); 2116 2117 var style: Grid.Style = .{}; 2118 var width: usize = 0; 2119 const content = self.content; 2120 var i: usize = 0; 2121 while (i < content.len) { 2122 const byte = content[i]; 2123 if (byte == '\n') { 2124 width = @max(width, row.items.len); 2125 try rows.append(allocator, row); 2126 row = .empty; 2127 i += 1; 2128 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2129 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2130 var j = i + 2; 2131 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2132 if (j >= content.len) { 2133 i = content.len; // malformed trailing escape; stop 2134 } else { 2135 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2136 i = j + 1; 2137 } 2138 } else { 2139 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2140 const end = @min(content.len, i + len); 2141 const rune = content[i..end]; 2142 const transparent = end - i == 1 and rune[0] == ' ' and 2143 style.fg == null and style.bg == null and !style.inverted; 2144 try row.append(allocator, .{ 2145 .rune = if (transparent) null else rune, 2146 .style = style, 2147 }); 2148 i = end; 2149 } 2150 } 2151 // a trailing row with content but no closing newline 2152 if (row.items.len > 0) { 2153 width = @max(width, row.items.len); 2154 try rows.append(allocator, row); 2155 } else { 2156 row.deinit(allocator); 2157 } 2158 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2159 2160 const height = rows.items.len; 2161 if (width == 0 or height == 0) return; 2162 2163 const clamped_w = @min(width, constraint.max_size.width orelse width); 2164 const clamped_h = @min(height, constraint.max_size.height orelse height); 2165 2166 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2167 errdefer grid.deinit(); 2168 for (rows.items[0..clamped_h], 0..) |r, y| { 2169 const n = @min(r.items.len, clamped_w); 2170 for (r.items[0..n], 0..) |cell, x| { 2171 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2172 } 2173 } 2174 self.grid = grid; 2175 } 2176 2177 pub fn clearGrid(self: *AnsiArt) void { 2178 if (self.grid) |*grid| { 2179 grid.deinit(); 2180 self.grid = null; 2181 } 2182 } 2183 2184 pub fn getGrid(self: AnsiArt) ?Grid { 2185 return self.grid; 2186 } 2187 2188 pub fn getFocus(self: *AnsiArt) *Focus { 2189 return self.focus; 2190 } 2191 2192 fn applySgr(style: *Grid.Style, params: []const u8) void { 2193 var nums: [16]u32 = undefined; 2194 var n: usize = 0; 2195 var it = std.mem.splitScalar(u8, params, ';'); 2196 while (it.next()) |tok| { 2197 if (n >= nums.len) break; 2198 // an empty parameter (e.g. bare "\x1b[m") means 0 2199 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2200 n += 1; 2201 } 2202 if (n == 0) { 2203 style.* = .{}; 2204 return; 2205 } 2206 var i: usize = 0; 2207 while (i < n) : (i += 1) { 2208 switch (nums[i]) { 2209 0 => style.* = .{}, 2210 7 => style.inverted = true, 2211 27 => style.inverted = false, 2212 39 => style.fg = null, 2213 49 => style.bg = null, 2214 38, 48 => { 2215 // truecolor form: 38;2;r;g;b — anything else is ignored 2216 if (i + 4 < n and nums[i + 1] == 2) { 2217 const c = Grid.Color{ 2218 .r = @truncate(nums[i + 2]), 2219 .g = @truncate(nums[i + 3]), 2220 .b = @truncate(nums[i + 4]), 2221 }; 2222 if (nums[i] == 38) style.fg = c else style.bg = c; 2223 i += 4; 2224 } 2225 }, 2226 else => {}, 2227 } 2228 } 2229 } 2230 }; 2231 2232 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2233 pub const AnsiBackground = struct { 2234 grid: ?Grid, 2235 child: *Widget, 2236 art: AnsiArt, 2237 session: *Session, 2238 2239 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2240 var cw = child_widget; 2241 const child = allocator.create(Widget) catch |e| { 2242 cw.deinit(allocator); 2243 return e; 2244 }; 2245 child.* = cw; 2246 errdefer { 2247 child.deinit(allocator); 2248 allocator.destroy(child); 2249 } 2250 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2251 } 2252 2253 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2254 if (self.grid) |*grid| grid.deinit(); 2255 self.art.deinit(allocator); 2256 self.child.deinit(allocator); 2257 allocator.destroy(self.child); 2258 } 2259 2260 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2261 self.clearGrid(); 2262 2263 // make the wrapped page fill the whole available area 2264 try self.child.build(allocator, .{ 2265 .min_size = .{ 2266 .width = constraint.max_size.width orelse constraint.min_size.width, 2267 .height = constraint.max_size.height orelse constraint.min_size.height, 2268 }, 2269 .max_size = constraint.max_size, 2270 }, root_focus); 2271 2272 if (self.session.data.enable_ansi) { 2273 if (self.child.getGrid()) |fg| { 2274 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2275 } 2276 } 2277 } 2278 2279 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2280 try self.child.input(allocator, key, root_focus); 2281 } 2282 2283 pub fn clearGrid(self: *AnsiBackground) void { 2284 if (self.grid) |*grid| { 2285 grid.deinit(); 2286 self.grid = null; 2287 } 2288 self.child.clearGrid(); 2289 self.art.clearGrid(); 2290 } 2291 2292 pub fn getGrid(self: AnsiBackground) ?Grid { 2293 return self.grid orelse self.child.getGrid(); 2294 } 2295 2296 pub fn getFocus(self: *AnsiBackground) *Focus { 2297 return self.child.getFocus(); 2298 } 2299 2300 // which top corner the art hugs. 2301 const ArtAnchor = enum { top_left, top_right }; 2302 2303 // a foreground cell counts as blank if it's empty or a bare space without styling 2304 fn cellIsBlank(cell: Grid.Cell) bool { 2305 if (cell.rune) |rune| { 2306 return std.mem.eql(u8, rune, " ") and 2307 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2308 } 2309 return true; 2310 } 2311 2312 // the art is dimmed to this fraction of its brightness so foreground text 2313 // stays legible over it 2314 const art_brightness = 45; // percent 2315 2316 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2317 const c = color orelse return null; 2318 return .{ 2319 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2320 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2321 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2322 }; 2323 } 2324 2325 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2326 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2327 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2328 // snap such channels clear of that range so the backdrop doesn't blink or 2329 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2330 // actually render truecolor. 2331 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2332 const c = color orelse return null; 2333 const snap = struct { 2334 fn f(v: u8) u8 { 2335 return if (v >= 1 and v <= 9) 10 else v; 2336 } 2337 }.f; 2338 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2339 } 2340 2341 // composites ANSI art behind a foreground grid 2342 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2343 art.clearGrid(); 2344 try art.build(allocator, .{ 2345 .min_size = .{ .width = null, .height = null }, 2346 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2347 }, root_focus); 2348 2349 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2350 errdefer out.deinit(); 2351 2352 if (art.getGrid()) |art_grid| { 2353 const anchor_x = switch (anchor) { 2354 .top_left => 0, 2355 .top_right => foreground.size.width -| art_grid.size.width, 2356 }; 2357 for (0..art_grid.size.height) |y| { 2358 for (0..art_grid.size.width) |x| { 2359 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2360 if (src.rune == null) continue; 2361 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2362 const dst = &out.cells.items[idx]; 2363 // composite the dimmed art behind every cell so it shows 2364 // through the whole UI: a blank cell becomes the art, while a 2365 // cell with a glyph keeps the glyph but takes the dimmed art as 2366 // its background (replacing whatever background it had, which 2367 // would otherwise obscure the art). 2368 if (cellIsBlank(dst.*)) { 2369 dst.* = src; 2370 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2371 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2372 } else { 2373 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2374 } 2375 } 2376 } 2377 } 2378 return out; 2379 } 2380 }; 2381