╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
778 self.detailScroll().clampToContent(); 779 } else try self.focusList(root_focus); 780 } else if (!self.moveNav(root_focus, -1)) { 781 try self.focusList(root_focus); 782 } 783 }, 784 .arrow_right => { 785 if (on_content) { 786 sc.x += 1; 787 self.detailScroll().clampToContent(); 788 } else _ = self.moveNav(root_focus, 1); 789 }, 790 .arrow_up => { 791 if (on_content) { 792 if (self.session.is_terminal and sc.y > 0) { 793 sc.y -= 1; 794 self.detailScroll().clampToContent(); 795 } else self.focusNav(root_focus); // cross up to the links 796 } 797 }, 798 .arrow_down => { 799 if (on_content) { 800 if (self.session.is_terminal) { 801 sc.y += 1; 802 self.detailScroll().clampToContent(); 803 } 804 } else self.focusContent(root_focus); // links -> content 805 }, 806 .page_up => if (on_content and self.session.is_terminal) { 807 sc.y -= 10; 808 self.detailScroll().clampToContent(); 809 }, 810 .page_down => if (on_content and self.session.is_terminal) { 811 sc.y += 10; 812 self.detailScroll().clampToContent(); 813 }, 814 .home => if (on_content and self.session.is_terminal) { 815 sc.y = 0; 816 self.detailScroll().clampToContent(); 817 }, 818 .end => if (on_content and self.session.is_terminal) { 819 sc.y = std.math.maxInt(isize); 820 self.detailScroll().clampToContent(); 821 }, 822 .mouse => |mouse| switch (mouse.action) { 823 .scroll => |dir| if (on_content and self.session.is_terminal) { 824 sc.y += if (dir == .up) -5 else 5; 825 self.detailScroll().clampToContent(); 826 }, 827 else => {}, 828 }, 829 else => {}, 830 } 831 } 832 833 // focus the first (left-most) nav link; a no-op when there are none, leaving 834 // focus on the content. 835 fn focusNav(self: *View, root_focus: *Focus) void { 836 const nav_box = self.navBox(); 837 if (nav_box.children.count() == 0) return; 838 root_focus.setFocus(nav_box.children.keys()[0]); 839 } 840 841 fn focusContent(self: *View, root_focus: *Focus) void { 842 const inner = self.detailInner(); 843 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 844 root_focus.setFocus(id); 845 } 846 847 // move focus `delta` rows within the nav box; returns false (without moving) 848 // when that would step off either end. 849 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 850 const nav_box = self.navBox(); 851 const keys = nav_box.children.keys(); 852 const cur_id = nav_box.getFocus().child_id orelse return false; 853 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 854 const target = cur + delta; 855 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 856 root_focus.setFocus(keys[@intCast(target)]); 857 return true; 858 } 859 860 // enter the detail pane, landing on the "next" link when there is one (so 861 // right-arrow reaches it), else on the content. a directory or ".." row has 862 // neither, so there's nothing to enter. selecting the frame's child (then 863 // focusing the frame) also handles the too-narrow case where the pane isn't 864 // laid out yet — focus lands inside it after the next build. 865 fn focusDetail(self: *View, root_focus: *Focus) !void { 866 const frame = self.detailOuter(); 867 if (self.navBox().children.count() > 0) { 868 frame.getFocus().child_id = self.navBox().getFocus().id; 869 } else if (self.detailInner().children.count() > 0) { 870 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 871 } else return; 872 root_focus.setFocus(frame.getFocus().id); 873 } 874 875 // return to the list. 876 fn focusList(self: *View, root_focus: *Focus) !void { 877 root_focus.setFocus(self.listScroll().getFocus().id); 878 } 879 880 pub fn clearGrid(self: *View) void { 881 self.box.clearGrid(); 882 } 883 884 pub fn getGrid(self: View) ?Grid { 885 return self.box.getGrid(); 886 } 887 888 pub fn getFocus(self: *View) *Focus { 889 return self.box.getFocus(); 890 } 891 892 fn contentAtTop(self: *View) bool { 893 if (self.detailActive()) { 894 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 895 return self.navBox().children.count() == 0 and 896 (!self.session.is_terminal or self.detailScroll().y == 0); 897 } 898 const lb = self.listBox(); 899 const cid = lb.getFocus().child_id orelse return true; 900 return lb.children.getIndex(cid) == 0; 901 } 902 903 // only the header's own row sits directly below the repository header. 904 pub fn atTop(self: *View) bool { 905 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 906 return self.headerActive() or (!focusable and self.contentAtTop()); 907 } 908 909 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 910 const header_view = self.header() orelse return false; 911 return header_view.focusHeader(root_focus); 912 } 913 }; 914 915 // join `lines` into one string, each prefixed with its 1-based file line number 916 // (right-aligned in a column wide enough for the last number, then a space), 917 // like the diffs view. `start` is the window's 0-based first line, so numbers 918 // continue across paginated windows. 919 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 920 if (lines.len == 0) return ""; 921 const width = std.fmt.count("{d}", .{start + lines.len}); 922 const indent = try arena.alloc(u8, width); 923 @memset(indent, ' '); 924 925 var out: std.ArrayList(u8) = .empty; 926 for (lines, 0..) |line, i| { 927 if (i != 0) try out.append(arena, '\n'); 928 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 929 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 930 try out.appendSlice(arena, num_str); 931 try out.append(arena, ' '); 932 try out.appendSlice(arena, line); 933 } 934 return out.toOwnedSlice(arena); 935 } 936 937 // the index of the "README"/"README.md" file entry (case-insensitive), or null 938 // if none. used to pick the default selection at the repo root. 939 fn readmeIndex(entries: []const Entry) ?usize { 940 for (entries, 0..) |entry, i| { 941 if (!entry.is_dir and isReadme(entry.name)) return i; 942 } 943 return null; 944 } 945 946 fn isReadme(name: []const u8) bool { 947 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 948 } 949 950 // the index of the entry the route named (data.selected_file), or null if none. 951 fn selectedFileIndex(data: *const Self) ?usize { 952 const name = data.selected_file orelse return null; 953 for (data.entries, 0..) |entry, i| { 954 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 955 } 956 return null; 957 } 958 959 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 960 // following it navigates to that directory's listing. 961 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 962 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 963 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 964 } 965 966 // a link to the file's route. a preloaded file gets an "ai:" link 967 // (crossPageLink ignores it, so a click selects the row in place and shows its 968 // contents); an unloaded file gets an "a:" link, so activating it reloads the 969 // page with that file selected and its content read. 970 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 971 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 972 const prefix = if (loaded) "ai:" else "a:"; 973 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 974 } 975 976 // the url `line` value for a content window: its first line's 1-based number, 977 // except the first window, which serializes as unset (0). 978 fn lineNumber(window_start: usize) usize { 979 return if (window_start == 0) 0 else window_start + 1; 980 } 981 982 // `dir`/`name`, or just `name` at the root. 983 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 984 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 985 } 986 987 // `dir` with its last segment removed ("" when `dir` has a single segment). 988 fn parentDir(dir: []const u8) []const u8 { 989 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 990 return dir[0..slash]; 991 } 992 993 // a search result's bottom label: the directory it sits in, empty at the root. 994 // a directory too wide for the list column is clipped from the left. 995 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 996 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 997 const dir = path[0..slash]; 998 // the row's border and the label's padding take two columns each 999 const max_width = View.list_max_width - 4; 1000 var width = try xitui.width.displayWidth(dir); 1001 if (width <= max_width) return std.fmt.allocPrint(aa, " {s} ", .{dir}); 1002 1003 const ellipsis = ".."; 1004 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1005 var start: usize = 0; 1006 while (width > max_width - ellipsis.len) { 1007 const slice = iter.nextCodepointSlice() orelse break; 1008 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1009 start += slice.len; 1010 } 1011 return std.fmt.allocPrint(aa, " " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1012 } 1013