╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
765 return null; 766 } 767 if (std.mem.eql(u8, tab, "events")) 768 return if (segments.next() == null) repoEventsRoute(pair, null, "") else null; 769 if (std.mem.startsWith(u8, tab, "event:")) { 770 const event_id = tab["event:".len..]; 771 const kind_segment = segments.next() orelse return null; 772 if (!std.mem.startsWith(u8, kind_segment, "kind:")) return null; 773 const kind = std.meta.stringToEnum(evt.EventKind, kind_segment["kind:".len..]) orelse return null; 774 if (event_id.len == 0 or segments.next() != null) return null; 775 return repoEventsRoute(pair, kind, event_id); 776 } 777 if (std.mem.eql(u8, tab, files_seg)) { 778 params.scanPairs(&segments) catch return null; 779 if (!params.only(&.{ .line, .branch, .tag, .object })) return null; 780 const line = params.line() orelse return null; 781 const dir = pathValue(segments.rest()) orelse return null; 782 const ref = (params.ref() catch return null) orelse 783 return if (dir.len == 0) repoFilesRoute(pair, null, "", "", line) else null; 784 return repoFilesRoute(pair, ref.kind, ref.value, dir, line); 785 } 786 if (std.mem.eql(u8, tab, commits_seg)) { 787 params.scanPairs(&segments) catch return null; 788 if (!params.only(&.{ .start, .branch, .tag, .object })) return null; 789 const start = params.start() orelse return null; 790 const ref = (params.ref() catch return null) orelse { 791 const file = pathValue(segments.rest()) orelse return null; 792 return if (file.len == 0) repoCommitsRoute(pair, null, "", start, "") else null; 793 }; 794 // the message page names its commit and nothing else 795 if (std.mem.eql(u8, segments.rest(), message_seg)) 796 return if (start == 0) repoCommitMessageRoute(pair, ref.kind, ref.value) else null; 797 const file = pathValue(segments.rest()) orelse return null; 798 return repoCommitsRoute(pair, ref.kind, ref.value, start, file); 799 } 800 return null; // unknown sub-path 801 } 802 803 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 804 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 805 return switch (a) { 806 .home_users => |a_start| a_start == b.home_users, 807 .home_repos => |a_start| a_start == b.home_repos, 808 .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, 809 .user_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.user_settings.slice()), 810 .user_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.user_auth.slice()), 811 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 812 a_f.ref_kind == b.repo_files.ref_kind and 813 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 814 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 815 a_f.line == b.repo_files.line, 816 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 817 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 818 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 819 switch (a_c.content) { 820 .diff => |a_d| switch (b.repo_commits.content) { 821 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 822 .message => false, 823 }, 824 .message => std.meta.activeTag(b.repo_commits.content) == .message, 825 }, 826 .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()), 827 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 828 std.mem.eql(u8, a_i.tag.slice(), b.repo_issues.tag.slice()) and 829 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 830 std.mem.eql(u8, a_i.theirs.slice(), b.repo_issues.theirs.slice()) and 831 a_i.view == b.repo_issues.view and 832 a_i.comments_start == b.repo_issues.comments_start and 833 std.mem.eql(u8, a_i.comment.slice(), b.repo_issues.comment.slice()), 834 .repo_events => |a_e| std.mem.eql(u8, a_e.name.slice(), b.repo_events.name.slice()) and 835 a_e.kind == b.repo_events.kind and 836 std.mem.eql(u8, a_e.selected.slice(), b.repo_events.selected.slice()), 837 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 838 .repo_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_auth.slice()), 839 else => true, 840 }; 841 } 842 843 // true when following a link from `b` to `a` (both repo routes) should reload 844 // rather than stay in page 845 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 846 if (a.parent() != .repo or b.parent() != .repo) return false; 847 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 848 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 849 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 850 return !a.eql(b); 851 } 852 853 // true when `a` and `b` are the same user paginated to a different repos 854 // window. switching between a user's tabs is in-page (header-handled), so 855 // only a changed `start` on the repos list navigates. 856 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 857 return switch (a) { 858 .user_repos => |aa| switch (b) { 859 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 860 else => false, 861 }, 862 else => false, 863 }; 864 } 865 866 // true when `a` and `b` are the same home list tab paginated to a different 867 // window. switching between the users/repos tabs is in-page (the home page 868 // holds both lists), so only a changed `after` on the same tab navigates. 869 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 870 return switch (a) { 871 .home_users => |aa| switch (b) { 872 .home_users => |bb| aa != bb, 873 else => false, 874 }, 875 .home_repos => |aa| switch (b) { 876 .home_repos => |bb| aa != bb, 877 else => false, 878 }, 879 else => false, 880 }; 881 } 882 }; 883 884 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 885 fn isUnreserved(c: u8) bool { 886 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 887 } 888 889 // percent-encode a ref name for use as a single url path segment. 890 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 891 var out: std.Io.Writer.Allocating = .init(aa); 892 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 893 return out.written(); 894 } 895 896 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 897 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 898 return struct { 899 pub const hex_len = hash.hexLen(repo_opts.hash); 900 901 // the concrete ref/oid (a null request resolves to the default branch) 902 ref_or_oid: RoutablePage.RefOrOid, 903 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 904 // the route layer — which splits on '/' — stores them encoded). duped 905 // into `aa`. 906 value: []const u8, 907 // the commit oid it points at 908 oid: [hex_len]u8, 909 910 const Self = @This(); 911 912 // resolve a requested ref/oid (null = the repo's default branch) to a 913 // concrete ref/oid plus the commit oid it points at. `requested_value` is 914 // the url-encoded form (as it appears in the route). null when it doesn't 915 // resolve (an unknown branch/tag, or a malformed/unknown oid). 916 pub fn init( 917 repo: *rp.Repo(repo_kind, repo_opts), 918 io: std.Io, 919 aa: std.mem.Allocator, 920 requested_ref_or_oid: ?RoutablePage.RefOrOid, 921 requested_value: []const u8, 922 ) !?Self { 923 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 924 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 925 var value: []const u8 = if (requested_ref_or_oid == null) 926 requested_value 927 else 928 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 929 // no ref named: fall back to HEAD's branch (or its oid when detached). 930 if (requested_ref_or_oid == null) { 931 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 932 if (repo.head(io, &head_buf)) |head| switch (head) { 933 .ref => |r| { 934 ref_or_oid = .branch; 935 value = r.name; 936 }, 937 .oid => |o| { 938 ref_or_oid = .object; 939 value = o; 940 }, 941 } else |_| {} 942 } 943 944 var oid: [hex_len]u8 = undefined; 945 switch (ref_or_oid) { 946 .object => { 947 if (value.len != hex_len) return null; 948 @memcpy(&oid, value); 949 }, 950 .branch, .tag => { 951 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 952 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 953 }, 954 } 955 // store url-encoded so the route layer can hold the value verbatim. 956 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 957 } 958 }; 959 } 960 961 // where a repo page reads its on-disk repo from 962 pub const RepoSource = struct { 963 path: []const u8, 964 repo_kind: rp.RepoKind, 965 }; 966 967 // per-connection mutable state. each SSH session / web session / local TUI 968 // run gets its own. 969 pub const Session = struct { 970 data: Data = .{}, 971 // session-lifetime allocations: login/user_id, persisted prefs, queued 972 // actions. lives as long as the connection. 973 arena: *std.heap.ArenaAllocator, 974 // current page's allocations: page data and the page-scoped strings widgets 975 // build (form actions, links). owned by Nav, swapped on each navigation, so 976 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 977 // where a render is one-shot, this points at the same arena as `arena`. 978 page_arena: *std.heap.ArenaAllocator, 979 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 980 // filesystem io and the path to <server>/repos, for opening on-disk repos 981 // during page construction. both null on wasm, which has no filesystem and 982 // rebuilds pages from the serialized snapshot rather than from disk. 983 io: ?std.Io = null, 984 repos_dir: ?[]const u8 = null, 985 // the single on-disk repo this session views, when running in local mode 986 // (haxy invoked with no arguments inside a repo). null on the server paths, 987 // which resolve repos from the admin db instead. 988 local: ?RepoSource = null, 989 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 990 // focus id -> the live TextInput, refreshed each frame by the views that own 991 // inputs. web/wasm form handling looks widgets up here by focus id. 992 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput(Widget)) = .empty, 993 // focus id -> the page-constant text the web overlay renders into an 994 // input initially, refreshed each frame alongside `text_inputs`. never 995 // the input's live content: the overlay html must not change as the 996 // user types, or the diff in _setOverlay would rebuild the element. 997 input_values: std.AutoHashMapUnmanaged(usize, []const u8) = .empty, 998 nav_back: bool = false, // set by input (escape) to request the native TUI pop a page; see Nav 999 refresh_requested: bool = false, // set by input (ctrl+r) 1000 // a requested forward navigation. set this (via navigate) to move to a new 1001 // page; Nav.sync builds it and then copies it into current_page, clearing 1002 // this back to null. setting current_page directly only updates the url and 1003 // does not navigate. 1004 next_page: ?RoutablePage = null, 1005 is_terminal: bool = false, // true on remote SSH and local TUI 1006 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 1007 // url. null on the web itself (no footer there). 1008 web_port: ?u16 = null, 1009 // a host operation requested by a widget 1010 host_request: ?HostRequest = null, 1011 quit_requested: bool = false, 1012 1013 pub const HostRequest = union(enum) { 1014 show_copyable_text: []const u8, 1015 sync_events, 1016 }; 1017 1018 const Self = @This(); 1019 1020 // serializable data sent down to web client 1021 pub const Data = struct { 1022 user_id: ?[]const u8 = null, 1023 // the logged-in user's name, for the views that show it 1024 user_name: ?[]const u8 = null, 1025 // a transient outcome to surface from the last /login POST attempt 1026 login_failure: ?Home.Auth.Login.Failure = null, 1027 // a transient local event-sync error 1028 sync_failure: ?[]const u8 = null, 1029 current_page: RoutablePage = .default, 1030 // whether to render the ANSI art backdrop 1031 enable_ansi: bool = true, 1032 // the bound clone-service ports. absent in local mode, where clone urls 1033 // are not shown. 1034 clone_http_port: ?u16 = null, 1035 clone_ssh_port: ?u16 = null, 1036 // true when this session views a single local repo. unlike `local` 1037 // (the host-side filesystem source) this travels in the snapshot, so 1038 // the wasm side also parses elided link urls and hides the multi-user 1039 // chrome. 1040 is_local: bool = false, 1041 }; 1042 1043 // a user-initiated state change. widgets enqueue these on the session during 1044 // input instead of mutating state or touching the DB themselves; the host 1045 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 1046 // side effects out of the widget tree and gives every render path one place to 1047 // turn a UI action into a state change + event. 1048 pub const Action = union(enum) { 1049 toggle_ansi, 1050 }; 1051 1052 pub fn init( 1053 arena: *std.heap.ArenaAllocator, 1054 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1055 data: Data, 1056 ) !Self { 1057 var session = Self{ 1058 .data = data, 1059 .arena = arena, 1060 // until a host swaps in a page-scoped arena (Nav does this), page 1061 // allocations land in the session arena. on the web path that's the 1062 // intended behavior, since the whole arena is per-request. 1063 .page_arena = arena, 1064 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 1065 }; 1066 try session.loadUser(); 1067 return session; 1068 } 1069 1070 // load the logged-in user's name and persisted preferences from the db 1071 pub fn loadUser(self: *Self) !void { 1072 const user_id = self.data.user_id orelse return; 1073 const moment = self.haxy_moment orelse return; 1074 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.arena, user_id)) |user| { 1075 self.data.user_name = user.event.name; 1076 self.data.enable_ansi = user.event.enable_ansi; 1077 } 1078 } 1079 1080 // the author for an event this session creates, or null when it may not 1081 // create one. local mode has no accounts, so it authors anonymously. 1082 pub fn eventAuthorEmail(self: *Self) !?[]const u8 { 1083 if (self.data.is_local) return "user@haxy"; // TODO: try the repo's git config user.email first 1084 const user_id = self.data.user_id orelse return null; 1085 const moment = self.haxy_moment orelse return null; 1086 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.page_arena, user_id)) orelse return null; 1087 return user.event.email; 1088 } 1089 1090 // queue an action for the host to drain this frame. 1091 pub fn push(self: *Self, action: Action) !void { 1092 try self.pending.append(self.arena.allocator(), action); 1093 } 1094 1095 // apply a single action's in-memory effect only (no persistence). 1096 fn apply(self: *Self, action: Action) void { 1097 switch (action) { 1098 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 1099 } 1100 } 1101 1102 // drain the session's queued actions by applying them to the session. 1103 // used on the wasm path, which has no repo to persist to. 1104 pub fn applyPending(self: *Self) void { 1105 for (self.pending.items) |action| self.apply(action); 1106 self.pending.clearRetainingCapacity(); 1107 } 1108 1109 // drain the session's queued actions by applying them to the session and 1110 // writing them to the db 1111 pub fn applyAndWritePending( 1112 self: *Self, 1113 io: std.Io, 1114 allocator: std.mem.Allocator, 1115 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1116 ) !void { 1117 defer self.pending.clearRetainingCapacity(); 1118 for (self.pending.items) |action| { 1119 self.apply(action); 1120 switch (action) { 1121 .toggle_ansi => if (self.data.user_id) |user_id| { 1122 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 1123 }, 1124 } 1125 } 1126 } 1127 1128 // reload the moment from the admin repo and re-read user preferences 1129 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 1130 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 1131 self.haxy_moment = moment; 1132 1133 const user_id = self.data.user_id orelse return; 1134 var arena = std.heap.ArenaAllocator.init(allocator); 1135 defer arena.deinit(); 1136 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, user_id)) |user| { 1137 self.data.enable_ansi = user.event.enable_ansi; 1138 } 1139 } 1140 1141 // request a forward navigation to `route`; the host consumes next_page 1142 // (Nav.sync on the terminal, the wasm tick on the web). 1143 pub fn navigate(self: *Session, route: RoutablePage) !void { 1144 self.next_page = route; 1145 } 1146 }; 1147 1148 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 1149 var nav = try Nav.init(allocator, session); 1150 defer nav.deinit(allocator); 1151 1152 var terminal = try term.Terminal.init(io, allocator); 1153 var terminal_live = true; 1154 defer if (terminal_live) terminal.deinit(io); 1155 1156 // set term as active so it will be properly cooked 1157 // when a panic/segfault happens 1158 term.setActive(&terminal); 1159 defer term.setActive(null); 1160 1161 var last_size = layout.Size{ .width = 0, .height = 0 }; 1162 var last_grid = try Grid.init(allocator, last_size); 1163 defer last_grid.deinit(); 1164 1165 while (!terminal.shouldQuit()) { 1166 const grid_changed = try terminal.render(&nav.root, &last_grid, &last_size); 1167 1168 // process any inputs. 1169 // 1170 // if the grid didn't change, then first do a blocking 1171 // read, so the thread will sleep until further input. 1172 // after that, all remaining reads are non-blocking so 1173 // we can process the rest of the queued inputs. 1174 // 1175 // if the grid *did* change, then only do non-blocking 1176 // reads. we do not want to sleep the thread because 1177 // there may be an animation that requires more looping. 1178 var blocking = !grid_changed; 1179 while (try terminal.readKey(io, blocking)) |key| { 1180 blocking = false; 1181 try inputKey(allocator, &nav.root, key, session); 1182 } 1183 1184 if (session.host_request) |request| { 1185 session.host_request = null; 1186 switch (request) { 1187 .show_copyable_text => |copyable_text| { 1188 term.setActive(null); 1189 terminal.deinit(io); 1190 terminal_live = false; 1191 showCopyableText(io, copyable_text) catch |show_err| { 1192 terminal = try term.Terminal.init(io, allocator); 1193 terminal_live = true; 1194 term.setActive(&terminal); 1195 return show_err; 1196 }; 1197 terminal = try term.Terminal.init(io, allocator); 1198 terminal_live = true; 1199 term.setActive(&terminal); 1200 last_size = .{ .width = 0, .height = 0 }; 1201 }, 1202 .sync_events => { 1203 try nav.root.build(allocator, .{ 1204 .min_size = .{ .width = null, .height = null }, 1205 .max_size = .{ .width = last_size.width, .height = last_size.height }, 1206 }, nav.root.getFocus()); 1207 _ = try terminal.render(&nav.root, &last_grid, &last_size); 1208 try Repo.Events.performSync(allocator, session); 1209 }, 1210 } 1211 } 1212 1213 // persist queued actions when there's a repo; local mode has none, 1214 // so just apply in-memory 1215 if (repo_maybe) |repo| { 1216 try session.applyAndWritePending(io, allocator, repo); 1217 // reload so the next navigation builds its page from the current moment 1218 try session.reloadMoment(allocator, repo); 1219 } else { 1220 session.applyPending(); 1221 } 1222 1223 // reconcile navigation: forward to a new page, or back on escape. 1224 try nav.sync(allocator, session); 1225 1226 // the quit button (on the quit tab) asks the host to tear down. 1227 if (session.quit_requested) terminal.requestQuit(); 1228 1229 try nav.root.build(allocator, .{ 1230 .min_size = .{ .width = null, .height = null }, 1231 .max_size = .{ .width = last_size.width, .height = last_size.height }, 1232 }, nav.root.getFocus()); 1233 } 1234 } 1235 1236 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 1237 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 1238 null 1239 else 1240 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 1241 defer if (tty) |file| file.close(io); 1242 1243 const input = if (tty) |file| file else std.Io.File.stdin(); 1244 const output = if (tty) |file| file else std.Io.File.stdout(); 1245 1246 var write_buf: [1024]u8 = undefined; 1247 var writer = output.writer(io, &write_buf); 1248 try writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 1249 try writer.interface.flush(); 1250 1251 var read_buf: [1]u8 = undefined; 1252 var reader = input.reader(io, &read_buf); 1253 while (true) { 1254 const byte = try reader.interface.takeByte(); 1255 if (byte == '\r' or byte == '\n') return; 1256 } 1257 } 1258 1259 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 1260 switch (key) { 1261 // request a navigation pop; the host's Nav.sync goes back a page, or 1262 // quits when there's no history left. 1263 .escape => session.nav_back = true, 1264 .ctrl => |letter| switch (letter) { 1265 'r' => session.refresh_requested = true, 1266 else => try root.input(allocator, key, root.getFocus()), 1267 }, 1268 .enter => { 1269 const root_focus = root.getFocus(); 1270 if (root_focus.grandchild_id) |gid| { 1271 // follow a cross-page link 1272 if (crossPageLink(root_focus, gid, session.data)) |route| { 1273 return session.navigate(route); 1274 } 1275 } 1276 try root.input(allocator, key, root_focus); 1277 }, 1278 .mouse => |mouse| { 1279 if (mouse.action == .press and mouse.action.press == .left) { 1280 const root_focus = root.getFocus(); 1281 var clicked: ?usize = null; 1282 var iter = root_focus.children.iterator(); 1283 while (iter.next()) |entry| { 1284 const child = entry.value_ptr.*; 1285 if (!child.focus.focusable) continue; 1286 const r = child.rect; 1287 if (mouse.x >= r.x and mouse.y >= r.y and 1288 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 1289 { 1290 clicked = entry.key_ptr.*; 1291 break; 1292 } 1293 } 1294 if (clicked) |focus_id| { 1295 // follow a cross-page link 1296 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 1297 return session.navigate(route); 1298 } 1299 root_focus.setFocus(focus_id); 1300 } 1301 // forward the press into the widget tree so buttons (and any 1302 // future click-aware widgets) can react. widgets that don't 1303 // care about presses ignore it. 1304 try root.input(allocator, key, root.getFocus()); 1305 } else { 1306 try root.input(allocator, key, root.getFocus()); 1307 } 1308 }, 1309 else => try root.input(allocator, key, root.getFocus()), 1310 } 1311 } 1312 1313 // if the focus target at focus_id is an `a:` link that should navigate (a 1314 // different parent page, or a different files directory within the repo page), 1315 // return its route; otherwise null. lets a host turn a click / enter on such a 1316 // link into a navigation rather than a focus change. 1317 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 1318 const current = data.current_page; 1319 const child = root_focus.children.get(focus_id) orelse return null; 1320 const custom = switch (child.focus.kind) { 1321 .custom => |c| c, 1322 else => return null, 1323 }; 1324 const a_prefix = "a:"; 1325 if (!std.mem.startsWith(u8, custom, a_prefix)) return null; 1326 const path = custom[a_prefix.len..]; 1327 // local sessions build (and parse) links with the repo identity elided 1328 const route = (if (data.is_local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path)) orelse return null; 1329 // a link to a different parent page always navigates; within a page, a 1330 // files-directory / commits-page / list-window change navigates while tab 1331 // links stay in-page (header-handled). 1332 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 1333 return null; 1334 } 1335 1336 // a display author, resolved against the admin db at read time 1337 pub const Author = union(enum) { 1338 unknown, 1339 email: []const u8, // an email no user matches 1340 user_name: []const u8, 1341 1342 // the commit author line's email, resolved like initFromEmail 1343 pub fn init( 1344 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1345 arena: *std.heap.ArenaAllocator, 1346 author_line: []const u8, 1347 ) !Author { 1348 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 1349 } 1350 1351 // from a record's stored author email: it resolves to the user currently 1352 // holding it, else stays the email. the result is allocated in `arena`, 1353 // so the email may borrow transient memory. 1354 pub fn initFromEmail( 1355 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1356 arena: *std.heap.ArenaAllocator, 1357 author_email: ?[]const u8, 1358 ) !Author { 1359 const email = author_email orelse return .unknown; 1360 if (haxy_moment) |moment| { 1361 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 1362 return .{ .user_name = user.event.name }; 1363 } 1364 } 1365 return .{ .email = try arena.allocator().dupe(u8, email) }; 1366 } 1367 }; 1368 1369 // the "a:" link to user `name`'s page. 1370 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 1371 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 1372 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 1373 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1374 } 1375 1376 // a focusable " author " box showing `author`, linking to their user page 1377 // when it is a known user 1378 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox(Widget) { 1379 const text = switch (author) { 1380 .unknown => "", 1381 .email, .user_name => |t| t, 1382 }; 1383 var tb = try wgt.TextBox(Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 1384 errdefer tb.deinit(allocator); 1385 tb.getFocus().focusable = true; 1386 switch (author) { 1387 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 1388 else => {}, 1389 } 1390 return tb; 1391 } 1392 1393 // native-TUI navigation 1394 pub const Nav = struct { 1395 root: Widget, 1396 route: RoutablePage, 1397 // backs the current page (its Page data and the page-scoped strings its 1398 // widgets build). owned here, swapped on each navigation. session.page_arena 1399 // tracks whichever of these is current so widgets allocate into it. 1400 arena: *std.heap.ArenaAllocator, 1401 history: std.ArrayList(Entry), 1402 1403 // each retained page keeps its own arena, freed when the page leaves the 1404 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 1405 // long-lived session from accumulating every page it ever visited. 1406 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 1407 1408 // cap on retained back-history so the chain can't grow memory without bound 1409 const max_history: usize = 16; 1410 1411 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 1412 const arena = try allocator.create(std.heap.ArenaAllocator); 1413 arena.* = std.heap.ArenaAllocator.init(allocator); 1414 errdefer { 1415 arena.deinit(); 1416 allocator.destroy(arena); 1417 } 1418 1419 session.page_arena = arena; 1420 const route = session.data.current_page; 1421 1422 const page = try arena.allocator().create(Page); 1423 page.* = try Page.init(arena, session, route); 1424 return .{ 1425 .root = try initRoot(allocator, page, session), 1426 .route = route, 1427 .arena = arena, 1428 .history = .empty, 1429 }; 1430 } 1431 1432 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 1433 self.root.deinit(allocator); 1434 freeArena(allocator, self.arena); 1435 for (self.history.items) |*entry| { 1436 entry.root.deinit(allocator); 1437 freeArena(allocator, entry.arena); 1438 } 1439 self.history.deinit(allocator); 1440 } 1441 1442 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 1443 arena.deinit(); 1444 allocator.destroy(arena); 1445 } 1446 1447 // reconcile the displayed root with the session's navigation state. a 1448 // forward nav (current_page moved to a different parent page) pushes the 1449 // current root and builds the new page in a fresh arena; a back request 1450 // frees the current page and restores the previous one. when escape is 1451 // pressed with no history left we switch to the quit tab instead of quitting. 1452 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 1453 // refresh: rebuild the current page in place from a current moment 1454 if (session.refresh_requested) { 1455 session.refresh_requested = false; 1456 if (session.haxy_moment != null or session.local != null) { 1457 const arena = try allocator.create(std.heap.ArenaAllocator); 1458 arena.* = std.heap.ArenaAllocator.init(allocator); 1459 errdefer freeArena(allocator, arena); 1460 1461 session.page_arena = arena; 1462 1463 const route = session.data.current_page; 1464 const page = try arena.allocator().create(Page); 1465 page.* = try Page.init(arena, session, route); 1466 const new_root = try initRoot(allocator, page, session); 1467 1468 self.root.deinit(allocator); 1469 freeArena(allocator, self.arena); 1470 self.root = new_root; 1471 self.route = route; 1472 self.arena = arena; 1473 } 1474 return; 1475 } 1476 1477 if (session.nav_back) { 1478 session.nav_back = false; 1479 if (self.history.pop()) |entry| { 1480 self.root.deinit(allocator); 1481 freeArena(allocator, self.arena); 1482 self.root = entry.root; 1483 self.route = entry.route; 1484 self.arena = entry.arena; 1485 session.page_arena = entry.arena; 1486 session.data.current_page = entry.route; 1487 return; 1488 } 1489 // nothing to go back to; switch to the quit confirmation 1490 const root_focus = self.root.getFocus(); 1491 var iter = root_focus.children.iterator(); 1492 while (iter.next()) |entry| { 1493 switch (entry.value_ptr.focus.kind) { 1494 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1495 root_focus.setFocus(entry.key_ptr.*); 1496 // a Stack only builds its selected child, so the quit 1497 // button isn't in the focus tree until a build runs with 1498 // the quit tab selected. build once, then send a 1499 // synthetic arrow_down to drop focus from the tab onto 1500 // the button so it's ready to confirm. 1501 try self.root.build(allocator, .{ 1502 .min_size = .{ .width = null, .height = 40 }, 1503 .max_size = .{ .width = 80, .height = null }, 1504 }, root_focus); 1505 try self.root.input(allocator, .arrow_down, root_focus); 1506 break; 1507 }, 1508 else => {}, 1509 } 1510 } 1511 return; 1512 } 1513 1514 // forward navigation: navigate() set next_page to the page to move to (a 1515 // cross-page link or tab change crossing pages) 1516 if (session.next_page) |route| { 1517 session.next_page = null; 1518 if (session.haxy_moment == null and session.local == null) return; 1519 // the page we navigated to becomes the current page 1520 session.data.current_page = route; 1521 1522 const arena = try allocator.create(std.heap.ArenaAllocator); 1523 arena.* = std.heap.ArenaAllocator.init(allocator); 1524 errdefer freeArena(allocator, arena); 1525 1526 session.page_arena = arena; 1527 1528 const page = try arena.allocator().create(Page); 1529 page.* = try Page.init(arena, session, route); 1530 const new_root = try initRoot(allocator, page, session); 1531 1532 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 1533 // drop the oldest entry (freeing its widget tree and arena) once over cap 1534 if (self.history.items.len > max_history) { 1535 var oldest = self.history.orderedRemove(0); 1536 oldest.root.deinit(allocator); 1537 freeArena(allocator, oldest.arena); 1538 } 1539 self.root = new_root; 1540 self.route = route; 1541 self.arena = arena; 1542 } 1543 } 1544 }; 1545 1546 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 1547 const page_widget: Widget = switch (page.*) { 1548 .home => |*p| .{ .home = try .init(allocator, p, session) }, 1549 .user => |*p| .{ .user = try .init(allocator, p, session) }, 1550 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 1551 }; 1552 1553 const demon_art = @embedFile("embed/demon.ans"); 1554 1555 // on the TUI/SSH, the page sits above a one-row footer showing the url 1556 var root = if (session.is_terminal) blk: { 1557 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1558 errdefer box.deinit(allocator); 1559 const bg_id = bg_blk: { 1560 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 1561 errdefer background.deinit(allocator); 1562 const id = background.getFocus().id; 1563 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 1564 break :bg_blk id; 1565 }; 1566 { 1567 var footer = try Footer.init(allocator, session); 1568 errdefer footer.deinit(allocator); 1569 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 1570 } 1571 box.getFocus().child_id = bg_id; 1572 break :blk Widget{ .box = box }; 1573 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 1574 errdefer root.deinit(allocator); 1575 1576 // input-owning views build their TextInputs in init — so reset the 1577 // focus-id -> *TextInput map here 1578 session.text_inputs.clearRetainingCapacity(); 1579 session.input_values.clearRetainingCapacity(); 1580 1581 try root.build(allocator, .{ 1582 .min_size = .{ .width = null, .height = 40 }, 1583 .max_size = .{ .width = 100, .height = null }, 1584 }, root.getFocus()); 1585 1586 return root; 1587 } 1588 1589 pub const Widget = union(enum) { 1590 text: wgt.Text(Widget), 1591 box: wgt.Box(Widget), 1592 text_box: wgt.TextBox(Widget), 1593 text_input: wgt.TextInput(Widget), 1594 scroll: wgt.Scroll(Widget), 1595 stack: wgt.Stack(Widget), 1596 flow_box: FlowBox, 1597 flow_box_scroll: FlowBox.Scroll, 1598 tag_flow: TagFlow, 1599 spacer: Spacer, 1600 center: Center, 1601 section_label: SectionLabel, 1602 submit_button: SubmitButton, 1603 background: AnsiBackground, 1604 home: Home.View, 1605 user: User.View, 1606 repo: Repo.View, 1607 quit: Quit.View, 1608 unauthorized: Unauthorized.View, 1609 title: Title.View, 1610 sub_title: SubTitle.View, 1611 home_header: Home.Header.View, 1612 user_header: User.Header.View, 1613 repo_header: Repo.Header.View, 1614 repo_files_header: Repo.Files.Header.View, 1615 repo_commits_header: Repo.Commits.Header.View, 1616 repo_issues_header: Repo.Issues.Header, 1617 repo_files: Repo.Files.View, 1618 repo_commits: Repo.Commits.View, 1619 repo_refs: Repo.Refs.View, 1620 repo_issues: Repo.Issues.View, 1621 repo_events_header: Repo.Events.Header.View, 1622 repo_events: Repo.Events.View, 1623 repo_comment: Repo.Comment.Item, 1624 clone_url: CloneUrl.View, 1625 home_users: Home.Users.View, 1626 home_repos: Home.Repos.View, 1627 auth_tab: Home.Header.AuthTab.View, 1628 home_settings: Home.Settings.View, 1629 home_auth: Home.Auth.View, 1630 footer: Footer, 1631 1632 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 1633 switch (self.*) { 1634 inline else => |*case| case.deinit(allocator), 1635 } 1636 } 1637 1638 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 1639 switch (self.*) { 1640 inline else => |*case| try case.build(allocator, constraint, root_focus), 1641 } 1642 } 1643 1644 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 1645 switch (self.*) { 1646 inline else => |*case| try case.input(allocator, key, root_focus), 1647 } 1648 } 1649 1650 pub fn clearGrid(self: *Widget) void { 1651 switch (self.*) { 1652 inline else => |*case| case.clearGrid(), 1653 } 1654 } 1655 1656 pub fn getGrid(self: Widget) ?Grid { 1657 switch (self) { 1658 inline else => |*case| return case.getGrid(), 1659 } 1660 } 1661 1662 pub fn getFocus(self: *Widget) *Focus { 1663 switch (self.*) { 1664 inline else => |*case| return case.getFocus(), 1665 } 1666 } 1667 }; 1668 1669 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 1670 // `scroll` to keep it visible. 1671 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 1672 const keys = box.children.keys(); 1673 if (keys.len == 0) return; 1674 const cur_id = box.getFocus().child_id orelse return; 1675 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 1676 const last: isize = @intCast(keys.len - 1); 1677 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 1678 if (next == @as(usize, @intCast(cur))) return; 1679 root_focus.setFocus(keys[next]); 1680 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 1681 } 1682 1683 pub const FlowBox = struct { 1684 focus: *Focus, 1685 grid: ?Grid, 1686 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1687 // backs the per-item strings the text boxes borrow: each box's `content` 1688 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 1689 // wholesale on each setItems so there's no per-string ownership to track. 1690 arena: std.heap.ArenaAllocator, 1691 // column count from the last build — FlowBox.Scroll.input uses it so arrow 1692 // up/down can step by a row's worth of items. 1693 last_cols: usize, 1694 options: Options, 1695 1696 const border_rows: usize = 2; 1697 1698 pub const Options = struct { 1699 cell_width: usize = 40, 1700 cell_height: usize = 3, 1701 }; 1702 1703 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1704 return .{ 1705 .focus = try Focus.create(allocator, .container), 1706 .grid = null, 1707 .text_boxes = .empty, 1708 .arena = std.heap.ArenaAllocator.init(allocator), 1709 .last_cols = 1, 1710 .options = options, 1711 }; 1712 } 1713 1714 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1715 self.focus.destroy(allocator); 1716 if (self.grid) |*grid| { 1717 grid.deinit(); 1718 self.grid = null; 1719 } 1720 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1721 self.text_boxes.deinit(allocator); 1722 self.arena.deinit(); 1723 } 1724 1725 // one flow item: its display text plus an optional link. a non-empty `link` 1726 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1727 // the web renderer turns into an anchor. 1728 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1729 1730 // both the item text and the link are copied into the arena, so the caller's 1731 // slices needn't outlive this call. 1732 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1733 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1734 self.text_boxes.clearAndFree(allocator); 1735 1736 // the old text boxes (and their borrowed content/links) are gone now, so 1737 // the arena that backed those strings can be reclaimed. 1738 _ = self.arena.reset(.retain_capacity); 1739 const aa = self.arena.allocator(); 1740 1741 self.focus.clear(); 1742 self.focus.child_id = null; 1743 1744 for (items) |item| { 1745 const line = try aa.dupe(u8, item.text); 1746 1747 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1748 errdefer text_box.deinit(allocator); 1749 text_box.getFocus().focusable = true; 1750 1751 if (item.link.len > 0) { 1752 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1753 } 1754 1755 try self.text_boxes.append(allocator, text_box); 1756 } 1757 1758 if (self.text_boxes.items.len > 0) { 1759 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1760 } 1761 } 1762 1763 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1764 self.clearGrid(); 1765 self.focus.clear(); 1766 1767 const cell_width = self.options.cell_width; 1768 const max_width = constraint.max_size.width orelse cell_width; 1769 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1770 self.last_cols = cols; 1771 1772 const count = self.text_boxes.items.len; 1773 if (count == 0) return; 1774 const slot_height = self.options.cell_height + border_rows; 1775 if (slot_height == 0) return; 1776 1777 // build at the slot size so every text box fits a single grid cell 1778 for (self.text_boxes.items) |*tb| { 1779 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1780 try tb.build(allocator, .{ 1781 .min_size = .{ .width = cell_width, .height = null }, 1782 .max_size = .{ .width = cell_width, .height = slot_height }, 1783 }, root_focus); 1784 } 1785 1786 const rows = (count + cols - 1) / cols; 1787 const content_height = rows * slot_height; 1788 const total_width = cols * cell_width; 1789 if (total_width == 0 or content_height == 0) return; 1790 1791 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1792 errdefer grid.deinit(); 1793 1794 for (self.text_boxes.items, 0..) |*tb, i| { 1795 const tb_grid = tb.getGrid() orelse continue; 1796 const col = i % cols; 1797 const row = i / cols; 1798 const cell_x = col * cell_width; 1799 const cell_y = row * slot_height; 1800 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1801 try grid.drawGrid(tb_grid, cell_x, cell_y); 1802 } 1803 1804 self.grid = grid; 1805 } 1806 1807 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1808 _ = self; 1809 _ = allocator; 1810 _ = key; 1811 _ = root_focus; 1812 } 1813 1814 pub fn clearGrid(self: *FlowBox) void { 1815 if (self.grid) |*grid| { 1816 grid.deinit(); 1817 self.grid = null; 1818 } 1819 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1820 } 1821 1822 pub fn getGrid(self: FlowBox) ?Grid { 1823 return self.grid; 1824 } 1825 1826 pub fn getFocus(self: *FlowBox) *Focus { 1827 return self.focus; 1828 } 1829 1830 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1831 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1832 const slot_height = self.options.cell_height + border_rows; 1833 const col = index % self.last_cols; 1834 const row = index / self.last_cols; 1835 return .{ 1836 .x = @intCast(col * self.options.cell_width), 1837 .y = @intCast(row * slot_height), 1838 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1839 }; 1840 } 1841 1842 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1843 for (self.text_boxes.items, 0..) |tb, i| { 1844 if (tb.box.focus.id == focus_id) return i; 1845 } 1846 return null; 1847 } 1848 1849 pub const Scroll = struct { 1850 scroll: wgt.Scroll(Widget), 1851 1852 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1853 var layout_inner = try FlowBox.init(allocator, options); 1854 errdefer layout_inner.deinit(allocator); 1855 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1856 errdefer scroll.deinit(allocator); 1857 return .{ .scroll = scroll }; 1858 } 1859 1860 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1861 self.scroll.deinit(allocator); 1862 } 1863 1864 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1865 self.scroll.x = 0; 1866 self.scroll.y = 0; 1867 try self.inner().setItems(allocator, items); 1868 } 1869 1870 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1871 try self.scroll.build(allocator, constraint, root_focus); 1872 } 1873 1874 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1875 _ = allocator; 1876 const in = self.inner(); 1877 const child_id = in.focus.child_id orelse return; 1878 const current_index = in.indexOfFocusId(child_id) orelse return; 1879 const count = in.text_boxes.items.len; 1880 if (count == 0) return; 1881 const cols = in.last_cols; 1882 const slot_height = in.options.cell_height + FlowBox.border_rows; 1883 1884 var index = current_index; 1885 switch (key) { 1886 .arrow_up => index -|= cols, 1887 .arrow_down => if (index + cols < count) { 1888 index += cols; 1889 }, 1890 .arrow_left => index -|= 1, 1891 .arrow_right => if (index + 1 < count) { 1892 index += 1; 1893 }, 1894 .home => index = 0, 1895 .end => index = count - 1, 1896 .page_up => { 1897 if (self.scroll.grid) |grid| if (slot_height > 0) { 1898 const rows_per_page = grid.size.height / slot_height; 1899 index -|= rows_per_page * cols; 1900 }; 1901 }, 1902 .page_down => { 1903 if (self.scroll.grid) |grid| if (slot_height > 0) { 1904 const rows_per_page = grid.size.height / slot_height; 1905 index = @min(index + rows_per_page * cols, count - 1); 1906 }; 1907 }, 1908 // scroll wheel moves the focused cell by a full row so the 1909 // viewport (via scrollToRect) follows in row-sized steps, 1910 // matching how a scroll wheel feels in a grid view. 1911 .mouse => |mouse| switch (mouse.action) { 1912 .scroll => |dir| switch (dir) { 1913 .up => index -|= cols, 1914 .down => if (index + cols < count) { 1915 index += cols; 1916 }, 1917 }, 1918 else => {}, 1919 }, 1920 else => {}, 1921 } 1922 1923 if (index != current_index) { 1924 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1925 if (in.cellRect(index)) |rect| { 1926 self.scroll.scrollToRect(rect); 1927 } 1928 } 1929 } 1930 1931 pub fn clearGrid(self: *Scroll) void { 1932 self.scroll.clearGrid(); 1933 } 1934 1935 pub fn getGrid(self: Scroll) ?Grid { 1936 return self.scroll.getGrid(); 1937 } 1938 1939 pub fn getFocus(self: *Scroll) *Focus { 1940 return self.scroll.getFocus(); 1941 } 1942 1943 pub fn getSelectedIndex(self: Scroll) ?usize { 1944 const in = self.scroll.child.flow_box; 1945 const child_id = in.focus.child_id orelse return null; 1946 return in.indexOfFocusId(child_id); 1947 } 1948 1949 fn inner(self: *Scroll) *FlowBox { 1950 return &self.scroll.child.flow_box; 1951 } 1952 }; 1953 }; 1954 1955 // a left-to-right flow of variable-width focusable items that wraps at the 1956 // available width, like words in a paragraph. unlike FlowBox there is no cell 1957 // grid: each item is as wide as its text. selection movement is driven by the 1958 // owning view via indexOfFocusId/verticalNeighbor. 1959 pub const TagFlow = struct { 1960 focus: *Focus, 1961 grid: ?Grid, 1962 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1963 // last build's item rects (content space), for vertical navigation and 1964 // scroll-into-view. 1965 rects: std.ArrayList(layout.IRect), 1966 // backs the per-item strings the text boxes borrow. 1967 arena: std.heap.ArenaAllocator, 1968 1969 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1970 1971 pub fn init(allocator: std.mem.Allocator) !TagFlow { 1972 return .{ 1973 .focus = try Focus.create(allocator, .container), 1974 .grid = null, 1975 .text_boxes = .empty, 1976 .rects = .empty, 1977 .arena = std.heap.ArenaAllocator.init(allocator), 1978 }; 1979 } 1980 1981 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 1982 self.focus.destroy(allocator); 1983 if (self.grid) |*grid| { 1984 grid.deinit(); 1985 self.grid = null; 1986 } 1987 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1988 self.text_boxes.deinit(allocator); 1989 self.rects.deinit(allocator); 1990 self.arena.deinit(); 1991 } 1992 1993 // both the item text and the link are copied into the arena, so the caller's 1994 // slices needn't outlive this call. 1995 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 1996 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1997 self.text_boxes.clearAndFree(allocator); 1998 1999 _ = self.arena.reset(.retain_capacity); 2000 const aa = self.arena.allocator(); 2001 2002 self.focus.clear(); 2003 self.focus.child_id = null; 2004 2005 for (items) |item| { 2006 const line = try aa.dupe(u8, item.text); 2007 2008 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2009 errdefer text_box.deinit(allocator); 2010 text_box.getFocus().focusable = true; 2011 2012 if (item.link.len > 0) { 2013 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 2014 } 2015 2016 try self.text_boxes.append(allocator, text_box); 2017 } 2018 2019 if (self.text_boxes.items.len > 0) { 2020 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 2021 } 2022 } 2023 2024 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2025 self.clearGrid(); 2026 self.focus.clear(); 2027 2028 if (self.text_boxes.items.len == 0) return; 2029 const max_width = constraint.max_size.width orelse 80; 2030 if (max_width == 0) return; 2031 2032 // lay the items out left to right, wrapping to a new row when the next 2033 // one wouldn't fit. 2034 self.rects.clearRetainingCapacity(); 2035 var x: usize = 0; 2036 var y: usize = 0; 2037 var row_height: usize = 0; 2038 var total_width: usize = 0; 2039 for (self.text_boxes.items) |*tb| { 2040 try tb.build(allocator, .{ 2041 .min_size = .{ .width = null, .height = null }, 2042 .max_size = .{ .width = max_width, .height = null }, 2043 }, root_focus); 2044 const tb_grid_maybe = tb.getGrid(); 2045 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 2046 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 2047 if (x > 0 and x + w > max_width) { 2048 x = 0; 2049 y += row_height; 2050 row_height = 0; 2051 } 2052 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 2053 if (h > row_height) row_height = h; 2054 x += w; 2055 if (x > total_width) total_width = x; 2056 } 2057 2058 const total_height = y + row_height; 2059 if (total_width == 0 or total_height == 0) return; 2060 2061 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 2062 errdefer grid.deinit(); 2063 2064 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 2065 const tb_grid = tb.getGrid() orelse continue; 2066 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 2067 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 2068 } 2069 2070 self.grid = grid; 2071 } 2072 2073 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2074 _ = self; 2075 _ = allocator; 2076 _ = key; 2077 _ = root_focus; 2078 } 2079 2080 pub fn clearGrid(self: *TagFlow) void { 2081 if (self.grid) |*grid| { 2082 grid.deinit(); 2083 self.grid = null; 2084 } 2085 for (self.text_boxes.items) |*tb| tb.clearGrid(); 2086 } 2087 2088 pub fn getGrid(self: TagFlow) ?Grid { 2089 return self.grid; 2090 } 2091 2092 pub fn getFocus(self: *TagFlow) *Focus { 2093 return self.focus; 2094 } 2095 2096 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 2097 for (self.text_boxes.items, 0..) |tb, i| { 2098 if (tb.box.focus.id == focus_id) return i; 2099 } 2100 return null; 2101 } 2102 2103 // the first item of the row below (when `downward`) or above, or null at 2104 // the flow's edge. items are laid out in order, so y is nondecreasing. 2105 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 2106 const rects = self.rects.items; 2107 if (index >= rects.len) return null; 2108 const cur_y = rects[index].y; 2109 if (downward) { 2110 for (rects[index + 1 ..], index + 1..) |r, i| { 2111 if (r.y > cur_y) return i; 2112 } 2113 return null; 2114 } 2115 // walk the earlier rows; the last one visited is the row above. 2116 var prev_first: ?usize = null; 2117 var i: usize = 0; 2118 while (i < rects.len and rects[i].y < cur_y) { 2119 prev_first = i; 2120 const y = rects[i].y; 2121 while (i < rects.len and rects[i].y == y) i += 1; 2122 } 2123 return prev_first; 2124 } 2125 }; 2126 2127 // an invisible widget that fills the horizontal space granted by its parent. 2128 // used inside Box(horiz) with a min_size so the box reserves space for the 2129 // children that follow, pushing them to the right. 2130 pub const Spacer = struct { 2131 focus: *Focus, 2132 grid: ?Grid, 2133 2134 pub fn init(allocator: std.mem.Allocator) !Spacer { 2135 return .{ 2136 .focus = try Focus.create(allocator, .container), 2137 .grid = null, 2138 }; 2139 } 2140 2141 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 2142 self.focus.destroy(allocator); 2143 if (self.grid) |*grid| { 2144 grid.deinit(); 2145 self.grid = null; 2146 } 2147 } 2148 2149 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2150 _ = root_focus; 2151 self.clearGrid(); 2152 const width = constraint.max_size.width orelse return; 2153 if (width == 0) return; 2154 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2155 } 2156 2157 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2158 _ = self; 2159 _ = allocator; 2160 _ = key; 2161 _ = root_focus; 2162 } 2163 2164 pub fn clearGrid(self: *Spacer) void { 2165 if (self.grid) |*grid| { 2166 grid.deinit(); 2167 self.grid = null; 2168 } 2169 } 2170 2171 pub fn getGrid(self: Spacer) ?Grid { 2172 return self.grid; 2173 } 2174 2175 pub fn getFocus(self: *Spacer) *Focus { 2176 return self.focus; 2177 } 2178 }; 2179 2180 // a one-row, non-focusable status bar showing the current page's url 2181 pub const Footer = struct { 2182 focus: *Focus, 2183 grid: ?Grid, 2184 session: *Session, 2185 arena: std.heap.ArenaAllocator, 2186 url: []const u8 = "", 2187 url_width: usize = 0, 2188 2189 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2190 return .{ 2191 .focus = try Focus.create(allocator, .container), 2192 .grid = null, 2193 .session = session, 2194 .arena = std.heap.ArenaAllocator.init(allocator), 2195 }; 2196 } 2197 2198 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2199 self.focus.destroy(allocator); 2200 if (self.grid) |*grid| grid.deinit(); 2201 self.arena.deinit(); 2202 } 2203 2204 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2205 _ = root_focus; 2206 self.clearGrid(); 2207 const width = constraint.max_size.width orelse return; 2208 if (width == 0) return; 2209 2210 _ = self.arena.reset(.retain_capacity); 2211 self.url = ""; 2212 self.url_width = 0; 2213 const aa = self.arena.allocator(); 2214 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2215 const text = if (self.session.web_port) |port| 2216 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 2217 else 2218 path; 2219 self.url = text; 2220 2221 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2222 errdefer grid.deinit(); 2223 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2224 var i: usize = 0; 2225 while (utf8.nextCodepointSlice()) |ch| { 2226 if (i == width) break; 2227 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2228 i += 1; 2229 } 2230 self.url_width = i; 2231 self.grid = grid; 2232 } 2233 2234 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2235 _ = allocator; 2236 switch (key) { 2237 .mouse => |mouse| { 2238 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 2239 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 2240 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 2241 }, 2242 else => {}, 2243 } 2244 } 2245 2246 pub fn clearGrid(self: *Footer) void { 2247 if (self.grid) |*grid| { 2248 grid.deinit(); 2249 self.grid = null; 2250 } 2251 } 2252 2253 pub fn getGrid(self: Footer) ?Grid { 2254 return self.grid; 2255 } 2256 2257 pub fn getFocus(self: *Footer) *Focus { 2258 return self.focus; 2259 } 2260 }; 2261 2262 // a single-child wrapper that builds the child at its natural size and 2263 // positions its grid in the middle of the area granted by the parent 2264 // a bordered label, indented past the content it precedes 2265 pub const SectionLabel = struct { 2266 box: wgt.Box(Widget), 2267 2268 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2269 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2270 errdefer box.deinit(allocator); 2271 2272 { 2273 var gap = try wgt.Text(Widget).init(allocator, " "); 2274 errdefer gap.deinit(allocator); 2275 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2276 } 2277 2278 { 2279 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2280 errdefer tb.deinit(allocator); 2281 tb.getFocus().focusable = true; 2282 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2283 } 2284 2285 box.getFocus().child_id = box.children.keys()[1]; 2286 return .{ .box = box }; 2287 } 2288 2289 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2290 self.box.deinit(allocator); 2291 } 2292 2293 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2294 try self.box.build(allocator, constraint, root_focus); 2295 } 2296 2297 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2298 _ = self; 2299 _ = allocator; 2300 _ = key; 2301 _ = root_focus; 2302 } 2303 2304 pub fn clearGrid(self: *SectionLabel) void { 2305 self.box.clearGrid(); 2306 } 2307 2308 pub fn getGrid(self: SectionLabel) ?Grid { 2309 return self.box.getGrid(); 2310 } 2311 2312 pub fn getFocus(self: *SectionLabel) *Focus { 2313 return self.box.getFocus(); 2314 } 2315 }; 2316 2317 // a submit button, indented past its form's content. the web overlay POSTs 2318 // the button's form; terminal hosts detect presses through `buttonId`. 2319 pub const SubmitButton = struct { 2320 box: wgt.Box(Widget), 2321 2322 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2323 return initLabeled(allocator, "submit"); 2324 } 2325 2326 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2327 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2328 errdefer box.deinit(allocator); 2329 2330 { 2331 var gap = try wgt.Text(Widget).init(allocator, " "); 2332 errdefer gap.deinit(allocator); 2333 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2334 } 2335 2336 { 2337 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2338 errdefer button.deinit(allocator); 2339 button.getFocus().focusable = true; 2340 // the renderer distinguishes plain clickables from buttons that 2341 // should POST to a server route by this kind. 2342 button.getFocus().kind = .{ .custom = "submit" }; 2343 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2344 } 2345 2346 box.getFocus().child_id = box.children.keys()[1]; 2347 return .{ .box = box }; 2348 } 2349 2350 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2351 self.box.deinit(allocator); 2352 } 2353 2354 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2355 try self.box.build(allocator, constraint, root_focus); 2356 } 2357 2358 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2359 _ = self; 2360 _ = allocator; 2361 _ = key; 2362 _ = root_focus; 2363 } 2364 2365 pub fn clearGrid(self: *SubmitButton) void { 2366 self.box.clearGrid(); 2367 } 2368 2369 pub fn getGrid(self: SubmitButton) ?Grid { 2370 return self.box.getGrid(); 2371 } 2372 2373 pub fn getFocus(self: *SubmitButton) *Focus { 2374 return self.box.getFocus(); 2375 } 2376 2377 // the button's focus id, for click hit-testing 2378 pub fn buttonId(self: *const SubmitButton) usize { 2379 return self.box.children.keys()[1]; 2380 } 2381 }; 2382 2383 pub const Center = struct { 2384 focus: *Focus, 2385 grid: ?Grid, 2386 child: *Widget, 2387 2388 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2389 const child = try allocator.create(Widget); 2390 errdefer allocator.destroy(child); 2391 child.* = child_widget; 2392 return .{ 2393 .focus = try Focus.create(allocator, .container), 2394 .grid = null, 2395 .child = child, 2396 }; 2397 } 2398 2399 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2400 self.focus.destroy(allocator); 2401 if (self.grid) |*grid| { 2402 grid.deinit(); 2403 self.grid = null; 2404 } 2405 self.child.deinit(allocator); 2406 allocator.destroy(self.child); 2407 } 2408 2409 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2410 self.clearGrid(); 2411 self.getFocus().clear(); 2412 2413 // build the child without forcing it to fill min_size; it sizes 2414 // itself to its content within the available max. 2415 try self.child.build(allocator, .{ 2416 .min_size = .{ .width = null, .height = null }, 2417 .max_size = constraint.max_size, 2418 }, root_focus); 2419 2420 const child_grid = self.child.getGrid() orelse return; 2421 2422 // prefer max when given; otherwise grow to max(min, child) so that 2423 // when only a min is set (e.g. the wasm path passes viewport rows 2424 // as min_height with no max), we can still vertically center while 2425 // letting taller content extend past the min. 2426 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; 2427 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; 2428 if (width == 0 or height == 0) return; 2429 2430 const offset_x: usize = (width -| child_grid.size.width) / 2; 2431 const offset_y: usize = (height -| child_grid.size.height) / 2; 2432 2433 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2434 errdefer grid.deinit(); 2435 try grid.drawGrid(child_grid, offset_x, offset_y); 2436 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2437 self.getFocus().child_id = self.child.getFocus().id; 2438 2439 self.grid = grid; 2440 } 2441 2442 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2443 try self.child.input(allocator, key, root_focus); 2444 } 2445 2446 pub fn clearGrid(self: *Center) void { 2447 if (self.grid) |*grid| { 2448 grid.deinit(); 2449 self.grid = null; 2450 } 2451 self.child.clearGrid(); 2452 } 2453 2454 pub fn getGrid(self: Center) ?Grid { 2455 return self.grid; 2456 } 2457 2458 pub fn getFocus(self: *Center) *Focus { 2459 return self.focus; 2460 } 2461 }; 2462 2463 // renders truecolor ANSI art 2464 pub const AnsiArt = struct { 2465 focus: *Focus, 2466 grid: ?Grid, 2467 content: []const u8, 2468 2469 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2470 return .{ 2471 .focus = try Focus.create(allocator, .container), 2472 .grid = null, 2473 .content = content, 2474 }; 2475 } 2476 2477 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2478 self.focus.destroy(allocator); 2479 if (self.grid) |*grid| { 2480 grid.deinit(); 2481 self.grid = null; 2482 } 2483 } 2484 2485 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2486 _ = root_focus; 2487 self.clearGrid(); 2488 2489 // parse into rows of cells, then copy into a rectangular grid 2490 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2491 defer { 2492 for (rows.items) |*row| row.deinit(allocator); 2493 rows.deinit(allocator); 2494 } 2495 var row: std.ArrayList(Grid.Cell) = .empty; 2496 errdefer row.deinit(allocator); 2497 2498 var style: Grid.Style = .{}; 2499 var width: usize = 0; 2500 const content = self.content; 2501 var i: usize = 0; 2502 while (i < content.len) { 2503 const byte = content[i]; 2504 if (byte == '\n') { 2505 width = @max(width, row.items.len); 2506 try rows.append(allocator, row); 2507 row = .empty; 2508 i += 1; 2509 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2510 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2511 var j = i + 2; 2512 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2513 if (j >= content.len) { 2514 i = content.len; // malformed trailing escape; stop 2515 } else { 2516 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2517 i = j + 1; 2518 } 2519 } else { 2520 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2521 const end = @min(content.len, i + len); 2522 const rune = content[i..end]; 2523 const transparent = end - i == 1 and rune[0] == ' ' and 2524 style.fg == null and style.bg == null and !style.inverted; 2525 try row.append(allocator, .{ 2526 .rune = if (transparent) null else rune, 2527 .style = style, 2528 }); 2529 i = end; 2530 } 2531 } 2532 // a trailing row with content but no closing newline 2533 if (row.items.len > 0) { 2534 width = @max(width, row.items.len); 2535 try rows.append(allocator, row); 2536 } else { 2537 row.deinit(allocator); 2538 } 2539 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2540 2541 const height = rows.items.len; 2542 if (width == 0 or height == 0) return; 2543 2544 const clamped_w = @min(width, constraint.max_size.width orelse width); 2545 const clamped_h = @min(height, constraint.max_size.height orelse height); 2546 2547 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2548 errdefer grid.deinit(); 2549 for (rows.items[0..clamped_h], 0..) |r, y| { 2550 const n = @min(r.items.len, clamped_w); 2551 for (r.items[0..n], 0..) |cell, x| { 2552 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2553 } 2554 } 2555 self.grid = grid; 2556 } 2557 2558 pub fn clearGrid(self: *AnsiArt) void { 2559 if (self.grid) |*grid| { 2560 grid.deinit(); 2561 self.grid = null; 2562 } 2563 } 2564 2565 pub fn getGrid(self: AnsiArt) ?Grid { 2566 return self.grid; 2567 } 2568 2569 pub fn getFocus(self: *AnsiArt) *Focus { 2570 return self.focus; 2571 } 2572 2573 fn applySgr(style: *Grid.Style, params: []const u8) void { 2574 var nums: [16]u32 = undefined; 2575 var n: usize = 0; 2576 var it = std.mem.splitScalar(u8, params, ';'); 2577 while (it.next()) |tok| { 2578 if (n >= nums.len) break; 2579 // an empty parameter (e.g. bare "\x1b[m") means 0 2580 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2581 n += 1; 2582 } 2583 if (n == 0) { 2584 style.* = .{}; 2585 return; 2586 } 2587 var i: usize = 0; 2588 while (i < n) : (i += 1) { 2589 switch (nums[i]) { 2590 0 => style.* = .{}, 2591 7 => style.inverted = true, 2592 27 => style.inverted = false, 2593 39 => style.fg = null, 2594 49 => style.bg = null, 2595 38, 48 => { 2596 // truecolor form: 38;2;r;g;b — anything else is ignored 2597 if (i + 4 < n and nums[i + 1] == 2) { 2598 const c = Grid.Color{ 2599 .r = @truncate(nums[i + 2]), 2600 .g = @truncate(nums[i + 3]), 2601 .b = @truncate(nums[i + 4]), 2602 }; 2603 if (nums[i] == 38) style.fg = c else style.bg = c; 2604 i += 4; 2605 } 2606 }, 2607 else => {}, 2608 } 2609 } 2610 } 2611 }; 2612 2613 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2614 pub const AnsiBackground = struct { 2615 grid: ?Grid, 2616 child: *Widget, 2617 art: AnsiArt, 2618 session: *Session, 2619 2620 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2621 var cw = child_widget; 2622 const child = allocator.create(Widget) catch |e| { 2623 cw.deinit(allocator); 2624 return e; 2625 }; 2626 child.* = cw; 2627 errdefer { 2628 child.deinit(allocator); 2629 allocator.destroy(child); 2630 } 2631 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2632 } 2633 2634 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2635 if (self.grid) |*grid| grid.deinit(); 2636 self.art.deinit(allocator); 2637 self.child.deinit(allocator); 2638 allocator.destroy(self.child); 2639 } 2640 2641 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2642 self.clearGrid(); 2643 2644 // make the wrapped page fill the whole available area 2645 try self.child.build(allocator, .{ 2646 .min_size = .{ 2647 .width = constraint.max_size.width orelse constraint.min_size.width, 2648 .height = constraint.max_size.height orelse constraint.min_size.height, 2649 }, 2650 .max_size = constraint.max_size, 2651 }, root_focus); 2652 2653 if (self.session.data.enable_ansi) { 2654 if (self.child.getGrid()) |fg| { 2655 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2656 } 2657 } 2658 } 2659 2660 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2661 try self.child.input(allocator, key, root_focus); 2662 } 2663 2664 pub fn clearGrid(self: *AnsiBackground) void { 2665 if (self.grid) |*grid| { 2666 grid.deinit(); 2667 self.grid = null; 2668 } 2669 self.child.clearGrid(); 2670 self.art.clearGrid(); 2671 } 2672 2673 pub fn getGrid(self: AnsiBackground) ?Grid { 2674 return self.grid orelse self.child.getGrid(); 2675 } 2676 2677 pub fn getFocus(self: *AnsiBackground) *Focus { 2678 return self.child.getFocus(); 2679 } 2680 2681 // which top corner the art hugs. 2682 const ArtAnchor = enum { top_left, top_right }; 2683 2684 // a foreground cell counts as blank if it's empty or a bare space without styling 2685 fn cellIsBlank(cell: Grid.Cell) bool { 2686 if (cell.rune) |rune| { 2687 return std.mem.eql(u8, rune, " ") and 2688 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2689 } 2690 return true; 2691 } 2692 2693 // the art is dimmed to this fraction of its brightness so foreground text 2694 // stays legible over it 2695 const art_brightness = 45; // percent 2696 2697 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2698 const c = color orelse return null; 2699 return .{ 2700 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2701 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2702 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2703 }; 2704 } 2705 2706 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2707 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2708 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2709 // snap such channels clear of that range so the backdrop doesn't blink or 2710 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2711 // actually render truecolor. 2712 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2713 const c = color orelse return null; 2714 const snap = struct { 2715 fn f(v: u8) u8 { 2716 return if (v >= 1 and v <= 9) 10 else v; 2717 } 2718 }.f; 2719 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2720 } 2721 2722 // composites ANSI art behind a foreground grid 2723 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2724 art.clearGrid(); 2725 try art.build(allocator, .{ 2726 .min_size = .{ .width = null, .height = null }, 2727 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2728 }, root_focus); 2729 2730 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2731 errdefer out.deinit(); 2732 2733 if (art.getGrid()) |art_grid| { 2734 const anchor_x = switch (anchor) { 2735 .top_left => 0, 2736 .top_right => foreground.size.width -| art_grid.size.width, 2737 }; 2738 for (0..art_grid.size.height) |y| { 2739 for (0..art_grid.size.width) |x| { 2740 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2741 if (src.rune == null) continue; 2742 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2743 const dst = &out.cells.items[idx]; 2744 // composite the dimmed art behind every cell so it shows 2745 // through the whole UI: a blank cell becomes the art, while a 2746 // cell with a glyph keeps the glyph but takes the dimmed art as 2747 // its background (replacing whatever background it had, which 2748 // would otherwise obscure the art). 2749 if (cellIsBlank(dst.*)) { 2750 dst.* = src; 2751 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2752 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2753 } else { 2754 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2755 dst.style.bg = background_maybe; 2756 // change the color of text overlaying ANSI art to ensure 2757 // it is readable. don't do this in the web UI, since we 2758 // have direct control of the theme there (also, inner 2759 // scroll panes allow text to move without a re-render 2760 // by the TUI, which would break this). also don't do this 2761 // if the text is explicitly styled. 2762 if (is_terminal and dst.style.fg == null) { 2763 if (background_maybe) |background| { 2764 // use near-black and near-white, which is easier on the eyes 2765 // while retaining high contrast 2766 const luminance = (@as(u32, background.r) * 299 + 2767 @as(u32, background.g) * 587 + 2768 @as(u32, background.b) * 114) / 1000; 2769 dst.style.fg = if (luminance >= 128) 2770 .{ .r = 16, .g = 16, .b = 16 } 2771 else 2772 .{ .r = 240, .g = 240, .b = 240 }; 2773 } 2774 } 2775 } 2776 } 2777 } 2778 } 2779 return out; 2780 } 2781 }; 2782