╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
883 return; 884 }, 885 .none => {}, 886 } 887 switch (key) { 888 .arrow_left => { 889 if (on_content) { 890 if (sc.x > 0) { 891 sc.x -= 1; 892 self.detailScroll().clampToContent(); 893 } else try self.focusList(root_focus); 894 } else if (!self.moveNav(root_focus, -1)) { 895 try self.focusList(root_focus); 896 } 897 }, 898 .arrow_right => { 899 if (on_content) { 900 sc.x += 1; 901 self.detailScroll().clampToContent(); 902 } else _ = self.moveNav(root_focus, 1); 903 }, 904 .page_up => if (on_content and self.session.is_terminal) { 905 sc.y -= 10; 906 self.detailScroll().clampToContent(); 907 }, 908 .page_down => if (on_content and self.session.is_terminal) { 909 sc.y += 10; 910 self.detailScroll().clampToContent(); 911 }, 912 .home => if (on_content and self.session.is_terminal) { 913 sc.y = 0; 914 self.detailScroll().clampToContent(); 915 }, 916 .end => if (on_content and self.session.is_terminal) { 917 sc.y = std.math.maxInt(isize); 918 self.detailScroll().clampToContent(); 919 }, 920 // step between the rendered markdown's links; the web leaves tab to the browser 921 .tab, .back_tab => if (on_content and self.session.is_terminal) if (self.markdownView()) |view| { 922 try view.stepLink(allocator, root_focus, sc, 0, 0, key == .tab); 923 }, 924 else => {}, 925 } 926 } 927 928 // focus the first (left-most) nav link; a no-op when there are none, leaving 929 // focus on the content. 930 fn focusNav(self: *View, root_focus: *Focus) void { 931 const nav_box = self.navBox(); 932 if (nav_box.children.count() == 0) return; 933 root_focus.setFocus(nav_box.children.keys()[0]); 934 } 935 936 fn focusContent(self: *View, root_focus: *Focus) void { 937 if (self.markdownView()) |view| return root_focus.setFocus(view.body.id); 938 const inner = self.detailInner(); 939 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 940 root_focus.setFocus(id); 941 } 942 943 // move focus `delta` rows within the nav box; returns false (without moving) 944 // when that would step off either end. 945 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 946 const nav_box = self.navBox(); 947 const keys = nav_box.children.keys(); 948 const cur_id = nav_box.getFocus().child_id orelse return false; 949 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 950 const target = cur + delta; 951 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 952 root_focus.setFocus(keys[@intCast(target)]); 953 return true; 954 } 955 956 // enter the detail pane, landing on the "next" link when there is one (so 957 // right-arrow reaches it), else on the content. a directory or ".." row has 958 // neither, so there's nothing to enter. selecting the frame's child (then 959 // focusing the frame) also handles the too-narrow case where the pane isn't 960 // laid out yet — focus lands inside it after the next build. 961 fn focusDetail(self: *View, root_focus: *Focus) !void { 962 const frame = self.detailOuter(); 963 if (self.navBox().children.count() > 0) { 964 frame.getFocus().child_id = self.navBox().getFocus().id; 965 } else if (self.detailInner().children.count() > 0) { 966 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 967 } else return; 968 root_focus.setFocus(frame.getFocus().id); 969 } 970 971 // return to the list. 972 fn focusList(self: *View, root_focus: *Focus) !void { 973 root_focus.setFocus(self.listScroll().getFocus().id); 974 } 975 976 pub fn clearGrid(self: *View) void { 977 self.box.clearGrid(); 978 } 979 980 pub fn getGrid(self: View) ?Grid { 981 return self.box.getGrid(); 982 } 983 984 pub fn getFocus(self: *View) *Focus { 985 return self.box.getFocus(); 986 } 987 988 fn contentAtTop(self: *View) bool { 989 if (self.detailActive()) { 990 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 991 return self.navBox().children.count() == 0 and 992 (!self.session.is_terminal or self.detailScroll().y == 0); 993 } 994 const lb = self.listBox(); 995 const cid = lb.getFocus().child_id orelse return true; 996 return lb.children.getIndex(cid) == 0; 997 } 998 999 // only the header's own row sits directly below the repository header. 1000 pub fn atTop(self: *View) bool { 1001 if (self.data.no_commits or self.data.isEmpty()) return true; 1002 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 1003 return self.headerActive() or (!focusable and self.contentAtTop()); 1004 } 1005 1006 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 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