╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
367 outer.getFocus().child_id = outer.children.keys()[content_index]; 368 return .{ .box = outer, .data = data, .session = session, .shown_index = null }; 369 } 370 371 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 372 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 373 errdefer row.deinit(allocator); 374 row.getFocus().focusable = true; 375 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 376 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 377 } 378 379 // the selected file's contents as a single focusable multi-line text box. 380 // it's borderless — the border lives on the surrounding scroll frame, which 381 // doubles when this content is focused. `text` lives in the page arena. 382 fn addContentBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8) !void { 383 _ = self; 384 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = null, .wrap_kind = .none }); 385 errdefer tb.deinit(allocator); 386 tb.getFocus().focusable = true; 387 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 388 } 389 390 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 391 self.box.deinit(allocator); 392 } 393 394 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 395 return &self.box.children.values()[content_index].widget.box; 396 } 397 398 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 399 return &self.contentBox().children.values()[list_index].widget.scroll; 400 } 401 402 fn listBox(self: *View) *wgt.Box(ui.Widget) { 403 return &self.listScroll().child.box; 404 } 405 406 fn detailOuter(self: *View) *wgt.Box(ui.Widget) { 407 return &self.contentBox().children.values()[detail_index].widget.box; 408 } 409 410 fn navBox(self: *View) *wgt.Box(ui.Widget) { 411 return &self.detailOuter().children.values()[detail_nav_index].widget.box; 412 } 413 414 // the bordered box wrapping the content scroll (border toggles with focus). 415 fn detailScrollFrame(self: *View) *wgt.Box(ui.Widget) { 416 return &self.detailOuter().children.values()[detail_scroll_index].widget.box; 417 } 418 419 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 420 return &self.detailScrollFrame().children.values()[0].widget.scroll; 421 } 422 423 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 424 return &self.detailScroll().child.box; 425 } 426 427 fn detailActive(self: *View) bool { 428 const content = self.contentBox(); 429 const cid = content.getFocus().child_id orelse return false; 430 return content.children.getIndex(cid) == detail_index; 431 } 432 433 // the selected list row's index, or null when nothing is focused. 434 fn selectedRowIndex(self: *View) ?usize { 435 const lb = self.listBox(); 436 const cid = lb.getFocus().child_id orelse return null; 437 return lb.children.getIndex(cid); 438 } 439 440 // the entry the selected row points at, or null for the ".." row (which 441 // isn't an entry). directory rows return their entry too, though they carry 442 // no contents. 443 fn selectedEntry(self: *View) ?Entry { 444 const idx = self.selectedRowIndex() orelse return null; 445 const base: usize = if (self.data.dir.len != 0) 1 else 0; // skip the ".." row 446 if (idx < base) return null; 447 const e = idx - base; 448 if (e >= self.data.entries.len) return null; 449 return self.data.entries[e]; 450 } 451 452 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 453 self.clearGrid(); 454 455 // swap the detail pane to the selected entry when the selection changes. 456 try self.refreshDetail(allocator); 457 458 // mirror the selected file into the url so it updates as the selection 459 // moves, but only while focus is inside this view (so landing on the tab 460 // keeps the page's base route). a file selection carries the file path and 461 // its content window offset; a directory / ".." row (or no selection) 462 // stays at the directory with no offset. the path is joined in a stack 463 // buffer since build runs every frame on the web. 464 if (root_focus.grandchild_id) |g| { 465 if (self.box.getFocus().children.contains(g)) { 466 var buf: [ui.RoutablePage.repo_route_max_len]u8 = undefined; 467 var sel_path = self.data.dir; 468 var sel_after: usize = 0; 469 if (self.selectedEntry()) |entry| { 470 if (!entry.is_dir) { 471 sel_path = if (self.data.dir.len == 0) 472 entry.name 473 else 474 std.fmt.bufPrint(&buf, "{s}/{s}", .{ self.data.dir, entry.name }) catch self.data.dir; 475 sel_after = entry.window_start; 476 } 477 } 478 if (ui.RoutablePage.repoFilesRoute(self.data.identity, self.data.ref_or_oid, self.data.ref_or_oid_value, sel_path, sel_after)) |route| 479 self.session.data.current_page = route; 480 } 481 } 482 483 // the selected list row shows a border (the focused TextBox upgrades it 484 // to a double border itself); the rest stay borderless. 485 const lb = self.listBox(); 486 for (lb.children.keys(), lb.children.values()) |id, *child| { 487 switch (child.widget) { 488 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 489 else => {}, 490 } 491 } 492 493 // the content scroll's border is single normally and double when the 494 // content is focused (the content box itself is borderless). 495 const has_content = self.detailInner().children.count() > 0; 496 const content_focused = if (root_focus.grandchild_id) |g| self.detailInner().children.contains(g) else false; 497 self.detailScrollFrame().options.border_style = if (!has_content) null else if (content_focused) .double else .single; 498 499 // same for the "next" link above the content. 500 const nav = self.navBox(); 501 for (nav.children.keys(), nav.children.values()) |id, *child| { 502 switch (child.widget) { 503 .text_box => |*tb| tb.options.border_style = if (nav.getFocus().child_id == id) .single else .hidden, 504 else => {}, 505 } 506 } 507 508 // cap the list at list_max_width only while the detail pane fits beside 509 // it. the box drops the detail pane when the width can't hold both 510 // minimums, so when it's that narrow we lift the cap and let the list 511 // fill the whole width. 512 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 513 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 514 515 // stretch the detail pane and its bordered scroll frame across the rest 516 // of the width so the pane fills the area rather than shrinking to its 517 // content, like the refs view's columns. the content scroll inside keeps 518 // its natural width: forcing it to the full inner width would make it one 519 // column too wide whenever the vertical scrollbar appears, leaving a 520 // phantom horizontal scroll. (height fills via the scroll clipping to the 521 // viewport.) 522 if (constraint.max_size.width) |w| { 523 const detail_w = if (both_panes_fit) w - list_max_width else w; 524 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_w, .height = null }; 525 self.detailOuter().children.values()[detail_scroll_index].min_size = .{ .width = detail_w, .height = null }; 526 } else { 527 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 528 self.detailOuter().children.values()[detail_scroll_index].min_size = null; 529 } 530 531 // crossing panes only re-selects the content box's child (focusList / 532 // focusDetail); when the window is too narrow to show both, the pane that 533 // was holding focus is dropped here. the framework recovers focus after 534 // the top-level build by re-deriving it down the selected-child chain, so 535 // there's nothing to fix up afterward. 536 try self.box.build(allocator, constraint, root_focus); 537 } 538 539 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 540 const cur = self.selectedRowIndex(); 541 if (std.meta.eql(cur, self.shown_index)) return; 542 try self.populateDetail(allocator); 543 self.shown_index = cur; 544 } 545 546 fn populateDetail(self: *View, allocator: std.mem.Allocator) !void { 547 const nav = self.navBox(); 548 const inner = self.detailInner(); 549 550 for (nav.children.values()) |*child| child.widget.deinit(allocator); 551 nav.children.clearAndFree(allocator); 552 nav.getFocus().child_id = null; 553 for (inner.children.values()) |*child| child.widget.deinit(allocator); 554 inner.children.clearAndFree(allocator); 555 inner.getFocus().child_id = null; 556 557 // only files have contents to show; directories and the ".." row leave 558 // the pane empty. 559 if (self.selectedEntry()) |entry| { 560 if (!entry.is_dir) { 561 // the "next" window link sits above the scroll, so it stays put 562 // while the content scrolls. 563 if (entry.has_more) try self.addNavLink(allocator, nav, "next lines →", entry, entry.window_start + file_page); 564 if (entry.is_binary) { 565 try self.addContentBox(allocator, inner, "(binary file)"); 566 } else { 567 const text = try numberedContent(self.session.page_arena.allocator(), entry.lines, entry.window_start); 568 try self.addContentBox(allocator, inner, text); 569 } 570 } 571 } 572 // point each box at its first child so focus can descend into it. 573 if (nav.children.count() > 0) nav.getFocus().child_id = nav.children.keys()[0]; 574 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 575 576 // reset the scroll to the top for the newly-shown file: directly on the 577 // terminal (the wasm offset), and via a version bump on the web (so the 578 // renderer's scroll id changes and JS drops the preserved position). 579 const sc = self.detailScroll(); 580 sc.x = 0; 581 sc.y = 0; 582 sc.getFocus().version +%= 1; 583 } 584 585 // a focusable "next" link above the content. it's an `a:` link to the 586 // selected file at `target_after`, so activating it (the host follows the 587 // link) reloads the page on the next window. 588 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_after: usize) !void { 589 const page_arena = self.session.page_arena; 590 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 591 const route = ui.RoutablePage.repoFilesRoute(self.data.identity, self.data.ref_or_oid, self.data.ref_or_oid_value, path, target_after) orelse return error.RouteTooLong; 592 const link = try std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.urlAlloc(page_arena)}); 593 var tb = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 594 errdefer tb.deinit(allocator); 595 tb.getFocus().focusable = true; 596 tb.getFocus().kind = .{ .custom = link }; 597 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 598 } 599 600 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 601 _ = allocator; 602 if (self.detailActive()) { 603 try self.detailInput(key, root_focus); 604 } else { 605 try self.listInput(key, root_focus); 606 } 607 } 608 609 fn listInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 610 // up/down (and the scroll wheel) move the selection; page up/down jump a 611 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 612 // directory (or "..") row are turned into navigation by the host 613 // (crossPageLink) before reaching here. 614 switch (key) { 615 .enter, .arrow_right => try self.focusDetail(root_focus), 616 .arrow_down => try self.moveSelection(root_focus, 1), 617 .arrow_up => try self.moveSelection(root_focus, -1), 618 .page_down => try self.moveSelection(root_focus, 10), 619 .page_up => try self.moveSelection(root_focus, -10), 620 .end => try self.moveSelection(root_focus, @intCast(self.listBox().children.count())), 621 .home => try self.moveSelection(root_focus, -@as(isize, @intCast(self.listBox().children.count()))), 622 .mouse => |mouse| switch (mouse.action) { 623 .scroll => |dir| try self.moveSelection(root_focus, if (dir == .up) -1 else 1), 624 else => {}, 625 }, 626 else => {}, 627 } 628 } 629 630 // whether focus is on the content box (vs. a "previous"/"next" nav link). 631 fn focusOnContent(self: *View, root_focus: *Focus) bool { 632 const g = root_focus.grandchild_id orelse return false; 633 return self.detailInner().children.contains(g); 634 } 635 636 fn detailInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 637 const sc = self.detailScroll(); 638 const on_content = self.focusOnContent(root_focus); 639 // the "next" link sits above the content scroll. on the link, left returns 640 // to the file list and down drops into the content. in the content, the 641 // terminal scrolls by offset and reaching the top then pressing up crosses 642 // back up to the link; on the web vertical scrolling is the browser's job 643 // so up just crosses to the link. 644 switch (key) { 645 .arrow_left => { 646 if (on_content) { 647 if (sc.x > 0) { 648 sc.x -= 1; 649 self.clampDetailScroll(); 650 } else try self.focusList(root_focus); 651 } else if (!self.moveNav(root_focus, -1)) { 652 try self.focusList(root_focus); 653 } 654 }, 655 .arrow_right => { 656 if (on_content) { 657 sc.x += 1; 658 self.clampDetailScroll(); 659 } else _ = self.moveNav(root_focus, 1); 660 }, 661 .arrow_up => { 662 if (on_content) { 663 if (self.session.is_terminal and sc.y > 0) { 664 sc.y -= 1; 665 self.clampDetailScroll(); 666 } else self.focusNav(root_focus); // cross up to the links 667 } 668 }, 669 .arrow_down => { 670 if (on_content) { 671 if (self.session.is_terminal) { 672 sc.y += 1; 673 self.clampDetailScroll(); 674 } 675 } else self.focusContent(root_focus); // links -> content 676 }, 677 .page_up => if (on_content and self.session.is_terminal) { 678 sc.y -= 10; 679 self.clampDetailScroll(); 680 }, 681 .page_down => if (on_content and self.session.is_terminal) { 682 sc.y += 10; 683 self.clampDetailScroll(); 684 }, 685 .home => if (on_content and self.session.is_terminal) { 686 sc.y = 0; 687 self.clampDetailScroll(); 688 }, 689 .end => if (on_content and self.session.is_terminal) { 690 sc.y = std.math.maxInt(isize); 691 self.clampDetailScroll(); 692 }, 693 .mouse => |mouse| switch (mouse.action) { 694 .scroll => |dir| if (on_content and self.session.is_terminal) { 695 sc.y += if (dir == .up) -5 else 5; 696 self.clampDetailScroll(); 697 }, 698 else => {}, 699 }, 700 else => {}, 701 } 702 } 703 704 // focus the first (left-most) nav link; a no-op when there are none, leaving 705 // focus on the content. 706 fn focusNav(self: *View, root_focus: *Focus) void { 707 const nav = self.navBox(); 708 if (nav.children.count() == 0) return; 709 root_focus.setFocus(nav.children.keys()[0]); 710 } 711 712 fn focusContent(self: *View, root_focus: *Focus) void { 713 const inner = self.detailInner(); 714 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 715 root_focus.setFocus(id); 716 } 717 718 // move focus `delta` rows within the nav box; returns false (without moving) 719 // when that would step off either end. 720 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 721 const nav = self.navBox(); 722 const keys = nav.children.keys(); 723 const cur_id = nav.getFocus().child_id orelse return false; 724 const cur: isize = @intCast(nav.children.getIndex(cur_id) orelse return false); 725 const target = cur + delta; 726 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 727 root_focus.setFocus(keys[@intCast(target)]); 728 return true; 729 } 730 731 // keep the detail scroll within its content, using the last build's grids. 732 // the scroll bar's reserved column/row isn't part of the content viewport, 733 // so exclude it or the last column/row stays unreachable. 734 fn clampDetailScroll(self: *View) void { 735 const sc = self.detailScroll(); 736 const vp = sc.grid orelse return; 737 const content = sc.child.box.grid orelse return; 738 const view_w = vp.size.width - sc.bar_w; 739 const view_h = vp.size.height - sc.bar_h; 740 const max_y: isize = if (content.size.height > view_h) @intCast(content.size.height - view_h) else 0; 741 const max_x: isize = if (content.size.width > view_w) @intCast(content.size.width - view_w) else 0; 742 sc.y = std.math.clamp(sc.y, 0, max_y); 743 sc.x = std.math.clamp(sc.x, 0, max_x); 744 } 745 746 // enter the detail pane, landing on the "next" link when there is one (so 747 // right-arrow reaches it), else on the content. a directory or ".." row has 748 // neither, so there's nothing to enter. selecting the frame's child (then 749 // focusing the frame) also handles the too-narrow case where the pane isn't 750 // laid out yet — focus lands inside it after the next build. 751 fn focusDetail(self: *View, root_focus: *Focus) !void { 752 const frame = self.detailOuter(); 753 if (self.navBox().children.count() > 0) { 754 frame.getFocus().child_id = self.navBox().getFocus().id; 755 } else if (self.detailInner().children.count() > 0) { 756 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 757 } else return; 758 root_focus.setFocus(frame.getFocus().id); 759 } 760 761 // return to the list. 762 fn focusList(self: *View, root_focus: *Focus) !void { 763 root_focus.setFocus(self.listScroll().getFocus().id); 764 } 765 766 fn moveSelection(self: *View, root_focus: *Focus, delta: isize) !void { 767 const lb = self.listBox(); 768 const keys = lb.children.keys(); 769 if (keys.len == 0) return; 770 const cur_id = lb.getFocus().child_id orelse return; 771 const cur: isize = @intCast(lb.children.getIndex(cur_id) orelse return); 772 const last: isize = @intCast(keys.len - 1); 773 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 774 if (next == @as(usize, @intCast(cur))) return; 775 root_focus.setFocus(keys[next]); 776 if (lb.children.values()[next].rect) |rect| self.listScroll().scrollToRect(rect); 777 } 778 779 pub fn clearGrid(self: *View) void { 780 self.box.clearGrid(); 781 } 782 783 pub fn getGrid(self: View) ?Grid { 784 return self.box.getGrid(); 785 } 786 787 pub fn getFocus(self: *View) *Focus { 788 return self.box.getFocus(); 789 } 790 791 // for the parent's "scroll up at the top jumps to the header" check. inside 792 // the detail pane up-arrow stays in the pane (scrolling); when the list holds 793 // focus, report its selection directly so up-arrow at the top row ascends. 794 pub fn getSelectedIndex(self: *View) ?usize { 795 if (self.detailActive()) return 1; 796 const lb = self.listBox(); 797 const cid = lb.getFocus().child_id orelse return null; 798 return lb.children.getIndex(cid); 799 } 800 }; 801 802 // join `lines` into one string, each prefixed with its 1-based file line number 803 // (right-aligned in a column wide enough for the last number, then a space), 804 // like the diffs view. `start` is the window's 0-based first line, so numbers 805 // continue across paginated windows. 806 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 807 if (lines.len == 0) return ""; 808 const width = std.fmt.count("{d}", .{start + lines.len}); 809 const indent = try arena.alloc(u8, width); 810 @memset(indent, ' '); 811 812 var out: std.ArrayList(u8) = .empty; 813 for (lines, 0..) |line, i| { 814 if (i != 0) try out.append(arena, '\n'); 815 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 816 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 817 try out.appendSlice(arena, num_str); 818 try out.append(arena, ' '); 819 try out.appendSlice(arena, line); 820 } 821 return out.toOwnedSlice(arena); 822 } 823 824 // the index of the "README"/"README.md" file entry (case-insensitive), or null 825 // if none. used to pick the default selection at the repo root. 826 fn readmeIndex(entries: []const Entry) ?usize { 827 for (entries, 0..) |entry, i| { 828 if (entry.is_dir) continue; 829 if (std.ascii.eqlIgnoreCase(entry.name, "README") or std.ascii.eqlIgnoreCase(entry.name, "README.md")) 830 return i; 831 } 832 return null; 833 } 834 835 // the index of the entry the route named (data.selected_file), or null if none. 836 fn selectedFileIndex(data: *const Self) ?usize { 837 const name = data.selected_file orelse return null; 838 for (data.entries, 0..) |entry, i| { 839 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 840 } 841 return null; 842 } 843 844 // the "a:" link for the files route at `dir`, pinned to the listing's ref. 845 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, dir: []const u8) ![]const u8 { 846 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, dir, 0) orelse return error.RouteTooLong; 847 const url = try route.urlAlloc(page_arena); 848 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 849 } 850 851 // `dir`/`name`, or just `name` at the root. 852 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 853 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 854 } 855 856 // `dir` with its last segment removed ("" when `dir` has a single segment). 857 fn parentDir(dir: []const u8) []const u8 { 858 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 859 return dir[0..slash]; 860 } 861