╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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