╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
681 } else self.focusNav(root_focus); // cross up to the links 682 } 683 }, 684 .arrow_down => { 685 if (on_content) { 686 if (self.session.is_terminal) { 687 sc.y += 1; 688 self.detailScroll().clampToContent(); 689 } 690 } else self.focusContent(root_focus); // links -> content 691 }, 692 .page_up => if (on_content and self.session.is_terminal) { 693 sc.y -= 10; 694 self.detailScroll().clampToContent(); 695 }, 696 .page_down => if (on_content and self.session.is_terminal) { 697 sc.y += 10; 698 self.detailScroll().clampToContent(); 699 }, 700 .home => if (on_content and self.session.is_terminal) { 701 sc.y = 0; 702 self.detailScroll().clampToContent(); 703 }, 704 .end => if (on_content and self.session.is_terminal) { 705 sc.y = std.math.maxInt(isize); 706 self.detailScroll().clampToContent(); 707 }, 708 .mouse => |mouse| switch (mouse.action) { 709 .scroll => |dir| if (on_content and self.session.is_terminal) { 710 sc.y += if (dir == .up) -5 else 5; 711 self.detailScroll().clampToContent(); 712 }, 713 else => {}, 714 }, 715 else => {}, 716 } 717 } 718 719 // focus the first (left-most) nav link; a no-op when there are none, leaving 720 // focus on the content. 721 fn focusNav(self: *View, root_focus: *Focus) void { 722 const nav_box = self.navBox(); 723 if (nav_box.children.count() == 0) return; 724 root_focus.setFocus(nav_box.children.keys()[0]); 725 } 726 727 fn focusContent(self: *View, root_focus: *Focus) void { 728 const inner = self.detailInner(); 729 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 730 root_focus.setFocus(id); 731 } 732 733 // move focus `delta` rows within the nav box; returns false (without moving) 734 // when that would step off either end. 735 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 736 const nav_box = self.navBox(); 737 const keys = nav_box.children.keys(); 738 const cur_id = nav_box.getFocus().child_id orelse return false; 739 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 740 const target = cur + delta; 741 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 742 root_focus.setFocus(keys[@intCast(target)]); 743 return true; 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 pub fn clearGrid(self: *View) void { 767 self.box.clearGrid(); 768 } 769 770 pub fn getGrid(self: View) ?Grid { 771 return self.box.getGrid(); 772 } 773 774 pub fn getFocus(self: *View) *Focus { 775 return self.box.getFocus(); 776 } 777 778 // for the parent's "scroll up at the top jumps to the header" check. inside 779 // the detail pane up-arrow stays in the pane (scrolling); when the list holds 780 // focus, report its selection directly so up-arrow at the top row ascends. 781 pub fn getSelectedIndex(self: *View) ?usize { 782 if (self.detailActive()) return 1; 783 const lb = self.listBox(); 784 const cid = lb.getFocus().child_id orelse return null; 785 return lb.children.getIndex(cid); 786 } 787 }; 788 789 // join `lines` into one string, each prefixed with its 1-based file line number 790 // (right-aligned in a column wide enough for the last number, then a space), 791 // like the diffs view. `start` is the window's 0-based first line, so numbers 792 // continue across paginated windows. 793 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 794 if (lines.len == 0) return ""; 795 const width = std.fmt.count("{d}", .{start + lines.len}); 796 const indent = try arena.alloc(u8, width); 797 @memset(indent, ' '); 798 799 var out: std.ArrayList(u8) = .empty; 800 for (lines, 0..) |line, i| { 801 if (i != 0) try out.append(arena, '\n'); 802 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 803 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 804 try out.appendSlice(arena, num_str); 805 try out.append(arena, ' '); 806 try out.appendSlice(arena, line); 807 } 808 return out.toOwnedSlice(arena); 809 } 810 811 // the index of the "README"/"README.md" file entry (case-insensitive), or null 812 // if none. used to pick the default selection at the repo root. 813 fn readmeIndex(entries: []const Entry) ?usize { 814 for (entries, 0..) |entry, i| { 815 if (entry.is_dir) continue; 816 if (std.ascii.eqlIgnoreCase(entry.name, "README") or std.ascii.eqlIgnoreCase(entry.name, "README.md")) 817 return i; 818 } 819 return null; 820 } 821 822 // the index of the entry the route named (data.selected_file), or null if none. 823 fn selectedFileIndex(data: *const Self) ?usize { 824 const name = data.selected_file orelse return null; 825 for (data.entries, 0..) |entry, i| { 826 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 827 } 828 return null; 829 } 830 831 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 832 // following it navigates to that directory's listing. 833 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 834 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 835 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.urlAlloc(page_arena)}); 836 } 837 838 // an "ai:" link to the file's route: crossPageLink ignores it, so a wasm click 839 // selects the row in place and shows its contents in the detail pane. 840 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 841 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 842 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.urlAlloc(page_arena)}); 843 } 844 845 // `dir`/`name`, or just `name` at the root. 846 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 847 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 848 } 849 850 // `dir` with its last segment removed ("" when `dir` has a single segment). 851 fn parentDir(dir: []const u8) []const u8 { 852 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 853 return dir[0..slash]; 854 } 855