╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
807 .scroll => |dir| if (on_content and self.session.is_terminal) { 808 sc.y += if (dir == .up) -5 else 5; 809 self.detailScroll().clampToContent(); 810 }, 811 else => {}, 812 }, 813 else => {}, 814 } 815 } 816 817 // focus the first (left-most) nav link; a no-op when there are none, leaving 818 // focus on the content. 819 fn focusNav(self: *View, root_focus: *Focus) void { 820 const nav_box = self.navBox(); 821 if (nav_box.children.count() == 0) return; 822 root_focus.setFocus(nav_box.children.keys()[0]); 823 } 824 825 fn focusContent(self: *View, root_focus: *Focus) void { 826 const inner = self.detailInner(); 827 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 828 root_focus.setFocus(id); 829 } 830 831 // move focus `delta` rows within the nav box; returns false (without moving) 832 // when that would step off either end. 833 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 834 const nav_box = self.navBox(); 835 const keys = nav_box.children.keys(); 836 const cur_id = nav_box.getFocus().child_id orelse return false; 837 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 838 const target = cur + delta; 839 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 840 root_focus.setFocus(keys[@intCast(target)]); 841 return true; 842 } 843 844 // enter the detail pane, landing on the "next" link when there is one (so 845 // right-arrow reaches it), else on the content. a directory or ".." row has 846 // neither, so there's nothing to enter. selecting the frame's child (then 847 // focusing the frame) also handles the too-narrow case where the pane isn't 848 // laid out yet — focus lands inside it after the next build. 849 fn focusDetail(self: *View, root_focus: *Focus) !void { 850 const frame = self.detailOuter(); 851 if (self.navBox().children.count() > 0) { 852 frame.getFocus().child_id = self.navBox().getFocus().id; 853 } else if (self.detailInner().children.count() > 0) { 854 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 855 } else return; 856 root_focus.setFocus(frame.getFocus().id); 857 } 858 859 // return to the list. 860 fn focusList(self: *View, root_focus: *Focus) !void { 861 root_focus.setFocus(self.listScroll().getFocus().id); 862 } 863 864 pub fn clearGrid(self: *View) void { 865 self.box.clearGrid(); 866 } 867 868 pub fn getGrid(self: View) ?Grid { 869 return self.box.getGrid(); 870 } 871 872 pub fn getFocus(self: *View) *Focus { 873 return self.box.getFocus(); 874 } 875 876 fn contentAtTop(self: *View) bool { 877 if (self.detailActive()) { 878 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 879 return self.navBox().children.count() == 0 and 880 (!self.session.is_terminal or self.detailScroll().y == 0); 881 } 882 const lb = self.listBox(); 883 const cid = lb.getFocus().child_id orelse return true; 884 return lb.children.getIndex(cid) == 0; 885 } 886 887 // only the header's own row sits directly below the repository header. 888 pub fn atTop(self: *View) bool { 889 return self.headerActive() or (!self.header().hasFocusable() and self.contentAtTop()); 890 } 891 892 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 893 return self.header().focusHeader(root_focus); 894 } 895 }; 896 897 // join `lines` into one string, each prefixed with its 1-based file line number 898 // (right-aligned in a column wide enough for the last number, then a space), 899 // like the diffs view. `start` is the window's 0-based first line, so numbers 900 // continue across paginated windows. 901 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 902 if (lines.len == 0) return ""; 903 const width = std.fmt.count("{d}", .{start + lines.len}); 904 const indent = try arena.alloc(u8, width); 905 @memset(indent, ' '); 906 907 var out: std.ArrayList(u8) = .empty; 908 for (lines, 0..) |line, i| { 909 if (i != 0) try out.append(arena, '\n'); 910 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 911 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 912 try out.appendSlice(arena, num_str); 913 try out.append(arena, ' '); 914 try out.appendSlice(arena, line); 915 } 916 return out.toOwnedSlice(arena); 917 } 918 919 // the index of the "README"/"README.md" file entry (case-insensitive), or null 920 // if none. used to pick the default selection at the repo root. 921 fn readmeIndex(entries: []const Entry) ?usize { 922 for (entries, 0..) |entry, i| { 923 if (!entry.is_dir and isReadme(entry.name)) return i; 924 } 925 return null; 926 } 927 928 fn isReadme(name: []const u8) bool { 929 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 930 } 931 932 // the index of the entry the route named (data.selected_file), or null if none. 933 fn selectedFileIndex(data: *const Self) ?usize { 934 const name = data.selected_file orelse return null; 935 for (data.entries, 0..) |entry, i| { 936 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 937 } 938 return null; 939 } 940 941 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 942 // following it navigates to that directory's listing. 943 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 944 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 945 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 946 } 947 948 // a link to the file's route. a preloaded file gets an "ai:" link 949 // (crossPageLink ignores it, so a click selects the row in place and shows its 950 // contents); an unloaded file gets an "a:" link, so activating it reloads the 951 // page with that file selected and its content read. 952 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 953 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 954 const prefix = if (loaded) "ai:" else "a:"; 955 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 956 } 957 958 // the url `line` value for a content window: its first line's 1-based number, 959 // except the first window, which serializes as unset (0). 960 fn lineNumber(window_start: usize) usize { 961 return if (window_start == 0) 0 else window_start + 1; 962 } 963 964 // `dir`/`name`, or just `name` at the root. 965 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 966 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 967 } 968 969 // `dir` with its last segment removed ("" when `dir` has a single segment). 970 fn parentDir(dir: []const u8) []const u8 { 971 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 972 return dir[0..slash]; 973 } 974 975 // a search result's bottom label: the directory it sits in, empty at the root. 976 // a directory too wide for the list column is clipped from the left. 977 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 978 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 979 const dir = path[0..slash]; 980 // the row's border and the label's padding take two columns each 981 const max_width = View.list_max_width - 4; 982 var width = try xitui.width.displayWidth(dir); 983 if (width <= max_width) return std.fmt.allocPrint(aa, " {s} ", .{dir}); 984 985 const ellipsis = ".."; 986 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 987 var start: usize = 0; 988 while (width > max_width - ellipsis.len) { 989 const slice = iter.nextCodepointSlice() orelse break; 990 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 991 start += slice.len; 992 } 993 return std.fmt.allocPrint(aa, " " ++ ellipsis ++ "{s} ", .{dir[start..]}); 994 } 995