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