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