╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const xit = @import("xit"); 3 const rp = xit.repo; 4 const rf = xit.ref; 5 const hash = xit.hash; 6 const xitui = xit.xitui; 7 const term = xitui.terminal; 8 const wgt = xitui.widget; 9 const layout = xitui.layout; 10 const inp = xitui.input; 11 const Grid = xitui.grid.Grid; 12 const Focus = xitui.focus.Focus; 13 const evt = @import("./event.zig"); 14 15 pub const Home = @import("./ui/Home.zig"); 16 pub const User = @import("./ui/User.zig"); 17 pub const Repo = @import("./ui/Repo.zig"); 18 pub const Title = @import("./ui/Title.zig"); 19 pub const SubTitle = @import("./ui/SubTitle.zig"); 20 pub const Quit = @import("./ui/Quit.zig"); 21 22 pub const PageKind = enum { 23 home, 24 user, 25 repo, 26 }; 27 28 pub const Page = union(PageKind) { 29 home: Home, 30 user: User, 31 repo: Repo, 32 33 pub fn init(arena: *std.heap.ArenaAllocator, session: *Session, route: RoutablePage) !Page { 34 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 35 return switch (route.parent()) { 36 .home => .{ .home = try Home.init(arena, haxy_moment, switch (route) { 37 .home_users => |after| after, 38 else => 0, 39 }, switch (route) { 40 .home_repos => |after| after, 41 else => 0, 42 }) }, 43 .user => switch (route) { 44 .user, .user_settings, .user_auth => |name| .{ .user = try User.init(arena, haxy_moment, name) }, 45 else => return error.UnexpectedRoute, 46 }, 47 .repo => switch (route) { 48 .repo, .repo_commits, .repo_refs, .repo_settings, .repo_auth => .{ .repo = try Repo.init(arena, session, route) }, 49 else => return error.UnexpectedRoute, 50 }, 51 }; 52 } 53 }; 54 55 // what the server hands to the client (and what main_wasm parses on _start). 56 // keeps Page free of any per-request session state. 57 pub const Snapshot = struct { 58 page: Page, 59 session: Session.Data = .{}, 60 }; 61 62 // a top-level "page" the user can navigate to. the user_* variants are the 63 // tabs of a user page; they each carry the user's name and all map to the 64 // .user parent page, so switching between them stays on that page (and just 65 // updates the url) rather than navigating away. 66 pub const RoutablePage = union(enum) { 67 home_users: usize, // 0 = first page 68 home_repos: usize, // 0 = first page 69 home_settings, 70 home_auth, 71 user: Array(evt.User.name_max_len), 72 user_settings: Array(evt.User.name_max_len), 73 user_auth: Array(evt.User.name_max_len), 74 repo: Array(repo_route_max_len), 75 repo_commits: struct { name: Array(repo_route_max_len), after: usize = 0 }, 76 repo_refs: struct { name: Array(repo_route_max_len), kind: RefKind = .branch, after: usize = 0 }, 77 repo_settings: Array(repo_route_max_len), 78 repo_auth: Array(repo_route_max_len), 79 80 pub const default: RoutablePage = .{ .home_users = 0 }; 81 82 pub const RefKind = enum { branch, tag }; 83 84 pub const RefOrOid = enum { 85 branch, 86 tag, 87 object, 88 89 fn fromSeg(s: []const u8) ?RefOrOid { 90 return std.meta.stringToEnum(RefOrOid, s); 91 } 92 }; 93 94 const user_segment = "/user/"; 95 const repo_segment = "/repo/"; 96 const files_seg = "files"; 97 const commits_seg = "commits"; 98 99 // a repo route is "username/reponame", optionally followed by the files tab's 100 // "/files/<refkind>/<refvalue>/<dir>" (refkind/refvalue name the ref the tree 101 // is read at). the bare "username/reponame" (and "username/reponame/files") 102 // mean the files root of the repo's default branch. 103 pub const repo_route_max_len = 1024; 104 105 // the parts of a `.repo` files route. all slices point into the route's 106 // stored string. ref_kind is null (and ref_value/dir empty) for the bare 107 // default-branch root; dir is "" at a ref's root. 108 pub const RepoFiles = struct { 109 identity: []const u8, // "owner/name" 110 owner: []const u8, 111 name: []const u8, 112 ref_kind: ?RefOrOid, 113 ref_value: []const u8, 114 dir: []const u8, 115 116 // parse a `.repo` route's stored string ("owner/name" or 117 // "owner/name/files/<refkind>/<refvalue>[/<dir>]"). 118 pub fn parse(s: []const u8) ?RepoFiles { 119 const s1 = std.mem.indexOfScalar(u8, s, '/') orelse return null; 120 const owner = s[0..s1]; 121 const after = s[s1 + 1 ..]; 122 const s2 = std.mem.indexOfScalar(u8, after, '/'); 123 const name = if (s2) |i| after[0..i] else after; 124 if (owner.len == 0 or name.len == 0) return null; 125 var result: RepoFiles = .{ 126 .identity = s[0 .. s1 + 1 + name.len], 127 .owner = owner, 128 .name = name, 129 .ref_kind = null, 130 .ref_value = "", 131 .dir = "", 132 }; 133 // the tail after "owner/name/". absent (or a bare "files") = default root. 134 const tail = if (s2) |i| after[i + 1 ..] else return result; 135 if (std.mem.eql(u8, tail, files_seg)) return result; 136 if (!std.mem.startsWith(u8, tail, files_seg ++ "/")) return result; 137 // "<refkind>/<refvalue>[/<dir>]" 138 const ref_part = tail[files_seg.len + 1 ..]; 139 const k1 = std.mem.indexOfScalar(u8, ref_part, '/') orelse return result; 140 const kind = RefOrOid.fromSeg(ref_part[0..k1]) orelse return result; 141 const after_kind = ref_part[k1 + 1 ..]; 142 const k2 = std.mem.indexOfScalar(u8, after_kind, '/'); 143 const value = if (k2) |i| after_kind[0..i] else after_kind; 144 if (value.len == 0) return result; 145 result.ref_kind = kind; 146 result.ref_value = value; 147 result.dir = if (k2) |i| after_kind[i + 1 ..] else ""; 148 return result; 149 } 150 }; 151 152 // build a `.repo` files route for "owner/name" (identity) at `dir` ("" = the 153 // ref's root) pinned to `ref_kind`/`ref_value`. a null ref_kind yields the 154 // bare default-branch root. null if the result doesn't fit the inline name. 155 pub fn repoFilesRoute(identity: []const u8, ref_kind: ?RefOrOid, ref_value: []const u8, dir: []const u8) ?RoutablePage { 156 const kind = ref_kind orelse return .{ .repo = Array(repo_route_max_len).from(identity) orelse return null }; 157 var buf: [repo_route_max_len]u8 = undefined; 158 const s = if (dir.len == 0) 159 std.fmt.bufPrint(&buf, "{s}/" ++ files_seg ++ "/{s}/{s}", .{ identity, @tagName(kind), ref_value }) catch return null 160 else 161 std.fmt.bufPrint(&buf, "{s}/" ++ files_seg ++ "/{s}/{s}/{s}", .{ identity, @tagName(kind), ref_value, dir }) catch return null; 162 return .{ .repo = Array(repo_route_max_len).from(s) orelse return null }; 163 } 164 165 // the ref/oid a `.repo_commits` route's stored string walks from. kind is 166 // null for the bare "owner/name/commits" (the repo's default branch). the 167 // returned value slices into `s`. 168 pub const CommitsRef = struct { ref_or_oid: ?RefOrOid, value: []const u8 }; 169 pub fn repoCommitsRef(s: []const u8) CommitsRef { 170 const marker = "/" ++ commits_seg ++ "/"; 171 const i = std.mem.indexOf(u8, s, marker) orelse return .{ .ref_or_oid = null, .value = "" }; 172 const ref_part = s[i + marker.len ..]; // "<refkind>/<refvalue>" 173 const k1 = std.mem.indexOfScalar(u8, ref_part, '/') orelse return .{ .ref_or_oid = null, .value = "" }; 174 const kind = RefOrOid.fromSeg(ref_part[0..k1]) orelse return .{ .ref_or_oid = null, .value = "" }; 175 const value = ref_part[k1 + 1 ..]; 176 if (value.len == 0) return .{ .ref_or_oid = null, .value = "" }; 177 return .{ .ref_or_oid = kind, .value = value }; 178 } 179 180 // build a `.repo_commits` route for "owner/name" (identity), walking from 181 // `ref_or_oid`/`value` (a null ref_or_oid yields the bare default-branch 182 // route). always carries the `commits` marker so the bare route 183 // ("owner/name/commits") doesn't collide with the files root. 184 pub fn repoCommitsRoute(identity: []const u8, ref_or_oid: ?RefOrOid, value: []const u8, after: usize) ?RoutablePage { 185 var buf: [repo_route_max_len]u8 = undefined; 186 const s = if (ref_or_oid) |kind| 187 std.fmt.bufPrint(&buf, "{s}/" ++ commits_seg ++ "/{s}/{s}", .{ identity, @tagName(kind), value }) catch return null 188 else 189 std.fmt.bufPrint(&buf, "{s}/" ++ commits_seg, .{identity}) catch return null; 190 return .{ .repo_commits = .{ .name = Array(repo_route_max_len).from(s) orelse return null, .after = after } }; 191 } 192 193 // build a `.repo_refs` route for "owner/name" (identity) paginating `kind`'s 194 // column to `after` (0 = the first window, the other column always at 0). 195 pub fn repoRefsRoute(identity: []const u8, kind: RefKind, after: usize) ?RoutablePage { 196 return .{ .repo_refs = .{ .name = Array(repo_route_max_len).from(identity) orelse return null, .kind = kind, .after = after } }; 197 } 198 199 // an inline, owned array of data. keeping it in the route (rather than a 200 // borrowed slice) makes RoutablePage a plain value: it can be copied, stored 201 // in history, and serialized without any arena tracking. 202 pub fn Array(comptime max_len: usize) type { 203 return struct { 204 bytes: [max_len]u8 = undefined, 205 len: u16 = 0, 206 207 pub fn from(s: []const u8) ?Array(max_len) { 208 if (s.len > max_len) return null; 209 var name = Array(max_len){ .len = @intCast(s.len) }; 210 @memcpy(name.bytes[0..s.len], s); 211 return name; 212 } 213 214 pub fn slice(self: *const Array(max_len)) []const u8 { 215 return self.bytes[0..self.len]; 216 } 217 }; 218 } 219 220 pub fn url(comptime self: RoutablePage) []const u8 { 221 return switch (self) { 222 .home_users => "/users", 223 .home_repos => "/repos", 224 .home_settings => "/settings", 225 .home_auth => "/auth", 226 .user, .user_settings, .user_auth => @compileError("user routes are dynamic; use urlAlloc"), 227 .repo, .repo_commits, .repo_refs, .repo_settings, .repo_auth => @compileError("repo routes are dynamic; use urlAlloc"), 228 }; 229 } 230 231 pub fn urlAlloc(self: RoutablePage, arena: *std.heap.ArenaAllocator) ![]const u8 { 232 return switch (self) { 233 .home_users => |after| if (after == 0) @as([]const u8, "/users") else try std.fmt.allocPrint(arena.allocator(), "/users?after={d}", .{after}), 234 .home_repos => |after| if (after == 0) @as([]const u8, "/repos") else try std.fmt.allocPrint(arena.allocator(), "/repos?after={d}", .{after}), 235 .user => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}", .{name.slice()}), 236 .user_settings => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/settings", .{name.slice()}), 237 .user_auth => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/auth", .{name.slice()}), 238 .repo => |name| try std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}", .{name.slice()}), 239 .repo_commits => |c| if (c.after == 0) 240 try std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}", .{c.name.slice()}) 241 else 242 try std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}?after={d}", .{ c.name.slice(), c.after }), 243 .repo_refs => |r| if (r.after == 0) 244 try std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}/refs", .{r.name.slice()}) 245 else 246 try std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}/refs?kind={s}&after={d}", .{ r.name.slice(), @tagName(r.kind), r.after }), 247 .repo_settings => |name| try std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}/settings", .{name.slice()}), 248 .repo_auth => |name| try std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}/auth", .{name.slice()}), 249 inline else => |_, tag| url(tag), 250 }; 251 } 252 253 pub fn parent(self: RoutablePage) PageKind { 254 return switch (self) { 255 .home_users, .home_repos, .home_settings, .home_auth => .home, 256 .user, .user_settings, .user_auth => .user, 257 .repo, .repo_commits, .repo_refs, .repo_settings, .repo_auth => .repo, 258 }; 259 } 260 261 // parse a path plus its raw query string 262 pub fn fromUrl(path: []const u8, query: ?[]const u8) ?RoutablePage { 263 const after = uintParam(query, "after") orelse 0; 264 if (std.mem.eql(u8, path, "/")) return default; 265 if (std.mem.eql(u8, path, "/users")) return .{ .home_users = after }; 266 if (std.mem.eql(u8, path, "/repos")) return .{ .home_repos = after }; 267 if (std.mem.eql(u8, path, "/settings")) return .home_settings; 268 if (std.mem.eql(u8, path, "/auth")) return .home_auth; 269 // /user/<name>[/settings|/auth] 270 if (std.mem.startsWith(u8, path, user_segment)) { 271 const rest = path[user_segment.len..]; 272 const slash = std.mem.indexOfScalar(u8, rest, '/'); 273 const name = if (slash) |s| rest[0..s] else rest; 274 if (name.len == 0) return null; 275 const parsed = Array(evt.User.name_max_len).from(name) orelse return null; // name too long 276 if (slash) |s| { 277 const sub = rest[s + 1 ..]; 278 if (std.mem.eql(u8, sub, "settings")) return .{ .user_settings = parsed }; 279 if (std.mem.eql(u8, sub, "auth")) return .{ .user_auth = parsed }; 280 return null; // unknown sub-path 281 } 282 return .{ .user = parsed }; 283 } 284 // /repo/<username>/<reponame> (files root), then optionally /settings, 285 // /auth, or /files/<dir> for a directory in the files tab. the `repo` 286 // variant stores "username/reponame" (root) or 287 // "username/reponame/files/<dir>"; settings/auth store just the pair. 288 if (std.mem.startsWith(u8, path, repo_segment)) { 289 const rest = path[repo_segment.len..]; 290 const user_slash = std.mem.indexOfScalar(u8, rest, '/') orelse return null; 291 const after_user = rest[user_slash + 1 ..]; 292 const repo_slash = std.mem.indexOfScalar(u8, after_user, '/'); 293 const repo_name = if (repo_slash) |s| after_user[0..s] else after_user; 294 // reject an empty username or reponame 295 if (user_slash == 0 or repo_name.len == 0) return null; 296 const pair = rest[0 .. user_slash + 1 + repo_name.len]; // "username/reponame" 297 if (repo_slash) |s| { 298 const sub = after_user[s + 1 ..]; 299 if (std.mem.eql(u8, sub, "refs")) return .{ .repo_refs = .{ 300 .name = Array(repo_route_max_len).from(pair) orelse return null, 301 .kind = refsKind(query), 302 .after = after, 303 } }; 304 if (std.mem.eql(u8, sub, "settings")) return .{ .repo_settings = Array(repo_route_max_len).from(pair) orelse return null }; 305 if (std.mem.eql(u8, sub, "auth")) return .{ .repo_auth = Array(repo_route_max_len).from(pair) orelse return null }; 306 if (std.mem.eql(u8, sub, files_seg)) return repoFilesRoute(pair, null, "", ""); // trailing /files == default-branch root 307 if (std.mem.startsWith(u8, sub, files_seg ++ "/")) { 308 // "files/<refkind>/<refvalue>[/<dir>]" 309 const ref_part = sub[files_seg.len + 1 ..]; 310 const k1 = std.mem.indexOfScalar(u8, ref_part, '/') orelse return null; 311 const kind = RefOrOid.fromSeg(ref_part[0..k1]) orelse return null; 312 const after_kind = ref_part[k1 + 1 ..]; 313 const k2 = std.mem.indexOfScalar(u8, after_kind, '/'); 314 const value = if (k2) |i| after_kind[0..i] else after_kind; 315 if (value.len == 0) return null; 316 const dir = if (k2) |i| after_kind[i + 1 ..] else ""; 317 return repoFilesRoute(pair, kind, value, dir); 318 } 319 if (std.mem.eql(u8, sub, commits_seg)) return repoCommitsRoute(pair, null, "", after); // trailing /commits == default branch 320 if (std.mem.startsWith(u8, sub, commits_seg ++ "/")) { 321 // "commits/<refkind>/<refvalue>" 322 const ref_part = sub[commits_seg.len + 1 ..]; 323 const k1 = std.mem.indexOfScalar(u8, ref_part, '/') orelse return null; 324 const kind = RefOrOid.fromSeg(ref_part[0..k1]) orelse return null; 325 const value = ref_part[k1 + 1 ..]; 326 if (value.len == 0) return null; 327 return repoCommitsRoute(pair, kind, value, after); 328 } 329 return null; // unknown sub-path 330 } 331 return repoFilesRoute(pair, null, "", ""); 332 } 333 return null; 334 } 335 336 // the refs column a "kind=" query param names; absent or unrecognized reads 337 // as the branches column. 338 fn refsKind(query: ?[]const u8) RefKind { 339 const qs = query orelse return .branch; 340 var it = std.mem.splitScalar(u8, qs, '&'); 341 while (it.next()) |pair| { 342 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 343 if (std.mem.eql(u8, pair[0..eq], "kind") and std.mem.eql(u8, pair[eq + 1 ..], "tag")) return .tag; 344 } 345 return .branch; 346 } 347 348 // read a "key=<uint>" value from a raw "a=1&b=2" query string. 349 fn uintParam(query: ?[]const u8, key: []const u8) ?usize { 350 const qs = query orelse return null; 351 var it = std.mem.splitScalar(u8, qs, '&'); 352 while (it.next()) |pair| { 353 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 354 if (std.mem.eql(u8, pair[0..eq], key)) 355 return std.fmt.parseInt(usize, pair[eq + 1 ..], 10) catch null; 356 } 357 return null; 358 } 359 360 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 361 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 362 return switch (a) { 363 .home_users => |a_after| a_after == b.home_users, 364 .home_repos => |a_after| a_after == b.home_repos, 365 .user => |a_name| std.mem.eql(u8, a_name.slice(), b.user.slice()), 366 .user_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.user_settings.slice()), 367 .user_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.user_auth.slice()), 368 .repo => |a_name| std.mem.eql(u8, a_name.slice(), b.repo.slice()), 369 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and a_c.after == b.repo_commits.after, 370 .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 a_r.after == b.repo_refs.after, 371 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 372 .repo_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_auth.slice()), 373 else => true, 374 }; 375 } 376 377 // the content a repo route renders: which repo, plus the files directory and 378 // commits page. tab switches keep this fixed (the page already holds every 379 // tab's data); a files-directory or commits-page change alters it. 380 const RepoContent = struct { 381 identity: []const u8, 382 ref_or_oid: ?RefOrOid, 383 ref_or_oid_value: []const u8, 384 files_dir: []const u8, 385 commits_ref_or_oid: ?RefOrOid, 386 commits_ref_or_oid_value: []const u8, 387 commits_after: usize, 388 refs_kind: RefKind, 389 refs_after: usize, 390 }; 391 392 // `r` is borrowed by pointer: the returned slices point into its inline 393 // route string, so it must outlive the RepoContent (passing by value would 394 // leave them dangling into a freed copy). 395 fn repoContent(r: *const RoutablePage) ?RepoContent { 396 const name = switch (r.*) { 397 .repo, .repo_settings, .repo_auth => |*n| n.slice(), 398 .repo_commits => |*c| c.name.slice(), 399 .repo_refs => |*r2| r2.name.slice(), 400 else => return null, 401 }; 402 const files = RepoFiles.parse(name) orelse return null; 403 // a 0 offset means no refs pagination, so canonicalize the column to 404 // branch then — otherwise the base /refs route would look like a content 405 // change relative to the same page mirrored with kind=tag. 406 const refs_after = switch (r.*) { 407 .repo_refs => |r2| r2.after, 408 else => 0, 409 }; 410 const is_files = std.meta.activeTag(r.*) == .repo; 411 const is_commits = std.meta.activeTag(r.*) == .repo_commits; 412 const commits_ref = if (is_commits) repoCommitsRef(name) else CommitsRef{ .ref_or_oid = null, .value = "" }; 413 return .{ 414 .identity = files.identity, 415 .ref_or_oid = if (is_files) files.ref_kind else null, 416 .ref_or_oid_value = if (is_files) files.ref_value else "", 417 .files_dir = if (is_files) files.dir else "", 418 .commits_ref_or_oid = commits_ref.ref_or_oid, 419 .commits_ref_or_oid_value = commits_ref.value, 420 .commits_after = switch (r.*) { 421 .repo_commits => |c| c.after, 422 else => 0, 423 }, 424 .refs_kind = switch (r.*) { 425 .repo_refs => |r2| if (refs_after == 0) .branch else r2.kind, 426 else => .branch, 427 }, 428 .refs_after = refs_after, 429 }; 430 } 431 432 // true when `a` and `b` are both repo routes that render different content 433 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 434 const ca = repoContent(&a) orelse return false; 435 const cb = repoContent(&b) orelse return false; 436 return !std.mem.eql(u8, ca.identity, cb.identity) or 437 ca.ref_or_oid != cb.ref_or_oid or 438 !std.mem.eql(u8, ca.ref_or_oid_value, cb.ref_or_oid_value) or 439 !std.mem.eql(u8, ca.files_dir, cb.files_dir) or 440 ca.commits_ref_or_oid != cb.commits_ref_or_oid or 441 !std.mem.eql(u8, ca.commits_ref_or_oid_value, cb.commits_ref_or_oid_value) or 442 ca.commits_after != cb.commits_after or 443 ca.refs_kind != cb.refs_kind or 444 ca.refs_after != cb.refs_after; 445 } 446 447 // true when `a` and `b` are the same home list tab paginated to a different 448 // window. switching between the users/repos tabs is in-page (the home page 449 // holds both lists), so only a changed `after` on the same tab navigates. 450 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 451 return switch (a) { 452 .home_users => |aa| switch (b) { 453 .home_users => |bb| aa != bb, 454 else => false, 455 }, 456 .home_repos => |aa| switch (b) { 457 .home_repos => |bb| aa != bb, 458 else => false, 459 }, 460 else => false, 461 }; 462 } 463 464 // serialize as { "kind": <tag>, "name"?: <name>, "after"?: <n> }. the default 465 // union(enum) codec doesn't round-trip here: Stringify emits a void 466 // variant as a bare tag string, but the parser expects an object — so the 467 // server->wasm Snapshot JSON would fail to parse. 468 pub fn jsonStringify(self: RoutablePage, jw: anytype) !void { 469 try jw.beginObject(); 470 try jw.objectField("kind"); 471 try jw.write(@tagName(self)); 472 switch (self) { 473 .user, .user_settings, .user_auth => |name| { 474 try jw.objectField("name"); 475 try jw.write(name.slice()); 476 }, 477 .repo, .repo_settings, .repo_auth => |name| { 478 try jw.objectField("name"); 479 try jw.write(name.slice()); 480 }, 481 .repo_commits => |c| { 482 try jw.objectField("name"); 483 try jw.write(c.name.slice()); 484 try jw.objectField("after"); 485 try jw.write(c.after); 486 }, 487 .repo_refs => |r| { 488 try jw.objectField("name"); 489 try jw.write(r.name.slice()); 490 // "kind" already holds the union tag, so the paginated column 491 // travels under a distinct field. 492 try jw.objectField("ref_kind"); 493 try jw.write(@tagName(r.kind)); 494 try jw.objectField("after"); 495 try jw.write(r.after); 496 }, 497 .home_users, .home_repos => |after| { 498 try jw.objectField("after"); 499 try jw.write(after); 500 }, 501 else => {}, 502 } 503 try jw.endObject(); 504 } 505 506 pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !RoutablePage { 507 const Helper = struct { kind: std.meta.Tag(RoutablePage), name: ?[]const u8 = null, after: usize = 0, ref_kind: ?[]const u8 = null }; 508 const helper = try std.json.innerParse(Helper, allocator, source, options); 509 const parseName = struct { 510 // errors must stay within std.json's ParseError set; ValueTooLong 511 // is its member for an over-long field. 512 fn f(comptime max_len: usize, maybe: ?[]const u8) !Array(max_len) { 513 return Array(max_len).from(maybe orelse return error.MissingField) orelse error.ValueTooLong; 514 } 515 }.f; 516 return switch (helper.kind) { 517 .home_users => .{ .home_users = helper.after }, 518 .home_repos => .{ .home_repos = helper.after }, 519 .home_settings => .home_settings, 520 .home_auth => .home_auth, 521 .user => .{ .user = try parseName(evt.User.name_max_len, helper.name) }, 522 .user_settings => .{ .user_settings = try parseName(evt.User.name_max_len, helper.name) }, 523 .user_auth => .{ .user_auth = try parseName(evt.User.name_max_len, helper.name) }, 524 .repo => .{ .repo = try parseName(repo_route_max_len, helper.name) }, 525 .repo_commits => .{ .repo_commits = .{ .name = try parseName(repo_route_max_len, helper.name), .after = helper.after } }, 526 .repo_refs => .{ .repo_refs = .{ 527 .name = try parseName(repo_route_max_len, helper.name), 528 .kind = if (helper.ref_kind) |k| (if (std.mem.eql(u8, k, "tag")) .tag else .branch) else .branch, 529 .after = helper.after, 530 } }, 531 .repo_settings => .{ .repo_settings = try parseName(repo_route_max_len, helper.name) }, 532 .repo_auth => .{ .repo_auth = try parseName(repo_route_max_len, helper.name) }, 533 }; 534 } 535 }; 536 537 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 538 pub const ResolvedRefOrOid = struct { 539 // the hex-oid length for the default repo options the on-disk repos use 540 pub const hex_len = hash.hexLen((rp.RepoOpts(.xit){}).hash); 541 542 // the concrete ref/oid (a null request resolves to the default branch) 543 ref_or_oid: RoutablePage.RefOrOid, 544 // its branch/tag name or oid, url-encoded (ref names may contain '/', so the 545 // route layer — which splits on '/' — stores them encoded). duped into `aa`. 546 value: []const u8, 547 // the commit oid it points at 548 oid: [hex_len]u8, 549 550 // resolve a requested ref/oid (null = the repo's default branch) to a 551 // concrete ref/oid plus the commit oid it points at. `requested_value` is the 552 // url-encoded form (as it appears in the route). null when it doesn't resolve 553 // (an unknown branch/tag, or a malformed/unknown oid). 554 pub fn init( 555 repo: *rp.Repo(.xit, .{}), 556 io: std.Io, 557 aa: std.mem.Allocator, 558 requested_ref_or_oid: ?RoutablePage.RefOrOid, 559 requested_value: []const u8, 560 ) !?ResolvedRefOrOid { 561 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 562 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 563 var value: []const u8 = if (requested_ref_or_oid == null) 564 requested_value 565 else 566 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 567 // no ref named: fall back to HEAD's branch (or its oid when detached). 568 if (requested_ref_or_oid == null) { 569 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 570 if (repo.head(io, &head_buf)) |head| switch (head) { 571 .ref => |r| { 572 ref_or_oid = .branch; 573 value = r.name; 574 }, 575 .oid => |o| { 576 ref_or_oid = .object; 577 value = o; 578 }, 579 } else |_| {} 580 } 581 582 var oid: [hex_len]u8 = undefined; 583 switch (ref_or_oid) { 584 .object => { 585 if (value.len != hex_len) return null; 586 @memcpy(&oid, value); 587 }, 588 .branch, .tag => { 589 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 590 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 591 }, 592 } 593 // store url-encoded so the route layer can hold the value verbatim. 594 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncode(aa, value), .oid = oid }; 595 } 596 597 fn isUnreserved(c: u8) bool { 598 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 599 } 600 601 // percent-encode a ref name for use as a single url path segment. 602 fn urlEncode(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 603 var out: std.Io.Writer.Allocating = .init(aa); 604 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 605 return out.written(); 606 } 607 }; 608 609 // per-connection mutable state. each SSH session / web session / local TUI 610 // run gets its own. 611 pub const Session = struct { 612 data: Data = .{}, 613 // session-lifetime allocations: login/user_id, persisted prefs, queued 614 // actions. lives as long as the connection. 615 arena: *std.heap.ArenaAllocator, 616 // current page's allocations: page data and the page-scoped strings widgets 617 // build (form actions, links). owned by Nav, swapped on each navigation, so 618 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 619 // where a render is one-shot, this points at the same arena as `arena`. 620 page_arena: *std.heap.ArenaAllocator, 621 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 622 // filesystem io and the path to <server>/repos, for opening on-disk repos 623 // during page construction. both null on wasm, which has no filesystem and 624 // rebuilds pages from the serialized snapshot rather than from disk. 625 io: ?std.Io = null, 626 repos_dir: ?[]const u8 = null, 627 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 628 // focus id -> the live TextInput, refreshed each frame by the views that own 629 // inputs. web/wasm form handling looks widgets up here by focus id. 630 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput(Widget)) = .empty, 631 nav_back: bool = false, // set by input (escape) to request the native TUI pop a page; see Nav 632 // a requested forward navigation. set this (via navigate) to move to a new 633 // page; Nav.sync builds it and then copies it into current_page, clearing 634 // this back to null. setting current_page directly only updates the url and 635 // does not navigate. 636 next_page: ?RoutablePage = null, 637 is_terminal: bool = false, // true on remote SSH and local TUI 638 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 639 // url. null on the web itself (no footer there). 640 web_port: ?u16 = null, 641 quit_requested: bool = false, 642 643 const Self = @This(); 644 645 // serializable data sent down to web client 646 pub const Data = struct { 647 user_id: ?[]const u8 = null, 648 // a transient outcome to surface from the last /login POST attempt 649 login_failure: ?Home.Auth.Login.Failure = null, 650 current_page: RoutablePage = .default, 651 // whether to render the ANSI art backdrop 652 enable_ansi: bool = true, 653 }; 654 655 // a user-initiated state change. widgets enqueue these on the session during 656 // input instead of mutating state or touching the DB themselves; the host 657 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 658 // side effects out of the widget tree and gives every render path one place to 659 // turn a UI action into a state change + event. 660 pub const Action = union(enum) { 661 toggle_ansi, 662 }; 663 664 pub fn init( 665 arena: *std.heap.ArenaAllocator, 666 repo: *rp.Repo(.xit, evt.admin_repo_opts), 667 data: Data, 668 ) !Self { 669 var session = Self{ 670 .data = data, 671 .arena = arena, 672 // until a host swaps in a page-scoped arena (Nav does this), page 673 // allocations land in the session arena. on the web path that's the 674 // intended behavior, since the whole arena is per-request. 675 .page_arena = arena, 676 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 677 }; 678 try session.loadUserPrefs(); 679 return session; 680 } 681 682 // load the logged-in user's persisted preferences from the db 683 pub fn loadUserPrefs(self: *Self) !void { 684 const user_id = self.data.user_id orelse return; 685 const moment = self.haxy_moment orelse return; 686 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.arena, user_id)) |user| { 687 self.data.enable_ansi = user.enable_ansi; 688 } 689 } 690 691 // queue an action for the host to drain this frame. 692 pub fn push(self: *Self, action: Action) !void { 693 try self.pending.append(self.arena.allocator(), action); 694 } 695 696 // apply a single action's in-memory effect only (no persistence). 697 fn apply(self: *Self, action: Action) void { 698 switch (action) { 699 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 700 } 701 } 702 703 // drain the session's queued actions by applying them to the session. 704 // used on the wasm path, which has no repo to persist to. 705 pub fn applyPending(self: *Self) void { 706 for (self.pending.items) |action| self.apply(action); 707 self.pending.clearRetainingCapacity(); 708 } 709 710 // drain the session's queued actions by applying them to the session and 711 // writing them to the db 712 pub fn applyAndWritePending( 713 self: *Self, 714 io: std.Io, 715 allocator: std.mem.Allocator, 716 repo: *rp.Repo(.xit, evt.admin_repo_opts), 717 ) !void { 718 defer self.pending.clearRetainingCapacity(); 719 var wrote = false; 720 for (self.pending.items) |action| { 721 self.apply(action); 722 switch (action) { 723 .toggle_ansi => if (self.data.user_id) |user_id| { 724 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 725 wrote = true; 726 }, 727 } 728 } 729 // reload the moment so a later read off it sees what we just applied 730 if (wrote) self.haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo); 731 } 732 733 // request a forward navigation to `route`; the host consumes next_page 734 // (Nav.sync on the terminal, the wasm tick on the web). 735 pub fn navigate(self: *Session, route: RoutablePage) !void { 736 self.next_page = route; 737 } 738 }; 739 740 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session) !void { 741 var nav = try Nav.init(allocator, session); 742 defer nav.deinit(allocator); 743 744 var terminal = try term.Terminal.init(io, allocator); 745 defer terminal.deinit(io); 746 747 // set term as active so it will be properly cooked 748 // when a panic/segfault happens 749 term.setActive(&terminal); 750 defer term.setActive(null); 751 752 var last_size = layout.Size{ .width = 0, .height = 0 }; 753 var last_grid = try Grid.init(allocator, last_size); 754 defer last_grid.deinit(); 755 756 while (!terminal.shouldQuit()) { 757 const grid_changed = try terminal.render(&nav.root, &last_grid, &last_size); 758 759 // process any inputs. 760 // 761 // if the grid didn't change, then first do a blocking 762 // read, so the thread will sleep until further input. 763 // after that, all remaining reads are non-blocking so 764 // we can process the rest of the queued inputs. 765 // 766 // if the grid *did* change, then only do non-blocking 767 // reads. we do not want to sleep the thread because 768 // there may be an animation that requires more looping. 769 var blocking = !grid_changed; 770 while (try terminal.readKey(io, blocking)) |key| { 771 blocking = false; 772 try inputKey(allocator, &nav.root, key, session); 773 } 774 775 // local run has no repo to persist to, so just apply in-memory 776 session.applyPending(); 777 778 // reconcile navigation: forward to a new page, or back on escape. 779 try nav.sync(allocator, session); 780 781 // the quit button (on the quit tab) asks the host to tear down. 782 if (session.quit_requested) terminal.requestQuit(); 783 784 try nav.root.build(allocator, .{ 785 .min_size = .{ .width = null, .height = null }, 786 .max_size = .{ .width = last_size.width, .height = last_size.height }, 787 }, nav.root.getFocus()); 788 } 789 } 790 791 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: inp.Key, session: *Session) !void { 792 switch (key) { 793 // request a navigation pop; the host's Nav.sync goes back a page, or 794 // quits when there's no history left. 795 .escape => session.nav_back = true, 796 .enter => { 797 const root_focus = root.getFocus(); 798 if (root_focus.grandchild_id) |gid| { 799 // follow a cross-page link 800 if (crossPageLink(root_focus, gid, session.data.current_page)) |route| { 801 return session.navigate(route); 802 } 803 } 804 try root.input(allocator, key, root_focus); 805 }, 806 .mouse => |mouse| { 807 if (mouse.action == .press and mouse.action.press == .left) { 808 const root_focus = root.getFocus(); 809 var clicked: ?usize = null; 810 var iter = root_focus.children.iterator(); 811 while (iter.next()) |entry| { 812 const child = entry.value_ptr.*; 813 if (!child.focus.focusable) continue; 814 const r = child.rect; 815 if (mouse.x >= r.x and mouse.y >= r.y and 816 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 817 { 818 clicked = entry.key_ptr.*; 819 break; 820 } 821 } 822 if (clicked) |focus_id| { 823 // follow a cross-page link 824 if (crossPageLink(root_focus, focus_id, session.data.current_page)) |route| { 825 return session.navigate(route); 826 } 827 try root_focus.setFocus(focus_id); 828 } 829 // forward the press into the widget tree so buttons (and any 830 // future click-aware widgets) can react. widgets that don't 831 // care about presses ignore it. 832 try root.input(allocator, key, root.getFocus()); 833 } else { 834 try root.input(allocator, key, root.getFocus()); 835 } 836 }, 837 else => try root.input(allocator, key, root.getFocus()), 838 } 839 } 840 841 // if the focus target at focus_id is an `a:` link that should navigate (a 842 // different parent page, or a different files directory within the repo page), 843 // return its route; otherwise null. lets a host turn a click / enter on such a 844 // link into a navigation rather than a focus change. 845 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, current: RoutablePage) ?RoutablePage { 846 const child = root_focus.children.get(focus_id) orelse return null; 847 const custom = switch (child.focus.kind) { 848 .custom => |c| c, 849 else => return null, 850 }; 851 const a_prefix = "a:"; 852 if (!std.mem.startsWith(u8, custom, a_prefix)) return null; 853 const url = custom[a_prefix.len..]; 854 const q = std.mem.indexOfScalar(u8, url, '?'); 855 const route = RoutablePage.fromUrl(if (q) |i| url[0..i] else url, if (q) |i| url[i + 1 ..] else null) orelse return null; 856 // a link to a different parent page always navigates; within a page, a 857 // files-directory / commits-page / list-window change navigates while tab 858 // links stay in-page (header-handled). 859 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.homePageChanged(route, current)) return route; 860 return null; 861 } 862 863 // native-TUI navigation 864 pub const Nav = struct { 865 root: Widget, 866 route: RoutablePage, 867 // backs the current page (its Page data and the page-scoped strings its 868 // widgets build). owned here, swapped on each navigation. session.page_arena 869 // tracks whichever of these is current so widgets allocate into it. 870 arena: *std.heap.ArenaAllocator, 871 history: std.ArrayList(Entry), 872 873 // each retained page keeps its own arena, freed when the page leaves the 874 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 875 // long-lived session from accumulating every page it ever visited. 876 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 877 878 // cap on retained back-history so the chain can't grow memory without bound 879 const max_history: usize = 16; 880 881 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 882 const arena = try allocator.create(std.heap.ArenaAllocator); 883 arena.* = std.heap.ArenaAllocator.init(allocator); 884 errdefer { 885 arena.deinit(); 886 allocator.destroy(arena); 887 } 888 889 session.page_arena = arena; 890 const route = session.data.current_page; 891 892 const page = try arena.allocator().create(Page); 893 page.* = try Page.init(arena, session, route); 894 return .{ 895 .root = try initRoot(allocator, page, session), 896 .route = route, 897 .arena = arena, 898 .history = .empty, 899 }; 900 } 901 902 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 903 self.root.deinit(allocator); 904 freeArena(allocator, self.arena); 905 for (self.history.items) |*entry| { 906 entry.root.deinit(allocator); 907 freeArena(allocator, entry.arena); 908 } 909 self.history.deinit(allocator); 910 } 911 912 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 913 arena.deinit(); 914 allocator.destroy(arena); 915 } 916 917 // reconcile the displayed root with the session's navigation state. a 918 // forward nav (current_page moved to a different parent page) pushes the 919 // current root and builds the new page in a fresh arena; a back request 920 // frees the current page and restores the previous one. when escape is 921 // pressed with no history left we switch to the quit tab instead of quitting. 922 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 923 if (session.nav_back) { 924 session.nav_back = false; 925 if (self.history.pop()) |entry| { 926 self.root.deinit(allocator); 927 freeArena(allocator, self.arena); 928 self.root = entry.root; 929 self.route = entry.route; 930 self.arena = entry.arena; 931 session.page_arena = entry.arena; 932 session.data.current_page = entry.route; 933 return; 934 } 935 // nothing to go back to; switch to the quit confirmation 936 const root_focus = self.root.getFocus(); 937 var iter = root_focus.children.iterator(); 938 while (iter.next()) |entry| { 939 switch (entry.value_ptr.focus.kind) { 940 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 941 try root_focus.setFocus(entry.key_ptr.*); 942 // a Stack only builds its selected child, so the quit 943 // button isn't in the focus tree until a build runs with 944 // the quit tab selected. build once, then send a 945 // synthetic arrow_down to drop focus from the tab onto 946 // the button so it's ready to confirm. 947 try self.root.build(allocator, .{ 948 .min_size = .{ .width = null, .height = 40 }, 949 .max_size = .{ .width = 80, .height = null }, 950 }, root_focus); 951 try self.root.input(allocator, .arrow_down, root_focus); 952 break; 953 }, 954 else => {}, 955 } 956 } 957 return; 958 } 959 960 // forward navigation: navigate() set next_page to the page to move to (a 961 // cross-page link or tab change crossing pages) 962 if (session.next_page) |route| { 963 session.next_page = null; 964 if (session.haxy_moment == null) return; 965 // the page we navigated to becomes the current page 966 session.data.current_page = route; 967 968 const arena = try allocator.create(std.heap.ArenaAllocator); 969 arena.* = std.heap.ArenaAllocator.init(allocator); 970 errdefer freeArena(allocator, arena); 971 972 session.page_arena = arena; 973 974 const page = try arena.allocator().create(Page); 975 page.* = try Page.init(arena, session, route); 976 const new_root = try initRoot(allocator, page, session); 977 978 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 979 // drop the oldest entry (freeing its widget tree and arena) once over cap 980 if (self.history.items.len > max_history) { 981 var oldest = self.history.orderedRemove(0); 982 oldest.root.deinit(allocator); 983 freeArena(allocator, oldest.arena); 984 } 985 self.root = new_root; 986 self.route = route; 987 self.arena = arena; 988 } 989 } 990 }; 991 992 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 993 const page_widget: Widget = switch (page.*) { 994 .home => |*p| .{ .home = try .init(allocator, p, session) }, 995 .user => |*p| .{ .user = try .init(allocator, p, session) }, 996 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 997 }; 998 999 const demon_art = @embedFile("embed/demon.ans"); 1000 1001 // on the TUI/SSH, the page sits above a one-row footer showing the url 1002 var root = if (session.is_terminal) blk: { 1003 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1004 errdefer box.deinit(allocator); 1005 const bg_id = bg_blk: { 1006 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 1007 errdefer background.deinit(allocator); 1008 const id = background.getFocus().id; 1009 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 1010 break :bg_blk id; 1011 }; 1012 { 1013 var footer = try Footer.init(allocator, session); 1014 errdefer footer.deinit(allocator); 1015 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 1016 } 1017 box.getFocus().child_id = bg_id; 1018 break :blk Widget{ .box = box }; 1019 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 1020 errdefer root.deinit(allocator); 1021 1022 // input-owning views build their TextInputs in init — so reset the 1023 // focus-id -> *TextInput map here 1024 session.text_inputs.clearRetainingCapacity(); 1025 1026 try root.build(allocator, .{ 1027 .min_size = .{ .width = null, .height = 40 }, 1028 .max_size = .{ .width = 80, .height = null }, 1029 }, root.getFocus()); 1030 if (root.getFocus().child_id) |child_id| { 1031 try root.getFocus().setFocus(child_id); 1032 } 1033 1034 return root; 1035 } 1036 1037 pub const Widget = union(enum) { 1038 text: wgt.Text(Widget), 1039 box: wgt.Box(Widget), 1040 text_box: wgt.TextBox(Widget), 1041 text_input: wgt.TextInput(Widget), 1042 scroll: wgt.Scroll(Widget), 1043 stack: wgt.Stack(Widget), 1044 flow_box: FlowBox, 1045 flow_box_scroll: FlowBox.Scroll, 1046 spacer: Spacer, 1047 center: Center, 1048 ansi_art: AnsiArt, 1049 background: AnsiBackground, 1050 home: Home.View, 1051 user: User.View, 1052 repo: Repo.View, 1053 quit: Quit.View, 1054 title: Title.View, 1055 sub_title: SubTitle.View, 1056 home_header: Home.Header.View, 1057 user_header: User.Header.View, 1058 repo_header: Repo.Header.View, 1059 repo_sub_header: Repo.SubHeader.View, 1060 repo_files: Repo.Files.View, 1061 repo_commits: Repo.Commits.View, 1062 repo_refs: Repo.Refs.View, 1063 home_users: Home.Users.View, 1064 home_repos: Home.Repos.View, 1065 auth_tab: Home.Header.AuthTab.View, 1066 home_settings: Home.Settings.View, 1067 home_auth: Home.Auth.View, 1068 footer: Footer, 1069 1070 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 1071 switch (self.*) { 1072 inline else => |*case| case.deinit(allocator), 1073 } 1074 } 1075 1076 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 1077 switch (self.*) { 1078 inline else => |*case| try case.build(allocator, constraint, root_focus), 1079 } 1080 } 1081 1082 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) anyerror!void { 1083 switch (self.*) { 1084 inline else => |*case| try case.input(allocator, key, root_focus), 1085 } 1086 } 1087 1088 pub fn clearGrid(self: *Widget) void { 1089 switch (self.*) { 1090 inline else => |*case| case.clearGrid(), 1091 } 1092 } 1093 1094 pub fn getGrid(self: Widget) ?Grid { 1095 switch (self) { 1096 inline else => |*case| return case.getGrid(), 1097 } 1098 } 1099 1100 pub fn getFocus(self: *Widget) *Focus { 1101 switch (self.*) { 1102 inline else => |*case| return case.getFocus(), 1103 } 1104 } 1105 }; 1106 1107 pub const FlowBox = struct { 1108 focus: *Focus, 1109 grid: ?Grid, 1110 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1111 // backs the per-item strings the text boxes borrow: each box's `content` 1112 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 1113 // wholesale on each setItems so there's no per-string ownership to track. 1114 arena: std.heap.ArenaAllocator, 1115 // column count from the last build — FlowBox.Scroll.input uses it so arrow 1116 // up/down can step by a row's worth of items. 1117 last_cols: usize, 1118 options: Options, 1119 1120 const border_rows: usize = 2; 1121 1122 pub const Options = struct { 1123 cell_width: usize = 40, 1124 cell_height: usize = 3, 1125 }; 1126 1127 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1128 return .{ 1129 .focus = try Focus.create(allocator, .container), 1130 .grid = null, 1131 .text_boxes = .empty, 1132 .arena = std.heap.ArenaAllocator.init(allocator), 1133 .last_cols = 1, 1134 .options = options, 1135 }; 1136 } 1137 1138 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1139 self.focus.destroy(allocator); 1140 if (self.grid) |*grid| { 1141 grid.deinit(); 1142 self.grid = null; 1143 } 1144 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1145 self.text_boxes.deinit(allocator); 1146 self.arena.deinit(); 1147 } 1148 1149 // one flow item: its display text plus an optional link. a non-empty `link` 1150 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1151 // the web renderer turns into an anchor. 1152 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1153 1154 // both the item text and the link are copied into the arena, so the caller's 1155 // slices needn't outlive this call. 1156 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1157 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1158 self.text_boxes.clearAndFree(allocator); 1159 1160 // the old text boxes (and their borrowed content/links) are gone now, so 1161 // the arena that backed those strings can be reclaimed. 1162 _ = self.arena.reset(.retain_capacity); 1163 const aa = self.arena.allocator(); 1164 1165 self.focus.clear(); 1166 self.focus.child_id = null; 1167 1168 for (items) |item| { 1169 const line = try aa.dupe(u8, item.text); 1170 1171 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1172 errdefer text_box.deinit(allocator); 1173 text_box.getFocus().focusable = true; 1174 1175 if (item.link.len > 0) { 1176 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1177 } 1178 1179 try self.text_boxes.append(allocator, text_box); 1180 } 1181 1182 if (self.text_boxes.items.len > 0) { 1183 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1184 } 1185 } 1186 1187 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1188 self.clearGrid(); 1189 self.focus.clear(); 1190 1191 const cell_width = self.options.cell_width; 1192 const max_width = constraint.max_size.width orelse cell_width; 1193 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1194 self.last_cols = cols; 1195 1196 const count = self.text_boxes.items.len; 1197 if (count == 0) return; 1198 const slot_height = self.options.cell_height + border_rows; 1199 if (slot_height == 0) return; 1200 1201 // build at the slot size so every text box fits a single grid cell 1202 for (self.text_boxes.items) |*tb| { 1203 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1204 try tb.build(allocator, .{ 1205 .min_size = .{ .width = cell_width, .height = null }, 1206 .max_size = .{ .width = cell_width, .height = slot_height }, 1207 }, root_focus); 1208 } 1209 1210 const rows = (count + cols - 1) / cols; 1211 const content_height = rows * slot_height; 1212 const total_width = cols * cell_width; 1213 if (total_width == 0 or content_height == 0) return; 1214 1215 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1216 errdefer grid.deinit(); 1217 1218 for (self.text_boxes.items, 0..) |*tb, i| { 1219 const tb_grid = tb.getGrid() orelse continue; 1220 const col = i % cols; 1221 const row = i / cols; 1222 const cell_x = col * cell_width; 1223 const cell_y = row * slot_height; 1224 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1225 try grid.drawGrid(tb_grid, cell_x, cell_y); 1226 } 1227 1228 self.grid = grid; 1229 } 1230 1231 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1232 _ = self; 1233 _ = allocator; 1234 _ = key; 1235 _ = root_focus; 1236 } 1237 1238 pub fn clearGrid(self: *FlowBox) void { 1239 if (self.grid) |*grid| { 1240 grid.deinit(); 1241 self.grid = null; 1242 } 1243 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1244 } 1245 1246 pub fn getGrid(self: FlowBox) ?Grid { 1247 return self.grid; 1248 } 1249 1250 pub fn getFocus(self: *FlowBox) *Focus { 1251 return self.focus; 1252 } 1253 1254 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1255 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1256 const slot_height = self.options.cell_height + border_rows; 1257 const col = index % self.last_cols; 1258 const row = index / self.last_cols; 1259 return .{ 1260 .x = @intCast(col * self.options.cell_width), 1261 .y = @intCast(row * slot_height), 1262 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1263 }; 1264 } 1265 1266 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1267 for (self.text_boxes.items, 0..) |tb, i| { 1268 if (tb.box.focus.id == focus_id) return i; 1269 } 1270 return null; 1271 } 1272 1273 pub const Scroll = struct { 1274 scroll: wgt.Scroll(Widget), 1275 1276 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1277 var layout_inner = try FlowBox.init(allocator, options); 1278 errdefer layout_inner.deinit(allocator); 1279 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1280 errdefer scroll.deinit(allocator); 1281 return .{ .scroll = scroll }; 1282 } 1283 1284 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1285 self.scroll.deinit(allocator); 1286 } 1287 1288 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1289 self.scroll.x = 0; 1290 self.scroll.y = 0; 1291 try self.inner().setItems(allocator, items); 1292 } 1293 1294 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1295 try self.scroll.build(allocator, constraint, root_focus); 1296 } 1297 1298 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1299 _ = allocator; 1300 const in = self.inner(); 1301 const child_id = in.focus.child_id orelse return; 1302 const current_index = in.indexOfFocusId(child_id) orelse return; 1303 const count = in.text_boxes.items.len; 1304 if (count == 0) return; 1305 const cols = in.last_cols; 1306 const slot_height = in.options.cell_height + FlowBox.border_rows; 1307 1308 var index = current_index; 1309 switch (key) { 1310 .arrow_up => index -|= cols, 1311 .arrow_down => if (index + cols < count) { 1312 index += cols; 1313 }, 1314 .arrow_left => index -|= 1, 1315 .arrow_right => if (index + 1 < count) { 1316 index += 1; 1317 }, 1318 .home => index = 0, 1319 .end => index = count - 1, 1320 .page_up => { 1321 if (self.scroll.grid) |grid| if (slot_height > 0) { 1322 const rows_per_page = grid.size.height / slot_height; 1323 index -|= rows_per_page * cols; 1324 }; 1325 }, 1326 .page_down => { 1327 if (self.scroll.grid) |grid| if (slot_height > 0) { 1328 const rows_per_page = grid.size.height / slot_height; 1329 index = @min(index + rows_per_page * cols, count - 1); 1330 }; 1331 }, 1332 // scroll wheel moves the focused cell by a full row so the 1333 // viewport (via scrollToRect) follows in row-sized steps, 1334 // matching how a scroll wheel feels in a grid view. 1335 .mouse => |mouse| switch (mouse.action) { 1336 .scroll => |dir| switch (dir) { 1337 .up => index -|= cols, 1338 .down => if (index + cols < count) { 1339 index += cols; 1340 }, 1341 }, 1342 else => {}, 1343 }, 1344 else => {}, 1345 } 1346 1347 if (index != current_index) { 1348 try root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1349 if (in.cellRect(index)) |rect| { 1350 self.scroll.scrollToRect(rect); 1351 } 1352 } 1353 } 1354 1355 pub fn clearGrid(self: *Scroll) void { 1356 self.scroll.clearGrid(); 1357 } 1358 1359 pub fn getGrid(self: Scroll) ?Grid { 1360 return self.scroll.getGrid(); 1361 } 1362 1363 pub fn getFocus(self: *Scroll) *Focus { 1364 return self.scroll.getFocus(); 1365 } 1366 1367 pub fn getSelectedIndex(self: Scroll) ?usize { 1368 const in = self.scroll.child.flow_box; 1369 const child_id = in.focus.child_id orelse return null; 1370 return in.indexOfFocusId(child_id); 1371 } 1372 1373 fn inner(self: *Scroll) *FlowBox { 1374 return &self.scroll.child.flow_box; 1375 } 1376 }; 1377 }; 1378 1379 // an invisible widget that fills the horizontal space granted by its parent. 1380 // used inside Box(horiz) with a min_size so the box reserves space for the 1381 // children that follow, pushing them to the right. 1382 pub const Spacer = struct { 1383 focus: *Focus, 1384 grid: ?Grid, 1385 1386 pub fn init(allocator: std.mem.Allocator) !Spacer { 1387 return .{ 1388 .focus = try Focus.create(allocator, .container), 1389 .grid = null, 1390 }; 1391 } 1392 1393 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1394 self.focus.destroy(allocator); 1395 if (self.grid) |*grid| { 1396 grid.deinit(); 1397 self.grid = null; 1398 } 1399 } 1400 1401 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1402 _ = root_focus; 1403 self.clearGrid(); 1404 const width = constraint.max_size.width orelse return; 1405 if (width == 0) return; 1406 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1407 } 1408 1409 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1410 _ = self; 1411 _ = allocator; 1412 _ = key; 1413 _ = root_focus; 1414 } 1415 1416 pub fn clearGrid(self: *Spacer) void { 1417 if (self.grid) |*grid| { 1418 grid.deinit(); 1419 self.grid = null; 1420 } 1421 } 1422 1423 pub fn getGrid(self: Spacer) ?Grid { 1424 return self.grid; 1425 } 1426 1427 pub fn getFocus(self: *Spacer) *Focus { 1428 return self.focus; 1429 } 1430 }; 1431 1432 // a one-row, non-focusable status bar showing the current page's url 1433 pub const Footer = struct { 1434 focus: *Focus, 1435 grid: ?Grid, 1436 session: *Session, 1437 arena: std.heap.ArenaAllocator, 1438 1439 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1440 return .{ 1441 .focus = try Focus.create(allocator, .container), 1442 .grid = null, 1443 .session = session, 1444 .arena = std.heap.ArenaAllocator.init(allocator), 1445 }; 1446 } 1447 1448 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1449 self.focus.destroy(allocator); 1450 if (self.grid) |*grid| grid.deinit(); 1451 self.arena.deinit(); 1452 } 1453 1454 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1455 _ = root_focus; 1456 self.clearGrid(); 1457 const width = constraint.max_size.width orelse return; 1458 if (width == 0) return; 1459 1460 _ = self.arena.reset(.retain_capacity); 1461 const aa = self.arena.allocator(); 1462 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1463 const text = if (self.session.web_port) |port| 1464 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1465 else 1466 path; 1467 1468 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1469 errdefer grid.deinit(); 1470 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1471 var i: usize = 0; 1472 while (utf8.nextCodepointSlice()) |ch| { 1473 if (i == width) break; 1474 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1475 i += 1; 1476 } 1477 self.grid = grid; 1478 } 1479 1480 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1481 _ = self; 1482 _ = allocator; 1483 _ = key; 1484 _ = root_focus; 1485 } 1486 1487 pub fn clearGrid(self: *Footer) void { 1488 if (self.grid) |*grid| { 1489 grid.deinit(); 1490 self.grid = null; 1491 } 1492 } 1493 1494 pub fn getGrid(self: Footer) ?Grid { 1495 return self.grid; 1496 } 1497 1498 pub fn getFocus(self: *Footer) *Focus { 1499 return self.focus; 1500 } 1501 }; 1502 1503 // a single-child wrapper that builds the child at its natural size and 1504 // positions its grid in the middle of the area granted by the parent 1505 pub const Center = struct { 1506 focus: *Focus, 1507 grid: ?Grid, 1508 child: *Widget, 1509 direction: Direction, 1510 1511 pub const Direction = enum { both, horiz, vert }; 1512 1513 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1514 const child = try allocator.create(Widget); 1515 errdefer allocator.destroy(child); 1516 child.* = child_widget; 1517 return .{ 1518 .focus = try Focus.create(allocator, .container), 1519 .grid = null, 1520 .child = child, 1521 .direction = direction, 1522 }; 1523 } 1524 1525 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1526 self.focus.destroy(allocator); 1527 if (self.grid) |*grid| { 1528 grid.deinit(); 1529 self.grid = null; 1530 } 1531 self.child.deinit(allocator); 1532 allocator.destroy(self.child); 1533 } 1534 1535 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1536 self.clearGrid(); 1537 self.getFocus().clear(); 1538 1539 // build the child without forcing it to fill min_size; it sizes 1540 // itself to its content within the available max. 1541 try self.child.build(allocator, .{ 1542 .min_size = .{ .width = null, .height = null }, 1543 .max_size = constraint.max_size, 1544 }, root_focus); 1545 1546 const child_grid = self.child.getGrid() orelse return; 1547 1548 // prefer max when given; otherwise grow to max(min, child) so that 1549 // when only a min is set (e.g. the wasm path passes viewport rows 1550 // as min_height with no max), we can still vertically center while 1551 // letting taller content extend past the min. 1552 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; 1553 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; 1554 if (width == 0 or height == 0) return; 1555 1556 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1557 (width -| child_grid.size.width) / 2 1558 else 1559 0; 1560 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1561 (height -| child_grid.size.height) / 2 1562 else 1563 0; 1564 1565 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1566 errdefer grid.deinit(); 1567 try grid.drawGrid(child_grid, offset_x, offset_y); 1568 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1569 self.getFocus().child_id = self.child.getFocus().id; 1570 1571 self.grid = grid; 1572 } 1573 1574 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1575 try self.child.input(allocator, key, root_focus); 1576 } 1577 1578 pub fn clearGrid(self: *Center) void { 1579 if (self.grid) |*grid| { 1580 grid.deinit(); 1581 self.grid = null; 1582 } 1583 self.child.clearGrid(); 1584 } 1585 1586 pub fn getGrid(self: Center) ?Grid { 1587 return self.grid; 1588 } 1589 1590 pub fn getFocus(self: *Center) *Focus { 1591 return self.focus; 1592 } 1593 }; 1594 1595 // renders truecolor ANSI art 1596 pub const AnsiArt = struct { 1597 focus: *Focus, 1598 grid: ?Grid, 1599 content: []const u8, 1600 1601 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1602 return .{ 1603 .focus = try Focus.create(allocator, .container), 1604 .grid = null, 1605 .content = content, 1606 }; 1607 } 1608 1609 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1610 self.focus.destroy(allocator); 1611 if (self.grid) |*grid| { 1612 grid.deinit(); 1613 self.grid = null; 1614 } 1615 } 1616 1617 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1618 _ = root_focus; 1619 self.clearGrid(); 1620 1621 // parse into rows of cells, then copy into a rectangular grid 1622 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1623 defer { 1624 for (rows.items) |*row| row.deinit(allocator); 1625 rows.deinit(allocator); 1626 } 1627 var row: std.ArrayList(Grid.Cell) = .empty; 1628 errdefer row.deinit(allocator); 1629 1630 var style: Grid.Style = .{}; 1631 var width: usize = 0; 1632 const content = self.content; 1633 var i: usize = 0; 1634 while (i < content.len) { 1635 const byte = content[i]; 1636 if (byte == '\n') { 1637 width = @max(width, row.items.len); 1638 try rows.append(allocator, row); 1639 row = .empty; 1640 i += 1; 1641 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1642 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1643 var j = i + 2; 1644 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1645 if (j >= content.len) { 1646 i = content.len; // malformed trailing escape; stop 1647 } else { 1648 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1649 i = j + 1; 1650 } 1651 } else { 1652 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1653 const end = @min(content.len, i + len); 1654 const rune = content[i..end]; 1655 const transparent = end - i == 1 and rune[0] == ' ' and 1656 style.fg == null and style.bg == null and !style.inverted; 1657 try row.append(allocator, .{ 1658 .rune = if (transparent) null else rune, 1659 .style = style, 1660 }); 1661 i = end; 1662 } 1663 } 1664 // a trailing row with content but no closing newline 1665 if (row.items.len > 0) { 1666 width = @max(width, row.items.len); 1667 try rows.append(allocator, row); 1668 } else { 1669 row.deinit(allocator); 1670 } 1671 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1672 1673 const height = rows.items.len; 1674 if (width == 0 or height == 0) return; 1675 1676 const clamped_w = @min(width, constraint.max_size.width orelse width); 1677 const clamped_h = @min(height, constraint.max_size.height orelse height); 1678 1679 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1680 errdefer grid.deinit(); 1681 for (rows.items[0..clamped_h], 0..) |r, y| { 1682 const n = @min(r.items.len, clamped_w); 1683 for (r.items[0..n], 0..) |cell, x| { 1684 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1685 } 1686 } 1687 self.grid = grid; 1688 } 1689 1690 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1691 _ = self; 1692 _ = allocator; 1693 _ = key; 1694 _ = root_focus; 1695 } 1696 1697 pub fn clearGrid(self: *AnsiArt) void { 1698 if (self.grid) |*grid| { 1699 grid.deinit(); 1700 self.grid = null; 1701 } 1702 } 1703 1704 pub fn getGrid(self: AnsiArt) ?Grid { 1705 return self.grid; 1706 } 1707 1708 pub fn getFocus(self: *AnsiArt) *Focus { 1709 return self.focus; 1710 } 1711 1712 fn applySgr(style: *Grid.Style, params: []const u8) void { 1713 var nums: [16]u32 = undefined; 1714 var n: usize = 0; 1715 var it = std.mem.splitScalar(u8, params, ';'); 1716 while (it.next()) |tok| { 1717 if (n >= nums.len) break; 1718 // an empty parameter (e.g. bare "\x1b[m") means 0 1719 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1720 n += 1; 1721 } 1722 if (n == 0) { 1723 style.* = .{}; 1724 return; 1725 } 1726 var i: usize = 0; 1727 while (i < n) : (i += 1) { 1728 switch (nums[i]) { 1729 0 => style.* = .{}, 1730 7 => style.inverted = true, 1731 27 => style.inverted = false, 1732 39 => style.fg = null, 1733 49 => style.bg = null, 1734 38, 48 => { 1735 // truecolor form: 38;2;r;g;b — anything else is ignored 1736 if (i + 4 < n and nums[i + 1] == 2) { 1737 const c = Grid.Color{ 1738 .r = @truncate(nums[i + 2]), 1739 .g = @truncate(nums[i + 3]), 1740 .b = @truncate(nums[i + 4]), 1741 }; 1742 if (nums[i] == 38) style.fg = c else style.bg = c; 1743 i += 4; 1744 } 1745 }, 1746 else => {}, 1747 } 1748 } 1749 } 1750 }; 1751 1752 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1753 pub const AnsiBackground = struct { 1754 grid: ?Grid, 1755 child: *Widget, 1756 art: AnsiArt, 1757 session: *Session, 1758 1759 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1760 var cw = child_widget; 1761 const child = allocator.create(Widget) catch |e| { 1762 cw.deinit(allocator); 1763 return e; 1764 }; 1765 child.* = cw; 1766 errdefer { 1767 child.deinit(allocator); 1768 allocator.destroy(child); 1769 } 1770 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1771 } 1772 1773 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1774 if (self.grid) |*grid| grid.deinit(); 1775 self.art.deinit(allocator); 1776 self.child.deinit(allocator); 1777 allocator.destroy(self.child); 1778 } 1779 1780 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1781 self.clearGrid(); 1782 1783 // make the wrapped page fill the whole available area 1784 try self.child.build(allocator, .{ 1785 .min_size = .{ 1786 .width = constraint.max_size.width orelse constraint.min_size.width, 1787 .height = constraint.max_size.height orelse constraint.min_size.height, 1788 }, 1789 .max_size = constraint.max_size, 1790 }, root_focus); 1791 1792 if (self.session.data.enable_ansi) { 1793 if (self.child.getGrid()) |fg| { 1794 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1795 } 1796 } 1797 } 1798 1799 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1800 try self.child.input(allocator, key, root_focus); 1801 } 1802 1803 pub fn clearGrid(self: *AnsiBackground) void { 1804 if (self.grid) |*grid| { 1805 grid.deinit(); 1806 self.grid = null; 1807 } 1808 self.child.clearGrid(); 1809 self.art.clearGrid(); 1810 } 1811 1812 pub fn getGrid(self: AnsiBackground) ?Grid { 1813 return self.grid orelse self.child.getGrid(); 1814 } 1815 1816 pub fn getFocus(self: *AnsiBackground) *Focus { 1817 return self.child.getFocus(); 1818 } 1819 1820 // which top corner the art hugs. 1821 const ArtAnchor = enum { top_left, top_right }; 1822 1823 // a foreground cell counts as blank if it's empty or a bare space without styling 1824 fn cellIsBlank(cell: Grid.Cell) bool { 1825 if (cell.rune) |rune| { 1826 return std.mem.eql(u8, rune, " ") and 1827 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1828 } 1829 return true; 1830 } 1831 1832 // the art is dimmed to this fraction of its brightness so foreground text 1833 // stays legible over it 1834 const art_brightness = 45; // percent 1835 1836 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1837 const c = color orelse return null; 1838 return .{ 1839 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1840 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1841 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1842 }; 1843 } 1844 1845 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1846 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1847 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1848 // snap such channels clear of that range so the backdrop doesn't blink or 1849 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1850 // actually render truecolor. 1851 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1852 const c = color orelse return null; 1853 const snap = struct { 1854 fn f(v: u8) u8 { 1855 return if (v >= 1 and v <= 9) 10 else v; 1856 } 1857 }.f; 1858 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1859 } 1860 1861 // composites ANSI art behind a foreground grid 1862 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1863 art.clearGrid(); 1864 try art.build(allocator, .{ 1865 .min_size = .{ .width = null, .height = null }, 1866 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1867 }, root_focus); 1868 1869 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1870 errdefer out.deinit(); 1871 1872 if (art.getGrid()) |art_grid| { 1873 const anchor_x = switch (anchor) { 1874 .top_left => 0, 1875 .top_right => foreground.size.width -| art_grid.size.width, 1876 }; 1877 for (0..art_grid.size.height) |y| { 1878 for (0..art_grid.size.width) |x| { 1879 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1880 if (src.rune == null) continue; 1881 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1882 const dst = &out.cells.items[idx]; 1883 // composite the dimmed art behind every cell so it shows 1884 // through the whole UI: a blank cell becomes the art, while a 1885 // cell with a glyph keeps the glyph but takes the dimmed art as 1886 // its background (replacing whatever background it had, which 1887 // would otherwise obscure the art). 1888 if (cellIsBlank(dst.*)) { 1889 dst.* = src; 1890 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1891 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1892 } else { 1893 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1894 } 1895 } 1896 } 1897 } 1898 return out; 1899 } 1900 }; 1901