╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
158 }; 159 } 160 } 161 162 // open the on-disk repo once and read every tab's data from it. files and 163 // commits resolve the same requested ref (the default branch when the 164 // route named none), so they end up viewing the same one and either's 165 // resolved ref can canonicalize the tab mirror urls below. no filesystem 166 // (wasm), nowhere to look, or a failed open: empty tabs. 167 const files, const commits, const refs, const issues = blk: { 168 read: { 169 const io = session.io orelse break :read; 170 const src = source orelse break :read; 171 const gpa = arena.child_allocator; 172 switch (src.repo_kind) { 173 inline else => |repo_kind| { 174 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, .{ .path = src.path }) catch break :read; 175 defer any_repo.deinit(io, gpa); 176 switch (any_repo) { 177 inline else => |*opened| { 178 // local mode: bring the event db up to date with the events branch 179 const is_local = session.local != null; 180 if (is_local) try evt.syncLocalEvents(repo_kind, opened.self_repo_opts, io, gpa, opened); 181 break :blk .{ 182 try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, rf.identity, requested_ref_or_oid, requested_ref_value, files_dir, files_after), 183 try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, rf.identity, requested_ref_or_oid, requested_ref_value, commits_after), 184 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, rf.identity, refs_kind, refs_after), 185 try Issues.init(repo_kind, opened.self_repo_opts, arena, opened, io, is_local, rf.identity, issues_tag, issues_selected, issues_view), 186 }; 187 }, 188 } 189 }, 190 } 191 } 192 const aa = arena.allocator(); 193 break :blk .{ 194 try Files.emptyResult(aa, rf.identity, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 195 try Commits.emptyResult(aa, rf.identity, requested_ref_or_oid orelse .branch, requested_ref_value), 196 try Refs.emptyResult(arena, rf.identity, refs_kind, refs_after), 197 try Issues.emptyResult(aa, rf.identity, issues_tag, issues_selected, issues_view), 198 }; 199 }; 200 201 // each tab mirror carries this page's route for that tab; tabs not targeted 202 // by the incoming route fall back to their root/first-page route. the files 203 // mirror carries the selected file (when the route named one) so the url 204 // keeps it before focus enters the view. 205 const files_path = if (files.selected_file) |f| try Files.childDir(arena.allocator(), files.dir, f) else files.dir; 206 // the content window offset only applies to a selected file, so the bare 207 // directory route drops it. 208 const files_route_after = if (files.selected_file != null) files_after else 0; 209 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; 210 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; 211 212 return .{ 213 // files and commits resolve the same ref, so either's serves the header, 214 // which points both tabs at it. 215 .header = try Header.init(arena, repo.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, issues_tag), 216 .repo = repo, 217 .files = files, 218 .commits = commits, 219 .refs = refs, 220 .issues = issues, 221 .settings = Settings.init(), 222 .auth = Auth.init(), 223 .quit = Quit.init(), 224 .route_name = route_name, 225 .files_after = files_route_after, 226 .commits_route_name = commits_route_name, 227 .commits_after = commits_after, 228 .identity = Array.from(rf.identity) orelse return error.NotFound, 229 }; 230 } 231 232 pub const View = struct { 233 box: wgt.Box(ui.Widget), 234 data: *const Self, 235 session: *ui.Session, 236 237 const header_index: usize = 0; 238 const stack_index: usize = 1; 239 240 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 241 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 242 errdefer box.deinit(allocator); 243 244 // build the header first so we can grab the files-tab id for the auth 245 // view (it focuses there after login). 246 var files_tab_id: usize = undefined; 247 { 248 var header_view = try Header.View.init(allocator, &data.header, session); 249 errdefer header_view.deinit(allocator); 250 files_tab_id = header_view.tab_ids.keys()[0]; 251 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 252 } 253 254 { 255 var stack = try wgt.Stack(ui.Widget).init(allocator); 256 errdefer stack.deinit(allocator); 257 258 // files — the default tab: the current directory's listing. 259 { 260 var files_view = try Files.View.init(allocator, &data.files, session); 261 errdefer files_view.deinit(allocator); 262 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 263 } 264 265 // commits — the current page of the commit log. 266 { 267 var commits_view = try Commits.View.init(allocator, &data.commits, session); 268 errdefer commits_view.deinit(allocator); 269 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 270 } 271 272 // refs — the repo's branches and tags. 273 { 274 var refs_view = try Refs.View.init(allocator, &data.refs, session); 275 errdefer refs_view.deinit(allocator); 276 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 277 } 278 279 // issues — the repo's issue tracker. 280 { 281 var issues_view = try Issues.View.init(allocator, &data.issues, session); 282 errdefer issues_view.deinit(allocator); 283 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 284 } 285 286 { 287 var settings_view = try Settings.View.init(allocator, session); 288 errdefer settings_view.deinit(allocator); 289 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 290 } 291 292 // the header has no auth tab in local mode, so keep the stack's 293 // children 1:1 with the tabs by skipping the auth view too. 294 if (!session.data.is_local) { 295 var auth_view = try Auth.View.init(allocator, &data.auth, session, files_tab_id); 296 errdefer auth_view.deinit(allocator); 297 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 298 } 299 300 if (session.is_terminal) { 301 var quit_view = try Quit.View.init(allocator, session); 302 errdefer quit_view.deinit(allocator); 303 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 304 } 305 306 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 307 } 308 309 var self = View{ .box = box, .data = data, .session = session }; 310 self.getFocus().child_id = box.children.keys()[header_index]; 311 return self; 312 } 313 314 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 315 self.box.deinit(allocator); 316 } 317 318 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 319 self.clearGrid(); 320 const header = &self.box.children.values()[header_index].widget.repo_header; 321 const stack = &self.box.children.values()[stack_index].widget.stack; 322 323 // each header tab maps 1:1 to a stack child by position. mirror the 324 // selection into current_page so the host can push the matching url; 325 // all repo tabs share the .repo parent, so this stays on the page 326 // rather than navigating. 327 if (header.getSelectedIndex()) |index| { 328 stack.getFocus().child_id = stack.children.keys()[index]; 329 switch (std.meta.activeTag(stack.children.values()[index])) { 330 // files/commits carry this page's content route (directory / log 331 // page); settings/auth carry only "owner/name". 332 .repo_commits => self.session.data.current_page = .{ .repo_commits = .{ .name = self.data.commits_route_name, .after = self.data.commits_after } }, 333 // the refs tab mirrors this page's paginated column + offset. 334 .repo_refs => self.session.data.current_page = .{ .repo_refs = .{ .name = self.data.identity, .kind = self.data.refs.kind, .after = self.data.refs.after } }, 335 // the issues tab mirrors this page's tag filter (issue urls 336 // themselves never carry the tag). 337 .repo_issues => self.session.data.current_page = ui.RoutablePage.repoIssuesRoute(self.data.identity.slice(), .open, self.data.issues.tag, "") orelse self.session.data.current_page, 338 .home_settings => self.session.data.current_page = .{ .repo_settings = self.data.identity }, 339 .home_auth => self.session.data.current_page = .{ .repo_auth = self.data.identity }, 340 // the quit tab is tty-only and not a route, so leave current_page 341 // alone (nothing to mirror into the url). 342 .quit => {}, 343 // the files tab (this page's directory route) 344 else => self.session.data.current_page = .{ .repo_files = .{ .name = self.data.route_name, .after = self.data.files_after } }, 345 } 346 } 347 try self.box.build(allocator, constraint, root_focus); 348 } 349 350 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 351 if (self.getFocus().child_id) |child_id| { 352 if (self.box.children.getIndex(child_id)) |current_index| { 353 const child = &self.box.children.values()[current_index].widget; 354 var index = current_index; 355 356 const direction = inp.vertDirection(key); 357 358 switch (direction) { 359 .up => { 360 switch (child.*) { 361 .repo_header => { 362 try child.input(allocator, key, root_focus); 363 }, 364 .stack => { 365 if (child.stack.getSelected()) |selected_widget| { 366 const at_top = switch (selected_widget.*) { 367 .repo_files => |*v| v.getSelectedIndex() == 0, 368 .repo_commits => |*v| v.getSelectedIndex() == 0, 369 .repo_refs => |*v| v.getSelectedIndex() == 0, 370 .repo_issues => |*v| v.getSelectedIndex() == 0, 371 .home_settings => |*v| v.getSelectedIndex() == 0, 372 .home_auth => |*v| v.getSelectedIndex() == 0, 373 .quit => |*v| v.getSelectedIndex() == 0, 374 else => false, 375 }; 376 if (at_top) { 377 index = header_index; 378 } else { 379 try child.input(allocator, key, root_focus); 380 } 381 } 382 }, 383 else => {}, 384 } 385 }, 386 .down => { 387 switch (child.*) { 388 .repo_header => { 389 index = stack_index; 390 }, 391 .stack => { 392 try child.input(allocator, key, root_focus); 393 }, 394 else => {}, 395 } 396 }, 397 .none => { 398 try child.input(allocator, key, root_focus); 399 }, 400 } 401 402 if (index != current_index) { 403 root_focus.setFocus(self.box.children.keys()[index]); 404 } 405 } 406 } 407 } 408 409 pub fn clearGrid(self: *View) void { 410 self.box.clearGrid(); 411 } 412 413 pub fn getGrid(self: View) ?Grid { 414 return self.box.getGrid(); 415 } 416 417 pub fn getFocus(self: *View) *Focus { 418 return self.box.getFocus(); 419 } 420 }; 421