╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1012 // following it navigates to that directory's listing. 1013 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 1014 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 1015 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1016 } 1017 1018 // a link to the file's route. a preloaded file gets an "ai:" link 1019 // (crossPageLink ignores it, so a click selects the row in place and shows its 1020 // contents); an unloaded file gets an "a:" link, so activating it reloads the 1021 // page with that file selected and its content read. 1022 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 1023 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 1024 const prefix = if (loaded) "ai:" else "a:"; 1025 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 1026 } 1027 1028 // the url `line` value for a content window: its first line's 1-based number, 1029 // except the first window, which serializes as unset (0). 1030 fn lineNumber(window_start: usize) usize { 1031 return if (window_start == 0) 0 else window_start + 1; 1032 } 1033 1034 // `dir`/`name`, or just `name` at the root. 1035 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 1036 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 1037 } 1038 1039 // `dir` with its last segment removed ("" when `dir` has a single segment). 1040 fn parentDir(dir: []const u8) []const u8 { 1041 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 1042 return dir[0..slash]; 1043 } 1044 1045 // a search result's bottom label: the directory it sits in, empty at the root. 1046 // a directory too wide for the list column is clipped from the left. 1047 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 1048 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 1049 const dir = path[0..slash]; 1050 // the row's border and the label's padding take two columns each 1051 const max_width = View.list_max_width - 4; 1052 var width = try xitui.width.displayWidth(dir); 1053 if (width <= max_width) return std.fmt.allocPrint(aa, " {s} ", .{dir}); 1054 1055 const ellipsis = ".."; 1056 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1057 var start: usize = 0; 1058 while (width > max_width - ellipsis.len) { 1059 const slice = iter.nextCodepointSlice() orelse break; 1060 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1061 start += slice.len; 1062 } 1063 return std.fmt.allocPrint(aa, " " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1064 } 1065