╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
784 return self.box.getGrid(); 785 } 786 787 pub fn getFocus(self: *View) *Focus { 788 return self.box.getFocus(); 789 } 790 791 // for the parent's "scroll up at the top jumps to the header" check. inside 792 // the detail pane up-arrow stays in the pane (scrolling); when the list holds 793 // focus, report its selection directly so up-arrow at the top row ascends. 794 pub fn getSelectedIndex(self: *View) ?usize { 795 if (self.detailActive()) return 1; 796 const lb = self.listBox(); 797 const cid = lb.getFocus().child_id orelse return null; 798 return lb.children.getIndex(cid); 799 } 800 }; 801 802 // join `lines` into one string, each prefixed with its 1-based file line number 803 // (right-aligned in a column wide enough for the last number, then a space), 804 // like the diffs view. `start` is the window's 0-based first line, so numbers 805 // continue across paginated windows. 806 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 807 if (lines.len == 0) return ""; 808 const width = std.fmt.count("{d}", .{start + lines.len}); 809 const indent = try arena.alloc(u8, width); 810 @memset(indent, ' '); 811 812 var out: std.ArrayList(u8) = .empty; 813 for (lines, 0..) |line, i| { 814 if (i != 0) try out.append(arena, '\n'); 815 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 816 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 817 try out.appendSlice(arena, num_str); 818 try out.append(arena, ' '); 819 try out.appendSlice(arena, line); 820 } 821 return out.toOwnedSlice(arena); 822 } 823 824 // the index of the "README"/"README.md" file entry (case-insensitive), or null 825 // if none. used to pick the default selection at the repo root. 826 fn readmeIndex(entries: []const Entry) ?usize { 827 for (entries, 0..) |entry, i| { 828 if (entry.is_dir) continue; 829 if (std.ascii.eqlIgnoreCase(entry.name, "README") or std.ascii.eqlIgnoreCase(entry.name, "README.md")) 830 return i; 831 } 832 return null; 833 } 834 835 // the index of the entry the route named (data.selected_file), or null if none. 836 fn selectedFileIndex(data: *const Self) ?usize { 837 const name = data.selected_file orelse return null; 838 for (data.entries, 0..) |entry, i| { 839 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 840 } 841 return null; 842 } 843 844 // the "a:" link for the files route at `dir`, pinned to the listing's ref. 845 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, dir: []const u8) ![]const u8 { 846 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, dir, 0) orelse return error.RouteTooLong; 847 const url = try route.urlAlloc(page_arena); 848 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 849 } 850 851 // `dir`/`name`, or just `name` at the root. 852 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 853 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 854 } 855 856 // `dir` with its last segment removed ("" when `dir` has a single segment). 857 fn parentDir(dir: []const u8) []const u8 { 858 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 859 return dir[0..slash]; 860 } 861