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