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