╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
761 return self.box.getGrid(); 762 } 763 764 pub fn getFocus(self: *View) *Focus { 765 return self.box.getFocus(); 766 } 767 768 fn contentAtTop(self: *View) bool { 769 if (self.detailActive()) { 770 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 771 return self.navBox().children.count() == 0 and 772 (!self.session.is_terminal or self.detailScroll().y == 0); 773 } 774 const lb = self.listBox(); 775 const cid = lb.getFocus().child_id orelse return true; 776 return lb.children.getIndex(cid) == 0; 777 } 778 779 // only the clone-url row sits directly below the repository header. 780 pub fn atTop(self: *View) bool { 781 return self.headerActive() or (!self.header().hasCloneUrl() and self.contentAtTop()); 782 } 783 784 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 785 return self.header().focusCloneUrl(root_focus); 786 } 787 }; 788 789 // join `lines` into one string, each prefixed with its 1-based file line number 790 // (right-aligned in a column wide enough for the last number, then a space), 791 // like the diffs view. `start` is the window's 0-based first line, so numbers 792 // continue across paginated windows. 793 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 794 if (lines.len == 0) return ""; 795 const width = std.fmt.count("{d}", .{start + lines.len}); 796 const indent = try arena.alloc(u8, width); 797 @memset(indent, ' '); 798 799 var out: std.ArrayList(u8) = .empty; 800 for (lines, 0..) |line, i| { 801 if (i != 0) try out.append(arena, '\n'); 802 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 803 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 804 try out.appendSlice(arena, num_str); 805 try out.append(arena, ' '); 806 try out.appendSlice(arena, line); 807 } 808 return out.toOwnedSlice(arena); 809 } 810 811 // the index of the "README"/"README.md" file entry (case-insensitive), or null 812 // if none. used to pick the default selection at the repo root. 813 fn readmeIndex(entries: []const Entry) ?usize { 814 for (entries, 0..) |entry, i| { 815 if (!entry.is_dir and isReadme(entry.name)) return i; 816 } 817 return null; 818 } 819 820 fn isReadme(name: []const u8) bool { 821 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 822 } 823 824 // the index of the entry the route named (data.selected_file), or null if none. 825 fn selectedFileIndex(data: *const Self) ?usize { 826 const name = data.selected_file orelse return null; 827 for (data.entries, 0..) |entry, i| { 828 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 829 } 830 return null; 831 } 832 833 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 834 // following it navigates to that directory's listing. 835 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 836 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 837 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 838 } 839 840 // a link to the file's route. a preloaded file gets an "ai:" link 841 // (crossPageLink ignores it, so a click selects the row in place and shows its 842 // contents); an unloaded file gets an "a:" link, so activating it reloads the 843 // page with that file selected and its content read. 844 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 845 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 846 const prefix = if (loaded) "ai:" else "a:"; 847 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 848 } 849 850 // the url `line` value for a content window: its first line's 1-based number, 851 // except the first window, which serializes as unset (0). 852 fn lineNumber(window_start: usize) usize { 853 return if (window_start == 0) 0 else window_start + 1; 854 } 855 856 // `dir`/`name`, or just `name` at the root. 857 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 858 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 859 } 860 861 // `dir` with its last segment removed ("" when `dir` has a single segment). 862 fn parentDir(dir: []const u8) []const u8 { 863 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 864 return dir[0..slash]; 865 } 866 867 // the "<ref_or_oid> <value>" banner shown above the listing. 868 pub const Header = struct { 869 content: []const u8, 870 871 // `value` arrives url-encoded, so decode it for display. 872 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 873 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 874 return .{ 875 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 876 }; 877 } 878 879 pub const View = struct { 880 box: wgt.Box(ui.Widget), 881 data: *const Header, 882 883 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !Header.View { 884 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 885 errdefer box.deinit(allocator); 886 887 if (session.data.host_kind == .server) { 888 if (try ui.widget.CopyableText.initClone(allocator, session, location)) |value| { 889 var clone_url = value; 890 errdefer clone_url.deinit(allocator); 891 const min_width = clone_url.minWidth(); 892 box.getFocus().child_id = clone_url.getFocus().id; 893 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 } }); 894 } 895 } 896 897 var spacer = try ui.widget.Spacer.init(allocator); 898 errdefer spacer.deinit(allocator); 899 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 900 901 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 902 errdefer text_box.deinit(allocator); 903 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 904 905 return .{ .box = box, .data = data }; 906 } 907 908 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 909 self.box.deinit(allocator); 910 } 911 912 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 913 self.clearGrid(); 914 try self.box.build(allocator, constraint, root_focus); 915 } 916 917 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 918 const clone_url = self.cloneUrl() orelse return; 919 try clone_url.input(allocator, key, root_focus); 920 } 921 922 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 923 const clone_url = self.cloneUrl() orelse return false; 924 root_focus.setFocus(clone_url.getFocus().id); 925 return true; 926 } 927 928 pub fn hasCloneUrl(self: *Header.View) bool { 929 return self.cloneUrl() != null; 930 } 931 932 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 933 for (self.box.children.values()) |*child| switch (child.widget) { 934 .copyable_text => |*copyable_text| return copyable_text, 935 else => {}, 936 }; 937 return null; 938 } 939 940 pub fn clearGrid(self: *Header.View) void { 941 self.box.clearGrid(); 942 } 943 944 pub fn getGrid(self: Header.View) ?Grid { 945 return self.box.getGrid(); 946 } 947 948 pub fn getFocus(self: *Header.View) *Focus { 949 return self.box.getFocus(); 950 } 951 }; 952 }; 953