╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1617 .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, 1618 .user_forks => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_forks.name.slice()) and a_u.start == b.user_forks.start, 1619 .user_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.user_settings.slice()), 1620 .user_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.user_auth.slice()), 1621 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 1622 a_f.ref_kind == b.repo_files.ref_kind and 1623 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 1624 std.mem.eql(u8, a_f.patchrev_id.slice(), b.repo_files.patchrev_id.slice()) and 1625 std.mem.eql(u8, a_f.find.slice(), b.repo_files.find.slice()) and 1626 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 1627 a_f.line == b.repo_files.line, 1628 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 1629 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 1630 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 1631 std.mem.eql(u8, a_c.base_oid.slice(), b.repo_commits.base_oid.slice()) and 1632 std.mem.eql(u8, a_c.search.slice(), b.repo_commits.search.slice()) and 1633 std.mem.eql(u8, a_c.from.slice(), b.repo_commits.from.slice()) and 1634 switch (a_c.content) { 1635 .diff => |a_d| switch (b.repo_commits.content) { 1636 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1637 .message => false, 1638 }, 1639 .message => std.meta.activeTag(b.repo_commits.content) == .message, 1640 }, 1641 .repo_diff => |a_d| std.mem.eql(u8, a_d.name.slice(), b.repo_diff.name.slice()) and 1642 a_d.ref_or_oid == b.repo_diff.ref_or_oid and 1643 std.mem.eql(u8, a_d.value.slice(), b.repo_diff.value.slice()) and 1644 std.mem.eql(u8, a_d.base_oid.slice(), b.repo_diff.base_oid.slice()) and 1645 std.mem.eql(u8, a_d.patchrev_id.slice(), b.repo_diff.patchrev_id.slice()) and 1646 a_d.start == b.repo_diff.start and std.mem.eql(u8, a_d.path.slice(), b.repo_diff.path.slice()), 1647 .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()), 1648 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 1649 std.mem.eql(u8, a_i.tag.slice(), b.repo_issues.tag.slice()) and 1650 std.mem.eql(u8, a_i.search.slice(), b.repo_issues.search.slice()) and 1651 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 1652 std.mem.eql(u8, a_i.theirs.slice(), b.repo_issues.theirs.slice()) and 1653 a_i.view == b.repo_issues.view and 1654 a_i.comments_start == b.repo_issues.comments_start and 1655 std.mem.eql(u8, a_i.comment.slice(), b.repo_issues.comment.slice()), 1656 .repo_discussions => |a_t| std.mem.eql(u8, a_t.name.slice(), b.repo_discussions.name.slice()) and 1657 std.mem.eql(u8, a_t.tag.slice(), b.repo_discussions.tag.slice()) and 1658 std.mem.eql(u8, a_t.search.slice(), b.repo_discussions.search.slice()) and 1659 std.mem.eql(u8, a_t.selected.slice(), b.repo_discussions.selected.slice()) and 1660 a_t.view == b.repo_discussions.view and 1661 a_t.comments_start == b.repo_discussions.comments_start and 1662 std.mem.eql(u8, a_t.comment.slice(), b.repo_discussions.comment.slice()), 1663 .repo_patches => |a_p| std.mem.eql(u8, a_p.name.slice(), b.repo_patches.name.slice()) and 1664 std.mem.eql(u8, a_p.tag.slice(), b.repo_patches.tag.slice()) and 1665 std.mem.eql(u8, a_p.search.slice(), b.repo_patches.search.slice()) and 1666 std.mem.eql(u8, a_p.selected.slice(), b.repo_patches.selected.slice()) and 1667 std.mem.eql(u8, a_p.theirs.slice(), b.repo_patches.theirs.slice()) and 1668 a_p.view == b.repo_patches.view and 1669 a_p.comments_start == b.repo_patches.comments_start and 1670 std.mem.eql(u8, a_p.comment.slice(), b.repo_patches.comment.slice()), 1671 .repo_events => |a_e| std.mem.eql(u8, a_e.name.slice(), b.repo_events.name.slice()) and 1672 a_e.view == b.repo_events.view and 1673 a_e.kind == b.repo_events.kind and 1674 std.mem.eql(u8, a_e.selected.slice(), b.repo_events.selected.slice()), 1675 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 1676 .repo_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_auth.slice()), 1677 .fork_patch => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_patch.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_patch.id.slice()), 1678 .fork_diff => |a_d| std.mem.eql(u8, a_d.fork.name.slice(), b.fork_diff.fork.name.slice()) and 1679 std.mem.eql(u8, a_d.fork.id.slice(), b.fork_diff.fork.id.slice()) and 1680 a_d.start == b.fork_diff.start and std.mem.eql(u8, a_d.path.slice(), b.fork_diff.path.slice()), 1681 .fork_files => |a_f| std.mem.eql(u8, a_f.fork.name.slice(), b.fork_files.fork.name.slice()) and 1682 std.mem.eql(u8, a_f.fork.id.slice(), b.fork_files.fork.id.slice()) and 1683 std.mem.eql(u8, a_f.oid.slice(), b.fork_files.oid.slice()) and 1684 std.mem.eql(u8, a_f.find.slice(), b.fork_files.find.slice()) and 1685 std.mem.eql(u8, a_f.path.slice(), b.fork_files.path.slice()) and 1686 a_f.line == b.fork_files.line, 1687 .fork_commits => |a_c| std.mem.eql(u8, a_c.fork.name.slice(), b.fork_commits.fork.name.slice()) and 1688 std.mem.eql(u8, a_c.fork.id.slice(), b.fork_commits.fork.id.slice()) and 1689 std.mem.eql(u8, a_c.oid.slice(), b.fork_commits.oid.slice()) and 1690 switch (a_c.content) { 1691 .diff => |a_d| switch (b.fork_commits.content) { 1692 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1693 .message => false, 1694 }, 1695 .message => std.meta.activeTag(b.fork_commits.content) == .message, 1696 }, 1697 .fork_settings => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_settings.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_settings.id.slice()), 1698 .fork_auth => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_auth.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_auth.id.slice()), 1699 else => true, 1700 }; 1701 } 1702 1703 // true when following a link from `b` to `a` (both repo routes) should reload 1704 // rather than stay in page 1705 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 1706 if (a.parent() != .repo or b.parent() != .repo) return false; 1707 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 1708 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 1709 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 1710 return !a.eql(b); 1711 } 1712 1713 pub fn forkPageChanged(a: RoutablePage, b: RoutablePage) bool { 1714 return a.parent() == .fork and b.parent() == .fork and !a.eql(b); 1715 } 1716 1717 // true when `a` and `b` are the same user paginated to a different repos 1718 // window. switching between a user's tabs is in-page, so 1719 // only a changed `start` on the repos list navigates. 1720 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 1721 return switch (a) { 1722 .user_repos => |aa| switch (b) { 1723 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1724 else => false, 1725 }, 1726 .user_forks => |aa| switch (b) { 1727 .user_forks => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1728 else => false, 1729 }, 1730 else => false, 1731 }; 1732 } 1733 1734 // true when `a` and `b` are the same home list tab paginated to a different 1735 // window. switching between the users/repos tabs is in-page (the home page 1736 // holds both lists), so only a changed `after` on the same tab navigates. 1737 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 1738 return switch (a) { 1739 .home_users => |aa| switch (b) { 1740 .home_users => |bb| aa != bb, 1741 else => false, 1742 }, 1743 .home_repos => |aa| switch (b) { 1744 .home_repos => |bb| aa != bb, 1745 else => false, 1746 }, 1747 else => false, 1748 }; 1749 } 1750 }; 1751 1752 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 1753 fn isUnreserved(c: u8) bool { 1754 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 1755 } 1756 1757 // percent-encode a ref name for use as a single url path segment. 1758 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 1759 var out: std.Io.Writer.Allocating = .init(aa); 1760 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 1761 return out.written(); 1762 } 1763 1764 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 1765 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1766 return struct { 1767 pub const hex_len = hash.hexLen(repo_opts.hash); 1768 1769 // the concrete ref/oid (a null request resolves to the default branch) 1770 ref_or_oid: RoutablePage.RefOrOid, 1771 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 1772 // the route layer — which splits on '/' — stores them encoded). duped 1773 // into `aa`. 1774 value: []const u8, 1775 // the commit oid it points at 1776 oid: [hex_len]u8, 1777 1778 const Self = @This(); 1779 1780 // resolve a requested ref/oid (null = the repo's default branch) to a 1781 // concrete ref/oid plus the commit oid it points at. `requested_value` is 1782 // the url-encoded form (as it appears in the route). null when it doesn't 1783 // resolve (an unknown branch/tag, or a malformed/unknown oid). 1784 pub fn init( 1785 repo: *rp.Repo(repo_kind, repo_opts), 1786 io: std.Io, 1787 aa: std.mem.Allocator, 1788 requested_ref_or_oid: ?RoutablePage.RefOrOid, 1789 requested_value: []const u8, 1790 ) !?Self { 1791 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 1792 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 1793 var value: []const u8 = if (requested_ref_or_oid == null) 1794 requested_value 1795 else 1796 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 1797 // no ref named: fall back to HEAD's branch (or its oid when detached). 1798 if (requested_ref_or_oid == null) { 1799 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1800 if (repo.head(io, &head_buf)) |head| switch (head) { 1801 .ref => |r| { 1802 ref_or_oid = .branch; 1803 value = r.name; 1804 }, 1805 .oid => |o| { 1806 ref_or_oid = .object; 1807 value = o; 1808 }, 1809 } else |_| {} 1810 } 1811 1812 var oid: [hex_len]u8 = undefined; 1813 switch (ref_or_oid) { 1814 .object => { 1815 if (value.len != hex_len) return null; 1816 @memcpy(&oid, value); 1817 }, 1818 .branch, .tag => { 1819 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 1820 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 1821 }, 1822 } 1823 // store url-encoded so the route layer can hold the value verbatim. 1824 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 1825 } 1826 }; 1827 } 1828 1829 // where a repo page reads its on-disk repo from 1830 pub const RepoSource = struct { 1831 path: []const u8, 1832 repo_kind: rp.RepoKind, 1833 // the user's global git config, so local mode picks up their identity and 1834 // their signing and ssh settings. null on the server paths, which must not 1835 // read whoever runs the server. 1836 global_config_path: ?[]const u8 = null, 1837 1838 pub fn localInitOpts(self: RepoSource) rp.InitOpts { 1839 return .{ .path = self.path, .global_config_path = self.global_config_path }; 1840 } 1841 1842 pub fn hasBranch(self: RepoSource, io: std.Io, allocator: std.mem.Allocator, branch: []const u8) !bool { 1843 if (!xit.ref.validateName(branch)) return false; 1844 return switch (self.repo_kind) { 1845 inline else => |repo_kind| blk: { 1846 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 1847 defer any_repo.deinit(io, allocator); 1848 break :blk switch (any_repo) { 1849 inline else => |*repo| (try repo.readRef(io, .{ .kind = .head, .name = branch })) != null, 1850 }; 1851 }, 1852 }; 1853 } 1854 1855 // whether the actor may change the event `id` names 1856 pub fn canModify( 1857 self: RepoSource, 1858 io: std.Io, 1859 allocator: std.mem.Allocator, 1860 actor: Actor, 1861 kind: evt.EventKind, 1862 id: *const [evt.event_id_size]u8, 1863 ) !bool { 1864 // a role that may change an event with no author needs no read 1865 if (actor.role.canModify(actor.author.email, null)) return true; 1866 1867 var arena = std.heap.ArenaAllocator.init(allocator); 1868 defer arena.deinit(); 1869 return switch (self.repo_kind) { 1870 inline else => |repo_kind| blk: { 1871 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 1872 defer any_repo.deinit(io, allocator); 1873 break :blk switch (any_repo) { 1874 inline else => |*repo| actor.role.canModify(actor.author.email, try evt.readAuthorEmail(repo_kind, repo.self_repo_opts, io, allocator, &arena, repo, kind, id)), 1875 }; 1876 }, 1877 }; 1878 } 1879 }; 1880 1881 // who a write acts as and the role they hold in its repo 1882 pub const Actor = struct { 1883 author: evt.CommitAuthor, 1884 // local mode has no accounts, so it has no user id 1885 user_id: ?[evt.event_id_size]u8 = null, 1886 role: evt.Repo.Role, 1887 }; 1888 1889 pub const Authorization = union(enum) { 1890 actor: Actor, 1891 login_required, 1892 repo_not_found, 1893 forbidden, 1894 }; 1895 1896 // the account `user_id` names, or null once it has been removed. a user id is 1897 // checked here wherever one enters, so the role lookups can trust theirs. 1898 pub fn activeUser( 1899 moment: evt.AdminDB.HashMap(.read_only), 1900 arena: *std.heap.ArenaAllocator, 1901 user_id: [evt.event_id_size]u8, 1902 ) !?evt.User.Record { 1903 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id)) orelse return null; 1904 return if (user.removed) null else user; 1905 } 1906 1907 // the role the user holds in the repo `identity` names, or null when no repo 1908 // has that identity. a null user is logged out. 1909 pub fn repoRole( 1910 moment: evt.AdminDB.HashMap(.read_only), 1911 arena: *std.heap.ArenaAllocator, 1912 user_id: ?[evt.event_id_size]u8, 1913 identity: []const u8, 1914 ) !?evt.Repo.Role { 1915 const owner_repo = evt.parseOwnerRepoPath(identity) orelse return null; 1916 const repo = (try evt.Repo.readByOwnerAndName(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, owner_repo.owner, owner_repo.name)) orelse return null; 1917 return evt.Repo.roleOf(repo.repo, user_id); 1918 } 1919 1920 // whether the user may write to the repo `identity` names with at least 1921 // `min_role` 1922 pub fn authorizeUser( 1923 moment: evt.AdminDB.HashMap(.read_only), 1924 arena: *std.heap.ArenaAllocator, 1925 user_id: [evt.event_id_size]u8, 1926 identity: []const u8, 1927 min_role: evt.Repo.Role, 1928 ) !Authorization { 1929 const user = (try activeUser(moment, arena, user_id)) orelse return .login_required; 1930 1931 const role = (try repoRole(moment, arena, user_id, identity)) orelse return .repo_not_found; 1932 // a repo the user can't read doesn't exist to them 1933 if (role == .none) return .repo_not_found; 1934 if (!role.atLeast(min_role)) return .forbidden; 1935 return .{ .actor = .{ 1936 .author = .{ .name = user.event.name, .email = user.event.email }, 1937 .user_id = user_id, 1938 .role = role, 1939 } }; 1940 } 1941 1942 // events created in local mode have no account behind them, so the commit 1943 // author comes from the repo's config, which the global config feeds into. 1944 pub const local_author_fallback = evt.CommitAuthor{ .name = "haxy", .email = "user@haxy" }; 1945 1946 pub fn localAuthor( 1947 src: RepoSource, 1948 io: std.Io, 1949 allocator: std.mem.Allocator, 1950 arena: *std.heap.ArenaAllocator, 1951 ) !evt.CommitAuthor { 1952 switch (src.repo_kind) { 1953 inline else => |repo_kind| { 1954 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()) catch return local_author_fallback; 1955 defer any_repo.deinit(io, allocator); 1956 1957 switch (any_repo) { 1958 inline else => |*repo| { 1959 var config = try repo.listConfig(io, allocator); 1960 defer config.deinit(); 1961 1962 const user = config.sections.get("user") orelse return local_author_fallback; 1963 const name = user.get("name") orelse return local_author_fallback; 1964 const email = user.get("email") orelse return local_author_fallback; 1965 return .{ 1966 .name = try arena.allocator().dupe(u8, name), 1967 .email = try arena.allocator().dupe(u8, email), 1968 }; 1969 }, 1970 } 1971 }, 1972 } 1973 } 1974 1975 // per-connection mutable state. each SSH session / web session / local TUI 1976 // run gets its own. 1977 pub const Session = struct { 1978 data: Data = .{}, 1979 // session-lifetime allocations: login/user_id, persisted prefs, queued 1980 // actions. lives as long as the connection. 1981 arena: *std.heap.ArenaAllocator, 1982 // current page's allocations: page data and the page-scoped strings widgets 1983 // build (form actions, links). owned by Nav, swapped on each navigation, so 1984 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 1985 // where a render is one-shot, this points at the same arena as `arena`. 1986 page_arena: *std.heap.ArenaAllocator, 1987 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 1988 // filesystem io and the path to <server>/repos, for opening on-disk repos 1989 // during page construction. both null on wasm, which has no filesystem and 1990 // rebuilds pages from the serialized snapshot rather than from disk. 1991 io: ?std.Io = null, 1992 admin_repo: ?*rp.Repo(.xit, evt.admin_repo_opts) = null, 1993 repos_dir: ?[]const u8 = null, 1994 // the single on-disk repo this session views, when running in local mode 1995 // (haxy invoked with no arguments inside a repo). null on the server paths, 1996 // which resolve repos from the admin db instead. 1997 local: ?RepoSource = null, 1998 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 1999 // focus id -> the live TextInput, refreshed each frame by the views that own 2000 // inputs. web/wasm form handling looks widgets up here by focus id. 2001 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput) = .empty, 2002 back: enum { unavailable, available, requested } = .unavailable, 2003 refresh_requested: bool = false, // set by input (ctrl+r) 2004 // a requested forward navigation. set this (via navigate) to move to a new 2005 // page; Nav.sync builds it and then copies it into current_page, clearing 2006 // this back to null. setting current_page directly only updates the url and 2007 // does not navigate. 2008 next_page: ?RoutablePage = null, 2009 is_terminal: bool = false, // true on remote SSH and local TUI 2010 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 2011 // url. null on the web itself (no footer there). 2012 web_port: ?u16 = null, 2013 // a host operation requested by a widget 2014 host_request: ?HostRequest = null, 2015 quit_requested: bool = false, 2016 2017 pub const HostRequest = union(enum) { 2018 show_copyable_text: []const u8, 2019 sync_events, 2020 }; 2021 2022 pub const FormFeedback = union(enum) { 2023 pub const LoginFailure = enum { unknown_user, wrong_password }; 2024 pub const ThreadFailure = enum { required_title }; 2025 pub const PatchFailure = enum { 2026 required_title, 2027 invalid_target_branch, 2028 invalid_source_branch, 2029 same_branch, 2030 unrelated_branches, 2031 2032 pub fn fromError(err: anyerror) ?PatchFailure { 2033 return switch (err) { 2034 error.InvalidTargetBranch => .invalid_target_branch, 2035 error.InvalidSourceBranch => .invalid_source_branch, 2036 error.SameBranch => .same_branch, 2037 error.UnrelatedBranches => .unrelated_branches, 2038 else => null, 2039 }; 2040 } 2041 }; 2042 2043 login: struct { 2044 failure: LoginFailure, 2045 username: []const u8, 2046 }, 2047 issue: struct { 2048 failure: ThreadFailure, 2049 fields: ?struct { 2050 title: []const u8, 2051 tags: []const u8, 2052 description: []const u8, 2053 } = null, 2054 }, 2055 patch: struct { 2056 failure: PatchFailure, 2057 fields: ?struct { 2058 title: []const u8, 2059 tags: []const u8, 2060 description: []const u8, 2061 target_branch: []const u8, 2062 source_branch: []const u8 = "", 2063 source_kind: Repo.Patches.Source.Kind = .fork, 2064 } = null, 2065 }, 2066 discussion: struct { 2067 failure: ThreadFailure, 2068 fields: ?struct { 2069 title: []const u8, 2070 tags: []const u8, 2071 description: []const u8, 2072 } = null, 2073 }, 2074 }; 2075 2076 const Self = @This(); 2077 2078 // serializable data sent down to web client 2079 pub const Data = struct { 2080 user_id: ?[]const u8 = null, 2081 // the logged-in user's name, for the views that show it 2082 user_name: ?[]const u8 = null, 2083 // a failed form retained long enough to render it for correction 2084 form_feedback: ?FormFeedback = null, 2085 // a transient local event-sync error 2086 sync_failure: ?[]const u8 = null, 2087 current_page: RoutablePage = .default, 2088 // whether to render the ANSI art backdrop 2089 enable_ansi: bool = true, 2090 // the selected ANSI art. the server serializes one image for WASM so 2091 // the browser does not need the whole build-generated collection. 2092 ansi_art: []const u8 = "", 2093 // the bound git-service ports. absent in local mode, where remote urls 2094 // are not shown. 2095 git_http_port: ?u16 = null, 2096 git_ssh_port: ?u16 = null, 2097 git_ssh_prefix: []const u8 = "", 2098 // whether this session views a local repo or the server. unlike `local` 2099 // (the host-side filesystem source) this travels in the snapshot, so 2100 // the wasm side also parses elided link urls and hides the multi-user 2101 // chrome. 2102 host_kind: evt.HostKind = .server, 2103 }; 2104 2105 // a user-initiated state change. widgets enqueue these on the session during 2106 // input instead of mutating state or touching the DB themselves; the host 2107 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 2108 // side effects out of the widget tree and gives every render path one place to 2109 // turn a UI action into a state change + event. 2110 pub const Action = union(enum) { 2111 toggle_ansi, 2112 }; 2113 2114 pub fn init( 2115 arena: *std.heap.ArenaAllocator, 2116 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2117 data: Data, 2118 ) !Self { 2119 var session = Self{ 2120 .data = data, 2121 .arena = arena, 2122 // until a host swaps in a page-scoped arena (Nav does this), page 2123 // allocations land in the session arena. on the web path that's the 2124 // intended behavior, since the whole arena is per-request. 2125 .page_arena = arena, 2126 .admin_repo = repo, 2127 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 2128 }; 2129 try session.loadUser(); 2130 return session; 2131 } 2132 2133 // load the logged-in user's name and persisted preferences from the db 2134 pub fn loadUser(self: *Self) !void { 2135 const user_id = self.userId() orelse return; 2136 const moment = self.haxy_moment orelse return; 2137 const user = (try activeUser(moment, self.arena, user_id)) orelse return self.logOut(); 2138 self.data.user_name = user.event.name; 2139 self.data.enable_ansi = user.event.enable_ansi; 2140 } 2141 2142 // forget the logged-in user, on a logout or once their account is removed 2143 pub fn logOut(self: *Self) void { 2144 self.data.user_id = null; 2145 self.data.user_name = null; 2146 } 2147 2148 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 2149 const bytes = self.data.user_id orelse return null; 2150 if (bytes.len != evt.event_id_size) return null; 2151 var id: [evt.event_id_size]u8 = undefined; 2152 @memcpy(&id, bytes); 2153 return id; 2154 } 2155 2156 pub fn formFeedback(self: *const Self, comptime tag: std.meta.Tag(FormFeedback)) ?@FieldType(FormFeedback, @tagName(tag)) { 2157 const feedback = self.data.form_feedback orelse return null; 2158 if (std.meta.activeTag(feedback) != tag) return null; 2159 return @field(feedback, @tagName(tag)); 2160 } 2161 2162 pub fn clearFormFeedback(self: *Self) void { 2163 self.data.form_feedback = null; 2164 } 2165 2166 // the actor for a write to the repo `identity` names, or null when the 2167 // session may not write there with at least `min_role`. local mode always 2168 // writes. 2169 pub fn authorize(self: *Self, identity: []const u8, min_role: evt.Repo.Role) !?Actor { 2170 if (self.data.host_kind == .local) { 2171 const author = blk: { 2172 const src = self.local orelse break :blk local_author_fallback; 2173 const io = self.io orelse break :blk local_author_fallback; 2174 break :blk try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 2175 }; 2176 return .{ .author = author, .role = .owner }; 2177 } 2178 const user_id = self.userId() orelse return null; 2179 // the session's moment predates this input, so read the roles afresh 2180 const admin_repo = self.admin_repo orelse return null; 2181 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2182 return switch (try authorizeUser(moment, self.page_arena, user_id, identity, min_role)) { 2183 .actor => |actor| actor, 2184 .login_required, .repo_not_found, .forbidden => null, 2185 }; 2186 } 2187 2188 // queue an action for the host to drain this frame. 2189 pub fn push(self: *Self, action: Action) !void { 2190 try self.pending.append(self.arena.allocator(), action); 2191 } 2192 2193 // apply a single action's in-memory effect only (no persistence). 2194 fn apply(self: *Self, action: Action) void { 2195 switch (action) { 2196 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 2197 } 2198 } 2199 2200 // drain the session's queued actions by applying them to the session. 2201 // used on the wasm path, which has no repo to persist to. 2202 pub fn applyPending(self: *Self) void { 2203 for (self.pending.items) |action| self.apply(action); 2204 self.pending.clearRetainingCapacity(); 2205 } 2206 2207 // drain the session's queued actions by applying them to the session and 2208 // writing them to the db 2209 pub fn applyAndWritePending( 2210 self: *Self, 2211 io: std.Io, 2212 allocator: std.mem.Allocator, 2213 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2214 ) !void { 2215 defer self.pending.clearRetainingCapacity(); 2216 for (self.pending.items) |action| { 2217 self.apply(action); 2218 switch (action) { 2219 .toggle_ansi => if (self.data.user_id) |user_id| { 2220 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 2221 }, 2222 } 2223 } 2224 } 2225 2226 // reload the moment from the admin repo and re-read user preferences 2227 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 2228 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 2229 self.haxy_moment = moment; 2230 2231 const user_id = self.userId() orelse return; 2232 var arena = std.heap.ArenaAllocator.init(allocator); 2233 defer arena.deinit(); 2234 const user = (try activeUser(moment, &arena, user_id)) orelse return self.logOut(); 2235 self.data.enable_ansi = user.event.enable_ansi; 2236 } 2237 2238 // request a forward navigation to `route`; the host consumes next_page 2239 // (Nav.sync on the terminal, the wasm tick on the web). 2240 pub fn navigate(self: *Session, route: RoutablePage) !void { 2241 self.clearFormFeedback(); 2242 self.next_page = route; 2243 } 2244 }; 2245 2246 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2247 var nav = try Nav.init(allocator, session); 2248 defer nav.deinit(allocator); 2249 2250 var terminal = try term.Terminal.init(io, allocator); 2251 var terminal_live = true; 2252 defer if (terminal_live) terminal.deinit(io); 2253 2254 // set term as active so it will be properly cooked 2255 // when a panic/segfault happens 2256 term.setActive(&terminal); 2257 defer term.setActive(null); 2258 2259 while (!terminal.shouldQuit()) { 2260 const grid_changed = try terminal.render(&nav.root); 2261 2262 // process any inputs. 2263 // 2264 // if the grid didn't change, then first do a blocking 2265 // read, so the thread will sleep until further input. 2266 // after that, all remaining reads are non-blocking so 2267 // we can process the rest of the queued inputs. 2268 // 2269 // if the grid *did* change, then only do non-blocking 2270 // reads. we do not want to sleep the thread because 2271 // there may be an animation that requires more looping. 2272 var blocking = !grid_changed; 2273 while (try terminal.readKey(io, blocking)) |key| { 2274 blocking = false; 2275 try inputKey(allocator, &nav.root, key, session); 2276 } 2277 2278 if (session.host_request) |request| { 2279 session.host_request = null; 2280 switch (request) { 2281 .show_copyable_text => |copyable_text| { 2282 term.setActive(null); 2283 terminal.deinit(io); 2284 terminal_live = false; 2285 showCopyableText(io, copyable_text) catch |show_err| { 2286 terminal = try term.Terminal.init(io, allocator); 2287 terminal_live = true; 2288 term.setActive(&terminal); 2289 return show_err; 2290 }; 2291 terminal = try term.Terminal.init(io, allocator); 2292 terminal_live = true; 2293 term.setActive(&terminal); 2294 }, 2295 .sync_events => { 2296 try nav.root.build(allocator, .{ 2297 .min_size = .{ .width = null, .height = null }, 2298 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2299 }, nav.root.getFocus()); 2300 _ = try terminal.render(&nav.root); 2301 try Repo.Events.performSync(allocator, session); 2302 }, 2303 } 2304 } 2305 2306 // persist queued actions when there's a repo; local mode has none, 2307 // so just apply in-memory 2308 if (repo_maybe) |repo| { 2309 try session.applyAndWritePending(io, allocator, repo); 2310 // reload so the next navigation builds its page from the current moment 2311 try session.reloadMoment(allocator, repo); 2312 } else { 2313 session.applyPending(); 2314 } 2315 2316 // reconcile navigation: forward to a new page, or back on escape. 2317 try nav.sync(allocator, session); 2318 2319 // the quit button (on the quit tab) asks the host to tear down. 2320 if (session.quit_requested) terminal.requestQuit(); 2321 2322 try nav.root.build(allocator, .{ 2323 .min_size = .{ .width = null, .height = null }, 2324 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2325 }, nav.root.getFocus()); 2326 } 2327 } 2328 2329 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2330 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2331 null 2332 else 2333 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2334 defer if (tty) |file| file.close(io); 2335 2336 const input = if (tty) |file| file else std.Io.File.stdin(); 2337 const output = if (tty) |file| file else std.Io.File.stdout(); 2338 2339 var write_buf: [1024]u8 = undefined; 2340 var writer = output.writer(io, &write_buf); 2341 try writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 2342 try writer.interface.flush(); 2343 2344 var read_buf: [1]u8 = undefined; 2345 var reader = input.reader(io, &read_buf); 2346 while (true) { 2347 const byte = try reader.interface.takeByte(); 2348 if (byte == '\r' or byte == '\n') return; 2349 } 2350 } 2351 2352 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2353 const root_focus = root.getFocus(); 2354 const focused_before = root_focus.grandchild_id; 2355 var activate_in_page_link = false; 2356 switch (key) { 2357 // request a navigation pop; the host's Nav.sync goes back a page, or 2358 // quits when there's no history left. 2359 .escape => { 2360 session.back = .requested; 2361 return; 2362 }, 2363 .ctrl => |letter| switch (letter) { 2364 'r' => { 2365 session.refresh_requested = true; 2366 return; 2367 }, 2368 else => try root.input(allocator, key, root_focus), 2369 }, 2370 .enter => { 2371 if (root_focus.grandchild_id) |gid| { 2372 // follow a cross-page link 2373 if (crossPageLink(root_focus, gid, session.data)) |route| { 2374 return session.navigate(route); 2375 } 2376 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2377 } 2378 activate_in_page_link = true; 2379 try root.input(allocator, key, root_focus); 2380 }, 2381 .mouse => |mouse| { 2382 if (mouse.action == .press and mouse.action.press == .left) { 2383 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2384 const focus_id = hit.id; 2385 if (hit.kind == .background) { 2386 root_focus.setFocus(focus_id); 2387 return; 2388 } 2389 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2390 if (session.back == .available) session.back = .requested; 2391 return; 2392 } 2393 // follow a cross-page link 2394 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2395 return session.navigate(route); 2396 } 2397 if (rawLink(root_focus, focus_id)) |url| { 2398 if (!mouse.ctrl) try requestRawLink(session, url); 2399 return; 2400 } 2401 root_focus.setFocus(focus_id); 2402 activate_in_page_link = true; 2403 } 2404 // forward the press into the widget tree so buttons (and any 2405 // future click-aware widgets) can react. widgets that don't 2406 // care about presses ignore it. 2407 try root.input(allocator, key, root_focus); 2408 } else { 2409 try root.input(allocator, key, root_focus); 2410 } 2411 }, 2412 else => try root.input(allocator, key, root_focus), 2413 } 2414 2415 const focused_after = root_focus.grandchild_id; 2416 if (!activate_in_page_link and focused_before == focused_after) return; 2417 const focus_id = focused_after orelse return; 2418 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2419 } 2420 2421 // a link to bytes the server serves directly rather than to a page route, so 2422 // it never resolves to a RoutablePage 2423 pub const raw_link_prefix = "ax:"; 2424 2425 // a route already loaded into the current page; focusing it changes the url 2426 // without rebuilding the page 2427 pub const in_page_link_prefix = "ai:"; 2428 2429 // an in-page tab's base route, or the exact current route when it is selected 2430 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2431 const target = if (selected) session.data.current_page else route; 2432 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2433 } 2434 2435 // a button the web renderer covers with a file picker that posts the chosen 2436 // file to the url after the prefix 2437 pub const file_input_prefix = "file:"; 2438 2439 // a submit button that overrides its enclosing form's action 2440 pub const submit_action_prefix = "submit:"; 2441 2442 // the url an `ax:` link points at, or null for anything else. a host that 2443 // suppresses the anchor's own navigation needs this to follow the link. 2444 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2445 const child = root_focus.children.get(focus_id) orelse return null; 2446 const custom = switch (child.focus.kind) { 2447 .custom => |c| c, 2448 else => return null, 2449 }; 2450 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2451 return custom[raw_link_prefix.len..]; 2452 } 2453 2454 fn requestRawLink(session: *Session, url: []const u8) !void { 2455 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2456 } 2457 2458 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2459 return if (session.web_port) |port| 2460 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2461 else 2462 path; 2463 } 2464 2465 // if the focus target at focus_id is an `a:` link that should navigate (a 2466 // different parent page, or a different files directory within the repo page), 2467 // return its route; otherwise null. lets a host turn a click / enter on such a 2468 // link into a navigation rather than a focus change. 2469 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2470 const current = data.current_page; 2471 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2472 // a link to a different parent page always navigates; within a page, a 2473 // files-directory / commits-page / list-window change navigates while tab 2474 // links stay in-page. 2475 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.forkPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 2476 return null; 2477 } 2478 2479 // the route carried by an `ai:` focus target, or null for any other target 2480 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2481 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2482 } 2483 2484 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2485 const child = root_focus.children.get(focus_id) orelse return null; 2486 const custom = switch (child.focus.kind) { 2487 .custom => |c| c, 2488 else => return null, 2489 }; 2490 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2491 const path = custom[prefix.len..]; 2492 // local sessions build (and parse) links with the repo identity elided 2493 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2494 } 2495 2496 // a display author, resolved against the admin db at read time 2497 pub const Author = union(enum) { 2498 unknown, 2499 email: []const u8, // an email no user matches 2500 user_name: []const u8, 2501 2502 // the commit author line's email, resolved like initFromEmail 2503 pub fn init( 2504 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2505 arena: *std.heap.ArenaAllocator, 2506 author_line: []const u8, 2507 ) !Author { 2508 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2509 } 2510 2511 // from a record's stored author email: it resolves to the user currently 2512 // holding it, else stays the email. the result is allocated in `arena`, 2513 // so the email may borrow transient memory. 2514 pub fn initFromEmail( 2515 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2516 arena: *std.heap.ArenaAllocator, 2517 author_email: ?[]const u8, 2518 ) !Author { 2519 const email = author_email orelse return .unknown; 2520 if (haxy_moment) |moment| { 2521 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2522 return .{ .user_name = user.event.name }; 2523 } 2524 } 2525 return .{ .email = try arena.allocator().dupe(u8, email) }; 2526 } 2527 }; 2528 2529 // the "a:" link to user `name`'s page. 2530 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2531 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2532 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2533 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2534 } 2535 2536 // a focusable " author " box showing `author`, linking to their user page 2537 // when it is a known user 2538 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2539 const text = switch (author) { 2540 .unknown => "", 2541 .email, .user_name => |t| t, 2542 }; 2543 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2544 errdefer tb.deinit(allocator); 2545 tb.getFocus().mode = .all; 2546 switch (author) { 2547 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2548 else => {}, 2549 } 2550 return tb; 2551 } 2552 2553 // native-TUI navigation 2554 pub const Nav = struct { 2555 root: Widget, 2556 // backs the current page (its Page data and the page-scoped strings its 2557 // widgets build). owned here, swapped on each navigation. session.page_arena 2558 // tracks whichever of these is current so widgets allocate into it. 2559 arena: *std.heap.ArenaAllocator, 2560 history: std.ArrayList(Entry), 2561 2562 // each retained page keeps its own arena, freed when the page leaves the 2563 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2564 // long-lived session from accumulating every page it ever visited. 2565 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2566 2567 // cap on retained back-history so the chain can't grow memory without bound 2568 const max_history: usize = 16; 2569 2570 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2571 session.back = .unavailable; 2572 const arena = try allocator.create(std.heap.ArenaAllocator); 2573 arena.* = std.heap.ArenaAllocator.init(allocator); 2574 errdefer { 2575 arena.deinit(); 2576 allocator.destroy(arena); 2577 } 2578 2579 session.page_arena = arena; 2580 const route = session.data.current_page; 2581 2582 const page = try arena.allocator().create(Page); 2583 page.* = try Page.init(arena, session, route); 2584 return .{ 2585 .root = try initRoot(allocator, page, session), 2586 .arena = arena, 2587 .history = .empty, 2588 }; 2589 } 2590 2591 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2592 self.root.deinit(allocator); 2593 freeArena(allocator, self.arena); 2594 for (self.history.items) |*entry| { 2595 entry.root.deinit(allocator); 2596 freeArena(allocator, entry.arena); 2597 } 2598 self.history.deinit(allocator); 2599 } 2600 2601 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2602 arena.deinit(); 2603 allocator.destroy(arena); 2604 } 2605 2606 // reconcile the displayed root with the session's navigation state. a 2607 // forward nav (current_page moved to a different parent page) pushes the 2608 // current root and builds the new page in a fresh arena; a back request 2609 // frees the current page and restores the previous one. when escape is 2610 // pressed with no history left we switch to the quit tab instead of quitting. 2611 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2612 // refresh: rebuild the current page in place from a current moment 2613 if (session.refresh_requested) { 2614 session.refresh_requested = false; 2615 if (session.haxy_moment != null or session.local != null) { 2616 const arena = try allocator.create(std.heap.ArenaAllocator); 2617 arena.* = std.heap.ArenaAllocator.init(allocator); 2618 errdefer freeArena(allocator, arena); 2619 2620 session.page_arena = arena; 2621 2622 const route = session.data.current_page; 2623 const page = try arena.allocator().create(Page); 2624 page.* = try Page.init(arena, session, route); 2625 const new_root = try initRoot(allocator, page, session); 2626 2627 self.root.deinit(allocator); 2628 freeArena(allocator, self.arena); 2629 self.root = new_root; 2630 self.arena = arena; 2631 } 2632 return; 2633 } 2634 2635 if (session.back == .requested) { 2636 if (self.history.pop()) |entry| { 2637 session.back = if (self.history.items.len == 0) .unavailable else .available; 2638 self.root.deinit(allocator); 2639 freeArena(allocator, self.arena); 2640 self.root = entry.root; 2641 self.arena = entry.arena; 2642 session.page_arena = entry.arena; 2643 session.data.current_page = entry.route; 2644 chooseAnsiArtForNavigation(session); 2645 return; 2646 } 2647 session.back = .unavailable; 2648 // nothing to go back to; switch to the quit confirmation 2649 const root_focus = self.root.getFocus(); 2650 var iter = root_focus.children.iterator(); 2651 while (iter.next()) |entry| { 2652 switch (entry.value_ptr.focus.kind) { 2653 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2654 root_focus.setFocus(entry.key_ptr.*); 2655 // a Stack only builds its selected child, so the quit 2656 // button isn't in the focus tree until a build runs with 2657 // the quit tab selected. build once, then send a 2658 // synthetic arrow_down to drop focus from the tab onto 2659 // the button so it's ready to confirm. 2660 try self.root.build(allocator, .{ 2661 .min_size = .{ .width = null, .height = 40 }, 2662 .max_size = .{ .width = 80, .height = null }, 2663 }, root_focus); 2664 try self.root.input(allocator, .arrow_down, root_focus); 2665 break; 2666 }, 2667 else => {}, 2668 } 2669 } 2670 return; 2671 } 2672 2673 // forward navigation: navigate() set next_page to the page to move to (a 2674 // cross-page link or tab change crossing pages) 2675 if (session.next_page) |route| { 2676 session.next_page = null; 2677 if (session.haxy_moment == null and session.local == null) return; 2678 const previous_route = session.data.current_page; 2679 // the page we navigated to becomes the current page 2680 session.data.current_page = route; 2681 session.back = .available; 2682 2683 const arena = try allocator.create(std.heap.ArenaAllocator); 2684 arena.* = std.heap.ArenaAllocator.init(allocator); 2685 errdefer freeArena(allocator, arena); 2686 2687 session.page_arena = arena; 2688 2689 const page = try arena.allocator().create(Page); 2690 page.* = try Page.init(arena, session, route); 2691 const new_root = try initRoot(allocator, page, session); 2692 2693 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2694 // drop the oldest entry (freeing its widget tree and arena) once over cap 2695 if (self.history.items.len > max_history) { 2696 var oldest = self.history.orderedRemove(0); 2697 oldest.root.deinit(allocator); 2698 freeArena(allocator, oldest.arena); 2699 } 2700 self.root = new_root; 2701 self.arena = arena; 2702 } 2703 } 2704 }; 2705 2706 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2707 const page_widget: Widget = switch (page.*) { 2708 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2709 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2710 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2711 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2712 }; 2713 2714 chooseAnsiArtForNavigation(session); 2715 2716 // on the TUI/SSH, the page sits above a one-row footer showing the url 2717 var root = if (session.is_terminal) blk: { 2718 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2719 errdefer box.deinit(allocator); 2720 const bg_id = bg_blk: { 2721 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2722 errdefer background.deinit(allocator); 2723 const id = background.getFocus().id; 2724 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2725 break :bg_blk id; 2726 }; 2727 { 2728 var footer = try widget.Footer.init(allocator, session); 2729 errdefer footer.deinit(allocator); 2730 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2731 } 2732 box.getFocus().child_id = bg_id; 2733 break :blk Widget{ .box = box }; 2734 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2735 errdefer root.deinit(allocator); 2736 2737 // input-owning views build their TextInputs in init, so reset their 2738 // registrations here 2739 session.text_inputs.clearRetainingCapacity(); 2740 2741 try root.build(allocator, .{ 2742 .min_size = .{ .width = null, .height = 40 }, 2743 .max_size = .{ .width = 100, .height = null }, 2744 }, root.getFocus()); 2745 2746 return root; 2747 } 2748 2749 // native and server-side sessions choose from the embedded collection. WASM 2750 // keeps the art serialized by the server instead. 2751 fn chooseAnsiArtForNavigation(session: *Session) void { 2752 if (comptime ansi_arts.len == 0) return; 2753 const io = session.io orelse return; 2754 var random: usize = undefined; 2755 io.random(std.mem.asBytes(&random)); 2756 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2757 } 2758