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