╭ 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 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 59 60 // every repo route's stored string starts with "owner/name"; the files and 61 // commits routes additionally encode a ref/oid (files also a directory). 62 const name_str: []const u8 = switch (route) { 63 .repo_files => |f| f.name.slice(), 64 .repo_settings, .repo_auth => |n| n.slice(), 65 .repo_commits => |c| c.name.slice(), 66 .repo_refs => |r| r.name.slice(), 67 .repo_issues => |i| i.name.slice(), 68 else => return error.UnexpectedRoute, 69 }; 70 const rf = ui.RoutablePage.RepoFiles.parse(name_str) orelse return error.NotFound; 71 const tag = std.meta.activeTag(route); 72 73 // the files and commits tabs share one ref/oid: whichever the incoming route 74 // names (it rides on the route's target tab), or the default branch when 75 // neither tab is targeted. building both views at it keeps switching tabs 76 // (even by key, without a reload) on the same ref. a null ref means the 77 // default branch (Files/Commits.init resolve it). the directory and the diff 78 // window only apply to their own tab. 79 const commits_ref = if (tag == .repo_commits) ui.RoutablePage.repoCommitsRef(name_str) else ui.RoutablePage.CommitsRef{ .ref_or_oid = null, .value = "" }; 80 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (tag) { 81 .repo_files => rf.ref_kind, 82 .repo_commits => commits_ref.ref_or_oid, 83 else => null, 84 }; 85 const requested_ref_value: []const u8 = switch (tag) { 86 .repo_files => rf.ref_value, 87 .repo_commits => commits_ref.value, 88 else => "", 89 }; 90 const files_dir = if (tag == .repo_files) rf.dir else ""; 91 // the line offset the files view's selected file content window starts at. 92 const files_after: usize = switch (route) { 93 .repo_files => |f| f.after, 94 else => 0, 95 }; 96 // how many diff hunks the commits view's selected commit shows ("load more"). 97 const commits_after: usize = switch (route) { 98 .repo_commits => |c| c.after, 99 else => 0, 100 }; 101 // the refs tab paginates one column at a time: this offset applies to 102 // `refs_kind`'s column, the other stays at its first window. 103 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 104 .repo_refs => |r| r.kind, 105 else => .branch, 106 }; 107 const refs_after: usize = switch (route) { 108 .repo_refs => |r| r.after, 109 else => 0, 110 }; 111 // the issues tab's tag filter and the issue its window is rooted at. 112 const issues_tag: []const u8 = switch (route) { 113 .repo_issues => |i| i.tag.slice(), 114 else => "", 115 }; 116 const issues_selected: []const u8 = switch (route) { 117 .repo_issues => |i| i.selected.slice(), 118 else => "", 119 }; 120 121 const found = (try evt.Repo.readByOwnerAndName(DB, hash_kind, haxy_moment, arena, rf.owner, rf.name)) orelse return error.NotFound; 122 const repo = found.repo; 123 124 // resolve the creating user so the header can show their name to the left 125 // of the repo title. 126 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.user_id)) orelse return error.NotFound; 127 128 // build files and commits first so their resolved ref (the default branch 129 // when the route named none) can canonicalize each tab's mirror url to the 130 // explicit ref it's viewing rather than leaving it bare. both resolve the 131 // same requested ref, so they end up viewing the same one. 132 const files = try Files.init(arena, session, &found.event_id, rf.identity, requested_ref_or_oid, requested_ref_value, files_dir, files_after); 133 const commits = try Commits.init(arena, session, &found.event_id, rf.identity, requested_ref_or_oid, requested_ref_value, commits_after); 134 135 // each tab mirror carries this page's route for that tab; tabs not targeted 136 // by the incoming route fall back to their root/first-page route. the files 137 // mirror carries the selected file (when the route named one) so the url 138 // keeps it before focus enters the view. 139 const files_path = if (files.selected_file) |f| try Files.childDir(arena.allocator(), files.dir, f) else files.dir; 140 // the content window offset only applies to a selected file, so the bare 141 // directory route drops it. 142 const files_route_after = if (files.selected_file != null) files_after else 0; 143 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; 144 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; 145 146 return .{ 147 // files and commits resolve the same ref, so either's serves the header, 148 // which points both tabs at it. 149 .header = try Header.init(arena, repo.name, owner.name, files.ref_or_oid, files.ref_or_oid_value, issues_tag), 150 .repo = repo, 151 .files = files, 152 .commits = commits, 153 .refs = try Refs.init(arena, session, &found.event_id, rf.identity, refs_kind, refs_after), 154 .issues = try Issues.init(arena, session, &found.event_id, rf.identity, issues_tag, issues_selected), 155 .settings = Settings.init(), 156 .auth = Auth.init(), 157 .quit = Quit.init(), 158 .route_name = route_name, 159 .files_after = files_route_after, 160 .commits_route_name = commits_route_name, 161 .commits_after = commits_after, 162 .identity = Array.from(rf.identity) orelse return error.NotFound, 163 }; 164 } 165 166 pub const View = struct { 167 box: wgt.Box(ui.Widget), 168 data: *const Self, 169 session: *ui.Session, 170 171 const header_index: usize = 0; 172 const stack_index: usize = 1; 173 174 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 175 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 176 errdefer box.deinit(allocator); 177 178 // build the header first so we can grab the files-tab id for the auth 179 // view (it focuses there after login). 180 var files_tab_id: usize = undefined; 181 { 182 var header_view = try Header.View.init(allocator, &data.header, session); 183 errdefer header_view.deinit(allocator); 184 files_tab_id = header_view.tab_ids.keys()[0]; 185 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 186 } 187 188 { 189 var stack = try wgt.Stack(ui.Widget).init(allocator); 190 errdefer stack.deinit(allocator); 191 192 // files — the default tab: the current directory's listing. 193 { 194 var files_view = try Files.View.init(allocator, &data.files, session); 195 errdefer files_view.deinit(allocator); 196 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 197 } 198 199 // commits — the current page of the commit log. 200 { 201 var commits_view = try Commits.View.init(allocator, &data.commits, session); 202 errdefer commits_view.deinit(allocator); 203 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 204 } 205 206 // refs — the repo's branches and tags. 207 { 208 var refs_view = try Refs.View.init(allocator, &data.refs, session); 209 errdefer refs_view.deinit(allocator); 210 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 211 } 212 213 // issues — the repo's issue tracker. 214 { 215 var issues_view = try Issues.View.init(allocator, &data.issues, session); 216 errdefer issues_view.deinit(allocator); 217 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 218 } 219 220 { 221 var settings_view = try Settings.View.init(allocator, session); 222 errdefer settings_view.deinit(allocator); 223 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 224 } 225 226 { 227 var auth_view = try Auth.View.init(allocator, &data.auth, session, files_tab_id); 228 errdefer auth_view.deinit(allocator); 229 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 230 } 231 232 if (session.is_terminal) { 233 var quit_view = try Quit.View.init(allocator, session); 234 errdefer quit_view.deinit(allocator); 235 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 236 } 237 238 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 239 } 240 241 var self = View{ .box = box, .data = data, .session = session }; 242 self.getFocus().child_id = box.children.keys()[header_index]; 243 return self; 244 } 245 246 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 247 self.box.deinit(allocator); 248 } 249 250 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 251 self.clearGrid(); 252 const header = &self.box.children.values()[header_index].widget.repo_header; 253 const stack = &self.box.children.values()[stack_index].widget.stack; 254 255 // each header tab maps 1:1 to a stack child by position. mirror the 256 // selection into current_page so the host can push the matching url; 257 // all repo tabs share the .repo parent, so this stays on the page 258 // rather than navigating. 259 if (header.getSelectedIndex()) |index| { 260 stack.getFocus().child_id = stack.children.keys()[index]; 261 switch (std.meta.activeTag(stack.children.values()[index])) { 262 // files/commits carry this page's content route (directory / log 263 // page); settings/auth carry only "owner/name". 264 .repo_commits => self.session.data.current_page = .{ .repo_commits = .{ .name = self.data.commits_route_name, .after = self.data.commits_after } }, 265 // the refs tab mirrors this page's paginated column + offset. 266 .repo_refs => self.session.data.current_page = .{ .repo_refs = .{ .name = self.data.identity, .kind = self.data.refs.kind, .after = self.data.refs.after } }, 267 // the issues tab mirrors this page's tag filter (issue urls 268 // themselves never carry the tag). 269 .repo_issues => self.session.data.current_page = ui.RoutablePage.repoIssuesRoute(self.data.identity.slice(), self.data.issues.tag, "") orelse self.session.data.current_page, 270 .home_settings => self.session.data.current_page = .{ .repo_settings = self.data.identity }, 271 .home_auth => self.session.data.current_page = .{ .repo_auth = self.data.identity }, 272 // the quit tab is tty-only and not a route, so leave current_page 273 // alone (nothing to mirror into the url). 274 .quit => {}, 275 // the files tab (this page's directory route) 276 else => self.session.data.current_page = .{ .repo_files = .{ .name = self.data.route_name, .after = self.data.files_after } }, 277 } 278 } 279 try self.box.build(allocator, constraint, root_focus); 280 } 281 282 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 283 if (self.getFocus().child_id) |child_id| { 284 if (self.box.children.getIndex(child_id)) |current_index| { 285 const child = &self.box.children.values()[current_index].widget; 286 var index = current_index; 287 288 const direction = inp.vertDirection(key); 289 290 switch (direction) { 291 .up => { 292 switch (child.*) { 293 .repo_header => { 294 try child.input(allocator, key, root_focus); 295 }, 296 .stack => { 297 if (child.stack.getSelected()) |selected_widget| { 298 const at_top = switch (selected_widget.*) { 299 .repo_files => |*v| v.getSelectedIndex() == 0, 300 .repo_commits => |*v| v.getSelectedIndex() == 0, 301 .repo_refs => |*v| v.getSelectedIndex() == 0, 302 .repo_issues => |*v| v.getSelectedIndex() == 0, 303 .home_settings => |*v| v.getSelectedIndex() == 0, 304 .home_auth => |*v| v.getSelectedIndex() == 0, 305 .quit => |*v| v.getSelectedIndex() == 0, 306 else => false, 307 }; 308 if (at_top) { 309 index = header_index; 310 } else { 311 try child.input(allocator, key, root_focus); 312 } 313 } 314 }, 315 else => {}, 316 } 317 }, 318 .down => { 319 switch (child.*) { 320 .repo_header => { 321 index = stack_index; 322 }, 323 .stack => { 324 try child.input(allocator, key, root_focus); 325 }, 326 else => {}, 327 } 328 }, 329 .none => { 330 try child.input(allocator, key, root_focus); 331 }, 332 } 333 334 if (index != current_index) { 335 root_focus.setFocus(self.box.children.keys()[index]); 336 } 337 } 338 } 339 } 340 341 pub fn clearGrid(self: *View) void { 342 self.box.clearGrid(); 343 } 344 345 pub fn getGrid(self: View) ?Grid { 346 return self.box.getGrid(); 347 } 348 349 pub fn getFocus(self: *View) *Focus { 350 return self.box.getFocus(); 351 } 352 }; 353