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