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