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