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