╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
779 for (entries, 0..) |entry, i| { 780 if (!entry.is_dir and isReadme(entry.name)) return i; 781 } 782 return null; 783 } 784 785 fn isReadme(name: []const u8) bool { 786 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 787 } 788 789 // the index of the entry the route named (data.selected_file), or null if none. 790 fn selectedFileIndex(data: *const Self) ?usize { 791 const name = data.selected_file orelse return null; 792 for (data.entries, 0..) |entry, i| { 793 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 794 } 795 return null; 796 } 797 798 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 799 // following it navigates to that directory's listing. 800 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 801 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 802 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 803 } 804 805 // a link to the file's route. a preloaded file gets an "ai:" link 806 // (crossPageLink ignores it, so a click selects the row in place and shows its 807 // contents); an unloaded file gets an "a:" link, so activating it reloads the 808 // page with that file selected and its content read. 809 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool) ![]const u8 { 810 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 811 const prefix = if (loaded) "ai:" else "a:"; 812 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 813 } 814 815 // the url `line` value for a content window: its first line's 1-based number, 816 // except the first window, which serializes as unset (0). 817 fn lineNumber(window_start: usize) usize { 818 return if (window_start == 0) 0 else window_start + 1; 819 } 820 821 // `dir`/`name`, or just `name` at the root. 822 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 823 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 824 } 825 826 // `dir` with its last segment removed ("" when `dir` has a single segment). 827 fn parentDir(dir: []const u8) []const u8 { 828 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 829 return dir[0..slash]; 830 } 831 832 // the "viewing <ref_or_oid> <value>" banner shown above the listing. 833 pub const Header = struct { 834 content: []const u8, 835 836 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 837 return .{ 838 .content = try std.fmt.allocPrint(aa, "viewing {s} {s}", .{ @tagName(ref_or_oid), value }), 839 }; 840 } 841 842 pub const View = struct { 843 box: wgt.Box(ui.Widget), 844 data: *const Header, 845 846 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session) !Header.View { 847 _ = session; 848 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 849 errdefer box.deinit(allocator); 850 851 var text_box = try wgt.TextBox(ui.Widget).init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 852 errdefer text_box.deinit(allocator); 853 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 854 855 return .{ .box = box, .data = data }; 856 } 857 858 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 859 self.box.deinit(allocator); 860 } 861 862 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 863 self.clearGrid(); 864 try self.box.build(allocator, constraint, root_focus); 865 } 866 867 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 868 _ = self; 869 _ = allocator; 870 _ = key; 871 _ = root_focus; 872 } 873 874 pub fn clearGrid(self: *Header.View) void { 875 self.box.clearGrid(); 876 } 877 878 pub fn getGrid(self: Header.View) ?Grid { 879 return self.box.getGrid(); 880 } 881 882 pub fn getFocus(self: *Header.View) *Focus { 883 return self.box.getFocus(); 884 } 885 }; 886 }; 887