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