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