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