╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
869 root_focus.setFocus(frame.getFocus().id); 870 } 871 872 // return to the list. 873 fn focusList(self: *View, root_focus: *Focus) !void { 874 root_focus.setFocus(self.listScroll().getFocus().id); 875 } 876 877 pub fn clearGrid(self: *View) void { 878 self.box.clearGrid(); 879 } 880 881 pub fn getGrid(self: View) ?Grid { 882 return self.box.getGrid(); 883 } 884 885 pub fn getFocus(self: *View) *Focus { 886 return self.box.getFocus(); 887 } 888 889 fn contentAtTop(self: *View) bool { 890 if (self.detailActive()) { 891 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 892 return self.navBox().children.count() == 0 and 893 (!self.session.is_terminal or self.detailScroll().y == 0); 894 } 895 const lb = self.listBox(); 896 const cid = lb.getFocus().child_id orelse return true; 897 return lb.children.getIndex(cid) == 0; 898 } 899 900 // only the header's own row sits directly below the repository header. 901 pub fn atTop(self: *View) bool { 902 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 903 return self.headerActive() or (!focusable and self.contentAtTop()); 904 } 905 906 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 907 const header_view = self.header() orelse return false; 908 return header_view.focusHeader(root_focus); 909 } 910 }; 911 912 // join `lines` into one string, each prefixed with its 1-based file line number 913 // (right-aligned in a column wide enough for the last number, then a space), 914 // like the diffs view. `start` is the window's 0-based first line, so numbers 915 // continue across paginated windows. 916 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 917 if (lines.len == 0) return ""; 918 const width = std.fmt.count("{d}", .{start + lines.len}); 919 const indent = try arena.alloc(u8, width); 920 @memset(indent, ' '); 921 922 var out: std.ArrayList(u8) = .empty; 923 for (lines, 0..) |line, i| { 924 if (i != 0) try out.append(arena, '\n'); 925 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 926 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 927 try out.appendSlice(arena, num_str); 928 try out.append(arena, ' '); 929 try out.appendSlice(arena, line); 930 } 931 return out.toOwnedSlice(arena); 932 } 933 934 // the index of the "README"/"README.md" file entry (case-insensitive), or null 935 // if none. used to pick the default selection at the repo root. 936 fn readmeIndex(entries: []const Entry) ?usize { 937 for (entries, 0..) |entry, i| { 938 if (!entry.is_dir and isReadme(entry.name)) return i; 939 } 940 return null; 941 } 942 943 fn isReadme(name: []const u8) bool { 944 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 945 } 946 947 // the index of the entry the route named (data.selected_file), or null if none. 948 fn selectedFileIndex(data: *const Self) ?usize { 949 const name = data.selected_file orelse return null; 950 for (data.entries, 0..) |entry, i| { 951 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 952 } 953 return null; 954 } 955 956 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 957 // following it navigates to that directory's listing. 958 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 959 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 960 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 961 } 962 963 // a link to the file's route. a preloaded file gets an "ai:" link 964 // (crossPageLink ignores it, so a click selects the row in place and shows its 965 // contents); an unloaded file gets an "a:" link, so activating it reloads the 966 // page with that file selected and its content read. 967 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 968 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 969 const prefix = if (loaded) "ai:" else "a:"; 970 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 971 } 972 973 // the url `line` value for a content window: its first line's 1-based number, 974 // except the first window, which serializes as unset (0). 975 fn lineNumber(window_start: usize) usize { 976 return if (window_start == 0) 0 else window_start + 1; 977 } 978 979 // `dir`/`name`, or just `name` at the root. 980 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 981 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 982 } 983 984 // `dir` with its last segment removed ("" when `dir` has a single segment). 985 fn parentDir(dir: []const u8) []const u8 { 986 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 987 return dir[0..slash]; 988 } 989 990 // a search result's bottom label: the directory it sits in, empty at the root. 991 // a directory too wide for the list column is clipped from the left. 992 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 993 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 994 const dir = path[0..slash]; 995 // the row's border and the label's padding take two columns each 996 const max_width = View.list_max_width - 4; 997 var width = try xitui.width.displayWidth(dir); 998 if (width <= max_width) return std.fmt.allocPrint(aa, " {s} ", .{dir}); 999 1000 const ellipsis = ".."; 1001 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1002 var start: usize = 0; 1003 while (width > max_width - ellipsis.len) { 1004 const slice = iter.nextCodepointSlice() orelse break; 1005 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1006 start += slice.len; 1007 } 1008 return std.fmt.allocPrint(aa, " " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1009 } 1010