╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
645 646 // round-trip as a json string instead of an array of numbers. 647 pub fn jsonStringify(self: *const Array(max_len), jw: anytype) !void { 648 try jw.write(self.slice()); 649 } 650 651 pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !Array(max_len) { 652 const s = try std.json.innerParse([]const u8, allocator, source, options); 653 // ValueTooLong is std.json's error for an over-long field. 654 return from(s) orelse error.ValueTooLong; 655 } 656 }; 657 } 658 659 pub fn toUrl(self: RoutablePage, arena: *std.heap.ArenaAllocator) ![]const u8 { 660 return switch (self) { 661 .home_users => |start| if (start == 0) @as([]const u8, "/users") else try std.fmt.allocPrint(arena.allocator(), "/users/" ++ start_seg ++ "{d}", .{start}), 662 .home_repos => |start| if (start == 0) @as([]const u8, "/repos") else try std.fmt.allocPrint(arena.allocator(), "/repos/" ++ start_seg ++ "{d}", .{start}), 663 .home_settings => "/settings", 664 .home_auth => "/auth", 665 .user_repos => |u| if (u.start == 0) 666 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/repos", .{u.name.slice()}) 667 else 668 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/repos/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 669 .user_forks => |u| if (u.start == 0) 670 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/forks", .{u.name.slice()}) 671 else 672 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/forks/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 673 .user_settings => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/settings", .{name.slice()}), 674 .user_auth => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/auth", .{name.slice()}), 675 .repo_files => |f| blk: { 676 const prefix = try repoUrlPrefix(arena, f.name.slice()); 677 if (f.ref_kind == null and f.line == 0) break :blk if (prefix.len == 0) "/" else prefix; 678 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 679 try out.writer.print("{s}/" ++ files_seg, .{prefix}); 680 if (f.ref_kind) |kind| if (f.ref_value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), f.ref_value.slice() }); 681 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 682 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 683 break :blk out.written(); 684 }, 685 .repo_commits => |c| blk: { 686 const prefix = try repoUrlPrefix(arena, c.name.slice()); 687 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 688 try out.writer.print("{s}/" ++ commits_seg, .{prefix}); 689 if (c.ref_or_oid) |kind| if (c.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), c.value.slice() }); 690 switch (c.content) { 691 .diff => |d| { 692 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 693 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 694 }, 695 .message => try out.writer.print("/" ++ message_seg, .{}), 696 } 697 break :blk out.written(); 698 }, 699 .repo_refs => |r| blk: { 700 const prefix = try repoUrlPrefix(arena, r.name.slice()); 701 break :blk if (r.from.len == 0) 702 try std.fmt.allocPrint(arena.allocator(), "{s}/refs", .{prefix}) 703 else 704 try std.fmt.allocPrint(arena.allocator(), "{s}/refs/{s}:{s}", .{ prefix, @tagName(r.kind), r.from.slice() }); 705 }, 706 .repo_issues => |i| blk: { 707 const prefix = try repoUrlPrefix(arena, i.name.slice()); 708 if (i.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/edit", .{ prefix, i.selected.slice() }); 709 if (i.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/description", .{ prefix, i.selected.slice() }); 710 if (i.view == .resolve) break :blk if (i.theirs.len == 0) 711 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/resolve", .{ prefix, i.selected.slice() }) 712 else 713 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, i.selected.slice(), i.theirs.slice() }); 714 if (i.view == .new_comment) break :blk if (i.comment.len == 0) 715 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/new", .{ prefix, i.selected.slice() }) 716 else 717 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, i.selected.slice(), i.comment.slice() }); 718 if (i.view == .edit_comment) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, i.selected.slice(), i.comment.slice() }); 719 if (i.view == .remove) break :blk if (i.comment.len == 0) 720 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/remove", .{ prefix, i.selected.slice() }) 721 else 722 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, i.selected.slice(), i.comment.slice() }); 723 if (i.view == .conflicts) break :blk if (i.selected.len == 0) 724 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/conflicts", .{prefix}) 725 else 726 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/" ++ start_seg ++ "{s}/conflicts", .{ prefix, i.selected.slice() }); 727 if (i.comment.len != 0) break :blk if (i.comments_start == 0) 728 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, i.selected.slice(), i.comment.slice() }) 729 else 730 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, i.selected.slice(), i.comment.slice(), i.comments_start }); 731 if (i.selected.len != 0) break :blk if (i.comments_start == 0) 732 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}", .{ prefix, i.selected.slice() }) 733 else 734 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, i.selected.slice(), i.comments_start }); 735 break :blk if (i.tag.len == 0) 736 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}", .{ prefix, @tagName(i.view) }) 737 else 738 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(i.view), i.tag.slice() }); 739 }, 740 .repo_patches => |p| blk: { 741 const prefix = try repoUrlPrefix(arena, p.name.slice()); 742 if (p.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/edit", .{ prefix, p.selected.slice() }); 743 if (p.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/description", .{ prefix, p.selected.slice() }); 744 if (p.view == .resolve) break :blk if (p.theirs.len == 0) 745 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/resolve", .{ prefix, p.selected.slice() }) 746 else 747 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, p.selected.slice(), p.theirs.slice() }); 748 if (p.view == .new_comment) break :blk if (p.comment.len == 0) 749 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/new", .{ prefix, p.selected.slice() }) 750 else 751 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, p.selected.slice(), p.comment.slice() }); 752 if (p.view == .edit_comment) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, p.selected.slice(), p.comment.slice() }); 753 if (p.view == .remove) break :blk if (p.comment.len == 0) 754 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/remove", .{ prefix, p.selected.slice() }) 755 else 756 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, p.selected.slice(), p.comment.slice() }); 757 if (p.view == .conflicts) break :blk if (p.selected.len == 0) 758 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/conflicts", .{prefix}) 759 else 760 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/" ++ start_seg ++ "{s}/conflicts", .{ prefix, p.selected.slice() }); 761 if (p.comment.len != 0) break :blk if (p.comments_start == 0) 762 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, p.selected.slice(), p.comment.slice() }) 763 else 764 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, p.selected.slice(), p.comment.slice(), p.comments_start }); 765 if (p.selected.len != 0) break :blk if (p.comments_start == 0) 766 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}", .{ prefix, p.selected.slice() }) 767 else 768 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, p.selected.slice(), p.comments_start }); 769 break :blk if (p.tag.len == 0) 770 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/{s}", .{ prefix, @tagName(p.view) }) 771 else 772 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(p.view), p.tag.slice() }); 773 }, 774 .repo_discussions => |t| blk: { 775 const prefix = try repoUrlPrefix(arena, t.name.slice()); 776 if (t.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/edit", .{ prefix, t.selected.slice() }); 777 if (t.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/description", .{ prefix, t.selected.slice() }); 778 if (t.view == .new_comment) break :blk if (t.comment.len == 0) 779 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/new", .{ prefix, t.selected.slice() }) 780 else 781 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, t.selected.slice(), t.comment.slice() }); 782 if (t.view == .edit_comment) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, t.selected.slice(), t.comment.slice() }); 783 if (t.view == .remove) break :blk if (t.comment.len == 0) 784 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/remove", .{ prefix, t.selected.slice() }) 785 else 786 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, t.selected.slice(), t.comment.slice() }); 787 if (t.comment.len != 0) break :blk if (t.comments_start == 0) 788 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, t.selected.slice(), t.comment.slice() }) 789 else 790 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, t.selected.slice(), t.comment.slice(), t.comments_start }); 791 if (t.selected.len != 0) break :blk if (t.comments_start == 0) 792 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}", .{ prefix, t.selected.slice() }) 793 else 794 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, t.selected.slice(), t.comments_start }); 795 break :blk if (t.tag.len == 0) 796 try std.fmt.allocPrint(arena.allocator(), "{s}/discussions/{s}", .{ prefix, @tagName(t.view) }) 797 else 798 try std.fmt.allocPrint(arena.allocator(), "{s}/discussions/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(t.view), t.tag.slice() }); 799 }, 800 .repo_events => |e| blk: { 801 const prefix = try repoUrlPrefix(arena, e.name.slice()); 802 break :blk if (e.kind) |kind| 803 try std.fmt.allocPrint(arena.allocator(), "{s}/event:{s}/kind:{s}", .{ prefix, e.selected.slice(), @tagName(kind) }) 804 else 805 try std.fmt.allocPrint(arena.allocator(), "{s}/events/{s}", .{ prefix, @tagName(e.view) }); 806 }, 807 .repo_settings => |name| try std.fmt.allocPrint(arena.allocator(), "{s}/settings", .{try repoUrlPrefix(arena, name.slice())}), 808 .repo_auth => |name| try std.fmt.allocPrint(arena.allocator(), "{s}/auth", .{try repoUrlPrefix(arena, name.slice())}), 809 }; 810 } 811 812 pub fn fromUrl(path: []const u8) ?RoutablePage { 813 if (std.mem.eql(u8, path, "/")) return default; 814 if (path.len < 2 or path[0] != '/') return null; 815 var segments = std.mem.splitScalar(u8, path[1..], '/'); 816 const first = segments.next() orelse return null; 817 if (std.mem.eql(u8, first, "users")) return .{ .home_users = listStart(&segments) orelse return null }; 818 if (std.mem.eql(u8, first, "repos")) return .{ .home_repos = listStart(&segments) orelse return null }; 819 if (std.mem.eql(u8, first, "settings")) return if (segments.next() == null) .home_settings else null; 820 if (std.mem.eql(u8, first, "auth")) return if (segments.next() == null) .home_auth else null; 821 // "user/<name>[/repos[/start:<n>]|/settings|/auth]" 822 if (std.mem.eql(u8, first, "user")) { 823 const name = segments.next() orelse return null; 824 if (name.len == 0) return null; 825 const parsed = Array(evt.User.name_max_len).from(name) orelse return null; // name too long 826 const sub = segments.next() orelse return .{ .user_repos = .{ .name = parsed } }; 827 if (std.mem.eql(u8, sub, "repos")) return .{ .user_repos = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 828 if (std.mem.eql(u8, sub, "forks")) return .{ .user_forks = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 829 if (std.mem.eql(u8, sub, "settings")) return if (segments.next() == null) .{ .user_settings = parsed } else null; 830 if (std.mem.eql(u8, sub, "auth")) return if (segments.next() == null) .{ .user_auth = parsed } else null; 831 return null; // unknown sub-path 832 } 833 // "repo/<username>/<reponame>[/<tab tail>]"; the bare pair is the 834 // files root 835 if (std.mem.eql(u8, first, "repo")) { 836 const rest = segments.rest(); // "username/reponame[/...]" 837 const owner = segments.next() orelse return null; 838 const repo_name = segments.next() orelse return null; 839 // reject an empty username or reponame 840 if (owner.len == 0 or repo_name.len == 0) return null; 841 const pair = rest[0 .. owner.len + 1 + repo_name.len]; // "username/reponame" 842 if (segments.peek() != null) { 843 return repoSubRoute(pair, segments.rest()); 844 } 845 return repoFilesRoute(pair, null, "", "", 0); 846 } 847 return null; 848 } 849 850 // parse a local-mode url, which elides the "/repo/<owner>/<name>" prefix: 851 // "/" is the files root and the sub-paths follow directly. 852 pub fn fromUrlLocal(path: []const u8) ?RoutablePage { 853 if (std.mem.eql(u8, path, "/")) return repoFilesRoute("", null, "", "", 0); 854 if (path.len < 2 or path[0] != '/') return null; 855 return repoSubRoute("", path[1..]); 856 } 857 858 // the url prefix for a repo route's identity: "/repo/<identity>", or "" 859 // when the identity is elided. 860 fn repoUrlPrefix(arena: *std.heap.ArenaAllocator, identity: []const u8) ![]const u8 { 861 if (identity.len == 0) return ""; 862 return std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}", .{identity}); 863 } 864 865 // true when the page builds at its content height on the web, letting the 866 // browser scroll the whole page, rather than being pinned to the viewport 867 pub fn fullHeight(self: RoutablePage) bool { 868 return switch (self) { 869 .repo_issues => |i| i.view == .resolve, 870 .repo_patches => |p| p.view == .resolve, 871 else => false, 872 }; 873 } 874 875 // the "owner/name" a repo route carries, null for any other route. the 876 // result borrows the route, so it must outlive the slice. 877 pub fn repoIdentity(self: *const RoutablePage) ?[]const u8 { 878 return switch (self.*) { 879 .repo_files => |*f| f.name.slice(), 880 .repo_commits => |*c| c.name.slice(), 881 .repo_refs => |*r| r.name.slice(), 882 .repo_issues => |*i| i.name.slice(), 883 .repo_patches => |*p| p.name.slice(), 884 .repo_discussions => |*t| t.name.slice(), 885 .repo_events => |*e| e.name.slice(), 886 .repo_settings, .repo_auth => |*name| name.slice(), 887 else => null, 888 }; 889 } 890 891 // the route this route's page lands on, with no tab or params: the users 892 // list, a user's repos, or a repo's files root. 893 pub fn pageRoot(self: RoutablePage) RoutablePage { 894 return switch (self.parent()) { 895 .home => .default, 896 .user => switch (self) { 897 .user_repos => |u| .{ .user_repos = .{ .name = u.name } }, 898 .user_forks => |u| .{ .user_repos = .{ .name = u.name } }, 899 .user_settings, .user_auth => |name| .{ .user_repos = .{ .name = name } }, 900 else => self, 901 }, 902 // every repo route stores the same identity, so it always fits 903 .repo => repoFilesRoute(self.repoIdentity() orelse return self, null, "", "", 0) orelse self, 904 }; 905 } 906 907 pub fn parent(self: RoutablePage) PageKind { 908 return switch (self) { 909 .home_users, .home_repos, .home_settings, .home_auth => .home, 910 .user_repos, .user_forks, .user_settings, .user_auth => .user, 911 .repo_files, .repo_commits, .repo_refs, .repo_issues, .repo_patches, .repo_discussions, .repo_events, .repo_settings, .repo_auth => .repo, 912 }; 913 } 914 915 // the dir a trailing "path:<dir>" names: the raw remainder ("" when 916 // absent, null when malformed) 917 fn pathValue(rest: []const u8) ?[]const u8 { 918 if (rest.len == 0) return ""; 919 if (!std.mem.startsWith(u8, rest, path_seg)) return null; 920 const dir = rest[path_seg.len..]; 921 return if (dir.len == 0) null else dir; 922 } 923 924 // a list route's tail: at most a "start:<n>" param, null on anything else 925 fn listStart(segments: *std.mem.SplitIterator(u8, .scalar)) ?usize { 926 var params = Params{}; 927 const word = params.scanTail(segments) catch return null; 928 if (word != null or !params.only(&.{.start})) return null; 929 return params.start(); 930 } 931 932 // parse the sub-path after a repo route's identity ("" for a local 933 // route): a tab keyword followed by "key:value" params in any order, plus 934 // the files tab's trailing "path:<dir>". 935 fn repoSubRoute(pair: []const u8, sub: []const u8) ?RoutablePage { 936 var segments = std.mem.splitScalar(u8, sub, '/'); 937 const tab = segments.next() orelse return null; 938 var params = Params{}; 939 if (std.mem.eql(u8, tab, "settings")) return if (segments.next() == null) .{ .repo_settings = Array(repo_route_max_len).from(pair) orelse return null } else null; 940 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; 941 if (std.mem.eql(u8, tab, "refs")) { 942 const word = params.scanTail(&segments) catch return null; 943 if (word != null or !params.only(&.{ .branch, .tag })) return null; 944 const ref = (params.ref() catch return null) orelse return repoRefsRoute(pair, .branch, ""); 945 const kind = std.meta.stringToEnum(RefKind, @tagName(ref.kind)) orelse return null; 946 return repoRefsRoute(pair, kind, ref.value); 947 } 948 if (std.mem.startsWith(u8, tab, issue_seg)) { 949 const issue_id = tab[issue_seg.len..]; 950 if (issue_id.len == 0) return null; 951 if (segments.peek()) |tail| { 952 if (std.mem.startsWith(u8, tail, comment_seg)) { 953 _ = segments.next(); 954 const comment_id = tail[comment_seg.len..]; 955 if (comment_id.len == 0) return null; 956 const word = params.scanTail(&segments) catch return null; 957 if (word) |last| { 958 if (!params.only(&.{})) return null; 959 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.issue, pair, issue_id, comment_id); 960 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.issue, pair, issue_id, comment_id); 961 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.issue, pair, issue_id, comment_id); 962 return null; 963 } 964 if (!params.only(&.{.start})) return null; 965 return repoThreadCommentRoute(.issue, pair, issue_id, comment_id, params.start() orelse return null); 966 } 967 } 968 const word = params.scanTail(&segments) catch return null; 969 if (word) |tail| { 970 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.issue, pair, issue_id, "") else null; 971 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.issue, pair, issue_id) else null; 972 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.issue, pair, issue_id, "") else null; 973 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.issue, pair, issue_id) else null; 974 if (std.mem.eql(u8, tail, "resolve")) { 975 if (!params.only(&.{.theirs})) return null; 976 return repoIssuesResolveRoute(pair, issue_id, params.values.get(.theirs) orelse ""); 977 } 978 return null; 979 } 980 if (!params.only(&.{.start})) return null; 981 return repoThreadCommentsRoute(.issue, pair, issue_id, params.start() orelse return null); 982 } 983 if (std.mem.startsWith(u8, tab, patch_seg)) { 984 const patch_id = tab[patch_seg.len..]; 985 if (patch_id.len == 0) return null; 986 if (segments.peek()) |tail| { 987 if (std.mem.startsWith(u8, tail, comment_seg)) { 988 _ = segments.next(); 989 const comment_id = tail[comment_seg.len..]; 990 if (comment_id.len == 0) return null; 991 const word = params.scanTail(&segments) catch return null; 992 if (word) |last| { 993 if (!params.only(&.{})) return null; 994 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.patch, pair, patch_id, comment_id); 995 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.patch, pair, patch_id, comment_id); 996 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.patch, pair, patch_id, comment_id); 997 return null; 998 } 999 if (!params.only(&.{.start})) return null; 1000 return repoThreadCommentRoute(.patch, pair, patch_id, comment_id, params.start() orelse return null); 1001 } 1002 } 1003 const word = params.scanTail(&segments) catch return null; 1004 if (word) |tail| { 1005 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.patch, pair, patch_id, "") else null; 1006 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.patch, pair, patch_id) else null; 1007 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.patch, pair, patch_id, "") else null; 1008 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.patch, pair, patch_id) else null; 1009 if (std.mem.eql(u8, tail, "resolve")) { 1010 if (!params.only(&.{.theirs})) return null; 1011 return repoPatchesResolveRoute(pair, patch_id, params.values.get(.theirs) orelse ""); 1012 } 1013 return null; 1014 } 1015 if (!params.only(&.{.start})) return null; 1016 return repoThreadCommentsRoute(.patch, pair, patch_id, params.start() orelse return null); 1017 } 1018 if (std.mem.startsWith(u8, tab, discuss_seg)) { 1019 const discussion_id = tab[discuss_seg.len..]; 1020 if (discussion_id.len == 0) return null; 1021 if (segments.peek()) |tail| { 1022 if (std.mem.startsWith(u8, tail, comment_seg)) { 1023 _ = segments.next(); 1024 const comment_id = tail[comment_seg.len..]; 1025 if (comment_id.len == 0) return null; 1026 const word = params.scanTail(&segments) catch return null; 1027 if (word) |last| { 1028 if (!params.only(&.{})) return null; 1029 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.discuss, pair, discussion_id, comment_id); 1030 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.discuss, pair, discussion_id, comment_id); 1031 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.discuss, pair, discussion_id, comment_id); 1032 return null; 1033 } 1034 if (!params.only(&.{.start})) return null; 1035 return repoThreadCommentRoute(.discuss, pair, discussion_id, comment_id, params.start() orelse return null); 1036 } 1037 } 1038 const word = params.scanTail(&segments) catch return null; 1039 if (word) |tail| { 1040 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.discuss, pair, discussion_id, "") else null; 1041 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.discuss, pair, discussion_id) else null; 1042 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.discuss, pair, discussion_id, "") else null; 1043 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.discuss, pair, discussion_id) else null; 1044 return null; 1045 } 1046 if (!params.only(&.{.start})) return null; 1047 return repoThreadCommentsRoute(.discuss, pair, discussion_id, params.start() orelse return null); 1048 } 1049 if (std.mem.eql(u8, tab, "issues")) { 1050 // the word is a list view; a filter with neither is never emitted. 1051 const word = params.scanTail(&segments) catch return null; 1052 const tag_value = params.values.get(.tag) orelse ""; 1053 const w = word orelse return if (params.only(&.{})) repoIssuesRoute(pair, .open, "", "") else null; 1054 // the conflicts list windows by an issue id, not a number, so it 1055 // reads the start param raw 1056 if (std.mem.eql(u8, w, "conflicts")) { 1057 if (!params.only(&.{.start})) return null; 1058 return repoIssuesConflictsRoute(pair, params.values.get(.start) orelse ""); 1059 } 1060 if (!params.only(&.{.tag})) return null; 1061 if (std.meta.stringToEnum(evt.Issue.Status, w)) |status| return repoIssuesRoute(pair, status, tag_value, ""); 1062 if (std.mem.eql(u8, w, "tags")) return repoThreadTagsRoute(.issue, pair, tag_value); 1063 if (std.mem.eql(u8, w, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.issue, pair) else null; 1064 return null; 1065 } 1066 if (std.mem.eql(u8, tab, "patches")) { 1067 const word = params.scanTail(&segments) catch return null; 1068 const tag_value = params.values.get(.tag) orelse ""; 1069 const view = word orelse return if (params.only(&.{})) repoPatchesRoute(pair, .open, "", "") else null; 1070 if (std.mem.eql(u8, view, "conflicts")) { 1071 if (!params.only(&.{.start})) return null; 1072 return repoPatchesConflictsRoute(pair, params.values.get(.start) orelse ""); 1073 } 1074 if (!params.only(&.{.tag})) return null; 1075 if (std.meta.stringToEnum(evt.Patch.Status, view)) |status| return repoPatchesRoute(pair, status, tag_value, ""); 1076 if (std.mem.eql(u8, view, "tags")) return repoThreadTagsRoute(.patch, pair, tag_value); 1077 if (std.mem.eql(u8, view, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.patch, pair) else null; 1078 if (std.mem.eql(u8, view, "drafts")) return if (tag_value.len == 0) repoPatchesDraftsRoute(pair) else null; 1079 return null; 1080 } 1081 if (std.mem.eql(u8, tab, "discussions")) { 1082 const word = params.scanTail(&segments) catch return null; 1083 const tag_value = params.values.get(.tag) orelse ""; 1084 const view = word orelse return if (params.only(&.{})) repoDiscussionsRoute(pair, "", "") else null; 1085 if (!params.only(&.{.tag})) return null; 1086 if (std.mem.eql(u8, view, "recent")) return repoDiscussionsRoute(pair, tag_value, ""); 1087 if (std.mem.eql(u8, view, "tags")) return repoThreadTagsRoute(.discuss, pair, tag_value); 1088 if (std.mem.eql(u8, view, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.discuss, pair) else null; 1089 return null; 1090 } 1091 if (std.mem.eql(u8, tab, "events")) { 1092 const view = if (segments.next()) |word| 1093 std.meta.stringToEnum(EventsView, word) orelse return null 1094 else 1095 .active; 1096 return if (segments.next() == null) repoEventsRoute(pair, view, null, "") else null; 1097 } 1098 if (std.mem.startsWith(u8, tab, "event:")) { 1099 const event_id = tab["event:".len..]; 1100 const kind_segment = segments.next() orelse return null; 1101 if (!std.mem.startsWith(u8, kind_segment, "kind:")) return null; 1102 const kind = std.meta.stringToEnum(evt.EventKind, kind_segment["kind:".len..]) orelse return null; 1103 if (event_id.len == 0 or segments.next() != null) return null; 1104 return repoEventsRoute(pair, .active, kind, event_id); 1105 } 1106 if (std.mem.eql(u8, tab, files_seg)) { 1107 params.scanPairs(&segments) catch return null; 1108 if (!params.only(&.{ .line, .branch, .tag, .object })) return null; 1109 const line = params.line() orelse return null; 1110 const dir = pathValue(segments.rest()) orelse return null; 1111 const ref = (params.ref() catch return null) orelse 1112 return if (dir.len == 0) repoFilesRoute(pair, null, "", "", line) else null; 1113 return repoFilesRoute(pair, ref.kind, ref.value, dir, line); 1114 } 1115 if (std.mem.eql(u8, tab, commits_seg)) { 1116 params.scanPairs(&segments) catch return null; 1117 if (!params.only(&.{ .start, .branch, .tag, .object })) return null; 1118 const start = params.start() orelse return null; 1119 const ref = (params.ref() catch return null) orelse { 1120 const file = pathValue(segments.rest()) orelse return null; 1121 return if (file.len == 0) repoCommitsRoute(pair, null, "", start, "") else null; 1122 }; 1123 // the message page names its commit and nothing else 1124 if (std.mem.eql(u8, segments.rest(), message_seg)) 1125 return if (start == 0) repoCommitMessageRoute(pair, ref.kind, ref.value) else null; 1126 const file = pathValue(segments.rest()) orelse return null; 1127 return repoCommitsRoute(pair, ref.kind, ref.value, start, file); 1128 } 1129 return null; // unknown sub-path 1130 } 1131 1132 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 1133 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1134 return switch (a) { 1135 .home_users => |a_start| a_start == b.home_users, 1136 .home_repos => |a_start| a_start == b.home_repos, 1137 .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, 1138 .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, 1139 .user_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.user_settings.slice()), 1140 .user_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.user_auth.slice()), 1141 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 1142 a_f.ref_kind == b.repo_files.ref_kind and 1143 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 1144 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 1145 a_f.line == b.repo_files.line, 1146 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 1147 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 1148 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 1149 switch (a_c.content) { 1150 .diff => |a_d| switch (b.repo_commits.content) { 1151 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1152 .message => false, 1153 }, 1154 .message => std.meta.activeTag(b.repo_commits.content) == .message, 1155 }, 1156 .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()), 1157 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 1158 std.mem.eql(u8, a_i.tag.slice(), b.repo_issues.tag.slice()) and 1159 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 1160 std.mem.eql(u8, a_i.theirs.slice(), b.repo_issues.theirs.slice()) and 1161 a_i.view == b.repo_issues.view and 1162 a_i.comments_start == b.repo_issues.comments_start and 1163 std.mem.eql(u8, a_i.comment.slice(), b.repo_issues.comment.slice()), 1164 .repo_discussions => |a_t| std.mem.eql(u8, a_t.name.slice(), b.repo_discussions.name.slice()) and 1165 std.mem.eql(u8, a_t.tag.slice(), b.repo_discussions.tag.slice()) and 1166 std.mem.eql(u8, a_t.selected.slice(), b.repo_discussions.selected.slice()) and 1167 a_t.view == b.repo_discussions.view and 1168 a_t.comments_start == b.repo_discussions.comments_start and 1169 std.mem.eql(u8, a_t.comment.slice(), b.repo_discussions.comment.slice()), 1170 .repo_patches => |a_p| std.mem.eql(u8, a_p.name.slice(), b.repo_patches.name.slice()) and 1171 std.mem.eql(u8, a_p.tag.slice(), b.repo_patches.tag.slice()) and 1172 std.mem.eql(u8, a_p.selected.slice(), b.repo_patches.selected.slice()) and 1173 std.mem.eql(u8, a_p.theirs.slice(), b.repo_patches.theirs.slice()) and 1174 a_p.view == b.repo_patches.view and 1175 a_p.comments_start == b.repo_patches.comments_start and 1176 std.mem.eql(u8, a_p.comment.slice(), b.repo_patches.comment.slice()), 1177 .repo_events => |a_e| std.mem.eql(u8, a_e.name.slice(), b.repo_events.name.slice()) and 1178 a_e.view == b.repo_events.view and 1179 a_e.kind == b.repo_events.kind and 1180 std.mem.eql(u8, a_e.selected.slice(), b.repo_events.selected.slice()), 1181 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 1182 .repo_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_auth.slice()), 1183 else => true, 1184 }; 1185 } 1186 1187 // true when following a link from `b` to `a` (both repo routes) should reload 1188 // rather than stay in page 1189 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 1190 if (a.parent() != .repo or b.parent() != .repo) return false; 1191 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 1192 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 1193 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 1194 return !a.eql(b); 1195 } 1196 1197 // true when `a` and `b` are the same user paginated to a different repos 1198 // window. switching between a user's tabs is in-page, so 1199 // only a changed `start` on the repos list navigates. 1200 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 1201 return switch (a) { 1202 .user_repos => |aa| switch (b) { 1203 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1204 else => false, 1205 }, 1206 .user_forks => |aa| switch (b) { 1207 .user_forks => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1208 else => false, 1209 }, 1210 else => false, 1211 }; 1212 } 1213 1214 // true when `a` and `b` are the same home list tab paginated to a different 1215 // window. switching between the users/repos tabs is in-page (the home page 1216 // holds both lists), so only a changed `after` on the same tab navigates. 1217 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 1218 return switch (a) { 1219 .home_users => |aa| switch (b) { 1220 .home_users => |bb| aa != bb, 1221 else => false, 1222 }, 1223 .home_repos => |aa| switch (b) { 1224 .home_repos => |bb| aa != bb, 1225 else => false, 1226 }, 1227 else => false, 1228 }; 1229 } 1230 }; 1231 1232 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 1233 fn isUnreserved(c: u8) bool { 1234 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 1235 } 1236 1237 // percent-encode a ref name for use as a single url path segment. 1238 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 1239 var out: std.Io.Writer.Allocating = .init(aa); 1240 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 1241 return out.written(); 1242 } 1243 1244 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 1245 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1246 return struct { 1247 pub const hex_len = hash.hexLen(repo_opts.hash); 1248 1249 // the concrete ref/oid (a null request resolves to the default branch) 1250 ref_or_oid: RoutablePage.RefOrOid, 1251 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 1252 // the route layer — which splits on '/' — stores them encoded). duped 1253 // into `aa`. 1254 value: []const u8, 1255 // the commit oid it points at 1256 oid: [hex_len]u8, 1257 1258 const Self = @This(); 1259 1260 // resolve a requested ref/oid (null = the repo's default branch) to a 1261 // concrete ref/oid plus the commit oid it points at. `requested_value` is 1262 // the url-encoded form (as it appears in the route). null when it doesn't 1263 // resolve (an unknown branch/tag, or a malformed/unknown oid). 1264 pub fn init( 1265 repo: *rp.Repo(repo_kind, repo_opts), 1266 io: std.Io, 1267 aa: std.mem.Allocator, 1268 requested_ref_or_oid: ?RoutablePage.RefOrOid, 1269 requested_value: []const u8, 1270 ) !?Self { 1271 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 1272 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 1273 var value: []const u8 = if (requested_ref_or_oid == null) 1274 requested_value 1275 else 1276 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 1277 // no ref named: fall back to HEAD's branch (or its oid when detached). 1278 if (requested_ref_or_oid == null) { 1279 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1280 if (repo.head(io, &head_buf)) |head| switch (head) { 1281 .ref => |r| { 1282 ref_or_oid = .branch; 1283 value = r.name; 1284 }, 1285 .oid => |o| { 1286 ref_or_oid = .object; 1287 value = o; 1288 }, 1289 } else |_| {} 1290 } 1291 1292 var oid: [hex_len]u8 = undefined; 1293 switch (ref_or_oid) { 1294 .object => { 1295 if (value.len != hex_len) return null; 1296 @memcpy(&oid, value); 1297 }, 1298 .branch, .tag => { 1299 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 1300 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 1301 }, 1302 } 1303 // store url-encoded so the route layer can hold the value verbatim. 1304 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 1305 } 1306 }; 1307 } 1308 1309 // where a repo page reads its on-disk repo from 1310 pub const RepoSource = struct { 1311 path: []const u8, 1312 repo_kind: rp.RepoKind, 1313 // the user's global git config, so local mode picks up their identity and 1314 // their signing and ssh settings. null on the server paths, which must not 1315 // read whoever runs the server. 1316 global_config_path: ?[]const u8 = null, 1317 1318 pub fn localInitOpts(self: RepoSource) rp.InitOpts { 1319 return .{ .path = self.path, .global_config_path = self.global_config_path }; 1320 } 1321 }; 1322 1323 // events created in local mode have no account behind them, so the commit 1324 // author comes from the repo's config, which the global config feeds into. 1325 pub const local_author_fallback = evt.CommitAuthor{ .name = "haxy", .email = "user@haxy" }; 1326 1327 pub fn localAuthor( 1328 src: RepoSource, 1329 io: std.Io, 1330 allocator: std.mem.Allocator, 1331 arena: *std.heap.ArenaAllocator, 1332 ) !evt.CommitAuthor { 1333 switch (src.repo_kind) { 1334 inline else => |repo_kind| { 1335 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()) catch return local_author_fallback; 1336 defer any_repo.deinit(io, allocator); 1337 1338 switch (any_repo) { 1339 inline else => |*repo| { 1340 var config = try repo.listConfig(io, allocator); 1341 defer config.deinit(); 1342 1343 const user = config.sections.get("user") orelse return local_author_fallback; 1344 const name = user.get("name") orelse return local_author_fallback; 1345 const email = user.get("email") orelse return local_author_fallback; 1346 return .{ 1347 .name = try arena.allocator().dupe(u8, name), 1348 .email = try arena.allocator().dupe(u8, email), 1349 }; 1350 }, 1351 } 1352 }, 1353 } 1354 } 1355 1356 // per-connection mutable state. each SSH session / web session / local TUI 1357 // run gets its own. 1358 pub const Session = struct { 1359 data: Data = .{}, 1360 // session-lifetime allocations: login/user_id, persisted prefs, queued 1361 // actions. lives as long as the connection. 1362 arena: *std.heap.ArenaAllocator, 1363 // current page's allocations: page data and the page-scoped strings widgets 1364 // build (form actions, links). owned by Nav, swapped on each navigation, so 1365 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 1366 // where a render is one-shot, this points at the same arena as `arena`. 1367 page_arena: *std.heap.ArenaAllocator, 1368 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 1369 // filesystem io and the path to <server>/repos, for opening on-disk repos 1370 // during page construction. both null on wasm, which has no filesystem and 1371 // rebuilds pages from the serialized snapshot rather than from disk. 1372 io: ?std.Io = null, 1373 admin_repo: ?*rp.Repo(.xit, evt.admin_repo_opts) = null, 1374 repos_dir: ?[]const u8 = null, 1375 // the single on-disk repo this session views, when running in local mode 1376 // (haxy invoked with no arguments inside a repo). null on the server paths, 1377 // which resolve repos from the admin db instead. 1378 local: ?RepoSource = null, 1379 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 1380 // focus id -> the live TextInput, refreshed each frame by the views that own 1381 // inputs. web/wasm form handling looks widgets up here by focus id. 1382 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput(Widget)) = .empty, 1383 // focus id -> the page-constant text the web renderer puts in an input 1384 // initially, refreshed each frame alongside `text_inputs`. never an 1385 // editable input's live content: the overlay html must not change as the 1386 // user types, or the diff in _setOverlay would rebuild the element. 1387 input_values: std.AutoHashMapUnmanaged(usize, []const u8) = .empty, 1388 nav_back: bool = false, // set by input (escape) to request the native TUI pop a page; see Nav 1389 refresh_requested: bool = false, // set by input (ctrl+r) 1390 // a requested forward navigation. set this (via navigate) to move to a new 1391 // page; Nav.sync builds it and then copies it into current_page, clearing 1392 // this back to null. setting current_page directly only updates the url and 1393 // does not navigate. 1394 next_page: ?RoutablePage = null, 1395 is_terminal: bool = false, // true on remote SSH and local TUI 1396 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 1397 // url. null on the web itself (no footer there). 1398 web_port: ?u16 = null, 1399 // a host operation requested by a widget 1400 host_request: ?HostRequest = null, 1401 quit_requested: bool = false, 1402 1403 pub const HostRequest = union(enum) { 1404 show_copyable_text: []const u8, 1405 sync_events, 1406 }; 1407 1408 const Self = @This(); 1409 1410 // serializable data sent down to web client 1411 pub const Data = struct { 1412 user_id: ?[]const u8 = null, 1413 // the logged-in user's name, for the views that show it 1414 user_name: ?[]const u8 = null, 1415 // a transient outcome to surface from the last /login POST attempt 1416 login_failure: ?Home.Auth.Login.Failure = null, 1417 // a transient local event-sync error 1418 sync_failure: ?[]const u8 = null, 1419 current_page: RoutablePage = .default, 1420 // whether to render the ANSI art backdrop 1421 enable_ansi: bool = true, 1422 // the selected ANSI art. the server serializes one image for WASM so 1423 // the browser does not need the whole build-generated collection. 1424 ansi_art: []const u8 = "", 1425 // the bound git-service ports. absent in local mode, where remote urls 1426 // are not shown. 1427 git_http_port: ?u16 = null, 1428 git_ssh_port: ?u16 = null, 1429 git_ssh_prefix: []const u8 = "", 1430 // true when this session views a single local repo. unlike `local` 1431 // (the host-side filesystem source) this travels in the snapshot, so 1432 // the wasm side also parses elided link urls and hides the multi-user 1433 // chrome. 1434 is_local: bool = false, 1435 }; 1436 1437 // a user-initiated state change. widgets enqueue these on the session during 1438 // input instead of mutating state or touching the DB themselves; the host 1439 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 1440 // side effects out of the widget tree and gives every render path one place to 1441 // turn a UI action into a state change + event. 1442 pub const Action = union(enum) { 1443 toggle_ansi, 1444 }; 1445 1446 pub fn init( 1447 arena: *std.heap.ArenaAllocator, 1448 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1449 data: Data, 1450 ) !Self { 1451 var session = Self{ 1452 .data = data, 1453 .arena = arena, 1454 // until a host swaps in a page-scoped arena (Nav does this), page 1455 // allocations land in the session arena. on the web path that's the 1456 // intended behavior, since the whole arena is per-request. 1457 .page_arena = arena, 1458 .admin_repo = repo, 1459 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 1460 }; 1461 try session.loadUser(); 1462 return session; 1463 } 1464 1465 // load the logged-in user's name and persisted preferences from the db 1466 pub fn loadUser(self: *Self) !void { 1467 const user_id = self.data.user_id orelse return; 1468 const moment = self.haxy_moment orelse return; 1469 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.arena, user_id)) |user| { 1470 self.data.user_name = user.event.name; 1471 self.data.enable_ansi = user.event.enable_ansi; 1472 } 1473 } 1474 1475 // the commit author for an event this session creates, or null when it may 1476 // not create one 1477 pub fn eventAuthor(self: *Self) !?evt.CommitAuthor { 1478 if (self.data.is_local) { 1479 const src = self.local orelse return local_author_fallback; 1480 const io = self.io orelse return local_author_fallback; 1481 return try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 1482 } 1483 const user_id = self.data.user_id orelse return null; 1484 const moment = self.haxy_moment orelse return null; 1485 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.page_arena, user_id)) orelse return null; 1486 return .{ .name = user.event.name, .email = user.event.email }; 1487 } 1488 1489 // queue an action for the host to drain this frame. 1490 pub fn push(self: *Self, action: Action) !void { 1491 try self.pending.append(self.arena.allocator(), action); 1492 } 1493 1494 // apply a single action's in-memory effect only (no persistence). 1495 fn apply(self: *Self, action: Action) void { 1496 switch (action) { 1497 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 1498 } 1499 } 1500 1501 // drain the session's queued actions by applying them to the session. 1502 // used on the wasm path, which has no repo to persist to. 1503 pub fn applyPending(self: *Self) void { 1504 for (self.pending.items) |action| self.apply(action); 1505 self.pending.clearRetainingCapacity(); 1506 } 1507 1508 // drain the session's queued actions by applying them to the session and 1509 // writing them to the db 1510 pub fn applyAndWritePending( 1511 self: *Self, 1512 io: std.Io, 1513 allocator: std.mem.Allocator, 1514 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1515 ) !void { 1516 defer self.pending.clearRetainingCapacity(); 1517 for (self.pending.items) |action| { 1518 self.apply(action); 1519 switch (action) { 1520 .toggle_ansi => if (self.data.user_id) |user_id| { 1521 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 1522 }, 1523 } 1524 } 1525 } 1526 1527 // reload the moment from the admin repo and re-read user preferences 1528 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 1529 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 1530 self.haxy_moment = moment; 1531 1532 const user_id = self.data.user_id orelse return; 1533 var arena = std.heap.ArenaAllocator.init(allocator); 1534 defer arena.deinit(); 1535 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, user_id)) |user| { 1536 self.data.enable_ansi = user.event.enable_ansi; 1537 } 1538 } 1539 1540 // request a forward navigation to `route`; the host consumes next_page 1541 // (Nav.sync on the terminal, the wasm tick on the web). 1542 pub fn navigate(self: *Session, route: RoutablePage) !void { 1543 self.next_page = route; 1544 } 1545 }; 1546 1547 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 1548 var nav = try Nav.init(allocator, session); 1549 defer nav.deinit(allocator); 1550 1551 var terminal = try term.Terminal.init(io, allocator); 1552 var terminal_live = true; 1553 defer if (terminal_live) terminal.deinit(io); 1554 1555 // set term as active so it will be properly cooked 1556 // when a panic/segfault happens 1557 term.setActive(&terminal); 1558 defer term.setActive(null); 1559 1560 var last_size = layout.Size{ .width = 0, .height = 0 }; 1561 var last_grid = try Grid.init(allocator, last_size); 1562 defer last_grid.deinit(); 1563 1564 while (!terminal.shouldQuit()) { 1565 const grid_changed = try terminal.render(&nav.root, &last_grid, &last_size); 1566 1567 // process any inputs. 1568 // 1569 // if the grid didn't change, then first do a blocking 1570 // read, so the thread will sleep until further input. 1571 // after that, all remaining reads are non-blocking so 1572 // we can process the rest of the queued inputs. 1573 // 1574 // if the grid *did* change, then only do non-blocking 1575 // reads. we do not want to sleep the thread because 1576 // there may be an animation that requires more looping. 1577 var blocking = !grid_changed; 1578 while (try terminal.readKey(io, blocking)) |key| { 1579 blocking = false; 1580 try inputKey(allocator, &nav.root, key, session); 1581 } 1582 1583 if (session.host_request) |request| { 1584 session.host_request = null; 1585 switch (request) { 1586 .show_copyable_text => |copyable_text| { 1587 term.setActive(null); 1588 terminal.deinit(io); 1589 terminal_live = false; 1590 showCopyableText(io, copyable_text) catch |show_err| { 1591 terminal = try term.Terminal.init(io, allocator); 1592 terminal_live = true; 1593 term.setActive(&terminal); 1594 return show_err; 1595 }; 1596 terminal = try term.Terminal.init(io, allocator); 1597 terminal_live = true; 1598 term.setActive(&terminal); 1599 last_size = .{ .width = 0, .height = 0 }; 1600 }, 1601 .sync_events => { 1602 try nav.root.build(allocator, .{ 1603 .min_size = .{ .width = null, .height = null }, 1604 .max_size = .{ .width = last_size.width, .height = last_size.height }, 1605 }, nav.root.getFocus()); 1606 _ = try terminal.render(&nav.root, &last_grid, &last_size); 1607 try Repo.Events.performSync(allocator, session); 1608 }, 1609 } 1610 } 1611 1612 // persist queued actions when there's a repo; local mode has none, 1613 // so just apply in-memory 1614 if (repo_maybe) |repo| { 1615 try session.applyAndWritePending(io, allocator, repo); 1616 // reload so the next navigation builds its page from the current moment 1617 try session.reloadMoment(allocator, repo); 1618 } else { 1619 session.applyPending(); 1620 } 1621 1622 // reconcile navigation: forward to a new page, or back on escape. 1623 try nav.sync(allocator, session); 1624 1625 // the quit button (on the quit tab) asks the host to tear down. 1626 if (session.quit_requested) terminal.requestQuit(); 1627 1628 try nav.root.build(allocator, .{ 1629 .min_size = .{ .width = null, .height = null }, 1630 .max_size = .{ .width = last_size.width, .height = last_size.height }, 1631 }, nav.root.getFocus()); 1632 } 1633 } 1634 1635 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 1636 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 1637 null 1638 else 1639 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 1640 defer if (tty) |file| file.close(io); 1641 1642 const input = if (tty) |file| file else std.Io.File.stdin(); 1643 const output = if (tty) |file| file else std.Io.File.stdout(); 1644 1645 var write_buf: [1024]u8 = undefined; 1646 var writer = output.writer(io, &write_buf); 1647 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}); 1648 try writer.interface.flush(); 1649 1650 var read_buf: [1]u8 = undefined; 1651 var reader = input.reader(io, &read_buf); 1652 while (true) { 1653 const byte = try reader.interface.takeByte(); 1654 if (byte == '\r' or byte == '\n') return; 1655 } 1656 } 1657 1658 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 1659 const root_focus = root.getFocus(); 1660 const focused_before = root_focus.grandchild_id; 1661 var activate_in_page_link = false; 1662 switch (key) { 1663 // request a navigation pop; the host's Nav.sync goes back a page, or 1664 // quits when there's no history left. 1665 .escape => { 1666 session.nav_back = true; 1667 return; 1668 }, 1669 .ctrl => |letter| switch (letter) { 1670 'r' => { 1671 session.refresh_requested = true; 1672 return; 1673 }, 1674 else => try root.input(allocator, key, root_focus), 1675 }, 1676 .enter => { 1677 if (root_focus.grandchild_id) |gid| { 1678 // follow a cross-page link 1679 if (crossPageLink(root_focus, gid, session.data)) |route| { 1680 return session.navigate(route); 1681 } 1682 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 1683 } 1684 activate_in_page_link = true; 1685 try root.input(allocator, key, root_focus); 1686 }, 1687 .mouse => |mouse| { 1688 if (mouse.action == .press and mouse.action.press == .left) { 1689 var clicked: ?usize = null; 1690 var iter = root_focus.children.iterator(); 1691 while (iter.next()) |entry| { 1692 const child = entry.value_ptr.*; 1693 if (!child.focus.focusable) continue; 1694 const r = child.rect; 1695 if (mouse.x >= r.x and mouse.y >= r.y and 1696 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 1697 { 1698 clicked = entry.key_ptr.*; 1699 break; 1700 } 1701 } 1702 if (clicked) |focus_id| { 1703 // follow a cross-page link 1704 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 1705 return session.navigate(route); 1706 } 1707 if (rawLink(root_focus, focus_id)) |url| { 1708 if (!mouse.ctrl) try requestRawLink(session, url); 1709 return; 1710 } 1711 root_focus.setFocus(focus_id); 1712 activate_in_page_link = true; 1713 } 1714 // forward the press into the widget tree so buttons (and any 1715 // future click-aware widgets) can react. widgets that don't 1716 // care about presses ignore it. 1717 try root.input(allocator, key, root_focus); 1718 } else { 1719 try root.input(allocator, key, root_focus); 1720 } 1721 }, 1722 else => try root.input(allocator, key, root_focus), 1723 } 1724 1725 const focused_after = root_focus.grandchild_id; 1726 if (!activate_in_page_link and focused_before == focused_after) return; 1727 const focus_id = focused_after orelse return; 1728 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 1729 } 1730 1731 // a link to bytes the server serves directly rather than to a page route, so 1732 // it never resolves to a RoutablePage 1733 pub const raw_link_prefix = "ax:"; 1734 1735 // a route already loaded into the current page; focusing it changes the url 1736 // without rebuilding the page 1737 pub const in_page_link_prefix = "ai:"; 1738 1739 // an in-page tab's base route, or the exact current route when it is selected 1740 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 1741 const target = if (selected) session.data.current_page else route; 1742 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 1743 } 1744 1745 // a button the web overlay covers with a file picker that posts the chosen 1746 // file to the url after the prefix 1747 pub const file_input_prefix = "file:"; 1748 1749 // a submit button that overrides its enclosing form's action 1750 pub const submit_action_prefix = "submit:"; 1751 1752 // the url an `ax:` link points at, or null for anything else. a host that 1753 // suppresses the anchor's own navigation needs this to follow the link. 1754 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 1755 const child = root_focus.children.get(focus_id) orelse return null; 1756 const custom = switch (child.focus.kind) { 1757 .custom => |c| c, 1758 else => return null, 1759 }; 1760 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 1761 return custom[raw_link_prefix.len..]; 1762 } 1763 1764 fn requestRawLink(session: *Session, url: []const u8) !void { 1765 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 1766 } 1767 1768 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 1769 return if (session.web_port) |port| 1770 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 1771 else 1772 path; 1773 } 1774 1775 // if the focus target at focus_id is an `a:` link that should navigate (a 1776 // different parent page, or a different files directory within the repo page), 1777 // return its route; otherwise null. lets a host turn a click / enter on such a 1778 // link into a navigation rather than a focus change. 1779 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 1780 const current = data.current_page; 1781 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 1782 // a link to a different parent page always navigates; within a page, a 1783 // files-directory / commits-page / list-window change navigates while tab 1784 // links stay in-page. 1785 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 1786 return null; 1787 } 1788 1789 // the route carried by an `ai:` focus target, or null for any other target 1790 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 1791 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 1792 } 1793 1794 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 1795 const child = root_focus.children.get(focus_id) orelse return null; 1796 const custom = switch (child.focus.kind) { 1797 .custom => |c| c, 1798 else => return null, 1799 }; 1800 if (!std.mem.startsWith(u8, custom, prefix)) return null; 1801 const path = custom[prefix.len..]; 1802 // local sessions build (and parse) links with the repo identity elided 1803 return if (data.is_local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 1804 } 1805 1806 // a display author, resolved against the admin db at read time 1807 pub const Author = union(enum) { 1808 unknown, 1809 email: []const u8, // an email no user matches 1810 user_name: []const u8, 1811 1812 // the commit author line's email, resolved like initFromEmail 1813 pub fn init( 1814 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1815 arena: *std.heap.ArenaAllocator, 1816 author_line: []const u8, 1817 ) !Author { 1818 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 1819 } 1820 1821 // from a record's stored author email: it resolves to the user currently 1822 // holding it, else stays the email. the result is allocated in `arena`, 1823 // so the email may borrow transient memory. 1824 pub fn initFromEmail( 1825 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1826 arena: *std.heap.ArenaAllocator, 1827 author_email: ?[]const u8, 1828 ) !Author { 1829 const email = author_email orelse return .unknown; 1830 if (haxy_moment) |moment| { 1831 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 1832 return .{ .user_name = user.event.name }; 1833 } 1834 } 1835 return .{ .email = try arena.allocator().dupe(u8, email) }; 1836 } 1837 }; 1838 1839 // the "a:" link to user `name`'s page. 1840 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 1841 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 1842 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 1843 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1844 } 1845 1846 // a focusable " author " box showing `author`, linking to their user page 1847 // when it is a known user 1848 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox(Widget) { 1849 const text = switch (author) { 1850 .unknown => "", 1851 .email, .user_name => |t| t, 1852 }; 1853 var tb = try wgt.TextBox(Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 1854 errdefer tb.deinit(allocator); 1855 tb.getFocus().focusable = true; 1856 switch (author) { 1857 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 1858 else => {}, 1859 } 1860 return tb; 1861 } 1862 1863 // native-TUI navigation 1864 pub const Nav = struct { 1865 root: Widget, 1866 route: RoutablePage, 1867 // backs the current page (its Page data and the page-scoped strings its 1868 // widgets build). owned here, swapped on each navigation. session.page_arena 1869 // tracks whichever of these is current so widgets allocate into it. 1870 arena: *std.heap.ArenaAllocator, 1871 history: std.ArrayList(Entry), 1872 1873 // each retained page keeps its own arena, freed when the page leaves the 1874 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 1875 // long-lived session from accumulating every page it ever visited. 1876 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 1877 1878 // cap on retained back-history so the chain can't grow memory without bound 1879 const max_history: usize = 16; 1880 1881 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 1882 const arena = try allocator.create(std.heap.ArenaAllocator); 1883 arena.* = std.heap.ArenaAllocator.init(allocator); 1884 errdefer { 1885 arena.deinit(); 1886 allocator.destroy(arena); 1887 } 1888 1889 session.page_arena = arena; 1890 const route = session.data.current_page; 1891 1892 const page = try arena.allocator().create(Page); 1893 page.* = try Page.init(arena, session, route); 1894 return .{ 1895 .root = try initRoot(allocator, page, session), 1896 .route = route, 1897 .arena = arena, 1898 .history = .empty, 1899 }; 1900 } 1901 1902 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 1903 self.root.deinit(allocator); 1904 freeArena(allocator, self.arena); 1905 for (self.history.items) |*entry| { 1906 entry.root.deinit(allocator); 1907 freeArena(allocator, entry.arena); 1908 } 1909 self.history.deinit(allocator); 1910 } 1911 1912 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 1913 arena.deinit(); 1914 allocator.destroy(arena); 1915 } 1916 1917 // reconcile the displayed root with the session's navigation state. a 1918 // forward nav (current_page moved to a different parent page) pushes the 1919 // current root and builds the new page in a fresh arena; a back request 1920 // frees the current page and restores the previous one. when escape is 1921 // pressed with no history left we switch to the quit tab instead of quitting. 1922 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 1923 // refresh: rebuild the current page in place from a current moment 1924 if (session.refresh_requested) { 1925 session.refresh_requested = false; 1926 if (session.haxy_moment != null or session.local != null) { 1927 const arena = try allocator.create(std.heap.ArenaAllocator); 1928 arena.* = std.heap.ArenaAllocator.init(allocator); 1929 errdefer freeArena(allocator, arena); 1930 1931 session.page_arena = arena; 1932 1933 const route = session.data.current_page; 1934 const page = try arena.allocator().create(Page); 1935 page.* = try Page.init(arena, session, route); 1936 const new_root = try initRoot(allocator, page, session); 1937 1938 self.root.deinit(allocator); 1939 freeArena(allocator, self.arena); 1940 self.root = new_root; 1941 self.route = route; 1942 self.arena = arena; 1943 } 1944 return; 1945 } 1946 1947 if (session.nav_back) { 1948 session.nav_back = false; 1949 if (self.history.pop()) |entry| { 1950 self.root.deinit(allocator); 1951 freeArena(allocator, self.arena); 1952 self.root = entry.root; 1953 self.route = entry.route; 1954 self.arena = entry.arena; 1955 session.page_arena = entry.arena; 1956 session.data.current_page = entry.route; 1957 chooseAnsiArtForNavigation(session); 1958 return; 1959 } 1960 // nothing to go back to; switch to the quit confirmation 1961 const root_focus = self.root.getFocus(); 1962 var iter = root_focus.children.iterator(); 1963 while (iter.next()) |entry| { 1964 switch (entry.value_ptr.focus.kind) { 1965 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1966 root_focus.setFocus(entry.key_ptr.*); 1967 // a Stack only builds its selected child, so the quit 1968 // button isn't in the focus tree until a build runs with 1969 // the quit tab selected. build once, then send a 1970 // synthetic arrow_down to drop focus from the tab onto 1971 // the button so it's ready to confirm. 1972 try self.root.build(allocator, .{ 1973 .min_size = .{ .width = null, .height = 40 }, 1974 .max_size = .{ .width = 80, .height = null }, 1975 }, root_focus); 1976 try self.root.input(allocator, .arrow_down, root_focus); 1977 break; 1978 }, 1979 else => {}, 1980 } 1981 } 1982 return; 1983 } 1984 1985 // forward navigation: navigate() set next_page to the page to move to (a 1986 // cross-page link or tab change crossing pages) 1987 if (session.next_page) |route| { 1988 session.next_page = null; 1989 if (session.haxy_moment == null and session.local == null) return; 1990 // the page we navigated to becomes the current page 1991 session.data.current_page = route; 1992 1993 const arena = try allocator.create(std.heap.ArenaAllocator); 1994 arena.* = std.heap.ArenaAllocator.init(allocator); 1995 errdefer freeArena(allocator, arena); 1996 1997 session.page_arena = arena; 1998 1999 const page = try arena.allocator().create(Page); 2000 page.* = try Page.init(arena, session, route); 2001 const new_root = try initRoot(allocator, page, session); 2002 2003 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 2004 // drop the oldest entry (freeing its widget tree and arena) once over cap 2005 if (self.history.items.len > max_history) { 2006 var oldest = self.history.orderedRemove(0); 2007 oldest.root.deinit(allocator); 2008 freeArena(allocator, oldest.arena); 2009 } 2010 self.root = new_root; 2011 self.route = route; 2012 self.arena = arena; 2013 } 2014 } 2015 }; 2016 2017 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2018 const page_widget: Widget = switch (page.*) { 2019 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2020 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2021 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2022 }; 2023 2024 chooseAnsiArtForNavigation(session); 2025 2026 // on the TUI/SSH, the page sits above a one-row footer showing the url 2027 var root = if (session.is_terminal) blk: { 2028 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2029 errdefer box.deinit(allocator); 2030 const bg_id = bg_blk: { 2031 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2032 errdefer background.deinit(allocator); 2033 const id = background.getFocus().id; 2034 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2035 break :bg_blk id; 2036 }; 2037 { 2038 var footer = try widget.Footer.init(allocator, session); 2039 errdefer footer.deinit(allocator); 2040 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2041 } 2042 box.getFocus().child_id = bg_id; 2043 break :blk Widget{ .box = box }; 2044 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2045 errdefer root.deinit(allocator); 2046 2047 // input-owning views build their TextInputs in init, so reset their 2048 // registrations here 2049 session.text_inputs.clearRetainingCapacity(); 2050 session.input_values.clearRetainingCapacity(); 2051 2052 try root.build(allocator, .{ 2053 .min_size = .{ .width = null, .height = 40 }, 2054 .max_size = .{ .width = 100, .height = null }, 2055 }, root.getFocus()); 2056 2057 return root; 2058 } 2059 2060 // native and server-side sessions choose from the embedded collection. WASM 2061 // keeps the art serialized by the server instead. 2062 fn chooseAnsiArtForNavigation(session: *Session) void { 2063 if (comptime ansi_arts.len == 0) return; 2064 const io = session.io orelse return; 2065 var random: usize = undefined; 2066 io.random(std.mem.asBytes(&random)); 2067 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2068 } 2069