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