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