╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
862 }, 863 .mouse => |mouse| switch (mouse.action) { 864 .scroll => |dir| if (on_content and self.session.is_terminal) { 865 sc.y += if (dir == .up) -5 else 5; 866 self.detailScroll().clampToContent(); 867 }, 868 else => {}, 869 }, 870 // step between the rendered markdown's links; the web leaves tab to the browser 871 .tab, .back_tab => if (on_content and self.session.is_terminal) if (self.markdownView()) |view| { 872 try view.stepLink(allocator, root_focus, sc, key == .tab); 873 }, 874 else => {}, 875 } 876 } 877 878 // focus the first (left-most) nav link; a no-op when there are none, leaving 879 // focus on the content. 880 fn focusNav(self: *View, root_focus: *Focus) void { 881 const nav_box = self.navBox(); 882 if (nav_box.children.count() == 0) return; 883 root_focus.setFocus(nav_box.children.keys()[0]); 884 } 885 886 fn focusContent(self: *View, root_focus: *Focus) void { 887 if (self.markdownView()) |view| return root_focus.setFocus(view.body.id); 888 const inner = self.detailInner(); 889 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 890 root_focus.setFocus(id); 891 } 892 893 // move focus `delta` rows within the nav box; returns false (without moving) 894 // when that would step off either end. 895 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 896 const nav_box = self.navBox(); 897 const keys = nav_box.children.keys(); 898 const cur_id = nav_box.getFocus().child_id orelse return false; 899 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 900 const target = cur + delta; 901 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 902 root_focus.setFocus(keys[@intCast(target)]); 903 return true; 904 } 905 906 // enter the detail pane, landing on the "next" link when there is one (so 907 // right-arrow reaches it), else on the content. a directory or ".." row has 908 // neither, so there's nothing to enter. selecting the frame's child (then 909 // focusing the frame) also handles the too-narrow case where the pane isn't 910 // laid out yet — focus lands inside it after the next build. 911 fn focusDetail(self: *View, root_focus: *Focus) !void { 912 const frame = self.detailOuter(); 913 if (self.navBox().children.count() > 0) { 914 frame.getFocus().child_id = self.navBox().getFocus().id; 915 } else if (self.detailInner().children.count() > 0) { 916 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 917 } else return; 918 root_focus.setFocus(frame.getFocus().id); 919 } 920 921 // return to the list. 922 fn focusList(self: *View, root_focus: *Focus) !void { 923 root_focus.setFocus(self.listScroll().getFocus().id); 924 } 925 926 pub fn clearGrid(self: *View) void { 927 self.box.clearGrid(); 928 } 929 930 pub fn getGrid(self: View) ?Grid { 931 return self.box.getGrid(); 932 } 933 934 pub fn getFocus(self: *View) *Focus { 935 return self.box.getFocus(); 936 } 937 938 fn contentAtTop(self: *View) bool { 939 if (self.detailActive()) { 940 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 941 return self.navBox().children.count() == 0 and 942 (!self.session.is_terminal or self.detailScroll().y == 0); 943 } 944 const lb = self.listBox(); 945 const cid = lb.getFocus().child_id orelse return true; 946 return lb.children.getIndex(cid) == 0; 947 } 948 949 // only the header's own row sits directly below the repository header. 950 pub fn atTop(self: *View) bool { 951 if (self.data.no_commits) return true; 952 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 953 return self.headerActive() or (!focusable and self.contentAtTop()); 954 } 955 956 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 957 const header_view = self.header() orelse return false; 958 return header_view.focusHeader(root_focus); 959 } 960 }; 961 962 // join `lines` into one string, each prefixed with its 1-based file line number 963 // (right-aligned in a column wide enough for the last number, then a space), 964 // like the diffs view. `start` is the window's 0-based first line, so numbers 965 // continue across paginated windows. 966 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 967 if (lines.len == 0) return ""; 968 const width = std.fmt.count("{d}", .{start + lines.len}); 969 const indent = try arena.alloc(u8, width); 970 @memset(indent, ' '); 971 972 var out: std.ArrayList(u8) = .empty; 973 for (lines, 0..) |line, i| { 974 if (i != 0) try out.append(arena, '\n'); 975 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 976 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 977 try out.appendSlice(arena, num_str); 978 try out.append(arena, ' '); 979 try out.appendSlice(arena, line); 980 } 981 return out.toOwnedSlice(arena); 982 } 983 984 // the index of the "README"/"README.md" file entry (case-insensitive), or null 985 // if none. used to pick the default selection at the repo root. 986 fn readmeIndex(entries: []const Entry) ?usize { 987 for (entries, 0..) |entry, i| { 988 if (!entry.is_dir and isReadme(entry.name)) return i; 989 } 990 return null; 991 } 992 993 fn isMarkdown(name: []const u8) bool { 994 const extension = std.fs.path.extension(name); 995 return std.ascii.eqlIgnoreCase(extension, ".md") or std.ascii.eqlIgnoreCase(extension, ".markdown"); 996 } 997 998 fn isReadme(name: []const u8) bool { 999 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 1000 } 1001 1002 // the index of the entry the route named (data.selected_file), or null if none. 1003 fn selectedFileIndex(data: *const Self) ?usize { 1004 const name = data.selected_file orelse return null; 1005 for (data.entries, 0..) |entry, i| { 1006 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 1007 } 1008 return null; 1009 } 1010 1011 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 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