╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
950 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 951 } else return; 952 root_focus.setFocus(frame.getFocus().id); 953 } 954 955 // return to the list. 956 fn focusList(self: *View, root_focus: *Focus) !void { 957 root_focus.setFocus(self.listScroll().getFocus().id); 958 } 959 960 pub fn clearGrid(self: *View) void { 961 self.box.clearGrid(); 962 } 963 964 pub fn getGrid(self: View) ?Grid { 965 return self.box.getGrid(); 966 } 967 968 pub fn getFocus(self: *View) *Focus { 969 return self.box.getFocus(); 970 } 971 972 fn contentAtTop(self: *View) bool { 973 if (self.detailActive()) { 974 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 975 return self.navBox().children.count() == 0 and 976 (!self.session.is_terminal or self.detailScroll().y == 0); 977 } 978 const lb = self.listBox(); 979 const cid = lb.getFocus().child_id orelse return true; 980 return lb.children.getIndex(cid) == 0; 981 } 982 983 // only the header's own row sits directly below the repository header. 984 pub fn atTop(self: *View) bool { 985 if (self.data.no_commits) return true; 986 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 987 return self.headerActive() or (!focusable and self.contentAtTop()); 988 } 989 990 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 991 const header_view = self.header() orelse return false; 992 return header_view.focusHeader(root_focus); 993 } 994 }; 995 996 // `lines` as spans, each prefixed with its 1-based file line number 997 // (right-aligned in a column wide enough for the last number, then a space), 998 // like the diffs view. each number links to the window starting at its line. 999 // `start` is the window's 0-based first line, so numbers continue across 1000 // paginated windows. 1001 fn numberedContent(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, lines: []const []const u8, start: usize) ![]const wgt.Span { 1002 const arena = page_arena.allocator(); 1003 const width = std.fmt.count("{d}", .{start + lines.len}); 1004 var spans: std.ArrayList(wgt.Span) = .empty; 1005 for (lines, 0..) |line, i| { 1006 const number = start + i + 1; 1007 const route = data.filesRoute(path, number) orelse return error.RouteTooLong; 1008 try spans.append(arena, .{ .text = try arena.print("{d: >[1]}", .{ number, width }), .link = try route.toUrl(page_arena) }); 1009 try spans.append(arena, .{ .text = try arena.print(" {s}{s}", .{ line, if (i + 1 < lines.len) "\n" else "" }) }); 1010 } 1011 return spans.toOwnedSlice(arena); 1012 } 1013 1014 // the index of the "README"/"README.md" file entry (case-insensitive), or null 1015 // if none. used to pick the default selection at the repo root. 1016 fn readmeIndex(entries: []const Entry) ?usize { 1017 for (entries, 0..) |entry, i| { 1018 if (!entry.is_dir and isReadme(entry.name)) return i; 1019 } 1020 return null; 1021 } 1022 1023 fn isMarkdown(name: []const u8) bool { 1024 const extension = std.Io.Dir.path.extension(name); 1025 return std.ascii.eqlIgnoreCase(extension, ".md") or std.ascii.eqlIgnoreCase(extension, ".markdown"); 1026 } 1027 1028 fn isReadme(name: []const u8) bool { 1029 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 1030 } 1031 1032 // the index of the entry the route named (data.selected_file), or null if none. 1033 fn selectedFileIndex(data: *const Self) ?usize { 1034 const name = data.selected_file orelse return null; 1035 for (data.entries, 0..) |entry, i| { 1036 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 1037 } 1038 return null; 1039 } 1040 1041 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 1042 // following it navigates to that directory's listing. 1043 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 1044 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 1045 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1046 } 1047 1048 // a link to the file's route. a preloaded file gets an "ai:" link 1049 // (crossPageLink ignores it, so a click selects the row in place and shows its 1050 // contents); an unloaded file gets an "a:" link, so activating it reloads the 1051 // page with that file selected and its content read. 1052 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 1053 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 1054 const prefix = if (loaded) "ai:" else "a:"; 1055 return page_arena.allocator().print("{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 1056 } 1057 1058 // the url `line` value for a content window: its first line's 1-based number, 1059 // except the first window, which serializes as unset (0). 1060 fn lineNumber(window_start: usize) usize { 1061 return if (window_start == 0) 0 else window_start + 1; 1062 } 1063 1064 // `dir`/`name`, or just `name` at the root. 1065 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 1066 return if (dir.len == 0) name else arena.print("{s}/{s}", .{ dir, name }); 1067 } 1068 1069 // `dir` with its last segment removed ("" when `dir` has a single segment). 1070 fn parentDir(dir: []const u8) []const u8 { 1071 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 1072 return dir[0..slash]; 1073 } 1074 1075 // a search result's bottom label: the directory it sits in, empty at the root. 1076 // a directory too wide for the list column is clipped from the left. 1077 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 1078 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 1079 const dir = path[0..slash]; 1080 // the row's border and the label's padding take two columns each 1081 const max_width = View.list_max_width - 4; 1082 var width = try xitui.width.displayWidth(dir); 1083 if (width <= max_width) return aa.print(" {s} ", .{dir}); 1084 1085 const ellipsis = ".."; 1086 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1087 var start: usize = 0; 1088 while (width > max_width - ellipsis.len) { 1089 const slice = iter.nextCodepointSlice() orelse break; 1090 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1091 start += slice.len; 1092 } 1093 return aa.print(" " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1094 } 1095