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