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