╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const ui = @import("../ui.zig"); 4 const xit = @import("xit"); 5 const xitui = xit.xitui; 6 const wgt = xitui.widget; 7 const layout = xitui.layout; 8 const Key = xitui.input.Key; 9 const Grid = xitui.grid.Grid; 10 const Focus = xitui.focus.Focus; 11 const inp = @import("./input.zig"); 12 13 pub const Header = @import("./Repo/Header.zig"); 14 pub const SubHeader = @import("./Repo/SubHeader.zig"); 15 pub const Files = @import("./Repo/Files.zig"); 16 pub const Commits = @import("./Repo/Commits.zig"); 17 pub const Refs = @import("./Repo/Refs.zig"); 18 pub const Issues = @import("./Repo/Issues.zig"); 19 pub const Settings = @import("./Settings.zig"); 20 pub const Auth = @import("./Auth.zig"); 21 pub const Quit = @import("./Quit.zig"); 22 23 const Array = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len); 24 25 header: Header, 26 repo: evt.Repo, 27 files: Files, 28 commits: Commits, 29 refs: Refs, 30 issues: Issues, 31 settings: Settings, 32 auth: Auth, 33 quit: Quit, 34 // the full files route this page renders ("owner/name" or "owner/name/files/<dir>"), 35 // mirrored into current_page when the files tab is selected. 36 route_name: Array, 37 // the content window (?after=N) of that files route; mirrored alongside the name 38 // so the url keeps the query param even when focus isn't in the files view. 39 files_after: usize, 40 // the commits route this page renders ("owner/name/commits[/<oid>]"), mirrored 41 // when the commits tab is selected. 42 commits_route_name: Array, 43 // the diff window (?after=N) of that commits route; mirrored alongside the name 44 // so the url keeps the query param even when focus isn't in the commits view. 45 commits_after: usize, 46 // "owner/name" alone, mirrored when the settings/auth tab is selected. 47 identity: Array, 48 49 const Self = @This(); 50 51 pub fn init( 52 arena: *std.heap.ArenaAllocator, 53 session: *ui.Session, 54 route: ui.RoutablePage, 55 ) !Self { 56 const DB = evt.AdminDB; 57 const hash_kind = evt.admin_repo_opts.hash; 58 59 // every repo route's stored string starts with "owner/name"; the files and 60 // commits routes additionally encode a ref/oid (files also a directory). 61 const name_str: []const u8 = switch (route) { 62 .repo_files => |f| f.name.slice(), 63 .repo_settings, .repo_auth => |n| n.slice(), 64 .repo_commits => |c| c.name.slice(), 65 .repo_refs => |r| r.name.slice(), 66 .repo_issues => |i| i.name.slice(), 67 else => return error.UnexpectedRoute, 68 }; 69 const rf = ui.RoutablePage.RepoFiles.parse(name_str) orelse return error.NotFound; 70 const tag = std.meta.activeTag(route); 71 72 // the files and commits tabs share one ref/oid: whichever the incoming route 73 // names (it rides on the route's target tab), or the default branch when 74 // neither tab is targeted. building both views at it keeps switching tabs 75 // (even by key, without a reload) on the same ref. a null ref means the 76 // default branch (Files/Commits.init resolve it). the directory and the diff 77 // window only apply to their own tab. 78 const commits_ref = if (tag == .repo_commits) ui.RoutablePage.repoCommitsRef(name_str) else ui.RoutablePage.CommitsRef{ .ref_or_oid = null, .value = "" }; 79 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (tag) { 80 .repo_files => rf.ref_kind, 81 .repo_commits => commits_ref.ref_or_oid, 82 else => null, 83 }; 84 const requested_ref_value: []const u8 = switch (tag) { 85 .repo_files => rf.ref_value, 86 .repo_commits => commits_ref.value, 87 else => "", 88 }; 89 const files_dir = if (tag == .repo_files) rf.dir else ""; 90 // the line offset the files view's selected file content window starts at. 91 const files_after: usize = switch (route) { 92 .repo_files => |f| f.after, 93 else => 0, 94 }; 95 // how many diff hunks the commits view's selected commit shows ("load more"). 96 const commits_after: usize = switch (route) { 97 .repo_commits => |c| c.after, 98 else => 0, 99 }; 100 // the refs tab paginates one column at a time: this offset applies to 101 // `refs_kind`'s column, the other stays at its first window. 102 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 103 .repo_refs => |r| r.kind, 104 else => .branch, 105 }; 106 const refs_after: usize = switch (route) { 107 .repo_refs => |r| r.after, 108 else => 0, 109 }; 110 // the issues tab's tag filter and the issue its window is rooted at. 111 const issues_tag: []const u8 = switch (route) { 112 .repo_issues => |i| i.tag.slice(), 113 else => "", 114 }; 115 const issues_selected: []const u8 = switch (route) { 116 .repo_issues => |i| i.selected.slice(), 117 else => "", 118 }; 119 120 // where the on-disk repo lives (null keeps the views' empty fallback), plus 121 // the repo and owner-name metadata the header shows. local mode already 122 // knows all three; the server paths resolve them from the admin db. 123 var source: ?ui.RepoSource = null; 124 var repo: evt.Repo = undefined; 125 var owner_name: []const u8 = undefined; 126 if (session.local) |local| { 127 source = local; 128 // local routes elide the identity, so the display name comes from the 129 // repo's directory rather than the route. 130 repo = .{ 131 .user_id = "", 132 .name = try arena.allocator().dupe(u8, std.fs.path.basename(local.path)), 133 .description = "", 134 .enable_issue = true, 135 }; 136 owner_name = ""; 137 } else { 138 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 139 const found = (try evt.Repo.readByOwnerAndName(DB, hash_kind, haxy_moment, arena, rf.owner, rf.name)) orelse return error.NotFound; 140 repo = found.repo; 141 142 // resolve the creating user so the header can show their name to the left 143 // of the repo title. 144 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.user_id)) orelse return error.NotFound; 145 owner_name = owner.name; 146 147 // the repo's working copy lives at <repos_dir>/<hex event id>. 148 if (session.repos_dir) |repos_dir| { 149 const hex = std.fmt.bytesToHex(found.event_id, .lower); 150 source = .{ 151 .path = try std.fs.path.join(arena.allocator(), &.{ repos_dir, &hex }), 152 .repo_kind = .xit, 153 }; 154 } 155 } 156 157 // build files and commits first so their resolved ref (the default branch 158 // when the route named none) can canonicalize each tab's mirror url to the 159 // explicit ref it's viewing rather than leaving it bare. both resolve the 160 // same requested ref, so they end up viewing the same one. 161 const files = try Files.init(arena, session, source, rf.identity, requested_ref_or_oid, requested_ref_value, files_dir, files_after); 162 const commits = try Commits.init(arena, session, source, rf.identity, requested_ref_or_oid, requested_ref_value, commits_after); 163 164 // each tab mirror carries this page's route for that tab; tabs not targeted 165 // by the incoming route fall back to their root/first-page route. the files 166 // mirror carries the selected file (when the route named one) so the url 167 // keeps it before focus enters the view. 168 const files_path = if (files.selected_file) |f| try Files.childDir(arena.allocator(), files.dir, f) else files.dir; 169 // the content window offset only applies to a selected file, so the bare 170 // directory route drops it. 171 const files_route_after = if (files.selected_file != null) files_after else 0; 172 const route_name = (ui.RoutablePage.repoFilesRoute(rf.identity, files.ref_or_oid, files.ref_or_oid_value, files_path, files_route_after) orelse return error.NotFound).repo_files.name; 173 const commits_route_name = (ui.RoutablePage.repoCommitsRoute(rf.identity, commits.ref_or_oid, commits.ref_or_oid_value, commits_after) orelse return error.NotFound).repo_commits.name; 174 175 return .{ 176 // files and commits resolve the same ref, so either's serves the header, 177 // which points both tabs at it. 178 .header = try Header.init(arena, repo.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, issues_tag), 179 .repo = repo, 180 .files = files, 181 .commits = commits, 182 .refs = try Refs.init(arena, session, source, rf.identity, refs_kind, refs_after), 183 .issues = try Issues.init(arena, session, source, rf.identity, issues_tag, issues_selected), 184 .settings = Settings.init(), 185 .auth = Auth.init(), 186 .quit = Quit.init(), 187 .route_name = route_name, 188 .files_after = files_route_after, 189 .commits_route_name = commits_route_name, 190 .commits_after = commits_after, 191 .identity = Array.from(rf.identity) orelse return error.NotFound, 192 }; 193 } 194 195 pub const View = struct { 196 box: wgt.Box(ui.Widget), 197 data: *const Self, 198 session: *ui.Session, 199 200 const header_index: usize = 0; 201 const stack_index: usize = 1; 202 203 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 204 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 205 errdefer box.deinit(allocator); 206 207 // build the header first so we can grab the files-tab id for the auth 208 // view (it focuses there after login). 209 var files_tab_id: usize = undefined; 210 { 211 var header_view = try Header.View.init(allocator, &data.header, session); 212 errdefer header_view.deinit(allocator); 213 files_tab_id = header_view.tab_ids.keys()[0]; 214 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 215 } 216 217 { 218 var stack = try wgt.Stack(ui.Widget).init(allocator); 219 errdefer stack.deinit(allocator); 220 221 // files — the default tab: the current directory's listing. 222 { 223 var files_view = try Files.View.init(allocator, &data.files, session); 224 errdefer files_view.deinit(allocator); 225 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 226 } 227 228 // commits — the current page of the commit log. 229 { 230 var commits_view = try Commits.View.init(allocator, &data.commits, session); 231 errdefer commits_view.deinit(allocator); 232 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 233 } 234 235 // refs — the repo's branches and tags. 236 { 237 var refs_view = try Refs.View.init(allocator, &data.refs, session); 238 errdefer refs_view.deinit(allocator); 239 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 240 } 241 242 // issues — the repo's issue tracker. 243 { 244 var issues_view = try Issues.View.init(allocator, &data.issues, session); 245 errdefer issues_view.deinit(allocator); 246 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 247 } 248 249 { 250 var settings_view = try Settings.View.init(allocator, session); 251 errdefer settings_view.deinit(allocator); 252 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 253 } 254 255 // the header has no auth tab in local mode, so keep the stack's 256 // children 1:1 with the tabs by skipping the auth view too. 257 if (!session.data.is_local) { 258 var auth_view = try Auth.View.init(allocator, &data.auth, session, files_tab_id); 259 errdefer auth_view.deinit(allocator); 260 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 261 } 262 263 if (session.is_terminal) { 264 var quit_view = try Quit.View.init(allocator, session); 265 errdefer quit_view.deinit(allocator); 266 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 267 } 268 269 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 270 } 271 272 var self = View{ .box = box, .data = data, .session = session }; 273 self.getFocus().child_id = box.children.keys()[header_index]; 274 return self; 275 } 276 277 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 278 self.box.deinit(allocator); 279 } 280 281 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 282 self.clearGrid(); 283 const header = &self.box.children.values()[header_index].widget.repo_header; 284 const stack = &self.box.children.values()[stack_index].widget.stack; 285 286 // each header tab maps 1:1 to a stack child by position. mirror the 287 // selection into current_page so the host can push the matching url; 288 // all repo tabs share the .repo parent, so this stays on the page 289 // rather than navigating. 290 if (header.getSelectedIndex()) |index| { 291 stack.getFocus().child_id = stack.children.keys()[index]; 292 switch (std.meta.activeTag(stack.children.values()[index])) { 293 // files/commits carry this page's content route (directory / log 294 // page); settings/auth carry only "owner/name". 295 .repo_commits => self.session.data.current_page = .{ .repo_commits = .{ .name = self.data.commits_route_name, .after = self.data.commits_after } }, 296 // the refs tab mirrors this page's paginated column + offset. 297 .repo_refs => self.session.data.current_page = .{ .repo_refs = .{ .name = self.data.identity, .kind = self.data.refs.kind, .after = self.data.refs.after } }, 298 // the issues tab mirrors this page's tag filter (issue urls 299 // themselves never carry the tag). 300 .repo_issues => self.session.data.current_page = ui.RoutablePage.repoIssuesRoute(self.data.identity.slice(), self.data.issues.tag, "") orelse self.session.data.current_page, 301 .home_settings => self.session.data.current_page = .{ .repo_settings = self.data.identity }, 302 .home_auth => self.session.data.current_page = .{ .repo_auth = self.data.identity }, 303 // the quit tab is tty-only and not a route, so leave current_page 304 // alone (nothing to mirror into the url). 305 .quit => {}, 306 // the files tab (this page's directory route) 307 else => self.session.data.current_page = .{ .repo_files = .{ .name = self.data.route_name, .after = self.data.files_after } }, 308 } 309 } 310 try self.box.build(allocator, constraint, root_focus); 311 } 312 313 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 314 if (self.getFocus().child_id) |child_id| { 315 if (self.box.children.getIndex(child_id)) |current_index| { 316 const child = &self.box.children.values()[current_index].widget; 317 var index = current_index; 318 319 const direction = inp.vertDirection(key); 320 321 switch (direction) { 322 .up => { 323 switch (child.*) { 324 .repo_header => { 325 try child.input(allocator, key, root_focus); 326 }, 327 .stack => { 328 if (child.stack.getSelected()) |selected_widget| { 329 const at_top = switch (selected_widget.*) { 330 .repo_files => |*v| v.getSelectedIndex() == 0, 331 .repo_commits => |*v| v.getSelectedIndex() == 0, 332 .repo_refs => |*v| v.getSelectedIndex() == 0, 333 .repo_issues => |*v| v.getSelectedIndex() == 0, 334 .home_settings => |*v| v.getSelectedIndex() == 0, 335 .home_auth => |*v| v.getSelectedIndex() == 0, 336 .quit => |*v| v.getSelectedIndex() == 0, 337 else => false, 338 }; 339 if (at_top) { 340 index = header_index; 341 } else { 342 try child.input(allocator, key, root_focus); 343 } 344 } 345 }, 346 else => {}, 347 } 348 }, 349 .down => { 350 switch (child.*) { 351 .repo_header => { 352 index = stack_index; 353 }, 354 .stack => { 355 try child.input(allocator, key, root_focus); 356 }, 357 else => {}, 358 } 359 }, 360 .none => { 361 try child.input(allocator, key, root_focus); 362 }, 363 } 364 365 if (index != current_index) { 366 root_focus.setFocus(self.box.children.keys()[index]); 367 } 368 } 369 } 370 } 371 372 pub fn clearGrid(self: *View) void { 373 self.box.clearGrid(); 374 } 375 376 pub fn getGrid(self: View) ?Grid { 377 return self.box.getGrid(); 378 } 379 380 pub fn getFocus(self: *View) *Focus { 381 return self.box.getFocus(); 382 } 383 }; 384