╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
934 // the index of the entry the route named (data.selected_file), or null if none. 935 fn selectedFileIndex(data: *const Self) ?usize { 936 const name = data.selected_file orelse return null; 937 for (data.entries, 0..) |entry, i| { 938 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 939 } 940 return null; 941 } 942 943 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 944 // following it navigates to that directory's listing. 945 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 946 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 947 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 948 } 949 950 // a link to the file's route. a preloaded file gets an "ai:" link 951 // (crossPageLink ignores it, so a click selects the row in place and shows its 952 // contents); an unloaded file gets an "a:" link, so activating it reloads the 953 // page with that file selected and its content read. 954 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 955 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 956 const prefix = if (loaded) "ai:" else "a:"; 957 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 958 } 959 960 // the url `line` value for a content window: its first line's 1-based number, 961 // except the first window, which serializes as unset (0). 962 fn lineNumber(window_start: usize) usize { 963 return if (window_start == 0) 0 else window_start + 1; 964 } 965 966 // `dir`/`name`, or just `name` at the root. 967 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 968 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 969 } 970 971 // `dir` with its last segment removed ("" when `dir` has a single segment). 972 fn parentDir(dir: []const u8) []const u8 { 973 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 974 return dir[0..slash]; 975 } 976 977 // a search result's bottom label: the directory it sits in, empty at the root. 978 // a directory too wide for the list column is clipped from the left. 979 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 980 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 981 const dir = path[0..slash]; 982 // the row's border and the label's padding take two columns each 983 const max_width = View.list_max_width - 4; 984 var width = try xitui.width.displayWidth(dir); 985 if (width <= max_width) return std.fmt.allocPrint(aa, " {s} ", .{dir}); 986 987 const ellipsis = ".."; 988 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 989 var start: usize = 0; 990 while (width > max_width - ellipsis.len) { 991 const slice = iter.nextCodepointSlice() orelse break; 992 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 993 start += slice.len; 994 } 995 return std.fmt.allocPrint(aa, " " ++ ellipsis ++ "{s} ", .{dir[start..]}); 996 } 997