╭ 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 and re-read user preferences 950 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 951 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 952 self.haxy_moment = moment; 953 954 const user_id = self.data.user_id orelse return; 955 var arena = std.heap.ArenaAllocator.init(allocator); 956 defer arena.deinit(); 957 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, user_id)) |user| { 958 self.data.enable_ansi = user.event.enable_ansi; 959 } 960 } 961 962 // request a forward navigation to `route`; the host consumes next_page 963 // (Nav.sync on the terminal, the wasm tick on the web). 964 pub fn navigate(self: *Session, route: RoutablePage) !void { 965 self.next_page = route; 966 } 967 }; 968 969 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 970 var nav = try Nav.init(allocator, session); 971 defer nav.deinit(allocator); 972 973 var terminal = try term.Terminal.init(io, allocator); 974 defer terminal.deinit(io); 975 976 // set term as active so it will be properly cooked 977 // when a panic/segfault happens 978 term.setActive(&terminal); 979 defer term.setActive(null); 980 981 var last_size = layout.Size{ .width = 0, .height = 0 }; 982 var last_grid = try Grid.init(allocator, last_size); 983 defer last_grid.deinit(); 984 985 while (!terminal.shouldQuit()) { 986 const grid_changed = try terminal.render(&nav.root, &last_grid, &last_size); 987 988 // process any inputs. 989 // 990 // if the grid didn't change, then first do a blocking 991 // read, so the thread will sleep until further input. 992 // after that, all remaining reads are non-blocking so 993 // we can process the rest of the queued inputs. 994 // 995 // if the grid *did* change, then only do non-blocking 996 // reads. we do not want to sleep the thread because 997 // there may be an animation that requires more looping. 998 var blocking = !grid_changed; 999 while (try terminal.readKey(io, blocking)) |key| { 1000 blocking = false; 1001 try inputKey(allocator, &nav.root, key, session); 1002 } 1003 1004 // local run has no repo to persist to, so just apply in-memory 1005 session.applyPending(); 1006 1007 // pick up data written by other handles so the next navigation 1008 // builds its page from a current moment (local mode has no admin repo) 1009 if (repo_maybe) |repo| try session.reloadMoment(repo); 1010 1011 // reconcile navigation: forward to a new page, or back on escape. 1012 try nav.sync(allocator, session); 1013 1014 // the quit button (on the quit tab) asks the host to tear down. 1015 if (session.quit_requested) terminal.requestQuit(); 1016 1017 try nav.root.build(allocator, .{ 1018 .min_size = .{ .width = null, .height = null }, 1019 .max_size = .{ .width = last_size.width, .height = last_size.height }, 1020 }, nav.root.getFocus()); 1021 } 1022 } 1023 1024 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 1025 switch (key) { 1026 // request a navigation pop; the host's Nav.sync goes back a page, or 1027 // quits when there's no history left. 1028 .escape => session.nav_back = true, 1029 .ctrl => |letter| switch (letter) { 1030 'r' => session.refresh_requested = true, 1031 else => try root.input(allocator, key, root.getFocus()), 1032 }, 1033 .enter => { 1034 const root_focus = root.getFocus(); 1035 if (root_focus.grandchild_id) |gid| { 1036 // follow a cross-page link 1037 if (crossPageLink(root_focus, gid, session.data)) |route| { 1038 return session.navigate(route); 1039 } 1040 } 1041 try root.input(allocator, key, root_focus); 1042 }, 1043 .mouse => |mouse| { 1044 if (mouse.action == .press and mouse.action.press == .left) { 1045 const root_focus = root.getFocus(); 1046 var clicked: ?usize = null; 1047 var iter = root_focus.children.iterator(); 1048 while (iter.next()) |entry| { 1049 const child = entry.value_ptr.*; 1050 if (!child.focus.focusable) continue; 1051 const r = child.rect; 1052 if (mouse.x >= r.x and mouse.y >= r.y and 1053 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 1054 { 1055 clicked = entry.key_ptr.*; 1056 break; 1057 } 1058 } 1059 if (clicked) |focus_id| { 1060 // follow a cross-page link 1061 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 1062 return session.navigate(route); 1063 } 1064 root_focus.setFocus(focus_id); 1065 } 1066 // forward the press into the widget tree so buttons (and any 1067 // future click-aware widgets) can react. widgets that don't 1068 // care about presses ignore it. 1069 try root.input(allocator, key, root.getFocus()); 1070 } else { 1071 try root.input(allocator, key, root.getFocus()); 1072 } 1073 }, 1074 else => try root.input(allocator, key, root.getFocus()), 1075 } 1076 } 1077 1078 // if the focus target at focus_id is an `a:` link that should navigate (a 1079 // different parent page, or a different files directory within the repo page), 1080 // return its route; otherwise null. lets a host turn a click / enter on such a 1081 // link into a navigation rather than a focus change. 1082 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 1083 const current = data.current_page; 1084 const child = root_focus.children.get(focus_id) orelse return null; 1085 const custom = switch (child.focus.kind) { 1086 .custom => |c| c, 1087 else => return null, 1088 }; 1089 const a_prefix = "a:"; 1090 if (!std.mem.startsWith(u8, custom, a_prefix)) return null; 1091 const path = custom[a_prefix.len..]; 1092 // local sessions build (and parse) links with the repo identity elided 1093 const route = (if (data.is_local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path)) orelse return null; 1094 // a link to a different parent page always navigates; within a page, a 1095 // files-directory / commits-page / list-window change navigates while tab 1096 // links stay in-page (header-handled). 1097 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 1098 return null; 1099 } 1100 1101 // a display author, resolved against the admin db at read time 1102 pub const Author = union(enum) { 1103 unknown, 1104 email: []const u8, // an email no user matches 1105 user_name: []const u8, 1106 1107 // the commit author line's email, resolved like initFromEmail 1108 pub fn init( 1109 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1110 arena: *std.heap.ArenaAllocator, 1111 author_line: []const u8, 1112 ) !Author { 1113 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 1114 } 1115 1116 // from a record's stored author email: it resolves to the user currently 1117 // holding it, else stays the email. the result is allocated in `arena`, 1118 // so the email may borrow transient memory. 1119 pub fn initFromEmail( 1120 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1121 arena: *std.heap.ArenaAllocator, 1122 author_email: ?[]const u8, 1123 ) !Author { 1124 const email = author_email orelse return .unknown; 1125 if (haxy_moment) |moment| { 1126 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 1127 return .{ .user_name = user.event.name }; 1128 } 1129 } 1130 return .{ .email = try arena.allocator().dupe(u8, email) }; 1131 } 1132 }; 1133 1134 // the "a:" link to user `name`'s page. 1135 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 1136 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 1137 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 1138 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1139 } 1140 1141 // a focusable " author " box showing `author`, linking to their user page 1142 // when it is a known user 1143 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox(Widget) { 1144 const text = switch (author) { 1145 .unknown => "", 1146 .email, .user_name => |t| t, 1147 }; 1148 var tb = try wgt.TextBox(Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 1149 errdefer tb.deinit(allocator); 1150 tb.getFocus().focusable = true; 1151 switch (author) { 1152 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 1153 else => {}, 1154 } 1155 return tb; 1156 } 1157 1158 // native-TUI navigation 1159 pub const Nav = struct { 1160 root: Widget, 1161 route: RoutablePage, 1162 // backs the current page (its Page data and the page-scoped strings its 1163 // widgets build). owned here, swapped on each navigation. session.page_arena 1164 // tracks whichever of these is current so widgets allocate into it. 1165 arena: *std.heap.ArenaAllocator, 1166 history: std.ArrayList(Entry), 1167 1168 // each retained page keeps its own arena, freed when the page leaves the 1169 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 1170 // long-lived session from accumulating every page it ever visited. 1171 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 1172 1173 // cap on retained back-history so the chain can't grow memory without bound 1174 const max_history: usize = 16; 1175 1176 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 1177 const arena = try allocator.create(std.heap.ArenaAllocator); 1178 arena.* = std.heap.ArenaAllocator.init(allocator); 1179 errdefer { 1180 arena.deinit(); 1181 allocator.destroy(arena); 1182 } 1183 1184 session.page_arena = arena; 1185 const route = session.data.current_page; 1186 1187 const page = try arena.allocator().create(Page); 1188 page.* = try Page.init(arena, session, route); 1189 return .{ 1190 .root = try initRoot(allocator, page, session), 1191 .route = route, 1192 .arena = arena, 1193 .history = .empty, 1194 }; 1195 } 1196 1197 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 1198 self.root.deinit(allocator); 1199 freeArena(allocator, self.arena); 1200 for (self.history.items) |*entry| { 1201 entry.root.deinit(allocator); 1202 freeArena(allocator, entry.arena); 1203 } 1204 self.history.deinit(allocator); 1205 } 1206 1207 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 1208 arena.deinit(); 1209 allocator.destroy(arena); 1210 } 1211 1212 // reconcile the displayed root with the session's navigation state. a 1213 // forward nav (current_page moved to a different parent page) pushes the 1214 // current root and builds the new page in a fresh arena; a back request 1215 // frees the current page and restores the previous one. when escape is 1216 // pressed with no history left we switch to the quit tab instead of quitting. 1217 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 1218 // refresh: rebuild the current page in place from a current moment 1219 if (session.refresh_requested) { 1220 session.refresh_requested = false; 1221 if (session.haxy_moment != null or session.local != null) { 1222 const arena = try allocator.create(std.heap.ArenaAllocator); 1223 arena.* = std.heap.ArenaAllocator.init(allocator); 1224 errdefer freeArena(allocator, arena); 1225 1226 session.page_arena = arena; 1227 1228 const page = try arena.allocator().create(Page); 1229 page.* = try Page.init(arena, session, self.route); 1230 const new_root = try initRoot(allocator, page, session); 1231 1232 self.root.deinit(allocator); 1233 freeArena(allocator, self.arena); 1234 self.root = new_root; 1235 self.arena = arena; 1236 } 1237 return; 1238 } 1239 1240 if (session.nav_back) { 1241 session.nav_back = false; 1242 if (self.history.pop()) |entry| { 1243 self.root.deinit(allocator); 1244 freeArena(allocator, self.arena); 1245 self.root = entry.root; 1246 self.route = entry.route; 1247 self.arena = entry.arena; 1248 session.page_arena = entry.arena; 1249 session.data.current_page = entry.route; 1250 return; 1251 } 1252 // nothing to go back to; switch to the quit confirmation 1253 const root_focus = self.root.getFocus(); 1254 var iter = root_focus.children.iterator(); 1255 while (iter.next()) |entry| { 1256 switch (entry.value_ptr.focus.kind) { 1257 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1258 root_focus.setFocus(entry.key_ptr.*); 1259 // a Stack only builds its selected child, so the quit 1260 // button isn't in the focus tree until a build runs with 1261 // the quit tab selected. build once, then send a 1262 // synthetic arrow_down to drop focus from the tab onto 1263 // the button so it's ready to confirm. 1264 try self.root.build(allocator, .{ 1265 .min_size = .{ .width = null, .height = 40 }, 1266 .max_size = .{ .width = 80, .height = null }, 1267 }, root_focus); 1268 try self.root.input(allocator, .arrow_down, root_focus); 1269 break; 1270 }, 1271 else => {}, 1272 } 1273 } 1274 return; 1275 } 1276 1277 // forward navigation: navigate() set next_page to the page to move to (a 1278 // cross-page link or tab change crossing pages) 1279 if (session.next_page) |route| { 1280 session.next_page = null; 1281 if (session.haxy_moment == null and session.local == null) return; 1282 // the page we navigated to becomes the current page 1283 session.data.current_page = route; 1284 1285 const arena = try allocator.create(std.heap.ArenaAllocator); 1286 arena.* = std.heap.ArenaAllocator.init(allocator); 1287 errdefer freeArena(allocator, arena); 1288 1289 session.page_arena = arena; 1290 1291 const page = try arena.allocator().create(Page); 1292 page.* = try Page.init(arena, session, route); 1293 const new_root = try initRoot(allocator, page, session); 1294 1295 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 1296 // drop the oldest entry (freeing its widget tree and arena) once over cap 1297 if (self.history.items.len > max_history) { 1298 var oldest = self.history.orderedRemove(0); 1299 oldest.root.deinit(allocator); 1300 freeArena(allocator, oldest.arena); 1301 } 1302 self.root = new_root; 1303 self.route = route; 1304 self.arena = arena; 1305 } 1306 } 1307 }; 1308 1309 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 1310 const page_widget: Widget = switch (page.*) { 1311 .home => |*p| .{ .home = try .init(allocator, p, session) }, 1312 .user => |*p| .{ .user = try .init(allocator, p, session) }, 1313 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 1314 }; 1315 1316 const demon_art = @embedFile("embed/demon.ans"); 1317 1318 // on the TUI/SSH, the page sits above a one-row footer showing the url 1319 var root = if (session.is_terminal) blk: { 1320 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1321 errdefer box.deinit(allocator); 1322 const bg_id = bg_blk: { 1323 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 1324 errdefer background.deinit(allocator); 1325 const id = background.getFocus().id; 1326 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 1327 break :bg_blk id; 1328 }; 1329 { 1330 var footer = try Footer.init(allocator, session); 1331 errdefer footer.deinit(allocator); 1332 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 1333 } 1334 box.getFocus().child_id = bg_id; 1335 break :blk Widget{ .box = box }; 1336 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 1337 errdefer root.deinit(allocator); 1338 1339 // input-owning views build their TextInputs in init — so reset the 1340 // focus-id -> *TextInput map here 1341 session.text_inputs.clearRetainingCapacity(); 1342 session.input_values.clearRetainingCapacity(); 1343 1344 try root.build(allocator, .{ 1345 .min_size = .{ .width = null, .height = 40 }, 1346 .max_size = .{ .width = 100, .height = null }, 1347 }, root.getFocus()); 1348 1349 return root; 1350 } 1351 1352 pub const Widget = union(enum) { 1353 text: wgt.Text(Widget), 1354 box: wgt.Box(Widget), 1355 text_box: wgt.TextBox(Widget), 1356 text_input: wgt.TextInput(Widget), 1357 scroll: wgt.Scroll(Widget), 1358 stack: wgt.Stack(Widget), 1359 flow_box: FlowBox, 1360 flow_box_scroll: FlowBox.Scroll, 1361 tag_flow: TagFlow, 1362 spacer: Spacer, 1363 center: Center, 1364 background: AnsiBackground, 1365 home: Home.View, 1366 user: User.View, 1367 repo: Repo.View, 1368 quit: Quit.View, 1369 unauthorized: Unauthorized.View, 1370 title: Title.View, 1371 sub_title: SubTitle.View, 1372 home_header: Home.Header.View, 1373 user_header: User.Header.View, 1374 repo_header: Repo.Header.View, 1375 repo_files_header: Repo.Files.Header.View, 1376 repo_commits_header: Repo.Commits.Header.View, 1377 repo_issues_header: Repo.Issues.Header, 1378 repo_files: Repo.Files.View, 1379 repo_commits: Repo.Commits.View, 1380 repo_refs: Repo.Refs.View, 1381 repo_issues: Repo.Issues.View, 1382 home_users: Home.Users.View, 1383 home_repos: Home.Repos.View, 1384 auth_tab: Home.Header.AuthTab.View, 1385 home_settings: Home.Settings.View, 1386 home_auth: Home.Auth.View, 1387 footer: Footer, 1388 1389 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 1390 switch (self.*) { 1391 inline else => |*case| case.deinit(allocator), 1392 } 1393 } 1394 1395 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 1396 switch (self.*) { 1397 inline else => |*case| try case.build(allocator, constraint, root_focus), 1398 } 1399 } 1400 1401 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 1402 switch (self.*) { 1403 inline else => |*case| try case.input(allocator, key, root_focus), 1404 } 1405 } 1406 1407 pub fn clearGrid(self: *Widget) void { 1408 switch (self.*) { 1409 inline else => |*case| case.clearGrid(), 1410 } 1411 } 1412 1413 pub fn getGrid(self: Widget) ?Grid { 1414 switch (self) { 1415 inline else => |*case| return case.getGrid(), 1416 } 1417 } 1418 1419 pub fn getFocus(self: *Widget) *Focus { 1420 switch (self.*) { 1421 inline else => |*case| return case.getFocus(), 1422 } 1423 } 1424 }; 1425 1426 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 1427 // `scroll` to keep it visible. 1428 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 1429 const keys = box.children.keys(); 1430 if (keys.len == 0) return; 1431 const cur_id = box.getFocus().child_id orelse return; 1432 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 1433 const last: isize = @intCast(keys.len - 1); 1434 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 1435 if (next == @as(usize, @intCast(cur))) return; 1436 root_focus.setFocus(keys[next]); 1437 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 1438 } 1439 1440 pub const FlowBox = struct { 1441 focus: *Focus, 1442 grid: ?Grid, 1443 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1444 // backs the per-item strings the text boxes borrow: each box's `content` 1445 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 1446 // wholesale on each setItems so there's no per-string ownership to track. 1447 arena: std.heap.ArenaAllocator, 1448 // column count from the last build — FlowBox.Scroll.input uses it so arrow 1449 // up/down can step by a row's worth of items. 1450 last_cols: usize, 1451 options: Options, 1452 1453 const border_rows: usize = 2; 1454 1455 pub const Options = struct { 1456 cell_width: usize = 40, 1457 cell_height: usize = 3, 1458 }; 1459 1460 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1461 return .{ 1462 .focus = try Focus.create(allocator, .container), 1463 .grid = null, 1464 .text_boxes = .empty, 1465 .arena = std.heap.ArenaAllocator.init(allocator), 1466 .last_cols = 1, 1467 .options = options, 1468 }; 1469 } 1470 1471 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1472 self.focus.destroy(allocator); 1473 if (self.grid) |*grid| { 1474 grid.deinit(); 1475 self.grid = null; 1476 } 1477 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1478 self.text_boxes.deinit(allocator); 1479 self.arena.deinit(); 1480 } 1481 1482 // one flow item: its display text plus an optional link. a non-empty `link` 1483 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1484 // the web renderer turns into an anchor. 1485 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1486 1487 // both the item text and the link are copied into the arena, so the caller's 1488 // slices needn't outlive this call. 1489 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1490 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1491 self.text_boxes.clearAndFree(allocator); 1492 1493 // the old text boxes (and their borrowed content/links) are gone now, so 1494 // the arena that backed those strings can be reclaimed. 1495 _ = self.arena.reset(.retain_capacity); 1496 const aa = self.arena.allocator(); 1497 1498 self.focus.clear(); 1499 self.focus.child_id = null; 1500 1501 for (items) |item| { 1502 const line = try aa.dupe(u8, item.text); 1503 1504 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1505 errdefer text_box.deinit(allocator); 1506 text_box.getFocus().focusable = true; 1507 1508 if (item.link.len > 0) { 1509 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1510 } 1511 1512 try self.text_boxes.append(allocator, text_box); 1513 } 1514 1515 if (self.text_boxes.items.len > 0) { 1516 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1517 } 1518 } 1519 1520 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1521 self.clearGrid(); 1522 self.focus.clear(); 1523 1524 const cell_width = self.options.cell_width; 1525 const max_width = constraint.max_size.width orelse cell_width; 1526 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1527 self.last_cols = cols; 1528 1529 const count = self.text_boxes.items.len; 1530 if (count == 0) return; 1531 const slot_height = self.options.cell_height + border_rows; 1532 if (slot_height == 0) return; 1533 1534 // build at the slot size so every text box fits a single grid cell 1535 for (self.text_boxes.items) |*tb| { 1536 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1537 try tb.build(allocator, .{ 1538 .min_size = .{ .width = cell_width, .height = null }, 1539 .max_size = .{ .width = cell_width, .height = slot_height }, 1540 }, root_focus); 1541 } 1542 1543 const rows = (count + cols - 1) / cols; 1544 const content_height = rows * slot_height; 1545 const total_width = cols * cell_width; 1546 if (total_width == 0 or content_height == 0) return; 1547 1548 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1549 errdefer grid.deinit(); 1550 1551 for (self.text_boxes.items, 0..) |*tb, i| { 1552 const tb_grid = tb.getGrid() orelse continue; 1553 const col = i % cols; 1554 const row = i / cols; 1555 const cell_x = col * cell_width; 1556 const cell_y = row * slot_height; 1557 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1558 try grid.drawGrid(tb_grid, cell_x, cell_y); 1559 } 1560 1561 self.grid = grid; 1562 } 1563 1564 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1565 _ = self; 1566 _ = allocator; 1567 _ = key; 1568 _ = root_focus; 1569 } 1570 1571 pub fn clearGrid(self: *FlowBox) void { 1572 if (self.grid) |*grid| { 1573 grid.deinit(); 1574 self.grid = null; 1575 } 1576 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1577 } 1578 1579 pub fn getGrid(self: FlowBox) ?Grid { 1580 return self.grid; 1581 } 1582 1583 pub fn getFocus(self: *FlowBox) *Focus { 1584 return self.focus; 1585 } 1586 1587 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1588 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1589 const slot_height = self.options.cell_height + border_rows; 1590 const col = index % self.last_cols; 1591 const row = index / self.last_cols; 1592 return .{ 1593 .x = @intCast(col * self.options.cell_width), 1594 .y = @intCast(row * slot_height), 1595 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1596 }; 1597 } 1598 1599 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1600 for (self.text_boxes.items, 0..) |tb, i| { 1601 if (tb.box.focus.id == focus_id) return i; 1602 } 1603 return null; 1604 } 1605 1606 pub const Scroll = struct { 1607 scroll: wgt.Scroll(Widget), 1608 1609 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1610 var layout_inner = try FlowBox.init(allocator, options); 1611 errdefer layout_inner.deinit(allocator); 1612 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1613 errdefer scroll.deinit(allocator); 1614 return .{ .scroll = scroll }; 1615 } 1616 1617 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1618 self.scroll.deinit(allocator); 1619 } 1620 1621 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1622 self.scroll.x = 0; 1623 self.scroll.y = 0; 1624 try self.inner().setItems(allocator, items); 1625 } 1626 1627 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1628 try self.scroll.build(allocator, constraint, root_focus); 1629 } 1630 1631 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1632 _ = allocator; 1633 const in = self.inner(); 1634 const child_id = in.focus.child_id orelse return; 1635 const current_index = in.indexOfFocusId(child_id) orelse return; 1636 const count = in.text_boxes.items.len; 1637 if (count == 0) return; 1638 const cols = in.last_cols; 1639 const slot_height = in.options.cell_height + FlowBox.border_rows; 1640 1641 var index = current_index; 1642 switch (key) { 1643 .arrow_up => index -|= cols, 1644 .arrow_down => if (index + cols < count) { 1645 index += cols; 1646 }, 1647 .arrow_left => index -|= 1, 1648 .arrow_right => if (index + 1 < count) { 1649 index += 1; 1650 }, 1651 .home => index = 0, 1652 .end => index = count - 1, 1653 .page_up => { 1654 if (self.scroll.grid) |grid| if (slot_height > 0) { 1655 const rows_per_page = grid.size.height / slot_height; 1656 index -|= rows_per_page * cols; 1657 }; 1658 }, 1659 .page_down => { 1660 if (self.scroll.grid) |grid| if (slot_height > 0) { 1661 const rows_per_page = grid.size.height / slot_height; 1662 index = @min(index + rows_per_page * cols, count - 1); 1663 }; 1664 }, 1665 // scroll wheel moves the focused cell by a full row so the 1666 // viewport (via scrollToRect) follows in row-sized steps, 1667 // matching how a scroll wheel feels in a grid view. 1668 .mouse => |mouse| switch (mouse.action) { 1669 .scroll => |dir| switch (dir) { 1670 .up => index -|= cols, 1671 .down => if (index + cols < count) { 1672 index += cols; 1673 }, 1674 }, 1675 else => {}, 1676 }, 1677 else => {}, 1678 } 1679 1680 if (index != current_index) { 1681 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1682 if (in.cellRect(index)) |rect| { 1683 self.scroll.scrollToRect(rect); 1684 } 1685 } 1686 } 1687 1688 pub fn clearGrid(self: *Scroll) void { 1689 self.scroll.clearGrid(); 1690 } 1691 1692 pub fn getGrid(self: Scroll) ?Grid { 1693 return self.scroll.getGrid(); 1694 } 1695 1696 pub fn getFocus(self: *Scroll) *Focus { 1697 return self.scroll.getFocus(); 1698 } 1699 1700 pub fn getSelectedIndex(self: Scroll) ?usize { 1701 const in = self.scroll.child.flow_box; 1702 const child_id = in.focus.child_id orelse return null; 1703 return in.indexOfFocusId(child_id); 1704 } 1705 1706 fn inner(self: *Scroll) *FlowBox { 1707 return &self.scroll.child.flow_box; 1708 } 1709 }; 1710 }; 1711 1712 // a left-to-right flow of variable-width focusable items that wraps at the 1713 // available width, like words in a paragraph. unlike FlowBox there is no cell 1714 // grid: each item is as wide as its text. selection movement is driven by the 1715 // owning view via indexOfFocusId/verticalNeighbor. 1716 pub const TagFlow = struct { 1717 focus: *Focus, 1718 grid: ?Grid, 1719 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1720 // last build's item rects (content space), for vertical navigation and 1721 // scroll-into-view. 1722 rects: std.ArrayList(layout.IRect), 1723 // backs the per-item strings the text boxes borrow. 1724 arena: std.heap.ArenaAllocator, 1725 1726 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1727 1728 pub fn init(allocator: std.mem.Allocator) !TagFlow { 1729 return .{ 1730 .focus = try Focus.create(allocator, .container), 1731 .grid = null, 1732 .text_boxes = .empty, 1733 .rects = .empty, 1734 .arena = std.heap.ArenaAllocator.init(allocator), 1735 }; 1736 } 1737 1738 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 1739 self.focus.destroy(allocator); 1740 if (self.grid) |*grid| { 1741 grid.deinit(); 1742 self.grid = null; 1743 } 1744 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1745 self.text_boxes.deinit(allocator); 1746 self.rects.deinit(allocator); 1747 self.arena.deinit(); 1748 } 1749 1750 // both the item text and the link are copied into the arena, so the caller's 1751 // slices needn't outlive this call. 1752 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 1753 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1754 self.text_boxes.clearAndFree(allocator); 1755 1756 _ = self.arena.reset(.retain_capacity); 1757 const aa = self.arena.allocator(); 1758 1759 self.focus.clear(); 1760 self.focus.child_id = null; 1761 1762 for (items) |item| { 1763 const line = try aa.dupe(u8, item.text); 1764 1765 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1766 errdefer text_box.deinit(allocator); 1767 text_box.getFocus().focusable = true; 1768 1769 if (item.link.len > 0) { 1770 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1771 } 1772 1773 try self.text_boxes.append(allocator, text_box); 1774 } 1775 1776 if (self.text_boxes.items.len > 0) { 1777 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1778 } 1779 } 1780 1781 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1782 self.clearGrid(); 1783 self.focus.clear(); 1784 1785 if (self.text_boxes.items.len == 0) return; 1786 const max_width = constraint.max_size.width orelse 80; 1787 if (max_width == 0) return; 1788 1789 // lay the items out left to right, wrapping to a new row when the next 1790 // one wouldn't fit. 1791 self.rects.clearRetainingCapacity(); 1792 var x: usize = 0; 1793 var y: usize = 0; 1794 var row_height: usize = 0; 1795 var total_width: usize = 0; 1796 for (self.text_boxes.items) |*tb| { 1797 try tb.build(allocator, .{ 1798 .min_size = .{ .width = null, .height = null }, 1799 .max_size = .{ .width = max_width, .height = null }, 1800 }, root_focus); 1801 const tb_grid_maybe = tb.getGrid(); 1802 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1803 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1804 if (x > 0 and x + w > max_width) { 1805 x = 0; 1806 y += row_height; 1807 row_height = 0; 1808 } 1809 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 1810 if (h > row_height) row_height = h; 1811 x += w; 1812 if (x > total_width) total_width = x; 1813 } 1814 1815 const total_height = y + row_height; 1816 if (total_width == 0 or total_height == 0) return; 1817 1818 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1819 errdefer grid.deinit(); 1820 1821 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1822 const tb_grid = tb.getGrid() orelse continue; 1823 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1824 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1825 } 1826 1827 self.grid = grid; 1828 } 1829 1830 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1831 _ = self; 1832 _ = allocator; 1833 _ = key; 1834 _ = root_focus; 1835 } 1836 1837 pub fn clearGrid(self: *TagFlow) void { 1838 if (self.grid) |*grid| { 1839 grid.deinit(); 1840 self.grid = null; 1841 } 1842 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1843 } 1844 1845 pub fn getGrid(self: TagFlow) ?Grid { 1846 return self.grid; 1847 } 1848 1849 pub fn getFocus(self: *TagFlow) *Focus { 1850 return self.focus; 1851 } 1852 1853 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1854 for (self.text_boxes.items, 0..) |tb, i| { 1855 if (tb.box.focus.id == focus_id) return i; 1856 } 1857 return null; 1858 } 1859 1860 // the first item of the row below (when `downward`) or above, or null at 1861 // the flow's edge. items are laid out in order, so y is nondecreasing. 1862 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1863 const rects = self.rects.items; 1864 if (index >= rects.len) return null; 1865 const cur_y = rects[index].y; 1866 if (downward) { 1867 for (rects[index + 1 ..], index + 1..) |r, i| { 1868 if (r.y > cur_y) return i; 1869 } 1870 return null; 1871 } 1872 // walk the earlier rows; the last one visited is the row above. 1873 var prev_first: ?usize = null; 1874 var i: usize = 0; 1875 while (i < rects.len and rects[i].y < cur_y) { 1876 prev_first = i; 1877 const y = rects[i].y; 1878 while (i < rects.len and rects[i].y == y) i += 1; 1879 } 1880 return prev_first; 1881 } 1882 }; 1883 1884 // an invisible widget that fills the horizontal space granted by its parent. 1885 // used inside Box(horiz) with a min_size so the box reserves space for the 1886 // children that follow, pushing them to the right. 1887 pub const Spacer = struct { 1888 focus: *Focus, 1889 grid: ?Grid, 1890 1891 pub fn init(allocator: std.mem.Allocator) !Spacer { 1892 return .{ 1893 .focus = try Focus.create(allocator, .container), 1894 .grid = null, 1895 }; 1896 } 1897 1898 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1899 self.focus.destroy(allocator); 1900 if (self.grid) |*grid| { 1901 grid.deinit(); 1902 self.grid = null; 1903 } 1904 } 1905 1906 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1907 _ = root_focus; 1908 self.clearGrid(); 1909 const width = constraint.max_size.width orelse return; 1910 if (width == 0) return; 1911 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1912 } 1913 1914 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1915 _ = self; 1916 _ = allocator; 1917 _ = key; 1918 _ = root_focus; 1919 } 1920 1921 pub fn clearGrid(self: *Spacer) void { 1922 if (self.grid) |*grid| { 1923 grid.deinit(); 1924 self.grid = null; 1925 } 1926 } 1927 1928 pub fn getGrid(self: Spacer) ?Grid { 1929 return self.grid; 1930 } 1931 1932 pub fn getFocus(self: *Spacer) *Focus { 1933 return self.focus; 1934 } 1935 }; 1936 1937 // a one-row, non-focusable status bar showing the current page's url 1938 pub const Footer = struct { 1939 focus: *Focus, 1940 grid: ?Grid, 1941 session: *Session, 1942 arena: std.heap.ArenaAllocator, 1943 1944 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1945 return .{ 1946 .focus = try Focus.create(allocator, .container), 1947 .grid = null, 1948 .session = session, 1949 .arena = std.heap.ArenaAllocator.init(allocator), 1950 }; 1951 } 1952 1953 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1954 self.focus.destroy(allocator); 1955 if (self.grid) |*grid| grid.deinit(); 1956 self.arena.deinit(); 1957 } 1958 1959 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1960 _ = root_focus; 1961 self.clearGrid(); 1962 const width = constraint.max_size.width orelse return; 1963 if (width == 0) return; 1964 1965 _ = self.arena.reset(.retain_capacity); 1966 const aa = self.arena.allocator(); 1967 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1968 const text = if (self.session.web_port) |port| 1969 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1970 else 1971 path; 1972 1973 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1974 errdefer grid.deinit(); 1975 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1976 var i: usize = 0; 1977 while (utf8.nextCodepointSlice()) |ch| { 1978 if (i == width) break; 1979 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1980 i += 1; 1981 } 1982 self.grid = grid; 1983 } 1984 1985 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1986 _ = self; 1987 _ = allocator; 1988 _ = key; 1989 _ = root_focus; 1990 } 1991 1992 pub fn clearGrid(self: *Footer) void { 1993 if (self.grid) |*grid| { 1994 grid.deinit(); 1995 self.grid = null; 1996 } 1997 } 1998 1999 pub fn getGrid(self: Footer) ?Grid { 2000 return self.grid; 2001 } 2002 2003 pub fn getFocus(self: *Footer) *Focus { 2004 return self.focus; 2005 } 2006 }; 2007 2008 // a single-child wrapper that builds the child at its natural size and 2009 // positions its grid in the middle of the area granted by the parent 2010 pub const Center = struct { 2011 focus: *Focus, 2012 grid: ?Grid, 2013 child: *Widget, 2014 2015 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2016 const child = try allocator.create(Widget); 2017 errdefer allocator.destroy(child); 2018 child.* = child_widget; 2019 return .{ 2020 .focus = try Focus.create(allocator, .container), 2021 .grid = null, 2022 .child = child, 2023 }; 2024 } 2025 2026 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2027 self.focus.destroy(allocator); 2028 if (self.grid) |*grid| { 2029 grid.deinit(); 2030 self.grid = null; 2031 } 2032 self.child.deinit(allocator); 2033 allocator.destroy(self.child); 2034 } 2035 2036 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2037 self.clearGrid(); 2038 self.getFocus().clear(); 2039 2040 // build the child without forcing it to fill min_size; it sizes 2041 // itself to its content within the available max. 2042 try self.child.build(allocator, .{ 2043 .min_size = .{ .width = null, .height = null }, 2044 .max_size = constraint.max_size, 2045 }, root_focus); 2046 2047 const child_grid = self.child.getGrid() orelse return; 2048 2049 // prefer max when given; otherwise grow to max(min, child) so that 2050 // when only a min is set (e.g. the wasm path passes viewport rows 2051 // as min_height with no max), we can still vertically center while 2052 // letting taller content extend past the min. 2053 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; 2054 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; 2055 if (width == 0 or height == 0) return; 2056 2057 const offset_x: usize = (width -| child_grid.size.width) / 2; 2058 const offset_y: usize = (height -| child_grid.size.height) / 2; 2059 2060 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2061 errdefer grid.deinit(); 2062 try grid.drawGrid(child_grid, offset_x, offset_y); 2063 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2064 self.getFocus().child_id = self.child.getFocus().id; 2065 2066 self.grid = grid; 2067 } 2068 2069 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2070 try self.child.input(allocator, key, root_focus); 2071 } 2072 2073 pub fn clearGrid(self: *Center) void { 2074 if (self.grid) |*grid| { 2075 grid.deinit(); 2076 self.grid = null; 2077 } 2078 self.child.clearGrid(); 2079 } 2080 2081 pub fn getGrid(self: Center) ?Grid { 2082 return self.grid; 2083 } 2084 2085 pub fn getFocus(self: *Center) *Focus { 2086 return self.focus; 2087 } 2088 }; 2089 2090 // renders truecolor ANSI art 2091 pub const AnsiArt = struct { 2092 focus: *Focus, 2093 grid: ?Grid, 2094 content: []const u8, 2095 2096 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2097 return .{ 2098 .focus = try Focus.create(allocator, .container), 2099 .grid = null, 2100 .content = content, 2101 }; 2102 } 2103 2104 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2105 self.focus.destroy(allocator); 2106 if (self.grid) |*grid| { 2107 grid.deinit(); 2108 self.grid = null; 2109 } 2110 } 2111 2112 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2113 _ = root_focus; 2114 self.clearGrid(); 2115 2116 // parse into rows of cells, then copy into a rectangular grid 2117 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2118 defer { 2119 for (rows.items) |*row| row.deinit(allocator); 2120 rows.deinit(allocator); 2121 } 2122 var row: std.ArrayList(Grid.Cell) = .empty; 2123 errdefer row.deinit(allocator); 2124 2125 var style: Grid.Style = .{}; 2126 var width: usize = 0; 2127 const content = self.content; 2128 var i: usize = 0; 2129 while (i < content.len) { 2130 const byte = content[i]; 2131 if (byte == '\n') { 2132 width = @max(width, row.items.len); 2133 try rows.append(allocator, row); 2134 row = .empty; 2135 i += 1; 2136 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2137 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2138 var j = i + 2; 2139 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2140 if (j >= content.len) { 2141 i = content.len; // malformed trailing escape; stop 2142 } else { 2143 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2144 i = j + 1; 2145 } 2146 } else { 2147 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2148 const end = @min(content.len, i + len); 2149 const rune = content[i..end]; 2150 const transparent = end - i == 1 and rune[0] == ' ' and 2151 style.fg == null and style.bg == null and !style.inverted; 2152 try row.append(allocator, .{ 2153 .rune = if (transparent) null else rune, 2154 .style = style, 2155 }); 2156 i = end; 2157 } 2158 } 2159 // a trailing row with content but no closing newline 2160 if (row.items.len > 0) { 2161 width = @max(width, row.items.len); 2162 try rows.append(allocator, row); 2163 } else { 2164 row.deinit(allocator); 2165 } 2166 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2167 2168 const height = rows.items.len; 2169 if (width == 0 or height == 0) return; 2170 2171 const clamped_w = @min(width, constraint.max_size.width orelse width); 2172 const clamped_h = @min(height, constraint.max_size.height orelse height); 2173 2174 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2175 errdefer grid.deinit(); 2176 for (rows.items[0..clamped_h], 0..) |r, y| { 2177 const n = @min(r.items.len, clamped_w); 2178 for (r.items[0..n], 0..) |cell, x| { 2179 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2180 } 2181 } 2182 self.grid = grid; 2183 } 2184 2185 pub fn clearGrid(self: *AnsiArt) void { 2186 if (self.grid) |*grid| { 2187 grid.deinit(); 2188 self.grid = null; 2189 } 2190 } 2191 2192 pub fn getGrid(self: AnsiArt) ?Grid { 2193 return self.grid; 2194 } 2195 2196 pub fn getFocus(self: *AnsiArt) *Focus { 2197 return self.focus; 2198 } 2199 2200 fn applySgr(style: *Grid.Style, params: []const u8) void { 2201 var nums: [16]u32 = undefined; 2202 var n: usize = 0; 2203 var it = std.mem.splitScalar(u8, params, ';'); 2204 while (it.next()) |tok| { 2205 if (n >= nums.len) break; 2206 // an empty parameter (e.g. bare "\x1b[m") means 0 2207 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2208 n += 1; 2209 } 2210 if (n == 0) { 2211 style.* = .{}; 2212 return; 2213 } 2214 var i: usize = 0; 2215 while (i < n) : (i += 1) { 2216 switch (nums[i]) { 2217 0 => style.* = .{}, 2218 7 => style.inverted = true, 2219 27 => style.inverted = false, 2220 39 => style.fg = null, 2221 49 => style.bg = null, 2222 38, 48 => { 2223 // truecolor form: 38;2;r;g;b — anything else is ignored 2224 if (i + 4 < n and nums[i + 1] == 2) { 2225 const c = Grid.Color{ 2226 .r = @truncate(nums[i + 2]), 2227 .g = @truncate(nums[i + 3]), 2228 .b = @truncate(nums[i + 4]), 2229 }; 2230 if (nums[i] == 38) style.fg = c else style.bg = c; 2231 i += 4; 2232 } 2233 }, 2234 else => {}, 2235 } 2236 } 2237 } 2238 }; 2239 2240 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2241 pub const AnsiBackground = struct { 2242 grid: ?Grid, 2243 child: *Widget, 2244 art: AnsiArt, 2245 session: *Session, 2246 2247 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2248 var cw = child_widget; 2249 const child = allocator.create(Widget) catch |e| { 2250 cw.deinit(allocator); 2251 return e; 2252 }; 2253 child.* = cw; 2254 errdefer { 2255 child.deinit(allocator); 2256 allocator.destroy(child); 2257 } 2258 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2259 } 2260 2261 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2262 if (self.grid) |*grid| grid.deinit(); 2263 self.art.deinit(allocator); 2264 self.child.deinit(allocator); 2265 allocator.destroy(self.child); 2266 } 2267 2268 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2269 self.clearGrid(); 2270 2271 // make the wrapped page fill the whole available area 2272 try self.child.build(allocator, .{ 2273 .min_size = .{ 2274 .width = constraint.max_size.width orelse constraint.min_size.width, 2275 .height = constraint.max_size.height orelse constraint.min_size.height, 2276 }, 2277 .max_size = constraint.max_size, 2278 }, root_focus); 2279 2280 if (self.session.data.enable_ansi) { 2281 if (self.child.getGrid()) |fg| { 2282 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2283 } 2284 } 2285 } 2286 2287 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2288 try self.child.input(allocator, key, root_focus); 2289 } 2290 2291 pub fn clearGrid(self: *AnsiBackground) void { 2292 if (self.grid) |*grid| { 2293 grid.deinit(); 2294 self.grid = null; 2295 } 2296 self.child.clearGrid(); 2297 self.art.clearGrid(); 2298 } 2299 2300 pub fn getGrid(self: AnsiBackground) ?Grid { 2301 return self.grid orelse self.child.getGrid(); 2302 } 2303 2304 pub fn getFocus(self: *AnsiBackground) *Focus { 2305 return self.child.getFocus(); 2306 } 2307 2308 // which top corner the art hugs. 2309 const ArtAnchor = enum { top_left, top_right }; 2310 2311 // a foreground cell counts as blank if it's empty or a bare space without styling 2312 fn cellIsBlank(cell: Grid.Cell) bool { 2313 if (cell.rune) |rune| { 2314 return std.mem.eql(u8, rune, " ") and 2315 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2316 } 2317 return true; 2318 } 2319 2320 // the art is dimmed to this fraction of its brightness so foreground text 2321 // stays legible over it 2322 const art_brightness = 45; // percent 2323 2324 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2325 const c = color orelse return null; 2326 return .{ 2327 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2328 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2329 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2330 }; 2331 } 2332 2333 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2334 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2335 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2336 // snap such channels clear of that range so the backdrop doesn't blink or 2337 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2338 // actually render truecolor. 2339 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2340 const c = color orelse return null; 2341 const snap = struct { 2342 fn f(v: u8) u8 { 2343 return if (v >= 1 and v <= 9) 10 else v; 2344 } 2345 }.f; 2346 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2347 } 2348 2349 // composites ANSI art behind a foreground grid 2350 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2351 art.clearGrid(); 2352 try art.build(allocator, .{ 2353 .min_size = .{ .width = null, .height = null }, 2354 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2355 }, root_focus); 2356 2357 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2358 errdefer out.deinit(); 2359 2360 if (art.getGrid()) |art_grid| { 2361 const anchor_x = switch (anchor) { 2362 .top_left => 0, 2363 .top_right => foreground.size.width -| art_grid.size.width, 2364 }; 2365 for (0..art_grid.size.height) |y| { 2366 for (0..art_grid.size.width) |x| { 2367 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2368 if (src.rune == null) continue; 2369 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2370 const dst = &out.cells.items[idx]; 2371 // composite the dimmed art behind every cell so it shows 2372 // through the whole UI: a blank cell becomes the art, while a 2373 // cell with a glyph keeps the glyph but takes the dimmed art as 2374 // its background (replacing whatever background it had, which 2375 // would otherwise obscure the art). 2376 if (cellIsBlank(dst.*)) { 2377 dst.* = src; 2378 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2379 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2380 } else { 2381 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2382 } 2383 } 2384 } 2385 } 2386 return out; 2387 } 2388 }; 2389