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