╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
864 .name = Array(repo_route_max_len).from(identity) orelse return null, 865 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 866 .theirs = Array(theirs_route_max_len).from(theirs) orelse return null, 867 .view = .resolve, 868 } }; 869 } 870 871 // an inline, owned array of data. keeping it in the route (rather than a 872 // borrowed slice) makes RoutablePage a plain value: it can be copied, stored 873 // in history, and serialized without any arena tracking. 874 pub fn Array(comptime max_len: usize) type { 875 return struct { 876 bytes: [max_len]u8 = undefined, 877 len: u16 = 0, 878 879 pub fn from(s: []const u8) ?Array(max_len) { 880 if (s.len > max_len) return null; 881 var name = Array(max_len){ .len = @intCast(s.len) }; 882 @memcpy(name.bytes[0..s.len], s); 883 return name; 884 } 885 886 pub fn slice(self: *const Array(max_len)) []const u8 { 887 return self.bytes[0..self.len]; 888 } 889 890 // round-trip as a json string instead of an array of numbers. 891 pub fn jsonStringify(self: *const Array(max_len), jw: anytype) !void { 892 try jw.write(self.slice()); 893 } 894 895 pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !Array(max_len) { 896 const s = try std.json.innerParse([]const u8, allocator, source, options); 897 // ValueTooLong is std.json's error for an over-long field. 898 return from(s) orelse error.ValueTooLong; 899 } 900 }; 901 } 902 903 pub fn toUrl(self: RoutablePage, arena: *std.heap.ArenaAllocator) ![]const u8 { 904 return switch (self) { 905 .home_users => |start| if (start == 0) @as([]const u8, "/users") else try std.fmt.allocPrint(arena.allocator(), "/users/" ++ start_seg ++ "{d}", .{start}), 906 .home_repos => |start| if (start == 0) @as([]const u8, "/repos") else try std.fmt.allocPrint(arena.allocator(), "/repos/" ++ start_seg ++ "{d}", .{start}), 907 .home_settings => "/settings", 908 .home_auth => "/auth", 909 .user_repos => |u| if (u.start == 0) 910 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/repos", .{u.name.slice()}) 911 else 912 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/repos/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 913 .user_forks => |u| if (u.start == 0) 914 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/forks", .{u.name.slice()}) 915 else 916 try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/forks/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 917 .user_settings => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/settings", .{name.slice()}), 918 .user_auth => |name| try std.fmt.allocPrint(arena.allocator(), user_segment ++ "{s}/auth", .{name.slice()}), 919 .repo_files => |f| blk: { 920 const prefix = try repoUrlPrefix(arena, f.name.slice()); 921 if (f.ref_kind == null and f.patchrev_id.len == 0 and f.find.len == 0 and f.line == 0) break :blk if (prefix.len == 0) "/" else prefix; 922 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 923 try out.writer.print("{s}/" ++ files_seg, .{prefix}); 924 if (f.ref_kind) |kind| if (f.ref_value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), f.ref_value.slice() }); 925 if (f.patchrev_id.len != 0) try out.writer.print("/patchrev:{s}", .{f.patchrev_id.slice()}); 926 if (f.find.len != 0) try out.writer.print("/" ++ find_seg ++ "{s}", .{f.find.slice()}); 927 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 928 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 929 break :blk out.written(); 930 }, 931 .repo_commits => |c| blk: { 932 const prefix = try repoUrlPrefix(arena, c.name.slice()); 933 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 934 try out.writer.print("{s}/" ++ commits_seg, .{prefix}); 935 if (c.ref_or_oid) |kind| if (c.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), c.value.slice() }); 936 if (c.base_oid.len != 0) try out.writer.print("/" ++ base_seg ++ "{s}", .{c.base_oid.slice()}); 937 switch (c.content) { 938 .diff => |d| { 939 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 940 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 941 }, 942 .message => try out.writer.print("/" ++ message_seg, .{}), 943 } 944 break :blk out.written(); 945 }, 946 .repo_diff => |d| blk: { 947 const prefix = try repoUrlPrefix(arena, d.name.slice()); 948 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 949 try out.writer.print("{s}/diff", .{prefix}); 950 if (d.ref_or_oid) |kind| if (d.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), d.value.slice() }); 951 if (d.patchrev_id.len != 0) try out.writer.print("/patchrev:{s}", .{d.patchrev_id.slice()}); 952 if (d.base_oid.len != 0) try out.writer.print("/" ++ base_seg ++ "{s}", .{d.base_oid.slice()}); 953 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 954 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 955 break :blk out.written(); 956 }, 957 .repo_refs => |r| blk: { 958 const prefix = try repoUrlPrefix(arena, r.name.slice()); 959 break :blk if (r.from.len == 0) 960 try std.fmt.allocPrint(arena.allocator(), "{s}/refs", .{prefix}) 961 else 962 try std.fmt.allocPrint(arena.allocator(), "{s}/refs/{s}:{s}", .{ prefix, @tagName(r.kind), r.from.slice() }); 963 }, 964 .repo_issues => |i| blk: { 965 const prefix = try repoUrlPrefix(arena, i.name.slice()); 966 if (i.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/edit", .{ prefix, i.selected.slice() }); 967 if (i.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/description", .{ prefix, i.selected.slice() }); 968 if (i.view == .resolve) break :blk if (i.theirs.len == 0) 969 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/resolve", .{ prefix, i.selected.slice() }) 970 else 971 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, i.selected.slice(), i.theirs.slice() }); 972 if (i.view == .new_comment) break :blk if (i.comment.len == 0) 973 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/new", .{ prefix, i.selected.slice() }) 974 else 975 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, i.selected.slice(), i.comment.slice() }); 976 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() }); 977 if (i.view == .remove) break :blk if (i.comment.len == 0) 978 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/remove", .{ prefix, i.selected.slice() }) 979 else 980 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, i.selected.slice(), i.comment.slice() }); 981 if (i.view == .conflicts) break :blk if (i.selected.len == 0) 982 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/conflicts", .{prefix}) 983 else 984 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/" ++ start_seg ++ "{s}/conflicts", .{ prefix, i.selected.slice() }); 985 if (i.comment.len != 0) break :blk if (i.comments_start == 0) 986 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, i.selected.slice(), i.comment.slice() }) 987 else 988 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 }); 989 if (i.selected.len != 0) break :blk if (i.comments_start == 0) 990 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}", .{ prefix, i.selected.slice() }) 991 else 992 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ issue_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, i.selected.slice(), i.comments_start }); 993 break :blk if (i.tag.len == 0) 994 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}", .{ prefix, @tagName(i.view) }) 995 else 996 try std.fmt.allocPrint(arena.allocator(), "{s}/issues/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(i.view), i.tag.slice() }); 997 }, 998 .repo_patches => |p| blk: { 999 const prefix = try repoUrlPrefix(arena, p.name.slice()); 1000 if (p.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/edit", .{ prefix, p.selected.slice() }); 1001 if (p.view == .publish) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/publish", .{ prefix, p.selected.slice() }); 1002 if (p.view == .merge) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/merge", .{ prefix, p.selected.slice() }); 1003 if (p.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/description", .{ prefix, p.selected.slice() }); 1004 if (p.view == .resolve) break :blk if (p.theirs.len == 0) 1005 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/resolve", .{ prefix, p.selected.slice() }) 1006 else 1007 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, p.selected.slice(), p.theirs.slice() }); 1008 if (p.view == .new_comment) break :blk if (p.comment.len == 0) 1009 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/new", .{ prefix, p.selected.slice() }) 1010 else 1011 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, p.selected.slice(), p.comment.slice() }); 1012 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() }); 1013 if (p.view == .remove) break :blk if (p.comment.len == 0) 1014 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/remove", .{ prefix, p.selected.slice() }) 1015 else 1016 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, p.selected.slice(), p.comment.slice() }); 1017 if (p.view == .conflicts) break :blk if (p.selected.len == 0) 1018 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/conflicts", .{prefix}) 1019 else 1020 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/" ++ start_seg ++ "{s}/conflicts", .{ prefix, p.selected.slice() }); 1021 if (p.comment.len != 0) break :blk if (p.comments_start == 0) 1022 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, p.selected.slice(), p.comment.slice() }) 1023 else 1024 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 }); 1025 if (p.selected.len != 0) break :blk if (p.comments_start == 0) 1026 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}", .{ prefix, p.selected.slice() }) 1027 else 1028 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ patch_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, p.selected.slice(), p.comments_start }); 1029 break :blk if (p.tag.len == 0) 1030 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/{s}", .{ prefix, @tagName(p.view) }) 1031 else 1032 try std.fmt.allocPrint(arena.allocator(), "{s}/patches/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(p.view), p.tag.slice() }); 1033 }, 1034 .repo_discussions => |t| blk: { 1035 const prefix = try repoUrlPrefix(arena, t.name.slice()); 1036 if (t.view == .edit) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/edit", .{ prefix, t.selected.slice() }); 1037 if (t.view == .description) break :blk try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/description", .{ prefix, t.selected.slice() }); 1038 if (t.view == .new_comment) break :blk if (t.comment.len == 0) 1039 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/new", .{ prefix, t.selected.slice() }) 1040 else 1041 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, t.selected.slice(), t.comment.slice() }); 1042 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() }); 1043 if (t.view == .remove) break :blk if (t.comment.len == 0) 1044 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/remove", .{ prefix, t.selected.slice() }) 1045 else 1046 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, t.selected.slice(), t.comment.slice() }); 1047 if (t.comment.len != 0) break :blk if (t.comments_start == 0) 1048 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, t.selected.slice(), t.comment.slice() }) 1049 else 1050 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 }); 1051 if (t.selected.len != 0) break :blk if (t.comments_start == 0) 1052 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}", .{ prefix, t.selected.slice() }) 1053 else 1054 try std.fmt.allocPrint(arena.allocator(), "{s}/" ++ discuss_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, t.selected.slice(), t.comments_start }); 1055 break :blk if (t.tag.len == 0) 1056 try std.fmt.allocPrint(arena.allocator(), "{s}/discussions/{s}", .{ prefix, @tagName(t.view) }) 1057 else 1058 try std.fmt.allocPrint(arena.allocator(), "{s}/discussions/{s}/" ++ tag_filter_seg ++ "{s}", .{ prefix, @tagName(t.view), t.tag.slice() }); 1059 }, 1060 .repo_events => |e| blk: { 1061 const prefix = try repoUrlPrefix(arena, e.name.slice()); 1062 break :blk if (e.kind) |kind| 1063 try std.fmt.allocPrint(arena.allocator(), "{s}/event:{s}/kind:{s}", .{ prefix, e.selected.slice(), @tagName(kind) }) 1064 else 1065 try std.fmt.allocPrint(arena.allocator(), "{s}/events/{s}", .{ prefix, @tagName(e.view) }); 1066 }, 1067 .repo_settings => |name| try std.fmt.allocPrint(arena.allocator(), "{s}/settings", .{try repoUrlPrefix(arena, name.slice())}), 1068 .repo_auth => |name| try std.fmt.allocPrint(arena.allocator(), "{s}/auth", .{try repoUrlPrefix(arena, name.slice())}), 1069 .fork_patch => |f| try std.fmt.allocPrint(arena.allocator(), fork_segment ++ "{s}/" ++ patch_seg ++ "{s}", .{ f.name.slice(), f.id.slice() }), 1070 .fork_diff => |d| blk: { 1071 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1072 try out.writer.print(fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/diff", .{ d.fork.name.slice(), d.fork.id.slice() }); 1073 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 1074 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 1075 break :blk out.written(); 1076 }, 1077 .fork_files => |f| blk: { 1078 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1079 try out.writer.print(fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/" ++ files_seg, .{ f.fork.name.slice(), f.fork.id.slice() }); 1080 if (f.oid.len != 0) try out.writer.print("/object:{s}", .{f.oid.slice()}); 1081 if (f.find.len != 0) try out.writer.print("/" ++ find_seg ++ "{s}", .{f.find.slice()}); 1082 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 1083 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 1084 break :blk out.written(); 1085 }, 1086 .fork_commits => |c| blk: { 1087 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1088 try out.writer.print(fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/" ++ commits_seg, .{ c.fork.name.slice(), c.fork.id.slice() }); 1089 if (c.oid.len != 0) try out.writer.print("/object:{s}", .{c.oid.slice()}); 1090 switch (c.content) { 1091 .diff => |d| { 1092 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 1093 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 1094 }, 1095 .message => try out.writer.print("/" ++ message_seg, .{}), 1096 } 1097 break :blk out.written(); 1098 }, 1099 .fork_settings => |f| try std.fmt.allocPrint(arena.allocator(), fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/settings", .{ f.name.slice(), f.id.slice() }), 1100 .fork_auth => |f| try std.fmt.allocPrint(arena.allocator(), fork_segment ++ "{s}/" ++ patch_seg ++ "{s}/auth", .{ f.name.slice(), f.id.slice() }), 1101 }; 1102 } 1103 1104 pub fn fromUrl(path: []const u8) ?RoutablePage { 1105 if (std.mem.eql(u8, path, "/")) return default; 1106 if (path.len < 2 or path[0] != '/') return null; 1107 var segments = std.mem.splitScalar(u8, path[1..], '/'); 1108 const first = segments.next() orelse return null; 1109 if (std.mem.eql(u8, first, "users")) return .{ .home_users = listStart(&segments) orelse return null }; 1110 if (std.mem.eql(u8, first, "repos")) return .{ .home_repos = listStart(&segments) orelse return null }; 1111 if (std.mem.eql(u8, first, "settings")) return if (segments.next() == null) .home_settings else null; 1112 if (std.mem.eql(u8, first, "auth")) return if (segments.next() == null) .home_auth else null; 1113 // "user/<name>[/repos[/start:<n>]|/settings|/auth]" 1114 if (std.mem.eql(u8, first, "user")) { 1115 const name = segments.next() orelse return null; 1116 if (name.len == 0) return null; 1117 const parsed = Array(evt.User.name_max_len).from(name) orelse return null; // name too long 1118 const sub = segments.next() orelse return .{ .user_repos = .{ .name = parsed } }; 1119 if (std.mem.eql(u8, sub, "repos")) return .{ .user_repos = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 1120 if (std.mem.eql(u8, sub, "forks")) return .{ .user_forks = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 1121 if (std.mem.eql(u8, sub, "settings")) return if (segments.next() == null) .{ .user_settings = parsed } else null; 1122 if (std.mem.eql(u8, sub, "auth")) return if (segments.next() == null) .{ .user_auth = parsed } else null; 1123 return null; // unknown sub-path 1124 } 1125 // "fork/<username>/<reponame>/patch:<id>[/files|/commits]"; the 1126 // patch branch is implicit, while object ids and view-specific params 1127 // use the same tails as the repo files and commits routes. 1128 if (std.mem.eql(u8, first, "fork")) { 1129 const rest = segments.rest(); 1130 const owner = segments.next() orelse return null; 1131 const repo_name = segments.next() orelse return null; 1132 if (owner.len == 0 or repo_name.len == 0) return null; 1133 const identity = rest[0 .. owner.len + 1 + repo_name.len]; 1134 const patch = segments.next() orelse return null; 1135 if (!std.mem.startsWith(u8, patch, patch_seg)) return null; 1136 const id = patch[patch_seg.len..]; 1137 if (id.len != evt.event_id_size * 2) return null; 1138 const tab = segments.next() orelse return forkPatchRoute(identity, id); 1139 var params = Params{}; 1140 if (std.mem.eql(u8, tab, "diff")) { 1141 params.scanPairs(&segments) catch return null; 1142 if (!params.only(&.{.start})) return null; 1143 return forkDiffRoute(identity, id, params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1144 } 1145 if (std.mem.eql(u8, tab, files_seg)) { 1146 params.scanPairs(&segments) catch return null; 1147 if (!params.only(&.{ .line, .object, .find })) return null; 1148 const line = params.line() orelse return null; 1149 const ref = params.ref() catch return null; 1150 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1151 const file_path = pathValue(segments.rest()) orelse return null; 1152 var route = forkFilesRoute(identity, id, oid, file_path, line) orelse return null; 1153 if (params.values.get(.find)) |find| route.fork_files.find = Array(find_route_max_len).from(find) orelse return null; 1154 return route; 1155 } 1156 if (std.mem.eql(u8, tab, commits_seg)) { 1157 params.scanPairs(&segments) catch return null; 1158 if (!params.only(&.{ .start, .object })) return null; 1159 const start = params.start() orelse return null; 1160 const ref = params.ref() catch return null; 1161 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1162 if (std.mem.eql(u8, segments.rest(), message_seg)) 1163 return if (start == 0 and oid.len != 0) forkCommitMessageRoute(identity, id, oid) else null; 1164 const file_path = pathValue(segments.rest()) orelse return null; 1165 return forkCommitsRoute(identity, id, oid, start, file_path); 1166 } 1167 if (std.mem.eql(u8, tab, "settings")) return if (segments.next() == null) forkSettingsRoute(identity, id) else null; 1168 if (std.mem.eql(u8, tab, "auth")) return if (segments.next() == null) forkAuthRoute(identity, id) else null; 1169 return null; 1170 } 1171 // "repo/<username>/<reponame>[/<tab tail>]"; the bare pair is the 1172 // files root 1173 if (std.mem.eql(u8, first, "repo")) { 1174 const rest = segments.rest(); // "username/reponame[/...]" 1175 const owner = segments.next() orelse return null; 1176 const repo_name = segments.next() orelse return null; 1177 // reject an empty username or reponame 1178 if (owner.len == 0 or repo_name.len == 0) return null; 1179 const pair = rest[0 .. owner.len + 1 + repo_name.len]; // "username/reponame" 1180 if (segments.peek() != null) { 1181 return repoSubRoute(pair, segments.rest()); 1182 } 1183 return repoFilesRoute(pair, null, "", "", 0); 1184 } 1185 return null; 1186 } 1187 1188 // parse a local-mode url, which elides the "/repo/<owner>/<name>" prefix: 1189 // "/" is the files root and the sub-paths follow directly. 1190 pub fn fromUrlLocal(path: []const u8) ?RoutablePage { 1191 if (std.mem.eql(u8, path, "/")) return repoFilesRoute("", null, "", "", 0); 1192 if (path.len < 2 or path[0] != '/') return null; 1193 return repoSubRoute("", path[1..]); 1194 } 1195 1196 // the url prefix for a repo route's identity: "/repo/<identity>", or "" 1197 // when the identity is elided. 1198 fn repoUrlPrefix(arena: *std.heap.ArenaAllocator, identity: []const u8) ![]const u8 { 1199 if (identity.len == 0) return ""; 1200 return std.fmt.allocPrint(arena.allocator(), repo_segment ++ "{s}", .{identity}); 1201 } 1202 1203 // true when the page builds at its content height on the web, letting the 1204 // browser scroll the whole page, rather than being pinned to the viewport 1205 pub fn fullHeight(self: RoutablePage) bool { 1206 return switch (self) { 1207 .repo_issues => |i| i.view == .resolve, 1208 .repo_patches => |p| p.view == .resolve, 1209 else => false, 1210 }; 1211 } 1212 1213 // the "owner/name" a repo route carries, null for any other route. the 1214 // result borrows the route, so it must outlive the slice. 1215 pub fn repoIdentity(self: *const RoutablePage) ?[]const u8 { 1216 return switch (self.*) { 1217 .repo_files => |*f| f.name.slice(), 1218 .repo_commits => |*c| c.name.slice(), 1219 .repo_diff => |*d| d.name.slice(), 1220 .repo_refs => |*r| r.name.slice(), 1221 .repo_issues => |*i| i.name.slice(), 1222 .repo_patches => |*p| p.name.slice(), 1223 .repo_discussions => |*t| t.name.slice(), 1224 .repo_events => |*e| e.name.slice(), 1225 .repo_settings, .repo_auth => |*name| name.slice(), 1226 else => null, 1227 }; 1228 } 1229 1230 pub fn forkRoute(self: *const RoutablePage) ?*const ForkRoute { 1231 return switch (self.*) { 1232 .fork_patch => |*f| f, 1233 .fork_diff => |*d| &d.fork, 1234 .fork_files => |*f| &f.fork, 1235 .fork_commits => |*f| &f.fork, 1236 .fork_settings => |*f| f, 1237 .fork_auth => |*f| f, 1238 else => null, 1239 }; 1240 } 1241 1242 // the route this route's page lands on, with no tab or params: the users 1243 // list, a user's repos, or a repo's files root. 1244 pub fn pageRoot(self: RoutablePage) RoutablePage { 1245 return switch (self.parent()) { 1246 .home => .default, 1247 .user => switch (self) { 1248 .user_repos => |u| .{ .user_repos = .{ .name = u.name } }, 1249 .user_forks => |u| .{ .user_repos = .{ .name = u.name } }, 1250 .user_settings, .user_auth => |name| .{ .user_repos = .{ .name = name } }, 1251 else => self, 1252 }, 1253 // every repo route stores the same identity, so it always fits 1254 .repo => repoFilesRoute(self.repoIdentity() orelse return self, null, "", "", 0) orelse self, 1255 .fork => blk: { 1256 const f = self.forkRoute() orelse break :blk self; 1257 break :blk forkPatchRoute(f.name.slice(), f.id.slice()) orelse self; 1258 }, 1259 }; 1260 } 1261 1262 pub fn parent(self: RoutablePage) PageKind { 1263 return switch (self) { 1264 .home_users, .home_repos, .home_settings, .home_auth => .home, 1265 .user_repos, .user_forks, .user_settings, .user_auth => .user, 1266 .repo_files, .repo_commits, .repo_diff, .repo_refs, .repo_issues, .repo_patches, .repo_discussions, .repo_events, .repo_settings, .repo_auth => .repo, 1267 .fork_patch, .fork_diff, .fork_files, .fork_commits, .fork_settings, .fork_auth => .fork, 1268 }; 1269 } 1270 1271 // the dir a trailing "path:<dir>" names: the raw remainder ("" when 1272 // absent, null when malformed) 1273 fn pathValue(rest: []const u8) ?[]const u8 { 1274 if (rest.len == 0) return ""; 1275 if (!std.mem.startsWith(u8, rest, path_seg)) return null; 1276 const dir = rest[path_seg.len..]; 1277 return if (dir.len == 0) null else dir; 1278 } 1279 1280 // a list route's tail: at most a "start:<n>" param, null on anything else 1281 fn listStart(segments: *std.mem.SplitIterator(u8, .scalar)) ?usize { 1282 var params = Params{}; 1283 const word = params.scanTail(segments) catch return null; 1284 if (word != null or !params.only(&.{.start})) return null; 1285 return params.start(); 1286 } 1287 1288 // parse the sub-path after a repo route's identity ("" for a local 1289 // route): a tab keyword followed by "key:value" params in any order, plus 1290 // the files tab's trailing "path:<dir>". 1291 fn repoSubRoute(pair: []const u8, sub: []const u8) ?RoutablePage { 1292 var segments = std.mem.splitScalar(u8, sub, '/'); 1293 const tab = segments.next() orelse return null; 1294 var params = Params{}; 1295 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; 1296 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; 1297 if (std.mem.eql(u8, tab, "refs")) { 1298 const word = params.scanTail(&segments) catch return null; 1299 if (word != null or !params.only(&.{ .branch, .tag })) return null; 1300 const ref = (params.ref() catch return null) orelse return repoRefsRoute(pair, .branch, ""); 1301 const kind = std.meta.stringToEnum(RefKind, @tagName(ref.kind)) orelse return null; 1302 return repoRefsRoute(pair, kind, ref.value); 1303 } 1304 if (std.mem.startsWith(u8, tab, issue_seg)) { 1305 const issue_id = tab[issue_seg.len..]; 1306 if (issue_id.len == 0) return null; 1307 if (segments.peek()) |tail| { 1308 if (std.mem.startsWith(u8, tail, comment_seg)) { 1309 _ = segments.next(); 1310 const comment_id = tail[comment_seg.len..]; 1311 if (comment_id.len == 0) return null; 1312 const word = params.scanTail(&segments) catch return null; 1313 if (word) |last| { 1314 if (!params.only(&.{})) return null; 1315 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.issue, pair, issue_id, comment_id); 1316 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.issue, pair, issue_id, comment_id); 1317 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.issue, pair, issue_id, comment_id); 1318 return null; 1319 } 1320 if (!params.only(&.{.start})) return null; 1321 return repoThreadCommentRoute(.issue, pair, issue_id, comment_id, params.start() orelse return null); 1322 } 1323 } 1324 const word = params.scanTail(&segments) catch return null; 1325 if (word) |tail| { 1326 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.issue, pair, issue_id, "") else null; 1327 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.issue, pair, issue_id) else null; 1328 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.issue, pair, issue_id, "") else null; 1329 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.issue, pair, issue_id) else null; 1330 if (std.mem.eql(u8, tail, "resolve")) { 1331 if (!params.only(&.{.theirs})) return null; 1332 return repoIssuesResolveRoute(pair, issue_id, params.values.get(.theirs) orelse ""); 1333 } 1334 return null; 1335 } 1336 if (!params.only(&.{.start})) return null; 1337 return repoThreadCommentsRoute(.issue, pair, issue_id, params.start() orelse return null); 1338 } 1339 if (std.mem.startsWith(u8, tab, patch_seg)) { 1340 const patch_id = tab[patch_seg.len..]; 1341 if (patch_id.len == 0) return null; 1342 if (segments.peek()) |tail| { 1343 if (std.mem.startsWith(u8, tail, comment_seg)) { 1344 _ = segments.next(); 1345 const comment_id = tail[comment_seg.len..]; 1346 if (comment_id.len == 0) return null; 1347 const word = params.scanTail(&segments) catch return null; 1348 if (word) |last| { 1349 if (!params.only(&.{})) return null; 1350 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.patch, pair, patch_id, comment_id); 1351 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.patch, pair, patch_id, comment_id); 1352 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.patch, pair, patch_id, comment_id); 1353 return null; 1354 } 1355 if (!params.only(&.{.start})) return null; 1356 return repoThreadCommentRoute(.patch, pair, patch_id, comment_id, params.start() orelse return null); 1357 } 1358 } 1359 const word = params.scanTail(&segments) catch return null; 1360 if (word) |tail| { 1361 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.patch, pair, patch_id, "") else null; 1362 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.patch, pair, patch_id) else null; 1363 if (std.mem.eql(u8, tail, "publish")) return if (params.only(&.{})) repoPatchPublishRoute(pair, patch_id) else null; 1364 if (std.mem.eql(u8, tail, "merge")) return if (params.only(&.{})) repoPatchMergeRoute(pair, patch_id) else null; 1365 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.patch, pair, patch_id, "") else null; 1366 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.patch, pair, patch_id) else null; 1367 if (std.mem.eql(u8, tail, "resolve")) { 1368 if (!params.only(&.{.theirs})) return null; 1369 return repoPatchesResolveRoute(pair, patch_id, params.values.get(.theirs) orelse ""); 1370 } 1371 return null; 1372 } 1373 if (!params.only(&.{.start})) return null; 1374 return repoThreadCommentsRoute(.patch, pair, patch_id, params.start() orelse return null); 1375 } 1376 if (std.mem.startsWith(u8, tab, discuss_seg)) { 1377 const discussion_id = tab[discuss_seg.len..]; 1378 if (discussion_id.len == 0) return null; 1379 if (segments.peek()) |tail| { 1380 if (std.mem.startsWith(u8, tail, comment_seg)) { 1381 _ = segments.next(); 1382 const comment_id = tail[comment_seg.len..]; 1383 if (comment_id.len == 0) return null; 1384 const word = params.scanTail(&segments) catch return null; 1385 if (word) |last| { 1386 if (!params.only(&.{})) return null; 1387 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.discuss, pair, discussion_id, comment_id); 1388 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.discuss, pair, discussion_id, comment_id); 1389 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.discuss, pair, discussion_id, comment_id); 1390 return null; 1391 } 1392 if (!params.only(&.{.start})) return null; 1393 return repoThreadCommentRoute(.discuss, pair, discussion_id, comment_id, params.start() orelse return null); 1394 } 1395 } 1396 const word = params.scanTail(&segments) catch return null; 1397 if (word) |tail| { 1398 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.discuss, pair, discussion_id, "") else null; 1399 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.discuss, pair, discussion_id) else null; 1400 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.discuss, pair, discussion_id, "") else null; 1401 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.discuss, pair, discussion_id) else null; 1402 return null; 1403 } 1404 if (!params.only(&.{.start})) return null; 1405 return repoThreadCommentsRoute(.discuss, pair, discussion_id, params.start() orelse return null); 1406 } 1407 if (std.mem.eql(u8, tab, "issues")) { 1408 // the word is a list view; a filter with neither is never emitted. 1409 const word = params.scanTail(&segments) catch return null; 1410 const tag_value = params.values.get(.tag) orelse ""; 1411 const w = word orelse return if (params.only(&.{})) repoIssuesRoute(pair, .open, "", "") else null; 1412 // the conflicts list windows by an issue id, not a number, so it 1413 // reads the start param raw 1414 if (std.mem.eql(u8, w, "conflicts")) { 1415 if (!params.only(&.{.start})) return null; 1416 return repoIssuesConflictsRoute(pair, params.values.get(.start) orelse ""); 1417 } 1418 if (!params.only(&.{.tag})) return null; 1419 if (std.meta.stringToEnum(evt.Issue.Status, w)) |status| return repoIssuesRoute(pair, status, tag_value, ""); 1420 if (std.mem.eql(u8, w, "tags")) return repoThreadTagsRoute(.issue, pair, tag_value); 1421 if (std.mem.eql(u8, w, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.issue, pair) else null; 1422 return null; 1423 } 1424 if (std.mem.eql(u8, tab, "patches")) { 1425 const word = params.scanTail(&segments) catch return null; 1426 const tag_value = params.values.get(.tag) orelse ""; 1427 const view = word orelse return if (params.only(&.{})) repoPatchesRoute(pair, .open, "", "") else null; 1428 if (std.mem.eql(u8, view, "conflicts")) { 1429 if (!params.only(&.{.start})) return null; 1430 return repoPatchesConflictsRoute(pair, params.values.get(.start) orelse ""); 1431 } 1432 if (!params.only(&.{.tag})) return null; 1433 if (std.meta.stringToEnum(evt.Patch.StatusKind, view)) |status| return repoPatchesRoute(pair, status, tag_value, ""); 1434 if (std.mem.eql(u8, view, "tags")) return repoThreadTagsRoute(.patch, pair, tag_value); 1435 if (std.mem.eql(u8, view, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.patch, pair) else null; 1436 if (std.mem.eql(u8, view, "drafts")) return if (tag_value.len == 0) repoPatchesDraftsRoute(pair) else null; 1437 return null; 1438 } 1439 if (std.mem.eql(u8, tab, "discussions")) { 1440 const word = params.scanTail(&segments) catch return null; 1441 const tag_value = params.values.get(.tag) orelse ""; 1442 const view = word orelse return if (params.only(&.{})) repoDiscussionsRoute(pair, "", "") else null; 1443 if (!params.only(&.{.tag})) return null; 1444 if (std.mem.eql(u8, view, "recent")) return repoDiscussionsRoute(pair, tag_value, ""); 1445 if (std.mem.eql(u8, view, "tags")) return repoThreadTagsRoute(.discuss, pair, tag_value); 1446 if (std.mem.eql(u8, view, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.discuss, pair) else null; 1447 return null; 1448 } 1449 if (std.mem.eql(u8, tab, "events")) { 1450 const view = if (segments.next()) |word| 1451 std.meta.stringToEnum(EventsView, word) orelse return null 1452 else 1453 .active; 1454 return if (segments.next() == null) repoEventsRoute(pair, view, null, "") else null; 1455 } 1456 if (std.mem.startsWith(u8, tab, "event:")) { 1457 const event_id = tab["event:".len..]; 1458 const kind_segment = segments.next() orelse return null; 1459 if (!std.mem.startsWith(u8, kind_segment, "kind:")) return null; 1460 const kind = std.meta.stringToEnum(evt.EventKind, kind_segment["kind:".len..]) orelse return null; 1461 if (event_id.len == 0 or segments.next() != null) return null; 1462 return repoEventsRoute(pair, .active, kind, event_id); 1463 } 1464 if (std.mem.eql(u8, tab, files_seg)) { 1465 params.scanPairs(&segments) catch return null; 1466 if (params.values.get(.patchrev)) |id| { 1467 if (!params.only(&.{ .line, .patchrev })) return null; 1468 return repoPatchRevFilesRoute(pair, id, pathValue(segments.rest()) orelse return null, params.line() orelse return null); 1469 } 1470 if (!params.only(&.{ .line, .branch, .tag, .object, .find })) return null; 1471 const line = params.line() orelse return null; 1472 const dir = pathValue(segments.rest()) orelse return null; 1473 const find = params.values.get(.find) orelse ""; 1474 const ref = params.ref() catch return null; 1475 // a find route's `path` names the selected result, so unlike a 1476 // directory path it doesn't need a ref beside it 1477 var route = if (ref) |r| 1478 repoFilesRoute(pair, r.kind, r.value, dir, line) orelse return null 1479 else if (dir.len == 0 or find.len != 0) 1480 repoFilesRoute(pair, null, "", "", line) orelse return null 1481 else 1482 return null; 1483 if (find.len != 0) { 1484 route.repo_files.find = Array(find_route_max_len).from(find) orelse return null; 1485 route.repo_files.path = Array(repo_route_max_len).from(dir) orelse return null; 1486 } 1487 return route; 1488 } 1489 if (std.mem.eql(u8, tab, commits_seg)) { 1490 params.scanPairs(&segments) catch return null; 1491 if (!params.only(&.{ .start, .branch, .tag, .object, .base })) return null; 1492 const base_oid = params.values.get(.base) orelse ""; 1493 const start = params.start() orelse return null; 1494 const ref = (params.ref() catch return null) orelse { 1495 const file = pathValue(segments.rest()) orelse return null; 1496 return if (file.len == 0) repoCommitsRoute(pair, null, "", start, "", base_oid) else null; 1497 }; 1498 // the message page has no diff window or file filter 1499 if (std.mem.eql(u8, segments.rest(), message_seg)) 1500 return if (start == 0) repoCommitMessageRoute(pair, ref.kind, ref.value, base_oid) else null; 1501 const file = pathValue(segments.rest()) orelse return null; 1502 return repoCommitsRoute(pair, ref.kind, ref.value, start, file, base_oid); 1503 } 1504 if (std.mem.eql(u8, tab, "diff")) { 1505 params.scanPairs(&segments) catch return null; 1506 if (params.values.get(.patchrev)) |id| { 1507 if (!params.only(&.{ .start, .patchrev })) return null; 1508 return repoPatchRevDiffRoute(pair, id, params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1509 } 1510 if (!params.only(&.{ .start, .branch, .tag, .object, .base })) return null; 1511 const base_oid = params.values.get(.base) orelse return null; 1512 const start = params.start() orelse return null; 1513 const ref = params.ref() catch return null; 1514 const file = pathValue(segments.rest()) orelse return null; 1515 return repoDiffRoute(pair, if (ref) |r| r.kind else null, if (ref) |r| r.value else "", start, file, base_oid); 1516 } 1517 return null; // unknown sub-path 1518 } 1519 1520 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 1521 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1522 return switch (a) { 1523 .home_users => |a_start| a_start == b.home_users, 1524 .home_repos => |a_start| a_start == b.home_repos, 1525 .user_repos => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_repos.name.slice()) and a_u.start == b.user_repos.start, 1526 .user_forks => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_forks.name.slice()) and a_u.start == b.user_forks.start, 1527 .user_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.user_settings.slice()), 1528 .user_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.user_auth.slice()), 1529 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 1530 a_f.ref_kind == b.repo_files.ref_kind and 1531 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 1532 std.mem.eql(u8, a_f.patchrev_id.slice(), b.repo_files.patchrev_id.slice()) and 1533 std.mem.eql(u8, a_f.find.slice(), b.repo_files.find.slice()) and 1534 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 1535 a_f.line == b.repo_files.line, 1536 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 1537 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 1538 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 1539 std.mem.eql(u8, a_c.base_oid.slice(), b.repo_commits.base_oid.slice()) and 1540 switch (a_c.content) { 1541 .diff => |a_d| switch (b.repo_commits.content) { 1542 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1543 .message => false, 1544 }, 1545 .message => std.meta.activeTag(b.repo_commits.content) == .message, 1546 }, 1547 .repo_diff => |a_d| std.mem.eql(u8, a_d.name.slice(), b.repo_diff.name.slice()) and 1548 a_d.ref_or_oid == b.repo_diff.ref_or_oid and 1549 std.mem.eql(u8, a_d.value.slice(), b.repo_diff.value.slice()) and 1550 std.mem.eql(u8, a_d.base_oid.slice(), b.repo_diff.base_oid.slice()) and 1551 std.mem.eql(u8, a_d.patchrev_id.slice(), b.repo_diff.patchrev_id.slice()) and 1552 a_d.start == b.repo_diff.start and std.mem.eql(u8, a_d.path.slice(), b.repo_diff.path.slice()), 1553 .repo_refs => |a_r| std.mem.eql(u8, a_r.name.slice(), b.repo_refs.name.slice()) and a_r.kind == b.repo_refs.kind and std.mem.eql(u8, a_r.from.slice(), b.repo_refs.from.slice()), 1554 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 1555 std.mem.eql(u8, a_i.tag.slice(), b.repo_issues.tag.slice()) and 1556 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 1557 std.mem.eql(u8, a_i.theirs.slice(), b.repo_issues.theirs.slice()) and 1558 a_i.view == b.repo_issues.view and 1559 a_i.comments_start == b.repo_issues.comments_start and 1560 std.mem.eql(u8, a_i.comment.slice(), b.repo_issues.comment.slice()), 1561 .repo_discussions => |a_t| std.mem.eql(u8, a_t.name.slice(), b.repo_discussions.name.slice()) and 1562 std.mem.eql(u8, a_t.tag.slice(), b.repo_discussions.tag.slice()) and 1563 std.mem.eql(u8, a_t.selected.slice(), b.repo_discussions.selected.slice()) and 1564 a_t.view == b.repo_discussions.view and 1565 a_t.comments_start == b.repo_discussions.comments_start and 1566 std.mem.eql(u8, a_t.comment.slice(), b.repo_discussions.comment.slice()), 1567 .repo_patches => |a_p| std.mem.eql(u8, a_p.name.slice(), b.repo_patches.name.slice()) and 1568 std.mem.eql(u8, a_p.tag.slice(), b.repo_patches.tag.slice()) and 1569 std.mem.eql(u8, a_p.selected.slice(), b.repo_patches.selected.slice()) and 1570 std.mem.eql(u8, a_p.theirs.slice(), b.repo_patches.theirs.slice()) and 1571 a_p.view == b.repo_patches.view and 1572 a_p.comments_start == b.repo_patches.comments_start and 1573 std.mem.eql(u8, a_p.comment.slice(), b.repo_patches.comment.slice()), 1574 .repo_events => |a_e| std.mem.eql(u8, a_e.name.slice(), b.repo_events.name.slice()) and 1575 a_e.view == b.repo_events.view and 1576 a_e.kind == b.repo_events.kind and 1577 std.mem.eql(u8, a_e.selected.slice(), b.repo_events.selected.slice()), 1578 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 1579 .repo_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_auth.slice()), 1580 .fork_patch => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_patch.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_patch.id.slice()), 1581 .fork_diff => |a_d| std.mem.eql(u8, a_d.fork.name.slice(), b.fork_diff.fork.name.slice()) and 1582 std.mem.eql(u8, a_d.fork.id.slice(), b.fork_diff.fork.id.slice()) and 1583 a_d.start == b.fork_diff.start and std.mem.eql(u8, a_d.path.slice(), b.fork_diff.path.slice()), 1584 .fork_files => |a_f| std.mem.eql(u8, a_f.fork.name.slice(), b.fork_files.fork.name.slice()) and 1585 std.mem.eql(u8, a_f.fork.id.slice(), b.fork_files.fork.id.slice()) and 1586 std.mem.eql(u8, a_f.oid.slice(), b.fork_files.oid.slice()) and 1587 std.mem.eql(u8, a_f.find.slice(), b.fork_files.find.slice()) and 1588 std.mem.eql(u8, a_f.path.slice(), b.fork_files.path.slice()) and 1589 a_f.line == b.fork_files.line, 1590 .fork_commits => |a_c| std.mem.eql(u8, a_c.fork.name.slice(), b.fork_commits.fork.name.slice()) and 1591 std.mem.eql(u8, a_c.fork.id.slice(), b.fork_commits.fork.id.slice()) and 1592 std.mem.eql(u8, a_c.oid.slice(), b.fork_commits.oid.slice()) and 1593 switch (a_c.content) { 1594 .diff => |a_d| switch (b.fork_commits.content) { 1595 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1596 .message => false, 1597 }, 1598 .message => std.meta.activeTag(b.fork_commits.content) == .message, 1599 }, 1600 .fork_settings => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_settings.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_settings.id.slice()), 1601 .fork_auth => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_auth.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_auth.id.slice()), 1602 else => true, 1603 }; 1604 } 1605 1606 // true when following a link from `b` to `a` (both repo routes) should reload 1607 // rather than stay in page 1608 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 1609 if (a.parent() != .repo or b.parent() != .repo) return false; 1610 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 1611 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 1612 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 1613 return !a.eql(b); 1614 } 1615 1616 pub fn forkPageChanged(a: RoutablePage, b: RoutablePage) bool { 1617 return a.parent() == .fork and b.parent() == .fork and !a.eql(b); 1618 } 1619 1620 // true when `a` and `b` are the same user paginated to a different repos 1621 // window. switching between a user's tabs is in-page, so 1622 // only a changed `start` on the repos list navigates. 1623 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 1624 return switch (a) { 1625 .user_repos => |aa| switch (b) { 1626 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1627 else => false, 1628 }, 1629 .user_forks => |aa| switch (b) { 1630 .user_forks => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1631 else => false, 1632 }, 1633 else => false, 1634 }; 1635 } 1636 1637 // true when `a` and `b` are the same home list tab paginated to a different 1638 // window. switching between the users/repos tabs is in-page (the home page 1639 // holds both lists), so only a changed `after` on the same tab navigates. 1640 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 1641 return switch (a) { 1642 .home_users => |aa| switch (b) { 1643 .home_users => |bb| aa != bb, 1644 else => false, 1645 }, 1646 .home_repos => |aa| switch (b) { 1647 .home_repos => |bb| aa != bb, 1648 else => false, 1649 }, 1650 else => false, 1651 }; 1652 } 1653 }; 1654 1655 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 1656 fn isUnreserved(c: u8) bool { 1657 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 1658 } 1659 1660 // percent-encode a ref name for use as a single url path segment. 1661 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 1662 var out: std.Io.Writer.Allocating = .init(aa); 1663 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 1664 return out.written(); 1665 } 1666 1667 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 1668 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1669 return struct { 1670 pub const hex_len = hash.hexLen(repo_opts.hash); 1671 1672 // the concrete ref/oid (a null request resolves to the default branch) 1673 ref_or_oid: RoutablePage.RefOrOid, 1674 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 1675 // the route layer — which splits on '/' — stores them encoded). duped 1676 // into `aa`. 1677 value: []const u8, 1678 // the commit oid it points at 1679 oid: [hex_len]u8, 1680 1681 const Self = @This(); 1682 1683 // resolve a requested ref/oid (null = the repo's default branch) to a 1684 // concrete ref/oid plus the commit oid it points at. `requested_value` is 1685 // the url-encoded form (as it appears in the route). null when it doesn't 1686 // resolve (an unknown branch/tag, or a malformed/unknown oid). 1687 pub fn init( 1688 repo: *rp.Repo(repo_kind, repo_opts), 1689 io: std.Io, 1690 aa: std.mem.Allocator, 1691 requested_ref_or_oid: ?RoutablePage.RefOrOid, 1692 requested_value: []const u8, 1693 ) !?Self { 1694 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 1695 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 1696 var value: []const u8 = if (requested_ref_or_oid == null) 1697 requested_value 1698 else 1699 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 1700 // no ref named: fall back to HEAD's branch (or its oid when detached). 1701 if (requested_ref_or_oid == null) { 1702 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1703 if (repo.head(io, &head_buf)) |head| switch (head) { 1704 .ref => |r| { 1705 ref_or_oid = .branch; 1706 value = r.name; 1707 }, 1708 .oid => |o| { 1709 ref_or_oid = .object; 1710 value = o; 1711 }, 1712 } else |_| {} 1713 } 1714 1715 var oid: [hex_len]u8 = undefined; 1716 switch (ref_or_oid) { 1717 .object => { 1718 if (value.len != hex_len) return null; 1719 @memcpy(&oid, value); 1720 }, 1721 .branch, .tag => { 1722 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 1723 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 1724 }, 1725 } 1726 // store url-encoded so the route layer can hold the value verbatim. 1727 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 1728 } 1729 }; 1730 } 1731 1732 // where a repo page reads its on-disk repo from 1733 pub const RepoSource = struct { 1734 path: []const u8, 1735 repo_kind: rp.RepoKind, 1736 // the user's global git config, so local mode picks up their identity and 1737 // their signing and ssh settings. null on the server paths, which must not 1738 // read whoever runs the server. 1739 global_config_path: ?[]const u8 = null, 1740 1741 pub fn localInitOpts(self: RepoSource) rp.InitOpts { 1742 return .{ .path = self.path, .global_config_path = self.global_config_path }; 1743 } 1744 1745 pub fn hasBranch(self: RepoSource, io: std.Io, allocator: std.mem.Allocator, branch: []const u8) !bool { 1746 if (!xit.ref.validateName(branch)) return false; 1747 return switch (self.repo_kind) { 1748 inline else => |repo_kind| blk: { 1749 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 1750 defer any_repo.deinit(io, allocator); 1751 break :blk switch (any_repo) { 1752 inline else => |*repo| (try repo.readRef(io, .{ .kind = .head, .name = branch })) != null, 1753 }; 1754 }, 1755 }; 1756 } 1757 1758 // whether the actor may change the event `id` names 1759 pub fn canModify( 1760 self: RepoSource, 1761 io: std.Io, 1762 allocator: std.mem.Allocator, 1763 actor: Actor, 1764 kind: evt.EventKind, 1765 id: *const [evt.event_id_size]u8, 1766 ) !bool { 1767 // a role that may change an event with no author needs no read 1768 if (actor.role.canModify(actor.author.email, null)) return true; 1769 1770 var arena = std.heap.ArenaAllocator.init(allocator); 1771 defer arena.deinit(); 1772 return switch (self.repo_kind) { 1773 inline else => |repo_kind| blk: { 1774 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 1775 defer any_repo.deinit(io, allocator); 1776 break :blk switch (any_repo) { 1777 inline else => |*repo| actor.role.canModify(actor.author.email, try evt.readAuthorEmail(repo_kind, repo.self_repo_opts, io, allocator, &arena, repo, kind, id)), 1778 }; 1779 }, 1780 }; 1781 } 1782 }; 1783 1784 // who a write acts as and the role they hold in its repo 1785 pub const Actor = struct { 1786 author: evt.CommitAuthor, 1787 // local mode has no accounts, so it has no user id 1788 user_id: ?[evt.event_id_size]u8 = null, 1789 role: evt.Repo.Role, 1790 }; 1791 1792 pub const Authorization = union(enum) { 1793 actor: Actor, 1794 login_required, 1795 repo_not_found, 1796 forbidden, 1797 }; 1798 1799 // the account `user_id` names, or null once it has been removed. a user id is 1800 // checked here wherever one enters, so the role lookups can trust theirs. 1801 pub fn activeUser( 1802 moment: evt.AdminDB.HashMap(.read_only), 1803 arena: *std.heap.ArenaAllocator, 1804 user_id: [evt.event_id_size]u8, 1805 ) !?evt.User.Record { 1806 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id)) orelse return null; 1807 return if (user.removed) null else user; 1808 } 1809 1810 // the role the user holds in the repo `identity` names, or null when no repo 1811 // has that identity. a null user is logged out. 1812 pub fn repoRole( 1813 moment: evt.AdminDB.HashMap(.read_only), 1814 arena: *std.heap.ArenaAllocator, 1815 user_id: ?[evt.event_id_size]u8, 1816 identity: []const u8, 1817 ) !?evt.Repo.Role { 1818 const owner_repo = evt.parseOwnerRepoPath(identity) orelse return null; 1819 const repo = (try evt.Repo.readByOwnerAndName(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, owner_repo.owner, owner_repo.name)) orelse return null; 1820 return evt.Repo.roleOf(repo.repo, user_id); 1821 } 1822 1823 // whether the user may write to the repo `identity` names with at least 1824 // `min_role` 1825 pub fn authorizeUser( 1826 moment: evt.AdminDB.HashMap(.read_only), 1827 arena: *std.heap.ArenaAllocator, 1828 user_id: [evt.event_id_size]u8, 1829 identity: []const u8, 1830 min_role: evt.Repo.Role, 1831 ) !Authorization { 1832 const user = (try activeUser(moment, arena, user_id)) orelse return .login_required; 1833 1834 const role = (try repoRole(moment, arena, user_id, identity)) orelse return .repo_not_found; 1835 // a repo the user can't read doesn't exist to them 1836 if (role == .none) return .repo_not_found; 1837 if (!role.atLeast(min_role)) return .forbidden; 1838 return .{ .actor = .{ 1839 .author = .{ .name = user.event.name, .email = user.event.email }, 1840 .user_id = user_id, 1841 .role = role, 1842 } }; 1843 } 1844 1845 // events created in local mode have no account behind them, so the commit 1846 // author comes from the repo's config, which the global config feeds into. 1847 pub const local_author_fallback = evt.CommitAuthor{ .name = "haxy", .email = "user@haxy" }; 1848 1849 pub fn localAuthor( 1850 src: RepoSource, 1851 io: std.Io, 1852 allocator: std.mem.Allocator, 1853 arena: *std.heap.ArenaAllocator, 1854 ) !evt.CommitAuthor { 1855 switch (src.repo_kind) { 1856 inline else => |repo_kind| { 1857 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()) catch return local_author_fallback; 1858 defer any_repo.deinit(io, allocator); 1859 1860 switch (any_repo) { 1861 inline else => |*repo| { 1862 var config = try repo.listConfig(io, allocator); 1863 defer config.deinit(); 1864 1865 const user = config.sections.get("user") orelse return local_author_fallback; 1866 const name = user.get("name") orelse return local_author_fallback; 1867 const email = user.get("email") orelse return local_author_fallback; 1868 return .{ 1869 .name = try arena.allocator().dupe(u8, name), 1870 .email = try arena.allocator().dupe(u8, email), 1871 }; 1872 }, 1873 } 1874 }, 1875 } 1876 } 1877 1878 // per-connection mutable state. each SSH session / web session / local TUI 1879 // run gets its own. 1880 pub const Session = struct { 1881 data: Data = .{}, 1882 // session-lifetime allocations: login/user_id, persisted prefs, queued 1883 // actions. lives as long as the connection. 1884 arena: *std.heap.ArenaAllocator, 1885 // current page's allocations: page data and the page-scoped strings widgets 1886 // build (form actions, links). owned by Nav, swapped on each navigation, so 1887 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 1888 // where a render is one-shot, this points at the same arena as `arena`. 1889 page_arena: *std.heap.ArenaAllocator, 1890 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 1891 // filesystem io and the path to <server>/repos, for opening on-disk repos 1892 // during page construction. both null on wasm, which has no filesystem and 1893 // rebuilds pages from the serialized snapshot rather than from disk. 1894 io: ?std.Io = null, 1895 admin_repo: ?*rp.Repo(.xit, evt.admin_repo_opts) = null, 1896 repos_dir: ?[]const u8 = null, 1897 // the single on-disk repo this session views, when running in local mode 1898 // (haxy invoked with no arguments inside a repo). null on the server paths, 1899 // which resolve repos from the admin db instead. 1900 local: ?RepoSource = null, 1901 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 1902 // focus id -> the live TextInput, refreshed each frame by the views that own 1903 // inputs. web/wasm form handling looks widgets up here by focus id. 1904 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput) = .empty, 1905 back: enum { unavailable, available, requested } = .unavailable, 1906 refresh_requested: bool = false, // set by input (ctrl+r) 1907 // a requested forward navigation. set this (via navigate) to move to a new 1908 // page; Nav.sync builds it and then copies it into current_page, clearing 1909 // this back to null. setting current_page directly only updates the url and 1910 // does not navigate. 1911 next_page: ?RoutablePage = null, 1912 is_terminal: bool = false, // true on remote SSH and local TUI 1913 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 1914 // url. null on the web itself (no footer there). 1915 web_port: ?u16 = null, 1916 // a host operation requested by a widget 1917 host_request: ?HostRequest = null, 1918 quit_requested: bool = false, 1919 1920 pub const HostRequest = union(enum) { 1921 show_copyable_text: []const u8, 1922 sync_events, 1923 }; 1924 1925 pub const FormFeedback = union(enum) { 1926 pub const LoginFailure = enum { unknown_user, wrong_password }; 1927 pub const ThreadFailure = enum { required_title }; 1928 pub const PatchFailure = enum { 1929 required_title, 1930 invalid_target_branch, 1931 invalid_source_branch, 1932 same_branch, 1933 unrelated_branches, 1934 1935 pub fn fromError(err: anyerror) ?PatchFailure { 1936 return switch (err) { 1937 error.InvalidTargetBranch => .invalid_target_branch, 1938 error.InvalidSourceBranch => .invalid_source_branch, 1939 error.SameBranch => .same_branch, 1940 error.UnrelatedBranches => .unrelated_branches, 1941 else => null, 1942 }; 1943 } 1944 }; 1945 1946 login: struct { 1947 failure: LoginFailure, 1948 username: []const u8, 1949 }, 1950 issue: struct { 1951 failure: ThreadFailure, 1952 fields: ?struct { 1953 title: []const u8, 1954 tags: []const u8, 1955 description: []const u8, 1956 } = null, 1957 }, 1958 patch: struct { 1959 failure: PatchFailure, 1960 fields: ?struct { 1961 title: []const u8, 1962 tags: []const u8, 1963 description: []const u8, 1964 target_branch: []const u8, 1965 source_branch: []const u8 = "", 1966 source_kind: Repo.Patches.Source.Kind = .fork, 1967 } = null, 1968 }, 1969 discussion: struct { 1970 failure: ThreadFailure, 1971 fields: ?struct { 1972 title: []const u8, 1973 tags: []const u8, 1974 description: []const u8, 1975 } = null, 1976 }, 1977 }; 1978 1979 const Self = @This(); 1980 1981 // serializable data sent down to web client 1982 pub const Data = struct { 1983 user_id: ?[]const u8 = null, 1984 // the logged-in user's name, for the views that show it 1985 user_name: ?[]const u8 = null, 1986 // a failed form retained long enough to render it for correction 1987 form_feedback: ?FormFeedback = null, 1988 // a transient local event-sync error 1989 sync_failure: ?[]const u8 = null, 1990 current_page: RoutablePage = .default, 1991 // whether to render the ANSI art backdrop 1992 enable_ansi: bool = true, 1993 // the selected ANSI art. the server serializes one image for WASM so 1994 // the browser does not need the whole build-generated collection. 1995 ansi_art: []const u8 = "", 1996 // the bound git-service ports. absent in local mode, where remote urls 1997 // are not shown. 1998 git_http_port: ?u16 = null, 1999 git_ssh_port: ?u16 = null, 2000 git_ssh_prefix: []const u8 = "", 2001 // whether this session views a local repo or the server. unlike `local` 2002 // (the host-side filesystem source) this travels in the snapshot, so 2003 // the wasm side also parses elided link urls and hides the multi-user 2004 // chrome. 2005 host_kind: evt.HostKind = .server, 2006 }; 2007 2008 // a user-initiated state change. widgets enqueue these on the session during 2009 // input instead of mutating state or touching the DB themselves; the host 2010 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 2011 // side effects out of the widget tree and gives every render path one place to 2012 // turn a UI action into a state change + event. 2013 pub const Action = union(enum) { 2014 toggle_ansi, 2015 }; 2016 2017 pub fn init( 2018 arena: *std.heap.ArenaAllocator, 2019 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2020 data: Data, 2021 ) !Self { 2022 var session = Self{ 2023 .data = data, 2024 .arena = arena, 2025 // until a host swaps in a page-scoped arena (Nav does this), page 2026 // allocations land in the session arena. on the web path that's the 2027 // intended behavior, since the whole arena is per-request. 2028 .page_arena = arena, 2029 .admin_repo = repo, 2030 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 2031 }; 2032 try session.loadUser(); 2033 return session; 2034 } 2035 2036 // load the logged-in user's name and persisted preferences from the db 2037 pub fn loadUser(self: *Self) !void { 2038 const user_id = self.userId() orelse return; 2039 const moment = self.haxy_moment orelse return; 2040 const user = (try activeUser(moment, self.arena, user_id)) orelse return self.logOut(); 2041 self.data.user_name = user.event.name; 2042 self.data.enable_ansi = user.event.enable_ansi; 2043 } 2044 2045 // forget the logged-in user, on a logout or once their account is removed 2046 pub fn logOut(self: *Self) void { 2047 self.data.user_id = null; 2048 self.data.user_name = null; 2049 } 2050 2051 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 2052 const bytes = self.data.user_id orelse return null; 2053 if (bytes.len != evt.event_id_size) return null; 2054 var id: [evt.event_id_size]u8 = undefined; 2055 @memcpy(&id, bytes); 2056 return id; 2057 } 2058 2059 pub fn formFeedback(self: *const Self, comptime tag: std.meta.Tag(FormFeedback)) ?@FieldType(FormFeedback, @tagName(tag)) { 2060 const feedback = self.data.form_feedback orelse return null; 2061 if (std.meta.activeTag(feedback) != tag) return null; 2062 return @field(feedback, @tagName(tag)); 2063 } 2064 2065 pub fn clearFormFeedback(self: *Self) void { 2066 self.data.form_feedback = null; 2067 } 2068 2069 // the actor for a write to the repo `identity` names, or null when the 2070 // session may not write there with at least `min_role`. local mode always 2071 // writes. 2072 pub fn authorize(self: *Self, identity: []const u8, min_role: evt.Repo.Role) !?Actor { 2073 if (self.data.host_kind == .local) { 2074 const author = blk: { 2075 const src = self.local orelse break :blk local_author_fallback; 2076 const io = self.io orelse break :blk local_author_fallback; 2077 break :blk try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 2078 }; 2079 return .{ .author = author, .role = .owner }; 2080 } 2081 const user_id = self.userId() orelse return null; 2082 // the session's moment predates this input, so read the roles afresh 2083 const admin_repo = self.admin_repo orelse return null; 2084 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2085 return switch (try authorizeUser(moment, self.page_arena, user_id, identity, min_role)) { 2086 .actor => |actor| actor, 2087 .login_required, .repo_not_found, .forbidden => null, 2088 }; 2089 } 2090 2091 // queue an action for the host to drain this frame. 2092 pub fn push(self: *Self, action: Action) !void { 2093 try self.pending.append(self.arena.allocator(), action); 2094 } 2095 2096 // apply a single action's in-memory effect only (no persistence). 2097 fn apply(self: *Self, action: Action) void { 2098 switch (action) { 2099 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 2100 } 2101 } 2102 2103 // drain the session's queued actions by applying them to the session. 2104 // used on the wasm path, which has no repo to persist to. 2105 pub fn applyPending(self: *Self) void { 2106 for (self.pending.items) |action| self.apply(action); 2107 self.pending.clearRetainingCapacity(); 2108 } 2109 2110 // drain the session's queued actions by applying them to the session and 2111 // writing them to the db 2112 pub fn applyAndWritePending( 2113 self: *Self, 2114 io: std.Io, 2115 allocator: std.mem.Allocator, 2116 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2117 ) !void { 2118 defer self.pending.clearRetainingCapacity(); 2119 for (self.pending.items) |action| { 2120 self.apply(action); 2121 switch (action) { 2122 .toggle_ansi => if (self.data.user_id) |user_id| { 2123 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 2124 }, 2125 } 2126 } 2127 } 2128 2129 // reload the moment from the admin repo and re-read user preferences 2130 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 2131 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 2132 self.haxy_moment = moment; 2133 2134 const user_id = self.userId() orelse return; 2135 var arena = std.heap.ArenaAllocator.init(allocator); 2136 defer arena.deinit(); 2137 const user = (try activeUser(moment, &arena, user_id)) orelse return self.logOut(); 2138 self.data.enable_ansi = user.event.enable_ansi; 2139 } 2140 2141 // request a forward navigation to `route`; the host consumes next_page 2142 // (Nav.sync on the terminal, the wasm tick on the web). 2143 pub fn navigate(self: *Session, route: RoutablePage) !void { 2144 self.clearFormFeedback(); 2145 self.next_page = route; 2146 } 2147 }; 2148 2149 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2150 var nav = try Nav.init(allocator, session); 2151 defer nav.deinit(allocator); 2152 2153 var terminal = try term.Terminal.init(io, allocator); 2154 var terminal_live = true; 2155 defer if (terminal_live) terminal.deinit(io); 2156 2157 // set term as active so it will be properly cooked 2158 // when a panic/segfault happens 2159 term.setActive(&terminal); 2160 defer term.setActive(null); 2161 2162 while (!terminal.shouldQuit()) { 2163 const grid_changed = try terminal.render(&nav.root); 2164 2165 // process any inputs. 2166 // 2167 // if the grid didn't change, then first do a blocking 2168 // read, so the thread will sleep until further input. 2169 // after that, all remaining reads are non-blocking so 2170 // we can process the rest of the queued inputs. 2171 // 2172 // if the grid *did* change, then only do non-blocking 2173 // reads. we do not want to sleep the thread because 2174 // there may be an animation that requires more looping. 2175 var blocking = !grid_changed; 2176 while (try terminal.readKey(io, blocking)) |key| { 2177 blocking = false; 2178 try inputKey(allocator, &nav.root, key, session); 2179 } 2180 2181 if (session.host_request) |request| { 2182 session.host_request = null; 2183 switch (request) { 2184 .show_copyable_text => |copyable_text| { 2185 term.setActive(null); 2186 terminal.deinit(io); 2187 terminal_live = false; 2188 showCopyableText(io, copyable_text) catch |show_err| { 2189 terminal = try term.Terminal.init(io, allocator); 2190 terminal_live = true; 2191 term.setActive(&terminal); 2192 return show_err; 2193 }; 2194 terminal = try term.Terminal.init(io, allocator); 2195 terminal_live = true; 2196 term.setActive(&terminal); 2197 }, 2198 .sync_events => { 2199 try nav.root.build(allocator, .{ 2200 .min_size = .{ .width = null, .height = null }, 2201 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2202 }, nav.root.getFocus()); 2203 _ = try terminal.render(&nav.root); 2204 try Repo.Events.performSync(allocator, session); 2205 }, 2206 } 2207 } 2208 2209 // persist queued actions when there's a repo; local mode has none, 2210 // so just apply in-memory 2211 if (repo_maybe) |repo| { 2212 try session.applyAndWritePending(io, allocator, repo); 2213 // reload so the next navigation builds its page from the current moment 2214 try session.reloadMoment(allocator, repo); 2215 } else { 2216 session.applyPending(); 2217 } 2218 2219 // reconcile navigation: forward to a new page, or back on escape. 2220 try nav.sync(allocator, session); 2221 2222 // the quit button (on the quit tab) asks the host to tear down. 2223 if (session.quit_requested) terminal.requestQuit(); 2224 2225 try nav.root.build(allocator, .{ 2226 .min_size = .{ .width = null, .height = null }, 2227 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2228 }, nav.root.getFocus()); 2229 } 2230 } 2231 2232 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2233 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2234 null 2235 else 2236 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2237 defer if (tty) |file| file.close(io); 2238 2239 const input = if (tty) |file| file else std.Io.File.stdin(); 2240 const output = if (tty) |file| file else std.Io.File.stdout(); 2241 2242 var write_buf: [1024]u8 = undefined; 2243 var writer = output.writer(io, &write_buf); 2244 try writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 2245 try writer.interface.flush(); 2246 2247 var read_buf: [1]u8 = undefined; 2248 var reader = input.reader(io, &read_buf); 2249 while (true) { 2250 const byte = try reader.interface.takeByte(); 2251 if (byte == '\r' or byte == '\n') return; 2252 } 2253 } 2254 2255 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2256 const root_focus = root.getFocus(); 2257 const focused_before = root_focus.grandchild_id; 2258 var activate_in_page_link = false; 2259 switch (key) { 2260 // request a navigation pop; the host's Nav.sync goes back a page, or 2261 // quits when there's no history left. 2262 .escape => { 2263 session.back = .requested; 2264 return; 2265 }, 2266 .ctrl => |letter| switch (letter) { 2267 'r' => { 2268 session.refresh_requested = true; 2269 return; 2270 }, 2271 else => try root.input(allocator, key, root_focus), 2272 }, 2273 .enter => { 2274 if (root_focus.grandchild_id) |gid| { 2275 // follow a cross-page link 2276 if (crossPageLink(root_focus, gid, session.data)) |route| { 2277 return session.navigate(route); 2278 } 2279 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2280 } 2281 activate_in_page_link = true; 2282 try root.input(allocator, key, root_focus); 2283 }, 2284 .mouse => |mouse| { 2285 if (mouse.action == .press and mouse.action.press == .left) { 2286 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2287 const focus_id = hit.id; 2288 if (hit.kind == .background) { 2289 root_focus.setFocus(focus_id); 2290 return; 2291 } 2292 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2293 if (session.back == .available) session.back = .requested; 2294 return; 2295 } 2296 // follow a cross-page link 2297 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2298 return session.navigate(route); 2299 } 2300 if (rawLink(root_focus, focus_id)) |url| { 2301 if (!mouse.ctrl) try requestRawLink(session, url); 2302 return; 2303 } 2304 root_focus.setFocus(focus_id); 2305 activate_in_page_link = true; 2306 } 2307 // forward the press into the widget tree so buttons (and any 2308 // future click-aware widgets) can react. widgets that don't 2309 // care about presses ignore it. 2310 try root.input(allocator, key, root_focus); 2311 } else { 2312 try root.input(allocator, key, root_focus); 2313 } 2314 }, 2315 else => try root.input(allocator, key, root_focus), 2316 } 2317 2318 const focused_after = root_focus.grandchild_id; 2319 if (!activate_in_page_link and focused_before == focused_after) return; 2320 const focus_id = focused_after orelse return; 2321 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2322 } 2323 2324 // a link to bytes the server serves directly rather than to a page route, so 2325 // it never resolves to a RoutablePage 2326 pub const raw_link_prefix = "ax:"; 2327 2328 // a route already loaded into the current page; focusing it changes the url 2329 // without rebuilding the page 2330 pub const in_page_link_prefix = "ai:"; 2331 2332 // an in-page tab's base route, or the exact current route when it is selected 2333 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2334 const target = if (selected) session.data.current_page else route; 2335 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2336 } 2337 2338 // a button the web renderer covers with a file picker that posts the chosen 2339 // file to the url after the prefix 2340 pub const file_input_prefix = "file:"; 2341 2342 // a submit button that overrides its enclosing form's action 2343 pub const submit_action_prefix = "submit:"; 2344 2345 // the url an `ax:` link points at, or null for anything else. a host that 2346 // suppresses the anchor's own navigation needs this to follow the link. 2347 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2348 const child = root_focus.children.get(focus_id) orelse return null; 2349 const custom = switch (child.focus.kind) { 2350 .custom => |c| c, 2351 else => return null, 2352 }; 2353 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2354 return custom[raw_link_prefix.len..]; 2355 } 2356 2357 fn requestRawLink(session: *Session, url: []const u8) !void { 2358 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2359 } 2360 2361 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2362 return if (session.web_port) |port| 2363 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2364 else 2365 path; 2366 } 2367 2368 // if the focus target at focus_id is an `a:` link that should navigate (a 2369 // different parent page, or a different files directory within the repo page), 2370 // return its route; otherwise null. lets a host turn a click / enter on such a 2371 // link into a navigation rather than a focus change. 2372 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2373 const current = data.current_page; 2374 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2375 // a link to a different parent page always navigates; within a page, a 2376 // files-directory / commits-page / list-window change navigates while tab 2377 // links stay in-page. 2378 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.forkPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 2379 return null; 2380 } 2381 2382 // the route carried by an `ai:` focus target, or null for any other target 2383 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2384 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2385 } 2386 2387 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2388 const child = root_focus.children.get(focus_id) orelse return null; 2389 const custom = switch (child.focus.kind) { 2390 .custom => |c| c, 2391 else => return null, 2392 }; 2393 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2394 const path = custom[prefix.len..]; 2395 // local sessions build (and parse) links with the repo identity elided 2396 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2397 } 2398 2399 // a display author, resolved against the admin db at read time 2400 pub const Author = union(enum) { 2401 unknown, 2402 email: []const u8, // an email no user matches 2403 user_name: []const u8, 2404 2405 // the commit author line's email, resolved like initFromEmail 2406 pub fn init( 2407 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2408 arena: *std.heap.ArenaAllocator, 2409 author_line: []const u8, 2410 ) !Author { 2411 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2412 } 2413 2414 // from a record's stored author email: it resolves to the user currently 2415 // holding it, else stays the email. the result is allocated in `arena`, 2416 // so the email may borrow transient memory. 2417 pub fn initFromEmail( 2418 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2419 arena: *std.heap.ArenaAllocator, 2420 author_email: ?[]const u8, 2421 ) !Author { 2422 const email = author_email orelse return .unknown; 2423 if (haxy_moment) |moment| { 2424 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2425 return .{ .user_name = user.event.name }; 2426 } 2427 } 2428 return .{ .email = try arena.allocator().dupe(u8, email) }; 2429 } 2430 }; 2431 2432 // the "a:" link to user `name`'s page. 2433 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2434 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2435 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2436 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2437 } 2438 2439 // a focusable " author " box showing `author`, linking to their user page 2440 // when it is a known user 2441 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2442 const text = switch (author) { 2443 .unknown => "", 2444 .email, .user_name => |t| t, 2445 }; 2446 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2447 errdefer tb.deinit(allocator); 2448 tb.getFocus().mode = .all; 2449 switch (author) { 2450 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2451 else => {}, 2452 } 2453 return tb; 2454 } 2455 2456 // native-TUI navigation 2457 pub const Nav = struct { 2458 root: Widget, 2459 // backs the current page (its Page data and the page-scoped strings its 2460 // widgets build). owned here, swapped on each navigation. session.page_arena 2461 // tracks whichever of these is current so widgets allocate into it. 2462 arena: *std.heap.ArenaAllocator, 2463 history: std.ArrayList(Entry), 2464 2465 // each retained page keeps its own arena, freed when the page leaves the 2466 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2467 // long-lived session from accumulating every page it ever visited. 2468 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2469 2470 // cap on retained back-history so the chain can't grow memory without bound 2471 const max_history: usize = 16; 2472 2473 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2474 session.back = .unavailable; 2475 const arena = try allocator.create(std.heap.ArenaAllocator); 2476 arena.* = std.heap.ArenaAllocator.init(allocator); 2477 errdefer { 2478 arena.deinit(); 2479 allocator.destroy(arena); 2480 } 2481 2482 session.page_arena = arena; 2483 const route = session.data.current_page; 2484 2485 const page = try arena.allocator().create(Page); 2486 page.* = try Page.init(arena, session, route); 2487 return .{ 2488 .root = try initRoot(allocator, page, session), 2489 .arena = arena, 2490 .history = .empty, 2491 }; 2492 } 2493 2494 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2495 self.root.deinit(allocator); 2496 freeArena(allocator, self.arena); 2497 for (self.history.items) |*entry| { 2498 entry.root.deinit(allocator); 2499 freeArena(allocator, entry.arena); 2500 } 2501 self.history.deinit(allocator); 2502 } 2503 2504 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2505 arena.deinit(); 2506 allocator.destroy(arena); 2507 } 2508 2509 // reconcile the displayed root with the session's navigation state. a 2510 // forward nav (current_page moved to a different parent page) pushes the 2511 // current root and builds the new page in a fresh arena; a back request 2512 // frees the current page and restores the previous one. when escape is 2513 // pressed with no history left we switch to the quit tab instead of quitting. 2514 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2515 // refresh: rebuild the current page in place from a current moment 2516 if (session.refresh_requested) { 2517 session.refresh_requested = false; 2518 if (session.haxy_moment != null or session.local != null) { 2519 const arena = try allocator.create(std.heap.ArenaAllocator); 2520 arena.* = std.heap.ArenaAllocator.init(allocator); 2521 errdefer freeArena(allocator, arena); 2522 2523 session.page_arena = arena; 2524 2525 const route = session.data.current_page; 2526 const page = try arena.allocator().create(Page); 2527 page.* = try Page.init(arena, session, route); 2528 const new_root = try initRoot(allocator, page, session); 2529 2530 self.root.deinit(allocator); 2531 freeArena(allocator, self.arena); 2532 self.root = new_root; 2533 self.arena = arena; 2534 } 2535 return; 2536 } 2537 2538 if (session.back == .requested) { 2539 if (self.history.pop()) |entry| { 2540 session.back = if (self.history.items.len == 0) .unavailable else .available; 2541 self.root.deinit(allocator); 2542 freeArena(allocator, self.arena); 2543 self.root = entry.root; 2544 self.arena = entry.arena; 2545 session.page_arena = entry.arena; 2546 session.data.current_page = entry.route; 2547 chooseAnsiArtForNavigation(session); 2548 return; 2549 } 2550 session.back = .unavailable; 2551 // nothing to go back to; switch to the quit confirmation 2552 const root_focus = self.root.getFocus(); 2553 var iter = root_focus.children.iterator(); 2554 while (iter.next()) |entry| { 2555 switch (entry.value_ptr.focus.kind) { 2556 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2557 root_focus.setFocus(entry.key_ptr.*); 2558 // a Stack only builds its selected child, so the quit 2559 // button isn't in the focus tree until a build runs with 2560 // the quit tab selected. build once, then send a 2561 // synthetic arrow_down to drop focus from the tab onto 2562 // the button so it's ready to confirm. 2563 try self.root.build(allocator, .{ 2564 .min_size = .{ .width = null, .height = 40 }, 2565 .max_size = .{ .width = 80, .height = null }, 2566 }, root_focus); 2567 try self.root.input(allocator, .arrow_down, root_focus); 2568 break; 2569 }, 2570 else => {}, 2571 } 2572 } 2573 return; 2574 } 2575 2576 // forward navigation: navigate() set next_page to the page to move to (a 2577 // cross-page link or tab change crossing pages) 2578 if (session.next_page) |route| { 2579 session.next_page = null; 2580 if (session.haxy_moment == null and session.local == null) return; 2581 const previous_route = session.data.current_page; 2582 // the page we navigated to becomes the current page 2583 session.data.current_page = route; 2584 session.back = .available; 2585 2586 const arena = try allocator.create(std.heap.ArenaAllocator); 2587 arena.* = std.heap.ArenaAllocator.init(allocator); 2588 errdefer freeArena(allocator, arena); 2589 2590 session.page_arena = arena; 2591 2592 const page = try arena.allocator().create(Page); 2593 page.* = try Page.init(arena, session, route); 2594 const new_root = try initRoot(allocator, page, session); 2595 2596 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2597 // drop the oldest entry (freeing its widget tree and arena) once over cap 2598 if (self.history.items.len > max_history) { 2599 var oldest = self.history.orderedRemove(0); 2600 oldest.root.deinit(allocator); 2601 freeArena(allocator, oldest.arena); 2602 } 2603 self.root = new_root; 2604 self.arena = arena; 2605 } 2606 } 2607 }; 2608 2609 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2610 const page_widget: Widget = switch (page.*) { 2611 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2612 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2613 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2614 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2615 }; 2616 2617 chooseAnsiArtForNavigation(session); 2618 2619 // on the TUI/SSH, the page sits above a one-row footer showing the url 2620 var root = if (session.is_terminal) blk: { 2621 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2622 errdefer box.deinit(allocator); 2623 const bg_id = bg_blk: { 2624 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2625 errdefer background.deinit(allocator); 2626 const id = background.getFocus().id; 2627 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2628 break :bg_blk id; 2629 }; 2630 { 2631 var footer = try widget.Footer.init(allocator, session); 2632 errdefer footer.deinit(allocator); 2633 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2634 } 2635 box.getFocus().child_id = bg_id; 2636 break :blk Widget{ .box = box }; 2637 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2638 errdefer root.deinit(allocator); 2639 2640 // input-owning views build their TextInputs in init, so reset their 2641 // registrations here 2642 session.text_inputs.clearRetainingCapacity(); 2643 2644 try root.build(allocator, .{ 2645 .min_size = .{ .width = null, .height = 40 }, 2646 .max_size = .{ .width = 100, .height = null }, 2647 }, root.getFocus()); 2648 2649 return root; 2650 } 2651 2652 // native and server-side sessions choose from the embedded collection. WASM 2653 // keeps the art serialized by the server instead. 2654 fn chooseAnsiArtForNavigation(session: *Session) void { 2655 if (comptime ansi_arts.len == 0) return; 2656 const io = session.io orelse return; 2657 var random: usize = undefined; 2658 io.random(std.mem.asBytes(&random)); 2659 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2660 } 2661