╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 = 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 = null, .direction = .horiz }); 380 errdefer box.deinit(allocator); 381 382 var shows_file = false; 383 384 // the directory listing on the left (one focusable row each). 385 { 386 var list_scroll = blk: { 387 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 388 errdefer list_box.deinit(allocator); 389 390 // link kinds are borrowed by the rows, so they live in the page 391 // arena (as long as this page's widget tree). row labels are 392 // copied by their text boxes. 393 const aa = session.page_arena.allocator(); 394 if (data.dir.len != 0) { 395 try addRow(allocator, &list_box, "..", "", try dirLink(session.page_arena, data, parentDir(data.dir))); 396 } 397 for (data.entries) |entry| { 398 const path = try childDir(aa, data.dir, entry.name); 399 // a search result shows its file name, with its directory 400 // in the bottom label. 401 const label = if (data.find != null) 402 std.Io.Dir.path.basenamePosix(entry.name) 403 else if (entry.is_dir) 404 try aa.print("{s}/", .{entry.name}) 405 else 406 entry.name; 407 const bottom_label = if (data.find != null) try resultDirLabel(aa, entry.name) else ""; 408 const link = if (entry.is_dir) 409 try dirLink(session.page_arena, data, path) 410 else 411 try fileLink(session.page_arena, data, path, entry.loaded, lineNumber(entry.window_start)); 412 try addRow(allocator, &list_box, label, bottom_label, link); 413 } 414 if (data.capped) try addRow(allocator, &list_box, "(list truncated)", "", ""); 415 // select the file the route named, else prefer a README so its 416 // contents greet the visitor in the detail pane, else the first 417 // row. a non-root directory has a leading ".." row, so the entry 418 // index is offset by one there. search results have no README 419 // default: the term decides what matters. 420 var sel: usize = 0; 421 if (list_box.children.count() > 0) { 422 const base: usize = if (data.dir.len != 0) 1 else 0; 423 const entry_idx = selectedFileIndex(data) orelse if (data.find == null) readmeIndex(data.entries) else null; 424 sel = if (entry_idx) |i| i + base else 0; 425 list_box.getFocus().child_id = list_box.children.keys()[sel]; 426 shows_file = sel >= base and sel - base < data.entries.len and !data.entries[sel - base].is_dir; 427 } 428 var sc = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 429 // start scrolled so the selected row begins at the top of the 430 // viewport. rows are a fixed height, so no layout pass is 431 // needed; a too-far offset near the end of the list is clamped 432 // in build (the browser clamps its own scrolling natively). 433 sc.y = @intCast(sel * row_height); 434 break :blk sc; 435 }; 436 errdefer list_scroll.deinit(allocator); 437 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 } }); 438 } 439 440 // the detail pane on the right — a "next" nav box above a scroll of the 441 // selected file's content window. both are repopulated per selection by 442 // populateDetail. 443 { 444 var detail_outer = blk: { 445 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 446 errdefer frame.deinit(allocator); 447 448 { 449 var nav_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 450 errdefer nav_box.deinit(allocator); 451 try frame.children.put(allocator, nav_box.getFocus().id, .{ .widget = .{ .box = nav_box }, .rect = null, .min_size = null }); 452 } 453 { 454 // the content scroll wrapped in a bordered box; build sets the 455 // border single normally and double when the content is focused 456 // (the content box itself is borderless). 457 var scroll_frame = blk2: { 458 var detail_scroll = blk3: { 459 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 460 errdefer detail_inner.deinit(allocator); 461 break :blk3 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }); 462 }; 463 errdefer detail_scroll.deinit(allocator); 464 var sf = try wgt.Box(ui.Widget).init(allocator, .{ .border = .single, .round_corners = true, .direction = .vert }); 465 errdefer sf.deinit(allocator); 466 // the wrapper's selected child is its scroll, so the focus 467 // chain reaches the content box (populateDetail points the 468 // scroll's inner box at it). 469 sf.getFocus().child_id = detail_scroll.getFocus().id; 470 try sf.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 471 break :blk2 sf; 472 }; 473 errdefer scroll_frame.deinit(allocator); 474 // entering the detail pane lands on the content scroll (focus 475 // recovery descends the frame's selected child). the nav links 476 // above are reached by scrolling to the top, then up. 477 frame.getFocus().child_id = scroll_frame.getFocus().id; 478 // shrink, so the fill scroll yields the nav box's row 479 // instead of consuming the frame's whole height. 480 try frame.children.put(allocator, scroll_frame.getFocus().id, .{ .widget = .{ .box = scroll_frame }, .rect = null, .min_size = null, .flex = .shrink }); 481 } 482 break :blk frame; 483 }; 484 errdefer detail_outer.deinit(allocator); 485 detail_outer.getFocus().mode = .mouse; 486 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 487 } 488 489 box.getFocus().child_id = box.children.keys()[if (shows_file) detail_index else list_index]; 490 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 491 492 // focus lives in the split, except that search results start in the 493 // search box so the term can be refined right away. 494 outer.getFocus().child_id = outer.children.keys()[if (data.find != null) header_index else outer.children.count() - 1]; 495 return .{ .box = outer, .data = data, .session = session, .shown_index = null }; 496 } 497 498 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, bottom_label: []const u8, link: []const u8) !void { 499 var row = try wgt.TextBox.init(allocator, label, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = bottom_label } }); 500 errdefer row.deinit(allocator); 501 row.getFocus().mode = .all; 502 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 503 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 504 } 505 506 // the selected file's contents as a single focusable multi-line text box. 507 // it's borderless — the border lives on the surrounding scroll frame, which 508 // doubles when this content is focused. a non-empty `link` makes activating 509 // the box follow it, like addRow. 510 fn addContentBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), spans: []const wgt.Span, link: []const u8) !void { 511 _ = self; 512 var tb = try wgt.TextBox.initSpans(allocator, spans, .{ .border = null, .wrap_kind = .none }); 513 errdefer tb.deinit(allocator); 514 tb.getFocus().mode = .all; 515 if (link.len != 0) tb.getFocus().kind = .{ .custom = link }; 516 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 517 } 518 519 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 520 self.box.deinit(allocator); 521 } 522 523 // the split is last, after the sub-header where there is one 524 fn contentIndex(self: *View) usize { 525 return self.box.children.count() - 1; 526 } 527 528 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 529 return &self.box.children.values()[self.contentIndex()].widget.box; 530 } 531 532 fn header(self: *View) ?*ui.widget.SearchHeader { 533 switch (self.box.children.values()[header_index].widget) { 534 .search_header => |*header_view| return header_view, 535 else => return null, 536 } 537 } 538 539 fn headerActive(self: *View) bool { 540 const header_view = self.header() orelse return false; 541 return self.box.getFocus().child_id == header_view.getFocus().id; 542 } 543 544 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 545 return &self.contentBox().children.values()[list_index].widget.scroll; 546 } 547 548 fn listBox(self: *View) *wgt.Box(ui.Widget) { 549 return &self.listScroll().child.box; 550 } 551 552 fn detailOuter(self: *View) *wgt.Box(ui.Widget) { 553 return &self.contentBox().children.values()[detail_index].widget.box; 554 } 555 556 fn navBox(self: *View) *wgt.Box(ui.Widget) { 557 return &self.detailOuter().children.values()[detail_nav_index].widget.box; 558 } 559 560 // the bordered box wrapping the content scroll (border toggles with focus). 561 fn detailScrollFrame(self: *View) *wgt.Box(ui.Widget) { 562 return &self.detailOuter().children.values()[detail_scroll_index].widget.box; 563 } 564 565 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 566 return &self.detailScrollFrame().children.values()[0].widget.scroll; 567 } 568 569 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 570 return &self.detailScroll().child.box; 571 } 572 573 fn detailActive(self: *View) bool { 574 const content = self.contentBox(); 575 const cid = content.getFocus().child_id orelse return false; 576 return content.children.getIndex(cid) == detail_index; 577 } 578 579 // the selected list row's index, or null when nothing is focused. 580 fn selectedRowIndex(self: *View) ?usize { 581 const lb = self.listBox(); 582 const cid = lb.getFocus().child_id orelse return null; 583 return lb.children.getIndex(cid); 584 } 585 586 // the entry the selected row points at, or null for the ".." row (which 587 // isn't an entry). directory rows return their entry too, though they carry 588 // no contents. 589 fn selectedEntry(self: *View) ?Entry { 590 const idx = self.selectedRowIndex() orelse return null; 591 const base: usize = if (self.data.dir.len != 0) 1 else 0; // skip the ".." row 592 if (idx < base) return null; 593 const e = idx - base; 594 if (e >= self.data.entries.len) return null; 595 return self.data.entries[e]; 596 } 597 598 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 599 self.clearGrid(); 600 if (self.data.no_commits) return self.box.build(allocator, constraint, root_focus); 601 602 // swap the detail pane to the selected entry when the selection changes. 603 try self.refreshDetail(allocator); 604 605 // the selected list row shows a border (the focused TextBox upgrades it 606 // to a double border itself); the rest stay borderless. 607 const lb = self.listBox(); 608 for (lb.children.keys(), lb.children.values()) |id, *child| { 609 switch (child.widget) { 610 .text_box => |*tb| ui.widget.markSelected(tb, lb.getFocus().child_id == id), 611 else => {}, 612 } 613 } 614 615 // the content scroll's border is single normally and double when the 616 // content is focused (the content box itself is borderless). 617 const has_content = self.detailInner().children.count() > 0; 618 self.detailScrollFrame().options.border = if (!has_content) null else if (self.focusOnContent(root_focus)) .double else .single; 619 620 // cap the list at list_max_width only while the detail pane fits beside 621 // it. the box drops the detail pane when the width can't hold both 622 // minimums, so when it's that narrow we lift the cap and let the list 623 // fill the whole width. 624 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 625 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 626 627 // stretch the detail pane and its bordered scroll frame across the rest 628 // of the width so the pane fills the area rather than shrinking to its 629 // content, like the refs view's columns. the content scroll inside keeps 630 // its natural width: forcing it to the full inner width would make it one 631 // column too wide whenever the vertical scrollbar appears, leaving a 632 // phantom horizontal scroll. (height fills via the scroll clipping to the 633 // viewport.) 634 if (constraint.max_size.width) |w| { 635 const detail_w = if (both_panes_fit) w - list_max_width else w; 636 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_w, .height = null }; 637 self.detailOuter().children.values()[detail_scroll_index].min_size = .{ .width = detail_w, .height = null }; 638 } else { 639 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 640 self.detailOuter().children.values()[detail_scroll_index].min_size = null; 641 } 642 643 // crossing panes only re-selects the content box's child (focusList / 644 // focusDetail); when the window is too narrow to show both, the pane that 645 // was holding focus is dropped here. the framework recovers focus after 646 // the top-level build by re-deriving it down the selected-child chain, so 647 // there's nothing to fix up afterward. 648 try self.box.build(allocator, constraint, root_focus); 649 650 // clamp the list's initial offset (a selected row near the end of the 651 // list) against this build's grids, for the next frame. initRoot's 652 // pre-sizing build is unbounded — its full-content "viewport" would 653 // clamp the offset to zero, so only a bounded build counts. 654 if (constraint.max_size.height != null) self.listScroll().clampToContent(); 655 } 656 657 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 658 const cur = self.selectedRowIndex(); 659 if (std.meta.eql(cur, self.shown_index)) return; 660 try self.populateDetail(allocator); 661 self.shown_index = cur; 662 } 663 664 fn populateDetail(self: *View, allocator: std.mem.Allocator) !void { 665 const nav_box = self.navBox(); 666 const inner = self.detailInner(); 667 668 for (nav_box.children.values()) |*child| child.widget.deinit(allocator); 669 nav_box.children.clearAndFree(allocator); 670 nav_box.getFocus().child_id = null; 671 for (inner.children.values()) |*child| child.widget.deinit(allocator); 672 inner.children.clearAndFree(allocator); 673 inner.getFocus().child_id = null; 674 675 // only files have contents to show; directories and the ".." row leave 676 // the pane empty. 677 var markdown = false; 678 if (self.selectedEntry()) |entry| { 679 if (!entry.is_dir) { 680 const path = try childDir(self.session.page_arena.allocator(), self.data.dir, entry.name); 681 // the window links sit above the scroll, so they stay put 682 // while the content scrolls. 683 if (entry.window_start > 0) try self.addNavLink(allocator, nav_box, "scroll to top", entry, 0); 684 if (entry.has_more) try self.addNavLink(allocator, nav_box, "next lines →", entry, lineNumber(entry.window_start + file_page)); 685 if (!entry.loaded) { 686 // the placeholder carries the file's "a:" link, so enter 687 // (or a click) here loads the file like on its list row. 688 const link = try fileLink(self.session.page_arena, self.data, path, false, 0); 689 try self.addContentBox(allocator, inner, &.{.{ .text = "(press enter to load this file)" }}, link); 690 } else if (entry.is_binary) { 691 try self.addContentBox(allocator, inner, &.{.{ .text = "(binary file)" }}, ""); 692 } else if (isMarkdown(entry.name) and entry.window_start == 0 and !entry.has_more and entry.lines.len <= max_markdown_lines) { 693 const page_arena = self.session.page_arena; 694 const doc = try md.parse(page_arena.allocator(), entry.lines); 695 var view = try Markdown.View.init(allocator, doc, self.data, path, page_arena); 696 errdefer view.deinit(allocator); 697 try inner.children.put(allocator, view.getFocus().id, .{ .widget = .{ .markdown = view }, .rect = null, .min_size = null }); 698 markdown = true; 699 } else { 700 try self.addContentBox(allocator, inner, try numberedContent(self.session.page_arena, self.data, path, entry.lines, entry.window_start), ""); 701 } 702 } 703 } 704 // point each box at its first child so focus can descend into it. 705 if (nav_box.children.count() > 0) nav_box.getFocus().child_id = nav_box.children.keys()[0]; 706 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 707 708 // reserve the nav row's height (the shrink scroll frame yields it). 709 self.detailOuter().children.values()[detail_nav_index].min_size = 710 if (nav_box.children.count() > 0) .{ .width = null, .height = row_height } else null; 711 712 // rendered markdown wraps to the pane's width, so it only scrolls down 713 const sc = self.detailScroll(); 714 sc.options.direction = if (markdown) .vert else .both; 715 716 // reset the scroll to the top for the newly-shown file: directly on the 717 // terminal (the wasm offset), and via a version bump on the web (so the 718 // renderer's scroll id changes and JS drops the preserved position). 719 sc.x = 0; 720 sc.y = 0; 721 sc.getFocus().version +%= 1; 722 } 723 724 // a focusable "next" link above the content. it's an `a:` link to the 725 // selected file at `target_start`, so activating it (the host follows the 726 // link) reloads the page on the next window. 727 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_start: usize) !void { 728 const page_arena = self.session.page_arena; 729 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 730 const route = self.data.filesRoute(path, target_start) orelse return error.RouteTooLong; 731 const link = try page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 732 var tb = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 733 errdefer tb.deinit(allocator); 734 tb.getFocus().mode = .all; 735 tb.getFocus().kind = .{ .custom = link }; 736 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 737 } 738 739 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 740 if (self.data.no_commits) return self.box.input(allocator, key, root_focus); 741 // scrolling crosses the sub header boundary the same way arrows do 742 const direction = inp.vertDirection(key); 743 if (self.headerActive()) { 744 // active means there is one 745 const header_view = self.header() orelse unreachable; 746 if (direction == .down) { 747 root_focus.setFocus(self.contentBox().getFocus().id); 748 return; 749 } 750 if (key == .enter) if (try header_view.submittedText(allocator)) |term| { 751 defer allocator.free(term); 752 return self.submit(term); 753 }; 754 return header_view.input(allocator, key, root_focus); 755 } 756 if (direction == .up and self.contentAtTop() and self.focusHeader(root_focus)) return; 757 if (self.detailActive()) { 758 try self.detailInput(allocator, key, root_focus); 759 } else { 760 try self.listInput(key, root_focus); 761 } 762 } 763 764 // navigate to the typed term's results, pinned to the listing's ref. an 765 // empty term leaves the results for the plain listing. 766 fn submit(self: *View, term: []const u8) !void { 767 if (term.len == 0 and self.data.find == null) return; 768 const route = self.data.filesRoute("", 0) orelse return; 769 try self.session.navigate(route.withFind(term) orelse return); 770 } 771 772 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 773 // up/down (and the scroll wheel) move the selection; page up/down jump a 774 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 775 // directory (or "..") row are turned into navigation by the host 776 // (crossPageLink) before reaching here. 777 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 778 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 779 return; 780 } 781 switch (key) { 782 .enter, .arrow_right => try self.focusDetail(root_focus), 783 else => {}, 784 } 785 } 786 787 // whether focus is on the content box (vs. a "previous"/"next" nav link). 788 fn focusOnContent(self: *View, root_focus: *Focus) bool { 789 const g = root_focus.grandchild_id orelse return false; 790 if (self.markdownView()) |view| return view.owns(g); 791 return self.detailInner().children.contains(g); 792 } 793 794 // the rendered markdown in the detail pane, if that's what it shows 795 fn markdownView(self: *View) ?*Markdown.View { 796 const inner = self.detailInner(); 797 if (inner.children.count() == 0) return null; 798 return switch (inner.children.values()[0].widget) { 799 .markdown => |*view| view, 800 else => null, 801 }; 802 } 803 804 fn detailInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 805 const sc = self.detailScroll(); 806 const on_content = self.focusOnContent(root_focus); 807 // the "next" link sits above the content scroll. on the link, left returns 808 // to the file list and down drops into the content. in the content, the 809 // terminal scrolls by offset and reaching the top then pressing up crosses 810 // back up to the link; on the web vertical scrolling is the browser's job 811 // so up just crosses to the link. 812 switch (key) { 813 .arrow_left => { 814 if (on_content) { 815 if (sc.x > 0) { 816 sc.x -= 1; 817 self.detailScroll().clampToContent(); 818 } else try self.focusList(root_focus); 819 } else if (!self.moveNav(root_focus, -1)) { 820 try self.focusList(root_focus); 821 } 822 }, 823 .arrow_right => { 824 if (on_content) { 825 sc.x += 1; 826 self.detailScroll().clampToContent(); 827 } else _ = self.moveNav(root_focus, 1); 828 }, 829 .arrow_up => { 830 if (on_content) { 831 if (self.session.is_terminal and sc.y > 0) { 832 sc.y -= 1; 833 self.detailScroll().clampToContent(); 834 } else self.focusNav(root_focus); // cross up to the links 835 } 836 }, 837 .arrow_down => { 838 if (on_content) { 839 if (self.session.is_terminal) { 840 sc.y += 1; 841 self.detailScroll().clampToContent(); 842 } 843 } else self.focusContent(root_focus); // links -> content 844 }, 845 .page_up => if (on_content and self.session.is_terminal) { 846 sc.y -= 10; 847 self.detailScroll().clampToContent(); 848 }, 849 .page_down => if (on_content and self.session.is_terminal) { 850 sc.y += 10; 851 self.detailScroll().clampToContent(); 852 }, 853 .home => if (on_content and self.session.is_terminal) { 854 sc.y = 0; 855 self.detailScroll().clampToContent(); 856 }, 857 .end => if (on_content and self.session.is_terminal) { 858 sc.y = std.math.maxInt(isize); 859 self.detailScroll().clampToContent(); 860 }, 861 .mouse => |mouse| switch (mouse.action) { 862 .scroll => |dir| if (on_content and self.session.is_terminal) { 863 sc.y += if (dir == .up) -5 else 5; 864 self.detailScroll().clampToContent(); 865 }, 866 else => {}, 867 }, 868 // step between the rendered markdown's links; the web leaves tab to the browser 869 .tab, .back_tab => if (on_content and self.session.is_terminal) if (self.markdownView()) |view| { 870 try view.stepLink(allocator, root_focus, sc, 0, 0, key == .tab); 871 }, 872 else => {}, 873 } 874 } 875 876 // focus the first (left-most) nav link; a no-op when there are none, leaving 877 // focus on the content. 878 fn focusNav(self: *View, root_focus: *Focus) void { 879 const nav_box = self.navBox(); 880 if (nav_box.children.count() == 0) return; 881 root_focus.setFocus(nav_box.children.keys()[0]); 882 } 883 884 fn focusContent(self: *View, root_focus: *Focus) void { 885 if (self.markdownView()) |view| return root_focus.setFocus(view.body.id); 886 const inner = self.detailInner(); 887 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 888 root_focus.setFocus(id); 889 } 890 891 // move focus `delta` rows within the nav box; returns false (without moving) 892 // when that would step off either end. 893 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 894 const nav_box = self.navBox(); 895 const keys = nav_box.children.keys(); 896 const cur_id = nav_box.getFocus().child_id orelse return false; 897 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 898 const target = cur + delta; 899 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 900 root_focus.setFocus(keys[@intCast(target)]); 901 return true; 902 } 903 904 // enter the detail pane, landing on the "next" link when there is one (so 905 // right-arrow reaches it), else on the content. a directory or ".." row has 906 // neither, so there's nothing to enter. selecting the frame's child (then 907 // focusing the frame) also handles the too-narrow case where the pane isn't 908 // laid out yet — focus lands inside it after the next build. 909 fn focusDetail(self: *View, root_focus: *Focus) !void { 910 const frame = self.detailOuter(); 911 if (self.navBox().children.count() > 0) { 912 frame.getFocus().child_id = self.navBox().getFocus().id; 913 } else if (self.detailInner().children.count() > 0) { 914 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 915 } else return; 916 root_focus.setFocus(frame.getFocus().id); 917 } 918 919 // return to the list. 920 fn focusList(self: *View, root_focus: *Focus) !void { 921 root_focus.setFocus(self.listScroll().getFocus().id); 922 } 923 924 pub fn clearGrid(self: *View) void { 925 self.box.clearGrid(); 926 } 927 928 pub fn getGrid(self: View) ?Grid { 929 return self.box.getGrid(); 930 } 931 932 pub fn getFocus(self: *View) *Focus { 933 return self.box.getFocus(); 934 } 935 936 fn contentAtTop(self: *View) bool { 937 if (self.detailActive()) { 938 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 939 return self.navBox().children.count() == 0 and 940 (!self.session.is_terminal or self.detailScroll().y == 0); 941 } 942 const lb = self.listBox(); 943 const cid = lb.getFocus().child_id orelse return true; 944 return lb.children.getIndex(cid) == 0; 945 } 946 947 // only the header's own row sits directly below the repository header. 948 pub fn atTop(self: *View) bool { 949 if (self.data.no_commits) return true; 950 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 951 return self.headerActive() or (!focusable and self.contentAtTop()); 952 } 953 954 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 955 const header_view = self.header() orelse return false; 956 return header_view.focusHeader(root_focus); 957 } 958 }; 959 960 // `lines` as spans, each prefixed with its 1-based file line number 961 // (right-aligned in a column wide enough for the last number, then a space), 962 // like the diffs view. each number links to the window starting at its line. 963 // `start` is the window's 0-based first line, so numbers continue across 964 // paginated windows. 965 fn numberedContent(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, lines: []const []const u8, start: usize) ![]const wgt.Span { 966 const arena = page_arena.allocator(); 967 const width = std.fmt.count("{d}", .{start + lines.len}); 968 var spans: std.ArrayList(wgt.Span) = .empty; 969 for (lines, 0..) |line, i| { 970 const number = start + i + 1; 971 const route = data.filesRoute(path, number) orelse return error.RouteTooLong; 972 try spans.append(arena, .{ .text = try arena.print("{d: >[1]}", .{ number, width }), .link = try route.toUrl(page_arena) }); 973 try spans.append(arena, .{ .text = try arena.print(" {s}{s}", .{ line, if (i + 1 < lines.len) "\n" else "" }) }); 974 } 975 return spans.toOwnedSlice(arena); 976 } 977 978 // the index of the "README"/"README.md" file entry (case-insensitive), or null 979 // if none. used to pick the default selection at the repo root. 980 fn readmeIndex(entries: []const Entry) ?usize { 981 for (entries, 0..) |entry, i| { 982 if (!entry.is_dir and isReadme(entry.name)) return i; 983 } 984 return null; 985 } 986 987 fn isMarkdown(name: []const u8) bool { 988 const extension = std.Io.Dir.path.extension(name); 989 return std.ascii.eqlIgnoreCase(extension, ".md") or std.ascii.eqlIgnoreCase(extension, ".markdown"); 990 } 991 992 fn isReadme(name: []const u8) bool { 993 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 994 } 995 996 // the index of the entry the route named (data.selected_file), or null if none. 997 fn selectedFileIndex(data: *const Self) ?usize { 998 const name = data.selected_file orelse return null; 999 for (data.entries, 0..) |entry, i| { 1000 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 1001 } 1002 return null; 1003 } 1004 1005 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 1006 // following it navigates to that directory's listing. 1007 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 1008 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 1009 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1010 } 1011 1012 // a link to the file's route. a preloaded file gets an "ai:" link 1013 // (crossPageLink ignores it, so a click selects the row in place and shows its 1014 // contents); an unloaded file gets an "a:" link, so activating it reloads the 1015 // page with that file selected and its content read. 1016 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 1017 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 1018 const prefix = if (loaded) "ai:" else "a:"; 1019 return page_arena.allocator().print("{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 1020 } 1021 1022 // the url `line` value for a content window: its first line's 1-based number, 1023 // except the first window, which serializes as unset (0). 1024 fn lineNumber(window_start: usize) usize { 1025 return if (window_start == 0) 0 else window_start + 1; 1026 } 1027 1028 // `dir`/`name`, or just `name` at the root. 1029 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 1030 return if (dir.len == 0) name else arena.print("{s}/{s}", .{ dir, name }); 1031 } 1032 1033 // `dir` with its last segment removed ("" when `dir` has a single segment). 1034 fn parentDir(dir: []const u8) []const u8 { 1035 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 1036 return dir[0..slash]; 1037 } 1038 1039 // a search result's bottom label: the directory it sits in, empty at the root. 1040 // a directory too wide for the list column is clipped from the left. 1041 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 1042 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 1043 const dir = path[0..slash]; 1044 // the row's border and the label's padding take two columns each 1045 const max_width = View.list_max_width - 4; 1046 var width = try xitui.width.displayWidth(dir); 1047 if (width <= max_width) return aa.print(" {s} ", .{dir}); 1048 1049 const ellipsis = ".."; 1050 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1051 var start: usize = 0; 1052 while (width > max_width - ellipsis.len) { 1053 const slice = iter.nextCodepointSlice() orelse break; 1054 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1055 start += slice.len; 1056 } 1057 return aa.print(" " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1058 } 1059