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