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