╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
25 const max_list_entries = 1000; 26 27 // one entry in the directory currently being viewed. 28 pub const Entry = struct { 29 name: []const u8, 30 is_dir: bool, 31 // a file's content window split into lines (each without its trailing 32 // newline); empty for directories and for binary or empty files. 33 lines: []const []const u8 = &.{}, 34 // true when xit's line iterator flagged the file as binary, so the detail 35 // pane shows a placeholder rather than its bytes. 36 is_binary: bool = false, 37 // the line index this window starts at (0 = the first window). non-zero only 38 // for the route's selected file, paginated by the url's `line`. 39 window_start: usize = 0, 40 // whether more lines exist after this window. 41 has_more: bool = false, 42 // whether this file's content was read; unloaded files load via their 43 // "a:" link. 44 loaded: bool = false, 45 }; 46 47 // where links and clone commands for this repository are rooted. 48 location: ui.RoutablePage.RepoLocation, 49 // the resolved ref this listing is read at (the default branch when the route 50 // didn't name one), so the view's directory links stay pinned to it. 51 ref_or_oid: ui.RoutablePage.RefOrOid, 52 ref_or_oid_value: []const u8, 53 patchrev_id: ?[]const u8 = null, 54 // the directory being viewed, relative to the repo root ("" at the root). 55 dir: []const u8, 56 // a file within `dir` to start selected (so its contents show and the url 57 // carries it), or null to fall back to a README / the first row. set when the 58 // route's path named a file rather than a directory. 59 selected_file: ?[]const u8 = null, 60 entries: []const Entry, 61 // true when the listing stopped at max_list_entries. 62 capped: bool = false, 63 // the decoded search term this listing holds the results of, or null for a 64 // plain directory listing. 65 find: ?[]const u8 = null, 66 // whether the viewed tree has a file index, so the search box shows and a 67 // find: route resolves. travels in the page json for the wasm view. 68 find_available: bool = false, 69 70 const Self = @This(); 71 72 pub fn initPatchRev( 73 comptime repo_kind: rp.RepoKind, 74 comptime repo_opts: rp.RepoOpts(repo_kind), 75 arena: *std.heap.ArenaAllocator, 76 repo: *rp.Repo(repo_kind, repo_opts), 77 io: std.Io, 78 gpa: std.mem.Allocator, 79 location: ui.RoutablePage.RepoLocation, 80 id_hex: []const u8, 81 path: []const u8, 82 line: usize, 83 ) !Self { 84 const id = evt.parseEventId(id_hex) catch return error.NotFound; 85 const record = (try evt.readFromRepo(evt.PatchRev, repo_kind, repo_opts, io, gpa, arena, repo, &id)) orelse return error.NotFound; 86 if (record.removed) return error.NotFound; 87 var data = try init(repo_kind, repo_opts, arena, repo, io, gpa, location, .object, record.head_tree_oid, path, line, ""); 88 data.patchrev_id = try arena.allocator().dupe(u8, id_hex); 89 return data; 90 } 91 92 pub fn filesRoute(self: *const Self, path: []const u8, line: usize) ?ui.RoutablePage { 93 if (self.patchrev_id) |id| return switch (self.location) { 94 .repo => |identity| ui.RoutablePage.repoPatchRevFilesRoute(identity, id, path, line), 95 .fork => null, 96 }; 97 const route = self.location.filesRoute(self.ref_or_oid, self.ref_or_oid_value, path, line) orelse return null; 98 return route.withFind(self.find orelse return route); 99 } 100 101 // build the listing for an opened repo. generic over the repo's backend and 102 // hash kind so the tree/diff types it threads through match the repo's opts. 103 // reads with the arena's backing allocator (transient); the listing is built 104 // into the page arena so it outlives the repo handle. 105 pub fn init( 106 comptime repo_kind: rp.RepoKind, 107 comptime repo_opts: rp.RepoOpts(repo_kind), 108 arena: *std.heap.ArenaAllocator, 109 repo: *rp.Repo(repo_kind, repo_opts), 110 io: std.Io, 111 gpa: std.mem.Allocator, 112 location: ui.RoutablePage.RepoLocation, 113 requested_ref_or_oid: ?ui.RoutablePage.RefOrOid, 114 requested_value: []const u8, 115 path: []const u8, 116 line: usize, 117 find: []const u8, 118 ) !Self { 119 const aa = arena.allocator(); 120 const term: ?[]const u8 = if (find.len == 0) null else std.Uri.percentDecodeInPlace(try aa.dupe(u8, find)); 121 122 // resolve the requested ref (or the default branch) to the object oid whose 123 // tree we list. a ref the route named explicitly that doesn't resolve is a 124 // bad url (NotFound -> 404); the default-branch path falls through to empty. 125 const resolved = (try ui.ResolvedRefOrOid(repo_kind, repo_opts).init(repo, io, aa, requested_ref_or_oid, requested_value)) orelse { 126 if (requested_ref_or_oid != null or term != null) return error.NotFound; 127 return emptyResult(aa, location, .branch, requested_value, path); 128 }; 129 130 // read just the viewed directory of that tree or commit. building the 131 // read-only state mirrors what repo.status does internally, but for an 132 // arbitrary commit rather than HEAD. a path that doesn't exist in the tree 133 // is a bad url (404). 134 var moment = repo.core.latestMoment() catch return emptyResult(aa, location, resolved.ref_or_oid, resolved.value, path); 135 const state = rp.Repo(repo_kind, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 136 137 var entries: std.ArrayList(Entry) = .empty; 138 var capped = false; 139 var preloaded_bytes: usize = 0; 140 var find_available = false; 141 142 // the index covers branch tips only, so any other view gets no search box 143 // and refuses a find: url. 144 if (comptime repo_kind == .xit) index: { 145 if (resolved.ref_or_oid != .branch) break :index; 146 const branch = std.Uri.percentDecodeInPlace(try aa.dupe(u8, resolved.value)); 147 const tree_oid = try fnd.rootTreeOid(repo_opts, state, io, gpa, &resolved.oid); 148 const files = (try fnd.lookup(repo_opts, moment, branch, &tree_oid)) orelse break :index; 149 find_available = true; 150 const value = term orelse break :index; 151 // a result's name is its full path, so the listing has no directory and 152 // the selected row is named in full. 153 const selected: ?[]const u8 = if (path.len == 0) null else try aa.dupe(u8, path); 154 const results = try fnd.search(repo_opts, files, aa, value, max_list_entries); 155 capped = results.capped; 156 var found = false; 157 for (results.matches) |match| { 158 if (selected) |sf| found = found or std.mem.eql(u8, sf, match.path); 159 try entries.append(aa, try fileEntry(repo_kind, repo_opts, state, io, gpa, aa, "", match.path, &match.oid, null, selected, line, &preloaded_bytes)); 160 } 161 // the cap can cut off the row the url names, so bring it back 162 if (capped and !found) { 163 if (selected) |sf| if (try fnd.get(repo_opts, files, aa, sf)) |*oid| { 164 try entries.append(aa, try fileEntry(repo_kind, repo_opts, state, io, gpa, aa, "", sf, oid, null, selected, line, &preloaded_bytes)); 165 }; 166 } 167 return .{ 168 .location = try location.dupe(aa), 169 .ref_or_oid = resolved.ref_or_oid, 170 .ref_or_oid_value = resolved.value, 171 .dir = "", 172 .selected_file = selected, 173 .entries = entries.items, 174 .capped = capped, 175 .find = value, 176 .find_available = true, 177 }; 178 } 179 if (term != null) return error.NotFound; 180 181 var tree_dir = tr.TreeDir(repo_kind, repo_opts).init(state, io, gpa, &resolved.oid, path) catch |err| switch (err) { 182 error.TreeEntryNotFound => return error.NotFound, 183 else => return emptyResult(aa, location, resolved.ref_or_oid, resolved.value, path), 184 }; 185 defer tree_dir.deinit(); 186 187 // when `path` named a file, the listing is its parent directory with that 188 // file selected. 189 var dir = path; 190 var selected_file: ?[]const u8 = null; 191 if (tree_dir.file_name != null) { 192 const slash = std.mem.lastIndexOfScalar(u8, path, '/'); 193 selected_file = try aa.dupe(u8, if (slash) |s| path[s + 1 ..] else path); 194 dir = if (slash) |s| path[0..s] else ""; 195 } 196 197 // tree objects store their entries in name order, so the listing is 198 // already sorted. group directories before files, keeping that order 199 // within each group, and dupe into the page arena. file contents are read 200 // here (into the page arena) so the detail pane can show them without 201 // another lookup. 202 try entries.ensureTotalCapacity(aa, @min(tree_dir.entries.count(), max_list_entries)); 203 for ([_]bool{ true, false }) |want_dir| { 204 for (tree_dir.entries.keys(), tree_dir.entries.values()) |name, tree_entry| { 205 if (tree_entry.isTree() != want_dir) continue; 206 if (want_dir) { 207 if (entries.items.len >= max_list_entries) { 208 capped = true; 209 continue; 210 } 211 try entries.append(aa, .{ .name = try aa.dupe(u8, name), .is_dir = true }); 212 } else { 213 // the rows the route and the default selection need are exempt 214 // from the cap, so a url naming a file always gets its row. 215 const exempt = isReadme(name) or (if (selected_file) |sf| std.mem.eql(u8, sf, name) else false); 216 if (entries.items.len >= max_list_entries and !exempt) { 217 capped = true; 218 continue; 219 } 220 try entries.append(aa, try fileEntry(repo_kind, repo_opts, state, io, gpa, aa, dir, name, &tree_entry.oid, tree_entry.mode, selected_file, line, &preloaded_bytes)); 221 } 222 } 223 } 224 225 return .{ 226 .location = try location.dupe(aa), 227 .ref_or_oid = resolved.ref_or_oid, 228 .ref_or_oid_value = resolved.value, 229 .dir = try aa.dupe(u8, dir), 230 .selected_file = selected_file, 231 .entries = entries.items, 232 .capped = capped, 233 .find_available = find_available, 234 }; 235 } 236 237 // build one file's row, reading its content window when the preload budget (or 238 // the route) calls for it. only the route's selected file paginates; the rest 239 // show their first window, for the in-page detail when selected. 240 fn fileEntry( 241 comptime repo_kind: rp.RepoKind, 242 comptime repo_opts: rp.RepoOpts(repo_kind), 243 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 244 io: std.Io, 245 gpa: std.mem.Allocator, 246 aa: std.mem.Allocator, 247 dir: []const u8, 248 name: []const u8, 249 oid: *const [xit.hash.byteLen(repo_opts.hash)]u8, 250 // null for a search result, which the index only holds files for 251 mode: ?xit.fs.Mode, 252 selected_file: ?[]const u8, 253 line: usize, 254 preloaded_bytes: *usize, 255 ) !Entry { 256 var entry: Entry = .{ .name = try aa.dupe(u8, name), .is_dir = false }; 257 const is_selected = if (selected_file) |sf| std.mem.eql(u8, sf, name) else false; 258 // the README always loads because it's the default selection. 259 if (preloaded_bytes.* >= max_preloaded_bytes and !is_selected and !isReadme(name)) return entry; 260 const file_path = try childDir(aa, dir, name); 261 // the url's `line` is 1-based (0 = unset); the window index is 0-based. 262 const window_start = if (is_selected) line -| 1 else 0; 263 const content = readFileContent(repo_kind, repo_opts, state, io, gpa, aa, file_path, oid, mode, window_start) catch 264 FileContent{ .lines = &.{} }; 265 for (content.lines) |l| preloaded_bytes.* += l.len; 266 entry.lines = content.lines; 267 entry.is_binary = content.is_binary; 268 entry.window_start = window_start; 269 entry.has_more = content.has_more; 270 entry.loaded = true; 271 return entry; 272 } 273 274 const FileContent = struct { 275 lines: []const []const u8, 276 is_binary: bool = false, 277 has_more: bool = false, 278 }; 279 280 // read the window [start, start+file_page) of a committed file's contents at 281 // `oid` into `arena`-owned lines, flagging whether more follow. xit's 282 // line iterator flags binary files (its source becomes `.binary`), in which case 283 // we report no lines and let the view show a placeholder. 284 fn readFileContent( 285 comptime repo_kind: rp.RepoKind, 286 comptime repo_opts: rp.RepoOpts(repo_kind), 287 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 288 io: std.Io, 289 gpa: std.mem.Allocator, 290 arena: std.mem.Allocator, 291 path: []const u8, 292 oid: *const [xit.hash.byteLen(repo_opts.hash)]u8, 293 mode: ?xit.fs.Mode, 294 start: usize, 295 ) !FileContent { 296 var line_iter = try df.LineIterator(repo_kind, repo_opts).initFromOid(state, io, gpa, path, oid, mode); 297 defer line_iter.deinit(); 298 if (line_iter.source == .binary) return .{ .lines = &.{}, .is_binary = true }; 299 300 // init reads through the file to validate it (and buffer it in memory), so 301 // the line count is known and a window can be sliced out of the buffer. 302 const total = line_iter.count(); 303 const end = @min(start +| file_page, total); 304 305 var lines: std.ArrayList([]const u8) = .empty; 306 var i = start; 307 while (i < end) : (i += 1) { 308 const line = try line_iter.get(i); 309 try lines.append(arena, try arena.dupe(u8, line)); 310 } 311 return .{ 312 .lines = try lines.toOwnedSlice(arena), 313 .has_more = end < total, 314 }; 315 } 316 317 // an empty listing pinned to a ref, for the wasm / no-repo / unresolved paths. 318 pub fn emptyResult(aa: std.mem.Allocator, location: ui.RoutablePage.RepoLocation, ref_or_oid: ui.RoutablePage.RefOrOid, ref_or_oid_value: []const u8, dir: []const u8) !Self { 319 return .{ 320 .location = try location.dupe(aa), 321 .ref_or_oid = ref_or_oid, 322 .ref_or_oid_value = try aa.dupe(u8, ref_or_oid_value), 323 .dir = try aa.dupe(u8, dir), 324 .entries = &.{}, 325 }; 326 } 327 328 pub const View = struct { 329 // a vertical stack: the search box and clone url on top, then a horizontal 330 // split with the file/directory list on the left and a detail pane on the 331 // right showing the selected file's contents. 332 box: wgt.Box(ui.Widget), // vert: [header_index] = header row, [content_index] = split 333 data: *const Self, 334 session: *ui.Session, 335 // the list row whose contents the detail pane currently shows. 336 shown_index: ?usize, 337 338 const header_index: usize = 0; 339 const content_index: usize = 1; 340 // indices within the content box (the horizontal split). 341 const list_index: usize = 0; 342 const detail_index: usize = 1; 343 // indices within the detail pane frame (nav box above the content scroll). 344 const detail_nav_index: usize = 0; 345 const detail_scroll_index: usize = 1; 346 const list_max_width: usize = 35; 347 const detail_min_width: usize = 40; 348 // a list row's height: one text line inside its (hidden) border. 349 const row_height: usize = 3; 350 351 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 352 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 353 errdefer outer.deinit(allocator); 354 355 // the search box and the clone url at the top. 356 { 357 var header_view = try ui.widget.SearchHeader.init(allocator, session, data.location, " find file ", "find", data.find, data.find_available); 358 errdefer header_view.deinit(allocator); 359 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .search_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 360 } 361 362 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 363 errdefer box.deinit(allocator); 364 365 // the directory listing on the left (one focusable row each). 366 { 367 var list_scroll = blk: { 368 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 369 errdefer list_box.deinit(allocator); 370 371 // link kinds are borrowed by the rows, so they live in the page 372 // arena (as long as this page's widget tree). row labels are 373 // copied by their text boxes. 374 const aa = session.page_arena.allocator(); 375 if (data.dir.len != 0) { 376 try addRow(allocator, &list_box, "..", "", try dirLink(session.page_arena, data, parentDir(data.dir))); 377 } 378 for (data.entries) |entry| { 379 const path = try childDir(aa, data.dir, entry.name); 380 // a search result shows its file name, with its directory 381 // in the bottom label. 382 const label = if (data.find != null) 383 std.fs.path.basenamePosix(entry.name) 384 else if (entry.is_dir) 385 try std.fmt.allocPrint(aa, "{s}/", .{entry.name}) 386 else 387 entry.name; 388 const bottom_label = if (data.find != null) try resultDirLabel(aa, entry.name) else ""; 389 const link = if (entry.is_dir) 390 try dirLink(session.page_arena, data, path) 391 else 392 try fileLink(session.page_arena, data, path, entry.loaded, lineNumber(entry.window_start)); 393 try addRow(allocator, &list_box, label, bottom_label, link); 394 } 395 if (data.capped) try addRow(allocator, &list_box, "(list truncated)", "", ""); 396 // select the file the route named, else prefer a README so its 397 // contents greet the visitor in the detail pane, else the first 398 // row. a non-root directory has a leading ".." row, so the entry 399 // index is offset by one there. search results have no README 400 // default: the term decides what matters. 401 var sel: usize = 0; 402 if (list_box.children.count() > 0) { 403 const base: usize = if (data.dir.len != 0) 1 else 0; 404 const entry_idx = selectedFileIndex(data) orelse if (data.find == null) readmeIndex(data.entries) else null; 405 sel = if (entry_idx) |i| i + base else 0; 406 list_box.getFocus().child_id = list_box.children.keys()[sel]; 407 } 408 var sc = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 409 // start scrolled so the selected row begins at the top of the 410 // viewport. rows are a fixed height, so no layout pass is 411 // needed; a too-far offset near the end of the list is clamped 412 // in build (the browser clamps its own scrolling natively). 413 sc.y = @intCast(sel * row_height); 414 break :blk sc; 415 }; 416 errdefer list_scroll.deinit(allocator); 417 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 418 } 419 420 // the detail pane on the right — a "next" nav box above a scroll of the 421 // selected file's content window. both are repopulated per selection by 422 // populateDetail. 423 { 424 var detail_outer = blk: { 425 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 426 errdefer frame.deinit(allocator); 427 428 { 429 var nav_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 430 errdefer nav_box.deinit(allocator); 431 try frame.children.put(allocator, nav_box.getFocus().id, .{ .widget = .{ .box = nav_box }, .rect = null, .min_size = null }); 432 } 433 { 434 // the content scroll wrapped in a bordered box; build sets the 435 // border single normally and double when the content is focused 436 // (the content box itself is borderless). 437 var scroll_frame = blk2: { 438 var detail_scroll = blk3: { 439 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 440 errdefer detail_inner.deinit(allocator); 441 break :blk3 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }); 442 }; 443 errdefer detail_scroll.deinit(allocator); 444 var sf = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .single, .rounded_corners = true, .direction = .vert }); 445 errdefer sf.deinit(allocator); 446 // the wrapper's selected child is its scroll, so the focus 447 // chain reaches the content box (populateDetail points the 448 // scroll's inner box at it). 449 sf.getFocus().child_id = detail_scroll.getFocus().id; 450 try sf.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 451 break :blk2 sf; 452 }; 453 errdefer scroll_frame.deinit(allocator); 454 // entering the detail pane lands on the content scroll (focus 455 // recovery descends the frame's selected child). the nav links 456 // above are reached by scrolling to the top, then up. 457 frame.getFocus().child_id = scroll_frame.getFocus().id; 458 // shrink, so the fill scroll yields the nav box's row 459 // instead of consuming the frame's whole height. 460 try frame.children.put(allocator, scroll_frame.getFocus().id, .{ .widget = .{ .box = scroll_frame }, .rect = null, .min_size = null, .flex = .shrink }); 461 } 462 break :blk frame; 463 }; 464 errdefer detail_outer.deinit(allocator); 465 detail_outer.getFocus().mode = .mouse; 466 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 467 } 468 469 box.getFocus().child_id = box.children.keys()[list_index]; 470 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 471 472 // focus lives in the split, except that search results start in the 473 // search box so the term can be refined right away. 474 outer.getFocus().child_id = outer.children.keys()[if (data.find != null) header_index else content_index]; 475 return .{ .box = outer, .data = data, .session = session, .shown_index = null }; 476 } 477 478 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, bottom_label: []const u8, link: []const u8) !void { 479 var row = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none, .bottom_label = bottom_label }); 480 errdefer row.deinit(allocator); 481 row.getFocus().mode = .all; 482 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 483 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 484 } 485 486 // the selected file's contents as a single focusable multi-line text box. 487 // it's borderless — the border lives on the surrounding scroll frame, which 488 // doubles when this content is focused. a non-empty `link` makes activating 489 // the box follow it, like addRow. 490 fn addContentBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 491 _ = self; 492 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = null, .wrap_kind = .none }); 493 errdefer tb.deinit(allocator); 494 tb.getFocus().mode = .all; 495 if (link.len != 0) tb.getFocus().kind = .{ .custom = link }; 496 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 497 } 498 499 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 500 self.box.deinit(allocator); 501 } 502 503 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 504 return &self.box.children.values()[content_index].widget.box; 505 } 506 507 fn header(self: *View) *ui.widget.SearchHeader { 508 return &self.box.children.values()[header_index].widget.search_header; 509 } 510 511 fn headerActive(self: *View) bool { 512 return self.box.getFocus().child_id == self.header().getFocus().id; 513 } 514 515 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 516 return &self.contentBox().children.values()[list_index].widget.scroll; 517 } 518 519 fn listBox(self: *View) *wgt.Box(ui.Widget) { 520 return &self.listScroll().child.box; 521 } 522 523 fn detailOuter(self: *View) *wgt.Box(ui.Widget) { 524 return &self.contentBox().children.values()[detail_index].widget.box; 525 } 526 527 fn navBox(self: *View) *wgt.Box(ui.Widget) { 528 return &self.detailOuter().children.values()[detail_nav_index].widget.box; 529 } 530 531 // the bordered box wrapping the content scroll (border toggles with focus). 532 fn detailScrollFrame(self: *View) *wgt.Box(ui.Widget) { 533 return &self.detailOuter().children.values()[detail_scroll_index].widget.box; 534 } 535 536 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 537 return &self.detailScrollFrame().children.values()[0].widget.scroll; 538 } 539 540 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 541 return &self.detailScroll().child.box; 542 } 543 544 fn detailActive(self: *View) bool { 545 const content = self.contentBox(); 546 const cid = content.getFocus().child_id orelse return false; 547 return content.children.getIndex(cid) == detail_index; 548 } 549 550 // the selected list row's index, or null when nothing is focused. 551 fn selectedRowIndex(self: *View) ?usize { 552 const lb = self.listBox(); 553 const cid = lb.getFocus().child_id orelse return null; 554 return lb.children.getIndex(cid); 555 } 556 557 // the entry the selected row points at, or null for the ".." row (which 558 // isn't an entry). directory rows return their entry too, though they carry 559 // no contents. 560 fn selectedEntry(self: *View) ?Entry { 561 const idx = self.selectedRowIndex() orelse return null; 562 const base: usize = if (self.data.dir.len != 0) 1 else 0; // skip the ".." row 563 if (idx < base) return null; 564 const e = idx - base; 565 if (e >= self.data.entries.len) return null; 566 return self.data.entries[e]; 567 } 568 569 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 570 self.clearGrid(); 571 572 // swap the detail pane to the selected entry when the selection changes. 573 try self.refreshDetail(allocator); 574 575 // the selected list row shows a border (the focused TextBox upgrades it 576 // to a double border itself); the rest stay borderless. 577 const lb = self.listBox(); 578 for (lb.children.keys(), lb.children.values()) |id, *child| { 579 switch (child.widget) { 580 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 581 else => {}, 582 } 583 } 584 585 // the content scroll's border is single normally and double when the 586 // content is focused (the content box itself is borderless). 587 const has_content = self.detailInner().children.count() > 0; 588 const content_focused = if (root_focus.grandchild_id) |g| self.detailInner().children.contains(g) else false; 589 self.detailScrollFrame().options.border_style = if (!has_content) null else if (content_focused) .double else .single; 590 591 // cap the list at list_max_width only while the detail pane fits beside 592 // it. the box drops the detail pane when the width can't hold both 593 // minimums, so when it's that narrow we lift the cap and let the list 594 // fill the whole width. 595 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 596 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 597 598 // stretch the detail pane and its bordered scroll frame across the rest 599 // of the width so the pane fills the area rather than shrinking to its 600 // content, like the refs view's columns. the content scroll inside keeps 601 // its natural width: forcing it to the full inner width would make it one 602 // column too wide whenever the vertical scrollbar appears, leaving a 603 // phantom horizontal scroll. (height fills via the scroll clipping to the 604 // viewport.) 605 if (constraint.max_size.width) |w| { 606 const detail_w = if (both_panes_fit) w - list_max_width else w; 607 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_w, .height = null }; 608 self.detailOuter().children.values()[detail_scroll_index].min_size = .{ .width = detail_w, .height = null }; 609 } else { 610 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 611 self.detailOuter().children.values()[detail_scroll_index].min_size = null; 612 } 613 614 // crossing panes only re-selects the content box's child (focusList / 615 // focusDetail); when the window is too narrow to show both, the pane that 616 // was holding focus is dropped here. the framework recovers focus after 617 // the top-level build by re-deriving it down the selected-child chain, so 618 // there's nothing to fix up afterward. 619 try self.box.build(allocator, constraint, root_focus); 620 621 // clamp the list's initial offset (a selected row near the end of the 622 // list) against this build's grids, for the next frame. initRoot's 623 // pre-sizing build is unbounded — its full-content "viewport" would 624 // clamp the offset to zero, so only a bounded build counts. 625 if (constraint.max_size.height != null) self.listScroll().clampToContent(); 626 } 627 628 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 629 const cur = self.selectedRowIndex(); 630 if (std.meta.eql(cur, self.shown_index)) return; 631 try self.populateDetail(allocator); 632 self.shown_index = cur; 633 } 634 635 fn populateDetail(self: *View, allocator: std.mem.Allocator) !void { 636 const nav_box = self.navBox(); 637 const inner = self.detailInner(); 638 639 for (nav_box.children.values()) |*child| child.widget.deinit(allocator); 640 nav_box.children.clearAndFree(allocator); 641 nav_box.getFocus().child_id = null; 642 for (inner.children.values()) |*child| child.widget.deinit(allocator); 643 inner.children.clearAndFree(allocator); 644 inner.getFocus().child_id = null; 645 646 // only files have contents to show; directories and the ".." row leave 647 // the pane empty. 648 if (self.selectedEntry()) |entry| { 649 if (!entry.is_dir) { 650 // the window links sit above the scroll, so they stay put 651 // while the content scrolls. 652 if (entry.window_start > 0) try self.addNavLink(allocator, nav_box, "scroll to top", entry, 0); 653 if (entry.has_more) try self.addNavLink(allocator, nav_box, "next lines →", entry, lineNumber(entry.window_start + file_page)); 654 if (!entry.loaded) { 655 // the placeholder carries the file's "a:" link, so enter 656 // (or a click) here loads the file like on its list row. 657 const path = try childDir(self.session.page_arena.allocator(), self.data.dir, entry.name); 658 const link = try fileLink(self.session.page_arena, self.data, path, false, 0); 659 try self.addContentBox(allocator, inner, "(press enter to load this file)", link); 660 } else if (entry.is_binary) { 661 try self.addContentBox(allocator, inner, "(binary file)", ""); 662 } else { 663 const text = try numberedContent(self.session.page_arena.allocator(), entry.lines, entry.window_start); 664 try self.addContentBox(allocator, inner, text, ""); 665 } 666 } 667 } 668 // point each box at its first child so focus can descend into it. 669 if (nav_box.children.count() > 0) nav_box.getFocus().child_id = nav_box.children.keys()[0]; 670 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 671 672 // reserve the nav row's height (the shrink scroll frame yields it). 673 self.detailOuter().children.values()[detail_nav_index].min_size = 674 if (nav_box.children.count() > 0) .{ .width = null, .height = row_height } else null; 675 676 // reset the scroll to the top for the newly-shown file: directly on the 677 // terminal (the wasm offset), and via a version bump on the web (so the 678 // renderer's scroll id changes and JS drops the preserved position). 679 const sc = self.detailScroll(); 680 sc.x = 0; 681 sc.y = 0; 682 sc.getFocus().version +%= 1; 683 } 684 685 // a focusable "next" link above the content. it's an `a:` link to the 686 // selected file at `target_start`, so activating it (the host follows the 687 // link) reloads the page on the next window. 688 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_start: usize) !void { 689 const page_arena = self.session.page_arena; 690 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 691 const route = self.data.filesRoute(path, target_start) orelse return error.RouteTooLong; 692 const link = try std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 693 var tb = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 694 errdefer tb.deinit(allocator); 695 tb.getFocus().mode = .all; 696 tb.getFocus().kind = .{ .custom = link }; 697 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 698 } 699 700 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 701 if (self.headerActive()) { 702 if (key == .arrow_down) { 703 root_focus.setFocus(self.contentBox().getFocus().id); 704 return; 705 } 706 if (key == .enter) if (try self.header().submittedText(allocator)) |term| { 707 defer allocator.free(term); 708 return self.submit(term); 709 }; 710 return self.header().input(allocator, key, root_focus); 711 } 712 if (key == .arrow_up and self.contentAtTop() and self.header().focusHeader(root_focus)) return; 713 if (self.detailActive()) { 714 try self.detailInput(key, root_focus); 715 } else { 716 try self.listInput(key, root_focus); 717 } 718 } 719 720 // navigate to the typed term's results, pinned to the listing's ref. an 721 // empty term leaves the results for the plain listing. 722 fn submit(self: *View, term: []const u8) !void { 723 if (term.len == 0 and self.data.find == null) return; 724 const route = self.data.filesRoute("", 0) orelse return; 725 try self.session.navigate(route.withFind(term) orelse return); 726 } 727 728 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 729 // up/down (and the scroll wheel) move the selection; page up/down jump a 730 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 731 // directory (or "..") row are turned into navigation by the host 732 // (crossPageLink) before reaching here. 733 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 734 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 735 return; 736 } 737 switch (key) { 738 .enter, .arrow_right => try self.focusDetail(root_focus), 739 else => {}, 740 } 741 } 742 743 // whether focus is on the content box (vs. a "previous"/"next" nav link). 744 fn focusOnContent(self: *View, root_focus: *Focus) bool { 745 const g = root_focus.grandchild_id orelse return false; 746 return self.detailInner().children.contains(g); 747 } 748 749 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 750 const sc = self.detailScroll(); 751 const on_content = self.focusOnContent(root_focus); 752 // the "next" link sits above the content scroll. on the link, left returns 753 // to the file list and down drops into the content. in the content, the 754 // terminal scrolls by offset and reaching the top then pressing up crosses 755 // back up to the link; on the web vertical scrolling is the browser's job 756 // so up just crosses to the link. 757 switch (key) { 758 .arrow_left => { 759 if (on_content) { 760 if (sc.x > 0) { 761 sc.x -= 1; 762 self.detailScroll().clampToContent(); 763 } else try self.focusList(root_focus); 764 } else if (!self.moveNav(root_focus, -1)) { 765 try self.focusList(root_focus); 766 } 767 }, 768 .arrow_right => { 769 if (on_content) { 770 sc.x += 1; 771 self.detailScroll().clampToContent(); 772 } else _ = self.moveNav(root_focus, 1); 773 }, 774 .arrow_up => { 775 if (on_content) { 776 if (self.session.is_terminal and sc.y > 0) { 777 sc.y -= 1; 778 self.detailScroll().clampToContent(); 779 } else self.focusNav(root_focus); // cross up to the links 780 } 781 }, 782 .arrow_down => { 783 if (on_content) { 784 if (self.session.is_terminal) { 785 sc.y += 1; 786 self.detailScroll().clampToContent(); 787 } 788 } else self.focusContent(root_focus); // links -> content 789 }, 790 .page_up => if (on_content and self.session.is_terminal) { 791 sc.y -= 10; 792 self.detailScroll().clampToContent(); 793 }, 794 .page_down => if (on_content and self.session.is_terminal) { 795 sc.y += 10; 796 self.detailScroll().clampToContent(); 797 }, 798 .home => if (on_content and self.session.is_terminal) { 799 sc.y = 0; 800 self.detailScroll().clampToContent(); 801 }, 802 .end => if (on_content and self.session.is_terminal) { 803 sc.y = std.math.maxInt(isize); 804 self.detailScroll().clampToContent(); 805 }, 806 .mouse => |mouse| switch (mouse.action) { 807 .scroll => |dir| if (on_content and self.session.is_terminal) { 808 sc.y += if (dir == .up) -5 else 5; 809 self.detailScroll().clampToContent(); 810 }, 811 else => {}, 812 }, 813 else => {}, 814 } 815 } 816 817 // focus the first (left-most) nav link; a no-op when there are none, leaving 818 // focus on the content. 819 fn focusNav(self: *View, root_focus: *Focus) void { 820 const nav_box = self.navBox(); 821 if (nav_box.children.count() == 0) return; 822 root_focus.setFocus(nav_box.children.keys()[0]); 823 } 824 825 fn focusContent(self: *View, root_focus: *Focus) void { 826 const inner = self.detailInner(); 827 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 828 root_focus.setFocus(id); 829 } 830 831 // move focus `delta` rows within the nav box; returns false (without moving) 832 // when that would step off either end. 833 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 834 const nav_box = self.navBox(); 835 const keys = nav_box.children.keys(); 836 const cur_id = nav_box.getFocus().child_id orelse return false; 837 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 838 const target = cur + delta; 839 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 840 root_focus.setFocus(keys[@intCast(target)]); 841 return true; 842 } 843 844 // enter the detail pane, landing on the "next" link when there is one (so 845 // right-arrow reaches it), else on the content. a directory or ".." row has 846 // neither, so there's nothing to enter. selecting the frame's child (then 847 // focusing the frame) also handles the too-narrow case where the pane isn't 848 // laid out yet — focus lands inside it after the next build. 849 fn focusDetail(self: *View, root_focus: *Focus) !void { 850 const frame = self.detailOuter(); 851 if (self.navBox().children.count() > 0) { 852 frame.getFocus().child_id = self.navBox().getFocus().id; 853 } else if (self.detailInner().children.count() > 0) { 854 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 855 } else return; 856 root_focus.setFocus(frame.getFocus().id); 857 } 858 859 // return to the list. 860 fn focusList(self: *View, root_focus: *Focus) !void { 861 root_focus.setFocus(self.listScroll().getFocus().id); 862 } 863 864 pub fn clearGrid(self: *View) void { 865 self.box.clearGrid(); 866 } 867 868 pub fn getGrid(self: View) ?Grid { 869 return self.box.getGrid(); 870 } 871 872 pub fn getFocus(self: *View) *Focus { 873 return self.box.getFocus(); 874 } 875 876 fn contentAtTop(self: *View) bool { 877 if (self.detailActive()) { 878 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 879 return self.navBox().children.count() == 0 and 880 (!self.session.is_terminal or self.detailScroll().y == 0); 881 } 882 const lb = self.listBox(); 883 const cid = lb.getFocus().child_id orelse return true; 884 return lb.children.getIndex(cid) == 0; 885 } 886 887 // only the header's own row sits directly below the repository header. 888 pub fn atTop(self: *View) bool { 889 return self.headerActive() or (!self.header().hasFocusable() and self.contentAtTop()); 890 } 891 892 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 893 return self.header().focusHeader(root_focus); 894 } 895 }; 896 897 // join `lines` into one string, each prefixed with its 1-based file line number 898 // (right-aligned in a column wide enough for the last number, then a space), 899 // like the diffs view. `start` is the window's 0-based first line, so numbers 900 // continue across paginated windows. 901 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 902 if (lines.len == 0) return ""; 903 const width = std.fmt.count("{d}", .{start + lines.len}); 904 const indent = try arena.alloc(u8, width); 905 @memset(indent, ' '); 906 907 var out: std.ArrayList(u8) = .empty; 908 for (lines, 0..) |line, i| { 909 if (i != 0) try out.append(arena, '\n'); 910 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 911 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 912 try out.appendSlice(arena, num_str); 913 try out.append(arena, ' '); 914 try out.appendSlice(arena, line); 915 } 916 return out.toOwnedSlice(arena); 917 } 918 919 // the index of the "README"/"README.md" file entry (case-insensitive), or null 920 // if none. used to pick the default selection at the repo root. 921 fn readmeIndex(entries: []const Entry) ?usize { 922 for (entries, 0..) |entry, i| { 923 if (!entry.is_dir and isReadme(entry.name)) return i; 924 } 925 return null; 926 } 927 928 fn isReadme(name: []const u8) bool { 929 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 930 } 931 932 // the index of the entry the route named (data.selected_file), or null if none. 933 fn selectedFileIndex(data: *const Self) ?usize { 934 const name = data.selected_file orelse return null; 935 for (data.entries, 0..) |entry, i| { 936 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 937 } 938 return null; 939 } 940 941 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 942 // following it navigates to that directory's listing. 943 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 944 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 945 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 946 } 947 948 // a link to the file's route. a preloaded file gets an "ai:" link 949 // (crossPageLink ignores it, so a click selects the row in place and shows its 950 // contents); an unloaded file gets an "a:" link, so activating it reloads the 951 // page with that file selected and its content read. 952 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 953 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 954 const prefix = if (loaded) "ai:" else "a:"; 955 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 956 } 957 958 // the url `line` value for a content window: its first line's 1-based number, 959 // except the first window, which serializes as unset (0). 960 fn lineNumber(window_start: usize) usize { 961 return if (window_start == 0) 0 else window_start + 1; 962 } 963 964 // `dir`/`name`, or just `name` at the root. 965 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 966 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 967 } 968 969 // `dir` with its last segment removed ("" when `dir` has a single segment). 970 fn parentDir(dir: []const u8) []const u8 { 971 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 972 return dir[0..slash]; 973 } 974 975 // a search result's bottom label: the directory it sits in, empty at the root. 976 // a directory too wide for the list column is clipped from the left. 977 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 978 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 979 const dir = path[0..slash]; 980 // the row's border and the label's padding take two columns each 981 const max_width = View.list_max_width - 4; 982 var width = try xitui.width.displayWidth(dir); 983 if (width <= max_width) return std.fmt.allocPrint(aa, " {s} ", .{dir}); 984 985 const ellipsis = ".."; 986 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 987 var start: usize = 0; 988 while (width > max_width - ellipsis.len) { 989 const slice = iter.nextCodepointSlice() orelse break; 990 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 991 start += slice.len; 992 } 993 return std.fmt.allocPrint(aa, " " ++ ellipsis ++ "{s} ", .{dir[start..]}); 994 } 995