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