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