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