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