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