╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
874 } 875 876 pub fn clearGrid(self: *View) void { 877 self.box.clearGrid(); 878 } 879 880 pub fn getGrid(self: View) ?Grid { 881 return self.box.getGrid(); 882 } 883 884 pub fn getFocus(self: *View) *Focus { 885 return self.box.getFocus(); 886 } 887 888 fn contentAtTop(self: *View) bool { 889 if (self.detailActive()) { 890 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 891 return self.navBox().children.count() == 0 and 892 (!self.session.is_terminal or self.detailScroll().y == 0); 893 } 894 const lb = self.listBox(); 895 const cid = lb.getFocus().child_id orelse return true; 896 return lb.children.getIndex(cid) == 0; 897 } 898 899 // only the header's own row sits directly below the repository header. 900 pub fn atTop(self: *View) bool { 901 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 902 return self.headerActive() or (!focusable and self.contentAtTop()); 903 } 904 905 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 906 const header_view = self.header() orelse return false; 907 return header_view.focusHeader(root_focus); 908 } 909 }; 910 911 // join `lines` into one string, each prefixed with its 1-based file line number 912 // (right-aligned in a column wide enough for the last number, then a space), 913 // like the diffs view. `start` is the window's 0-based first line, so numbers 914 // continue across paginated windows. 915 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 916 if (lines.len == 0) return ""; 917 const width = std.fmt.count("{d}", .{start + lines.len}); 918 const indent = try arena.alloc(u8, width); 919 @memset(indent, ' '); 920 921 var out: std.ArrayList(u8) = .empty; 922 for (lines, 0..) |line, i| { 923 if (i != 0) try out.append(arena, '\n'); 924 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 925 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 926 try out.appendSlice(arena, num_str); 927 try out.append(arena, ' '); 928 try out.appendSlice(arena, line); 929 } 930 return out.toOwnedSlice(arena); 931 } 932 933 // the index of the "README"/"README.md" file entry (case-insensitive), or null 934 // if none. used to pick the default selection at the repo root. 935 fn readmeIndex(entries: []const Entry) ?usize { 936 for (entries, 0..) |entry, i| { 937 if (!entry.is_dir and isReadme(entry.name)) return i; 938 } 939 return null; 940 } 941 942 fn isReadme(name: []const u8) bool { 943 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 944 } 945 946 // the index of the entry the route named (data.selected_file), or null if none. 947 fn selectedFileIndex(data: *const Self) ?usize { 948 const name = data.selected_file orelse return null; 949 for (data.entries, 0..) |entry, i| { 950 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 951 } 952 return null; 953 } 954 955 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 956 // following it navigates to that directory's listing. 957 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 958 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 959 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 960 } 961 962 // a link to the file's route. a preloaded file gets an "ai:" link 963 // (crossPageLink ignores it, so a click selects the row in place and shows its 964 // contents); an unloaded file gets an "a:" link, so activating it reloads the 965 // page with that file selected and its content read. 966 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 967 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 968 const prefix = if (loaded) "ai:" else "a:"; 969 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 970 } 971 972 // the url `line` value for a content window: its first line's 1-based number, 973 // except the first window, which serializes as unset (0). 974 fn lineNumber(window_start: usize) usize { 975 return if (window_start == 0) 0 else window_start + 1; 976 } 977 978 // `dir`/`name`, or just `name` at the root. 979 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 980 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 981 } 982 983 // `dir` with its last segment removed ("" when `dir` has a single segment). 984 fn parentDir(dir: []const u8) []const u8 { 985 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 986 return dir[0..slash]; 987 } 988 989 // a search result's bottom label: the directory it sits in, empty at the root. 990 // a directory too wide for the list column is clipped from the left. 991 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 992 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 993 const dir = path[0..slash]; 994 // the row's border and the label's padding take two columns each 995 const max_width = View.list_max_width - 4; 996 var width = try xitui.width.displayWidth(dir); 997 if (width <= max_width) return std.fmt.allocPrint(aa, " {s} ", .{dir}); 998 999 const ellipsis = ".."; 1000 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1001 var start: usize = 0; 1002 while (width > max_width - ellipsis.len) { 1003 const slice = iter.nextCodepointSlice() orelse break; 1004 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1005 start += slice.len; 1006 } 1007 return std.fmt.allocPrint(aa, " " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1008 } 1009