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