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