╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
866 self.detailScroll().clampToContent(); 867 }, 868 .mouse => |mouse| switch (mouse.action) { 869 .scroll => |dir| if (on_content and self.session.is_terminal) { 870 sc.y += if (dir == .up) -5 else 5; 871 self.detailScroll().clampToContent(); 872 }, 873 else => {}, 874 }, 875 // step between the rendered markdown's links; the web leaves tab to the browser 876 .tab, .back_tab => if (on_content and self.session.is_terminal) if (self.markdownView()) |view| { 877 try view.stepLink(allocator, root_focus, sc, 0, 0, key == .tab); 878 }, 879 else => {}, 880 } 881 } 882 883 // focus the first (left-most) nav link; a no-op when there are none, leaving 884 // focus on the content. 885 fn focusNav(self: *View, root_focus: *Focus) void { 886 const nav_box = self.navBox(); 887 if (nav_box.children.count() == 0) return; 888 root_focus.setFocus(nav_box.children.keys()[0]); 889 } 890 891 fn focusContent(self: *View, root_focus: *Focus) void { 892 if (self.markdownView()) |view| return root_focus.setFocus(view.body.id); 893 const inner = self.detailInner(); 894 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 895 root_focus.setFocus(id); 896 } 897 898 // move focus `delta` rows within the nav box; returns false (without moving) 899 // when that would step off either end. 900 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 901 const nav_box = self.navBox(); 902 const keys = nav_box.children.keys(); 903 const cur_id = nav_box.getFocus().child_id orelse return false; 904 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 905 const target = cur + delta; 906 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 907 root_focus.setFocus(keys[@intCast(target)]); 908 return true; 909 } 910 911 // enter the detail pane, landing on the "next" link when there is one (so 912 // right-arrow reaches it), else on the content. a directory or ".." row has 913 // neither, so there's nothing to enter. selecting the frame's child (then 914 // focusing the frame) also handles the too-narrow case where the pane isn't 915 // laid out yet — focus lands inside it after the next build. 916 fn focusDetail(self: *View, root_focus: *Focus) !void { 917 const frame = self.detailOuter(); 918 if (self.navBox().children.count() > 0) { 919 frame.getFocus().child_id = self.navBox().getFocus().id; 920 } else if (self.detailInner().children.count() > 0) { 921 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 922 } else return; 923 root_focus.setFocus(frame.getFocus().id); 924 } 925 926 // return to the list. 927 fn focusList(self: *View, root_focus: *Focus) !void { 928 root_focus.setFocus(self.listScroll().getFocus().id); 929 } 930 931 pub fn clearGrid(self: *View) void { 932 self.box.clearGrid(); 933 } 934 935 pub fn getGrid(self: View) ?Grid { 936 return self.box.getGrid(); 937 } 938 939 pub fn getFocus(self: *View) *Focus { 940 return self.box.getFocus(); 941 } 942 943 fn contentAtTop(self: *View) bool { 944 if (self.detailActive()) { 945 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 946 return self.navBox().children.count() == 0 and 947 (!self.session.is_terminal or self.detailScroll().y == 0); 948 } 949 const lb = self.listBox(); 950 const cid = lb.getFocus().child_id orelse return true; 951 return lb.children.getIndex(cid) == 0; 952 } 953 954 // only the header's own row sits directly below the repository header. 955 pub fn atTop(self: *View) bool { 956 if (self.data.no_commits) return true; 957 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 958 return self.headerActive() or (!focusable and self.contentAtTop()); 959 } 960 961 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 962 const header_view = self.header() orelse return false; 963 return header_view.focusHeader(root_focus); 964 } 965 }; 966 967 // `lines` as spans, each prefixed with its 1-based file line number 968 // (right-aligned in a column wide enough for the last number, then a space), 969 // like the diffs view. each number links to the window starting at its line. 970 // `start` is the window's 0-based first line, so numbers continue across 971 // paginated windows. 972 fn numberedContent(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, lines: []const []const u8, start: usize) ![]const wgt.Span { 973 const arena = page_arena.allocator(); 974 const width = std.fmt.count("{d}", .{start + lines.len}); 975 var spans: std.ArrayList(wgt.Span) = .empty; 976 for (lines, 0..) |line, i| { 977 const number = start + i + 1; 978 const route = data.filesRoute(path, number) orelse return error.RouteTooLong; 979 try spans.append(arena, .{ .text = try arena.print("{d: >[1]}", .{ number, width }), .link = try route.toUrl(page_arena) }); 980 try spans.append(arena, .{ .text = try arena.print(" {s}{s}", .{ line, if (i + 1 < lines.len) "\n" else "" }) }); 981 } 982 return spans.toOwnedSlice(arena); 983 } 984 985 // the index of the "README"/"README.md" file entry (case-insensitive), or null 986 // if none. used to pick the default selection at the repo root. 987 fn readmeIndex(entries: []const Entry) ?usize { 988 for (entries, 0..) |entry, i| { 989 if (!entry.is_dir and isReadme(entry.name)) return i; 990 } 991 return null; 992 } 993 994 fn isMarkdown(name: []const u8) bool { 995 const extension = std.Io.Dir.path.extension(name); 996 return std.ascii.eqlIgnoreCase(extension, ".md") or std.ascii.eqlIgnoreCase(extension, ".markdown"); 997 } 998 999 fn isReadme(name: []const u8) bool { 1000 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 1001 } 1002 1003 // the index of the entry the route named (data.selected_file), or null if none. 1004 fn selectedFileIndex(data: *const Self) ?usize { 1005 const name = data.selected_file orelse return null; 1006 for (data.entries, 0..) |entry, i| { 1007 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 1008 } 1009 return null; 1010 } 1011 1012 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 1013 // following it navigates to that directory's listing. 1014 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 1015 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 1016 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1017 } 1018 1019 // a link to the file's route. a preloaded file gets an "ai:" link 1020 // (crossPageLink ignores it, so a click selects the row in place and shows its 1021 // contents); an unloaded file gets an "a:" link, so activating it reloads the 1022 // page with that file selected and its content read. 1023 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 1024 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 1025 const prefix = if (loaded) "ai:" else "a:"; 1026 return page_arena.allocator().print("{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 1027 } 1028 1029 // the url `line` value for a content window: its first line's 1-based number, 1030 // except the first window, which serializes as unset (0). 1031 fn lineNumber(window_start: usize) usize { 1032 return if (window_start == 0) 0 else window_start + 1; 1033 } 1034 1035 // `dir`/`name`, or just `name` at the root. 1036 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 1037 return if (dir.len == 0) name else arena.print("{s}/{s}", .{ dir, name }); 1038 } 1039 1040 // `dir` with its last segment removed ("" when `dir` has a single segment). 1041 fn parentDir(dir: []const u8) []const u8 { 1042 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 1043 return dir[0..slash]; 1044 } 1045 1046 // a search result's bottom label: the directory it sits in, empty at the root. 1047 // a directory too wide for the list column is clipped from the left. 1048 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 1049 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 1050 const dir = path[0..slash]; 1051 // the row's border and the label's padding take two columns each 1052 const max_width = View.list_max_width - 4; 1053 var width = try xitui.width.displayWidth(dir); 1054 if (width <= max_width) return aa.print(" {s} ", .{dir}); 1055 1056 const ellipsis = ".."; 1057 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1058 var start: usize = 0; 1059 while (width > max_width - ellipsis.len) { 1060 const slice = iter.nextCodepointSlice() orelse break; 1061 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1062 start += slice.len; 1063 } 1064 return aa.print(" " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1065 } 1066