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