╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
745 } 746 747 // for the parent's "scroll up at the top jumps to the header" check. inside 748 // the detail pane up-arrow stays in the pane (scrolling); when the list holds 749 // focus, report its selection directly so up-arrow at the top row ascends. 750 pub fn getSelectedIndex(self: *View) ?usize { 751 if (self.detailActive()) return 1; 752 const lb = self.listBox(); 753 const cid = lb.getFocus().child_id orelse return null; 754 return lb.children.getIndex(cid); 755 } 756 }; 757 758 // join `lines` into one string, each prefixed with its 1-based file line number 759 // (right-aligned in a column wide enough for the last number, then a space), 760 // like the diffs view. `start` is the window's 0-based first line, so numbers 761 // continue across paginated windows. 762 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 763 if (lines.len == 0) return ""; 764 const width = std.fmt.count("{d}", .{start + lines.len}); 765 const indent = try arena.alloc(u8, width); 766 @memset(indent, ' '); 767 768 var out: std.ArrayList(u8) = .empty; 769 for (lines, 0..) |line, i| { 770 if (i != 0) try out.append(arena, '\n'); 771 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 772 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 773 try out.appendSlice(arena, num_str); 774 try out.append(arena, ' '); 775 try out.appendSlice(arena, line); 776 } 777 return out.toOwnedSlice(arena); 778 } 779 780 // the index of the "README"/"README.md" file entry (case-insensitive), or null 781 // if none. used to pick the default selection at the repo root. 782 fn readmeIndex(entries: []const Entry) ?usize { 783 for (entries, 0..) |entry, i| { 784 if (entry.is_dir) continue; 785 if (std.ascii.eqlIgnoreCase(entry.name, "README") or std.ascii.eqlIgnoreCase(entry.name, "README.md")) 786 return i; 787 } 788 return null; 789 } 790 791 // the index of the entry the route named (data.selected_file), or null if none. 792 fn selectedFileIndex(data: *const Self) ?usize { 793 const name = data.selected_file orelse return null; 794 for (data.entries, 0..) |entry, i| { 795 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 796 } 797 return null; 798 } 799 800 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 801 // following it navigates to that directory's listing. 802 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 803 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 804 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.urlAlloc(page_arena)}); 805 } 806 807 // an "ai:" link to the file's route: crossPageLink ignores it, so a wasm click 808 // selects the row in place and shows its contents in the detail pane. 809 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]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 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.urlAlloc(page_arena)}); 812 } 813 814 // `dir`/`name`, or just `name` at the root. 815 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 816 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 817 } 818 819 // `dir` with its last segment removed ("" when `dir` has a single segment). 820 fn parentDir(dir: []const u8) []const u8 { 821 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 822 return dir[0..slash]; 823 } 824