╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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(allocator, 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, allocator: std.mem.Allocator, 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 // when the window is too narrow to lay out the detail pane beside 784 // the list, a click on a row opens it like enter. the row was just 785 // selected, so the detail is swapped to it ahead of the build. 786 .mouse => |mouse| if (self.contentBox().children.values()[detail_index].rect == null and ui.widget.clickOnSelectedRow(self.listBox(), root_focus, mouse)) { 787 try self.refreshDetail(allocator); 788 try self.focusDetail(root_focus); 789 }, 790 else => {}, 791 } 792 } 793 794 // whether focus is on the content box (vs. a "previous"/"next" nav link). 795 fn focusOnContent(self: *View, root_focus: *Focus) bool { 796 const g = root_focus.grandchild_id orelse return false; 797 if (self.markdownView()) |view| return view.owns(g); 798 return self.detailInner().children.contains(g); 799 } 800 801 // the rendered markdown in the detail pane, if that's what it shows 802 fn markdownView(self: *View) ?*Markdown.View { 803 const inner = self.detailInner(); 804 if (inner.children.count() == 0) return null; 805 return switch (inner.children.values()[0].widget) { 806 .markdown => |*view| view, 807 else => null, 808 }; 809 } 810 811 fn detailInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 812 const sc = self.detailScroll(); 813 const on_content = self.focusOnContent(root_focus); 814 // the "next" link sits above the content scroll. on the link, left returns 815 // to the file list and down drops into the content. in the content, the 816 // terminal scrolls by offset and reaching the top then pressing up crosses 817 // back up to the link; on the web vertical scrolling is the browser's job 818 // so up just crosses to the link. 819 switch (key) { 820 .arrow_left => { 821 if (on_content) { 822 if (sc.x > 0) { 823 sc.x -= 1; 824 self.detailScroll().clampToContent(); 825 } else try self.focusList(root_focus); 826 } else if (!self.moveNav(root_focus, -1)) { 827 try self.focusList(root_focus); 828 } 829 }, 830 .arrow_right => { 831 if (on_content) { 832 sc.x += 1; 833 self.detailScroll().clampToContent(); 834 } else _ = self.moveNav(root_focus, 1); 835 }, 836 .arrow_up => { 837 if (on_content) { 838 if (self.session.is_terminal and sc.y > 0) { 839 sc.y -= 1; 840 self.detailScroll().clampToContent(); 841 } else self.focusNav(root_focus); // cross up to the links 842 } 843 }, 844 .arrow_down => { 845 if (on_content) { 846 if (self.session.is_terminal) { 847 sc.y += 1; 848 self.detailScroll().clampToContent(); 849 } 850 } else self.focusContent(root_focus); // links -> content 851 }, 852 .page_up => if (on_content and self.session.is_terminal) { 853 sc.y -= 10; 854 self.detailScroll().clampToContent(); 855 }, 856 .page_down => if (on_content and self.session.is_terminal) { 857 sc.y += 10; 858 self.detailScroll().clampToContent(); 859 }, 860 .home => if (on_content and self.session.is_terminal) { 861 sc.y = 0; 862 self.detailScroll().clampToContent(); 863 }, 864 .end => if (on_content and self.session.is_terminal) { 865 sc.y = std.math.maxInt(isize); 866 self.detailScroll().clampToContent(); 867 }, 868 .mouse => |mouse| switch (mouse.action) { 869 .scroll => |dir| if (on_content and self.session.is_terminal) { 870 sc.y += if (dir == .up) -5 else 5; 871 self.detailScroll().clampToContent(); 872 }, 873 else => {}, 874 }, 875 // step between the rendered markdown's links; the web leaves tab to the browser 876 .tab, .back_tab => if (on_content and self.session.is_terminal) if (self.markdownView()) |view| { 877 try view.stepLink(allocator, root_focus, sc, 0, 0, key == .tab); 878 }, 879 else => {}, 880 } 881 } 882 883 // focus the first (left-most) nav link; a no-op when there are none, leaving 884 // focus on the content. 885 fn focusNav(self: *View, root_focus: *Focus) void { 886 const nav_box = self.navBox(); 887 if (nav_box.children.count() == 0) return; 888 root_focus.setFocus(nav_box.children.keys()[0]); 889 } 890 891 fn focusContent(self: *View, root_focus: *Focus) void { 892 if (self.markdownView()) |view| return root_focus.setFocus(view.body.id); 893 const inner = self.detailInner(); 894 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 895 root_focus.setFocus(id); 896 } 897 898 // move focus `delta` rows within the nav box; returns false (without moving) 899 // when that would step off either end. 900 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 901 const nav_box = self.navBox(); 902 const keys = nav_box.children.keys(); 903 const cur_id = nav_box.getFocus().child_id orelse return false; 904 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 905 const target = cur + delta; 906 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 907 root_focus.setFocus(keys[@intCast(target)]); 908 return true; 909 } 910 911 // enter the detail pane, landing on the "next" link when there is one (so 912 // right-arrow reaches it), else on the content. a directory or ".." row has 913 // neither, so there's nothing to enter. selecting the frame's child (then 914 // focusing the frame) also handles the too-narrow case where the pane isn't 915 // laid out yet — focus lands inside it after the next build. 916 fn focusDetail(self: *View, root_focus: *Focus) !void { 917 const frame = self.detailOuter(); 918 if (self.navBox().children.count() > 0) { 919 frame.getFocus().child_id = self.navBox().getFocus().id; 920 } else if (self.detailInner().children.count() > 0) { 921 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 922 } else return; 923 root_focus.setFocus(frame.getFocus().id); 924 } 925 926 // return to the list. 927 fn focusList(self: *View, root_focus: *Focus) !void { 928 root_focus.setFocus(self.listScroll().getFocus().id); 929 } 930 931 pub fn clearGrid(self: *View) void { 932 self.box.clearGrid(); 933 } 934 935 pub fn getGrid(self: View) ?Grid { 936 return self.box.getGrid(); 937 } 938 939 pub fn getFocus(self: *View) *Focus { 940 return self.box.getFocus(); 941 } 942 943 fn contentAtTop(self: *View) bool { 944 if (self.detailActive()) { 945 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 946 return self.navBox().children.count() == 0 and 947 (!self.session.is_terminal or self.detailScroll().y == 0); 948 } 949 const lb = self.listBox(); 950 const cid = lb.getFocus().child_id orelse return true; 951 return lb.children.getIndex(cid) == 0; 952 } 953 954 // only the header's own row sits directly below the repository header. 955 pub fn atTop(self: *View) bool { 956 if (self.data.no_commits) return true; 957 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 958 return self.headerActive() or (!focusable and self.contentAtTop()); 959 } 960 961 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 962 const header_view = self.header() orelse return false; 963 return header_view.focusHeader(root_focus); 964 } 965 }; 966 967 // `lines` as spans, each prefixed with its 1-based file line number 968 // (right-aligned in a column wide enough for the last number, then a space), 969 // like the diffs view. each number links to the window starting at its line. 970 // `start` is the window's 0-based first line, so numbers continue across 971 // paginated windows. 972 fn numberedContent(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, lines: []const []const u8, start: usize) ![]const wgt.Span { 973 const arena = page_arena.allocator(); 974 const width = std.fmt.count("{d}", .{start + lines.len}); 975 var spans: std.ArrayList(wgt.Span) = .empty; 976 for (lines, 0..) |line, i| { 977 const number = start + i + 1; 978 const route = data.filesRoute(path, number) orelse return error.RouteTooLong; 979 try spans.append(arena, .{ .text = try arena.print("{d: >[1]}", .{ number, width }), .link = try route.toUrl(page_arena) }); 980 try spans.append(arena, .{ .text = try arena.print(" {s}{s}", .{ line, if (i + 1 < lines.len) "\n" else "" }) }); 981 } 982 return spans.toOwnedSlice(arena); 983 } 984 985 // the index of the "README"/"README.md" file entry (case-insensitive), or null 986 // if none. used to pick the default selection at the repo root. 987 fn readmeIndex(entries: []const Entry) ?usize { 988 for (entries, 0..) |entry, i| { 989 if (!entry.is_dir and isReadme(entry.name)) return i; 990 } 991 return null; 992 } 993 994 fn isMarkdown(name: []const u8) bool { 995 const extension = std.Io.Dir.path.extension(name); 996 return std.ascii.eqlIgnoreCase(extension, ".md") or std.ascii.eqlIgnoreCase(extension, ".markdown"); 997 } 998 999 fn isReadme(name: []const u8) bool { 1000 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 1001 } 1002 1003 // the index of the entry the route named (data.selected_file), or null if none. 1004 fn selectedFileIndex(data: *const Self) ?usize { 1005 const name = data.selected_file orelse return null; 1006 for (data.entries, 0..) |entry, i| { 1007 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 1008 } 1009 return null; 1010 } 1011 1012 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 1013 // following it navigates to that directory's listing. 1014 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 1015 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 1016 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1017 } 1018 1019 // a link to the file's route. a preloaded file gets an "ai:" link 1020 // (crossPageLink ignores it, so a click selects the row in place and shows its 1021 // contents); an unloaded file gets an "a:" link, so activating it reloads the 1022 // page with that file selected and its content read. 1023 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 1024 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 1025 const prefix = if (loaded) "ai:" else "a:"; 1026 return page_arena.allocator().print("{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 1027 } 1028 1029 // the url `line` value for a content window: its first line's 1-based number, 1030 // except the first window, which serializes as unset (0). 1031 fn lineNumber(window_start: usize) usize { 1032 return if (window_start == 0) 0 else window_start + 1; 1033 } 1034 1035 // `dir`/`name`, or just `name` at the root. 1036 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 1037 return if (dir.len == 0) name else arena.print("{s}/{s}", .{ dir, name }); 1038 } 1039 1040 // `dir` with its last segment removed ("" when `dir` has a single segment). 1041 fn parentDir(dir: []const u8) []const u8 { 1042 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 1043 return dir[0..slash]; 1044 } 1045 1046 // a search result's bottom label: the directory it sits in, empty at the root. 1047 // a directory too wide for the list column is clipped from the left. 1048 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 1049 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 1050 const dir = path[0..slash]; 1051 // the row's border and the label's padding take two columns each 1052 const max_width = View.list_max_width - 4; 1053 var width = try xitui.width.displayWidth(dir); 1054 if (width <= max_width) return aa.print(" {s} ", .{dir}); 1055 1056 const ellipsis = ".."; 1057 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1058 var start: usize = 0; 1059 while (width > max_width - ellipsis.len) { 1060 const slice = iter.nextCodepointSlice() orelse break; 1061 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1062 start += slice.len; 1063 } 1064 return aa.print(" " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1065 } 1066