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