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