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