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