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