╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1007 const header_view = self.header() orelse return false; 1008 return header_view.focusHeader(root_focus); 1009 } 1010 }; 1011 1012 // `lines` as spans, each prefixed with its 1-based file line number 1013 // (right-aligned in a column wide enough for the last number, then a space), 1014 // like the diffs view. each number links to the window starting at its line. 1015 // `start` is the window's 0-based first line, so numbers continue across 1016 // paginated windows. 1017 fn numberedContent(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, lines: []const []const u8, start: usize) ![]const wgt.Span { 1018 const arena = page_arena.allocator(); 1019 const width = std.fmt.count("{d}", .{start + lines.len}); 1020 var spans: std.ArrayList(wgt.Span) = .empty; 1021 for (lines, 0..) |line, i| { 1022 const number = start + i + 1; 1023 const route = data.filesRoute(path, number) orelse return error.RouteTooLong; 1024 try spans.append(arena, .{ .text = try arena.print("{d: >[1]}", .{ number, width }), .link = try route.toUrl(page_arena) }); 1025 try spans.append(arena, .{ .text = try arena.print(" {s}{s}", .{ line, if (i + 1 < lines.len) "\n" else "" }) }); 1026 } 1027 return spans.toOwnedSlice(arena); 1028 } 1029 1030 // the index of the "README"/"README.md" file entry (case-insensitive), or null 1031 // if none. used to pick the default selection at the repo root. 1032 fn readmeIndex(entries: []const Entry) ?usize { 1033 for (entries, 0..) |entry, i| { 1034 if (!entry.is_dir and isReadme(entry.name)) return i; 1035 } 1036 return null; 1037 } 1038 1039 fn isMarkdown(name: []const u8) bool { 1040 const extension = std.Io.Dir.path.extension(name); 1041 return std.ascii.eqlIgnoreCase(extension, ".md") or std.ascii.eqlIgnoreCase(extension, ".markdown"); 1042 } 1043 1044 fn isReadme(name: []const u8) bool { 1045 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 1046 } 1047 1048 // the index of the entry the route named (data.selected_file), or null if none. 1049 fn selectedFileIndex(data: *const Self) ?usize { 1050 const name = data.selected_file orelse return null; 1051 for (data.entries, 0..) |entry, i| { 1052 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 1053 } 1054 return null; 1055 } 1056 1057 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 1058 // following it navigates to that directory's listing. 1059 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 1060 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 1061 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1062 } 1063 1064 // a link to the file's route. a preloaded file gets an "ai:" link 1065 // (crossPageLink ignores it, so a click selects the row in place and shows its 1066 // contents); an unloaded file gets an "a:" link, so activating it reloads the 1067 // page with that file selected and its content read. 1068 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 1069 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 1070 const prefix = if (loaded) "ai:" else "a:"; 1071 return page_arena.allocator().print("{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 1072 } 1073 1074 // the url `line` value for a content window: its first line's 1-based number, 1075 // except the first window, which serializes as unset (0). 1076 fn lineNumber(window_start: usize) usize { 1077 return if (window_start == 0) 0 else window_start + 1; 1078 } 1079 1080 // `dir`/`name`, or just `name` at the root. 1081 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 1082 return if (dir.len == 0) name else arena.print("{s}/{s}", .{ dir, name }); 1083 } 1084 1085 // `dir` with its last segment removed ("" when `dir` has a single segment). 1086 fn parentDir(dir: []const u8) []const u8 { 1087 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 1088 return dir[0..slash]; 1089 } 1090 1091 // a search result's bottom label: the directory it sits in, empty at the root. 1092 // a directory too wide for the list column is clipped from the left. 1093 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 1094 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 1095 const dir = path[0..slash]; 1096 // the row's border and the label's padding take two columns each 1097 const max_width = View.list_max_width - 4; 1098 var width = try xitui.width.displayWidth(dir); 1099 if (width <= max_width) return aa.print(" {s} ", .{dir}); 1100 1101 const ellipsis = ".."; 1102 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1103 var start: usize = 0; 1104 while (width > max_width - ellipsis.len) { 1105 const slice = iter.nextCodepointSlice() orelse break; 1106 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1107 start += slice.len; 1108 } 1109 return aa.print(" " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1110 } 1111