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