╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
752 } 753 754 // a bordered button in the nav row carrying `link`, returning its focus id 755 fn addNavButton(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !usize { 756 var tb = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 757 errdefer tb.deinit(allocator); 758 tb.getFocus().mode = .all; 759 tb.getFocus().kind = .{ .custom = link }; 760 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 761 return tb.getFocus().id; 762 } 763 764 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 765 if (self.data.no_commits) return self.box.input(allocator, key, root_focus); 766 // scrolling crosses the sub header boundary the same way arrows do 767 const direction = inp.vertDirection(key); 768 if (self.headerActive()) { 769 // active means there is one 770 const header_view = self.header() orelse unreachable; 771 if (direction == .down) { 772 root_focus.setFocus(self.contentBox().getFocus().id); 773 return; 774 } 775 if (key == .enter) if (try header_view.submittedText(allocator)) |term| { 776 defer allocator.free(term); 777 return self.submit(term); 778 }; 779 return header_view.input(allocator, key, root_focus); 780 } 781 if (direction == .up and self.contentAtTop() and self.focusHeader(root_focus)) return; 782 if (self.detailActive()) { 783 try self.detailInput(allocator, key, root_focus); 784 } else { 785 try self.listInput(allocator, key, root_focus); 786 } 787 } 788 789 // navigate to the typed term's results, pinned to the listing's ref. an 790 // empty term leaves the results for the plain listing. 791 fn submit(self: *View, term: []const u8) !void { 792 if (term.len == 0 and self.data.find == null) return; 793 const route = self.data.filesRoute("", 0) orelse return; 794 try self.session.navigate(route.withFind(term) orelse return); 795 } 796 797 fn listInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 798 // up/down (and the scroll wheel) move the selection; page up/down jump a 799 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 800 // directory (or "..") row are turned into navigation by the host 801 // (crossPageLink) before reaching here. 802 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 803 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 804 return; 805 } 806 switch (key) { 807 .enter, .arrow_right => try self.focusDetail(root_focus), 808 // when the window is too narrow to lay out the detail pane beside 809 // the list, a click on a row opens it like enter. the row was just 810 // selected, so the detail is swapped to it ahead of the build. 811 .mouse => |mouse| if (self.contentBox().children.values()[detail_index].rect == null and ui.widget.clickOnSelectedRow(self.listBox(), root_focus, mouse)) { 812 try self.refreshDetail(allocator, true); 813 try self.focusDetail(root_focus); 814 }, 815 else => {}, 816 } 817 } 818 819 // whether focus is on the content box (vs. a "previous"/"next" nav link). 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 (inp.activated(root_focus, id, key)) 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 = switch (key) { 849 .scroll_up, .scroll_down => 5, 850 else => 1, 851 }; 852 switch (inp.vertDirection(key)) { 853 .up => { 854 if (!on_content) return; 855 if (self.session.is_terminal and sc.y > 0) { 856 sc.y -= step; 857 self.detailScroll().clampToContent(); 858 } else self.focusNav(root_focus); 859 return; 860 }, 861 .down => { 862 if (!on_content) return self.focusContent(root_focus); 863 if (self.session.is_terminal) { 864 sc.y += step; 865 self.detailScroll().clampToContent(); 866 } 867 return; 868 }, 869 .none => {}, 870 } 871 switch (key) { 872 .arrow_left => { 873 if (on_content) { 874 if (sc.x > 0) { 875 sc.x -= 1; 876 self.detailScroll().clampToContent(); 877 } else try self.focusList(root_focus); 878 } else if (!self.moveNav(root_focus, -1)) { 879 try self.focusList(root_focus); 880 } 881 }, 882 .arrow_right => { 883 if (on_content) { 884 sc.x += 1; 885 self.detailScroll().clampToContent(); 886 } else _ = self.moveNav(root_focus, 1); 887 }, 888 .page_up => if (on_content and self.session.is_terminal) { 889 sc.y -= 10; 890 self.detailScroll().clampToContent(); 891 }, 892 .page_down => if (on_content and self.session.is_terminal) { 893 sc.y += 10; 894 self.detailScroll().clampToContent(); 895 }, 896 .home => if (on_content and self.session.is_terminal) { 897 sc.y = 0; 898 self.detailScroll().clampToContent(); 899 }, 900 .end => if (on_content and self.session.is_terminal) { 901 sc.y = std.math.maxInt(isize); 902 self.detailScroll().clampToContent(); 903 }, 904 // step between the rendered markdown's links; the web leaves tab to the browser 905 .tab, .back_tab => if (on_content and self.session.is_terminal) if (self.markdownView()) |view| { 906 try view.stepLink(allocator, root_focus, sc, 0, 0, key == .tab); 907 }, 908 else => {}, 909 } 910 } 911 912 // focus the first (left-most) nav link; a no-op when there are none, leaving 913 // focus on the content. 914 fn focusNav(self: *View, root_focus: *Focus) void { 915 const nav_box = self.navBox(); 916 if (nav_box.children.count() == 0) return; 917 root_focus.setFocus(nav_box.children.keys()[0]); 918 } 919 920 fn focusContent(self: *View, root_focus: *Focus) void { 921 if (self.markdownView()) |view| return root_focus.setFocus(view.body.id); 922 const inner = self.detailInner(); 923 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 924 root_focus.setFocus(id); 925 } 926 927 // move focus `delta` rows within the nav box; returns false (without moving) 928 // when that would step off either end. 929 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 930 const nav_box = self.navBox(); 931 const keys = nav_box.children.keys(); 932 const cur_id = nav_box.getFocus().child_id orelse return false; 933 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 934 const target = cur + delta; 935 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 936 root_focus.setFocus(keys[@intCast(target)]); 937 return true; 938 } 939 940 // enter the detail pane, landing on the "next" link when there is one (so 941 // right-arrow reaches it), else on the content. a directory or ".." row has 942 // neither, so there's nothing to enter. selecting the frame's child (then 943 // focusing the frame) also handles the too-narrow case where the pane isn't 944 // laid out yet — focus lands inside it after the next build. 945 fn focusDetail(self: *View, root_focus: *Focus) !void { 946 const frame = self.detailOuter(); 947 if (self.navBox().children.count() > 0) { 948 frame.getFocus().child_id = self.navBox().getFocus().id; 949 } else if (self.detailInner().children.count() > 0) { 950 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 951 } else return; 952 root_focus.setFocus(frame.getFocus().id); 953 } 954 955 // return to the list. 956 fn focusList(self: *View, root_focus: *Focus) !void { 957 root_focus.setFocus(self.listScroll().getFocus().id); 958 } 959 960 pub fn clearGrid(self: *View) void { 961 self.box.clearGrid(); 962 } 963 964 pub fn getGrid(self: View) ?Grid { 965 return self.box.getGrid(); 966 } 967 968 pub fn getFocus(self: *View) *Focus { 969 return self.box.getFocus(); 970 } 971 972 fn contentAtTop(self: *View) bool { 973 if (self.detailActive()) { 974 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 975 return self.navBox().children.count() == 0 and 976 (!self.session.is_terminal or self.detailScroll().y == 0); 977 } 978 const lb = self.listBox(); 979 const cid = lb.getFocus().child_id orelse return true; 980 return lb.children.getIndex(cid) == 0; 981 } 982 983 // only the header's own row sits directly below the repository header. 984 pub fn atTop(self: *View) bool { 985 if (self.data.no_commits) return true; 986 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 987 return self.headerActive() or (!focusable and self.contentAtTop()); 988 } 989 990 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 991 const header_view = self.header() orelse return false; 992 return header_view.focusHeader(root_focus); 993 } 994 }; 995 996 // `lines` as spans, each prefixed with its 1-based file line number 997 // (right-aligned in a column wide enough for the last number, then a space), 998 // like the diffs view. each number links to the window starting at its line. 999 // `start` is the window's 0-based first line, so numbers continue across 1000 // paginated windows. 1001 fn numberedContent(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, lines: []const []const u8, start: usize) ![]const wgt.Span { 1002 const arena = page_arena.allocator(); 1003 const width = std.fmt.count("{d}", .{start + lines.len}); 1004 var spans: std.ArrayList(wgt.Span) = .empty; 1005 for (lines, 0..) |line, i| { 1006 const number = start + i + 1; 1007 const route = data.filesRoute(path, number) orelse return error.RouteTooLong; 1008 try spans.append(arena, .{ .text = try arena.print("{d: >[1]}", .{ number, width }), .link = try route.toUrl(page_arena) }); 1009 try spans.append(arena, .{ .text = try arena.print(" {s}{s}", .{ line, if (i + 1 < lines.len) "\n" else "" }) }); 1010 } 1011 return spans.toOwnedSlice(arena); 1012 } 1013 1014 // the index of the "README"/"README.md" file entry (case-insensitive), or null 1015 // if none. used to pick the default selection at the repo root. 1016 fn readmeIndex(entries: []const Entry) ?usize { 1017 for (entries, 0..) |entry, i| { 1018 if (!entry.is_dir and isReadme(entry.name)) return i; 1019 } 1020 return null; 1021 } 1022 1023 fn isMarkdown(name: []const u8) bool { 1024 const extension = std.Io.Dir.path.extension(name); 1025 return std.ascii.eqlIgnoreCase(extension, ".md") or std.ascii.eqlIgnoreCase(extension, ".markdown"); 1026 } 1027 1028 fn isReadme(name: []const u8) bool { 1029 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 1030 } 1031 1032 // the index of the entry the route named (data.selected_file), or null if none. 1033 fn selectedFileIndex(data: *const Self) ?usize { 1034 const name = data.selected_file orelse return null; 1035 for (data.entries, 0..) |entry, i| { 1036 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 1037 } 1038 return null; 1039 } 1040 1041 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 1042 // following it navigates to that directory's listing. 1043 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 1044 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 1045 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1046 } 1047 1048 // a link to the file's route. a preloaded file gets an "ai:" link 1049 // (crossPageLink ignores it, so a click selects the row in place and shows its 1050 // contents); an unloaded file gets an "a:" link, so activating it reloads the 1051 // page with that file selected and its content read. 1052 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 1053 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 1054 const prefix = if (loaded) "ai:" else "a:"; 1055 return page_arena.allocator().print("{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 1056 } 1057 1058 // the url `line` value for a content window: its first line's 1-based number, 1059 // except the first window, which serializes as unset (0). 1060 fn lineNumber(window_start: usize) usize { 1061 return if (window_start == 0) 0 else window_start + 1; 1062 } 1063 1064 // `dir`/`name`, or just `name` at the root. 1065 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 1066 return if (dir.len == 0) name else arena.print("{s}/{s}", .{ dir, name }); 1067 } 1068 1069 // `dir` with its last segment removed ("" when `dir` has a single segment). 1070 fn parentDir(dir: []const u8) []const u8 { 1071 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 1072 return dir[0..slash]; 1073 } 1074 1075 // a search result's bottom label: the directory it sits in, empty at the root. 1076 // a directory too wide for the list column is clipped from the left. 1077 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 1078 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 1079 const dir = path[0..slash]; 1080 // the row's border and the label's padding take two columns each 1081 const max_width = View.list_max_width - 4; 1082 var width = try xitui.width.displayWidth(dir); 1083 if (width <= max_width) return aa.print(" {s} ", .{dir}); 1084 1085 const ellipsis = ".."; 1086 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1087 var start: usize = 0; 1088 while (width > max_width - ellipsis.len) { 1089 const slice = iter.nextCodepointSlice() orelse break; 1090 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1091 start += slice.len; 1092 } 1093 return aa.print(" " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1094 } 1095