╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
929 return lb.children.getIndex(cid) == 0; 930 } 931 932 // only the header's own row sits directly below the repository header. 933 pub fn atTop(self: *View) bool { 934 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 935 return self.headerActive() or (!focusable and self.contentAtTop()); 936 } 937 938 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 939 const header_view = self.header() orelse return false; 940 return header_view.focusHeader(root_focus); 941 } 942 }; 943 944 // join `lines` into one string, each prefixed with its 1-based file line number 945 // (right-aligned in a column wide enough for the last number, then a space), 946 // like the diffs view. `start` is the window's 0-based first line, so numbers 947 // continue across paginated windows. 948 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 949 if (lines.len == 0) return ""; 950 const width = std.fmt.count("{d}", .{start + lines.len}); 951 const indent = try arena.alloc(u8, width); 952 @memset(indent, ' '); 953 954 var out: std.ArrayList(u8) = .empty; 955 for (lines, 0..) |line, i| { 956 if (i != 0) try out.append(arena, '\n'); 957 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 958 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 959 try out.appendSlice(arena, num_str); 960 try out.append(arena, ' '); 961 try out.appendSlice(arena, line); 962 } 963 return out.toOwnedSlice(arena); 964 } 965 966 // the index of the "README"/"README.md" file entry (case-insensitive), or null 967 // if none. used to pick the default selection at the repo root. 968 fn readmeIndex(entries: []const Entry) ?usize { 969 for (entries, 0..) |entry, i| { 970 if (!entry.is_dir and isReadme(entry.name)) return i; 971 } 972 return null; 973 } 974 975 fn isMarkdown(name: []const u8) bool { 976 const extension = std.fs.path.extension(name); 977 return std.ascii.eqlIgnoreCase(extension, ".md") or std.ascii.eqlIgnoreCase(extension, ".markdown"); 978 } 979 980 fn isReadme(name: []const u8) bool { 981 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 982 } 983 984 // the index of the entry the route named (data.selected_file), or null if none. 985 fn selectedFileIndex(data: *const Self) ?usize { 986 const name = data.selected_file orelse return null; 987 for (data.entries, 0..) |entry, i| { 988 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 989 } 990 return null; 991 } 992 993 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 994 // following it navigates to that directory's listing. 995 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 996 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 997 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 998 } 999 1000 // a link to the file's route. a preloaded file gets an "ai:" link 1001 // (crossPageLink ignores it, so a click selects the row in place and shows its 1002 // contents); an unloaded file gets an "a:" link, so activating it reloads the 1003 // page with that file selected and its content read. 1004 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 1005 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 1006 const prefix = if (loaded) "ai:" else "a:"; 1007 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 1008 } 1009 1010 // the url `line` value for a content window: its first line's 1-based number, 1011 // except the first window, which serializes as unset (0). 1012 fn lineNumber(window_start: usize) usize { 1013 return if (window_start == 0) 0 else window_start + 1; 1014 } 1015 1016 // `dir`/`name`, or just `name` at the root. 1017 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 1018 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 1019 } 1020 1021 // `dir` with its last segment removed ("" when `dir` has a single segment). 1022 fn parentDir(dir: []const u8) []const u8 { 1023 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 1024 return dir[0..slash]; 1025 } 1026 1027 // a search result's bottom label: the directory it sits in, empty at the root. 1028 // a directory too wide for the list column is clipped from the left. 1029 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 1030 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 1031 const dir = path[0..slash]; 1032 // the row's border and the label's padding take two columns each 1033 const max_width = View.list_max_width - 4; 1034 var width = try xitui.width.displayWidth(dir); 1035 if (width <= max_width) return std.fmt.allocPrint(aa, " {s} ", .{dir}); 1036 1037 const ellipsis = ".."; 1038 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1039 var start: usize = 0; 1040 while (width > max_width - ellipsis.len) { 1041 const slice = iter.nextCodepointSlice() orelse break; 1042 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1043 start += slice.len; 1044 } 1045 return std.fmt.allocPrint(aa, " " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1046 } 1047