╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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