╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1029 // the index of the entry the route named (data.selected_file), or null if none. 1030 fn selectedFileIndex(data: *const Self) ?usize { 1031 const name = data.selected_file orelse return null; 1032 for (data.entries, 0..) |entry, i| { 1033 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 1034 } 1035 return null; 1036 } 1037 1038 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 1039 // following it navigates to that directory's listing. 1040 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 1041 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 1042 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1043 } 1044 1045 // a link to the file's route. a preloaded file gets an "ai:" link 1046 // (crossPageLink ignores it, so a click selects the row in place and shows its 1047 // contents); an unloaded file gets an "a:" link, so activating it reloads the 1048 // page with that file selected and its content read. 1049 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 1050 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 1051 const prefix = if (loaded) "ai:" else "a:"; 1052 return page_arena.allocator().print("{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 1053 } 1054 1055 // the url `line` value for a content window: its first line's 1-based number, 1056 // except the first window, which serializes as unset (0). 1057 fn lineNumber(window_start: usize) usize { 1058 return if (window_start == 0) 0 else window_start + 1; 1059 } 1060 1061 // `dir`/`name`, or just `name` at the root. 1062 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 1063 return if (dir.len == 0) name else arena.print("{s}/{s}", .{ dir, name }); 1064 } 1065 1066 // `dir` with its last segment removed ("" when `dir` has a single segment). 1067 fn parentDir(dir: []const u8) []const u8 { 1068 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 1069 return dir[0..slash]; 1070 } 1071 1072 // a search result's bottom label: the directory it sits in, empty at the root. 1073 // a directory too wide for the list column is clipped from the left. 1074 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 1075 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 1076 const dir = path[0..slash]; 1077 // the row's border and the label's padding take two columns each 1078 const max_width = View.list_max_width - 4; 1079 var width = try xitui.width.displayWidth(dir); 1080 if (width <= max_width) return aa.print(" {s} ", .{dir}); 1081 1082 const ellipsis = ".."; 1083 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1084 var start: usize = 0; 1085 while (width > max_width - ellipsis.len) { 1086 const slice = iter.nextCodepointSlice() orelse break; 1087 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1088 start += slice.len; 1089 } 1090 return aa.print(" " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1091 } 1092