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