╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const ui = @import("../../ui.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const tr = xit.tree; 7 const df = xit.diff; 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const inp = xitui.input; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 15 const SubHeader = @import("SubHeader.zig"); 16 17 // how many lines of a file's content one window shows before a "next" link. 18 const file_page = 2000; 19 20 // the most directory entries (files + dirs) we read from a tree and display. 21 // each file's content is also loaded (windowed), so an unbounded directory could 22 // add up to a lot of memory; entries past this limit are ignored. 23 // 24 // TODO: this caps what we keep, but `tr.Tree.init` still flattens the entire 25 // repo tree (every path) into memory before we ever apply the cap. read the 26 // directory level-by-level instead, using `obj.Object` to load one tree object 27 // at a time: initCommit -> root tree, then descend each `dir` segment, reading 28 // only the immediate entries of the target tree (and applying max_entries 29 // there). that bounds the work to O(depth) tree objects rather than the whole 30 // repo. it's doable entirely in haxy (no xit change) — `obj.Object(...).init` 31 // on a tree oid already returns just that tree's immediate children. 32 const max_entries = 100; 33 34 // one entry in the directory currently being viewed. 35 pub const Entry = struct { 36 name: []const u8, 37 is_dir: bool, 38 // a file's content window split into lines (each without its trailing 39 // newline); empty for directories and for binary or empty files. 40 lines: []const []const u8 = &.{}, 41 // true when xit's line iterator flagged the file as binary, so the detail 42 // pane shows a placeholder rather than its bytes. 43 is_binary: bool = false, 44 // the line index this window starts at (0 = the first window). non-zero only 45 // for the route's selected file, paginated by the url's `after`. 46 window_start: usize = 0, 47 // whether more lines exist after this window. 48 has_more: bool = false, 49 }; 50 51 // "owner/name", so the view can build /repo/owner/name/files/... links. 52 identity: []const u8, 53 // the resolved ref this listing is read at (the default branch when the route 54 // didn't name one), so the view's directory links stay pinned to it. 55 ref_or_oid: ui.RoutablePage.RefOrOid, 56 ref_or_oid_value: []const u8, 57 // the directory being viewed, relative to the repo root ("" at the root). 58 dir: []const u8, 59 // a file within `dir` to start selected (so its contents show and the url 60 // carries it), or null to fall back to a README / the first row. set when the 61 // route's path named a file rather than a directory. 62 selected_file: ?[]const u8 = null, 63 entries: []const Entry, 64 // the "viewing <ref> <value>" banner shown above the listing. 65 sub_header: SubHeader, 66 67 const Self = @This(); 68 69 pub fn init( 70 arena: *std.heap.ArenaAllocator, 71 session: *ui.Session, 72 event_id: *const [evt.event_id_size]u8, 73 identity: []const u8, 74 requested_ref_or_oid: ?ui.RoutablePage.RefOrOid, 75 requested_value: []const u8, 76 path: []const u8, 77 // the line offset the selected file's content window starts at (0 = first). 78 after: usize, 79 ) !Self { 80 const aa = arena.allocator(); 81 82 // no filesystem (wasm) or nowhere to look: empty listing pinned to whatever 83 // ref the route asked for. the wasm path never calls init anyway — it 84 // rebuilds from the serialized snapshot. 85 const io = session.io orelse return emptyResult(aa, identity, requested_ref_or_oid orelse .branch, requested_value, path); 86 const repos_dir = session.repos_dir orelse return emptyResult(aa, identity, requested_ref_or_oid orelse .branch, requested_value, path); 87 88 // the repo's working copy lives at <repos_dir>/<hex event id>. 89 const hex = std.fmt.bytesToHex(event_id.*, .lower); 90 const repo_path = try std.fs.path.join(aa, &.{ repos_dir, &hex }); 91 92 // open + read the committed file list with the arena's backing allocator 93 // (transient; freed before init returns); the listing is built into the 94 // page arena so it outlives them. AnyRepo opens both sha1 and sha256 repos. 95 const gpa = arena.child_allocator; 96 var any_repo = rp.AnyRepo(.xit, .{}).open(io, gpa, .{ .path = repo_path }) catch return emptyResult(aa, identity, requested_ref_or_oid orelse .branch, requested_value, path); 97 defer any_repo.deinit(io, gpa); 98 99 return switch (any_repo) { 100 inline else => |*repo| listing(repo.self_repo_opts, arena, repo, io, gpa, identity, requested_ref_or_oid, requested_value, path, after), 101 }; 102 } 103 104 // build the listing for an opened repo. generic over the repo's hash kind so 105 // the tree/diff types it threads through match the repo's opts. 106 fn listing( 107 comptime repo_opts: rp.RepoOpts(.xit), 108 arena: *std.heap.ArenaAllocator, 109 repo: *rp.Repo(.xit, repo_opts), 110 io: std.Io, 111 gpa: std.mem.Allocator, 112 identity: []const u8, 113 requested_ref_or_oid: ?ui.RoutablePage.RefOrOid, 114 requested_value: []const u8, 115 path: []const u8, 116 after: usize, 117 ) !Self { 118 const aa = arena.allocator(); 119 120 // resolve the requested ref (or the default branch) to the commit oid whose 121 // tree we list. a ref the route named explicitly that doesn't resolve is a 122 // bad url (NotFound -> 404); the default-branch path falls through to empty. 123 const resolved = (try ui.ResolvedRefOrOid(repo_opts).init(repo, io, aa, requested_ref_or_oid, requested_value)) orelse { 124 if (requested_ref_or_oid != null) return error.NotFound; 125 return emptyResult(aa, identity, .branch, requested_value, path); 126 }; 127 128 // read the tree at that commit. building the read-only state mirrors what 129 // repo.status does internally, but for an arbitrary commit rather than HEAD. 130 var moment = repo.core.latestMoment() catch return emptyResult(aa, identity, resolved.ref_or_oid, resolved.value, path); 131 const state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 132 var tree = tr.Tree(.xit, repo_opts).init(state, io, gpa, &resolved.oid) catch return emptyResult(aa, identity, resolved.ref_or_oid, resolved.value, path); 133 defer tree.deinit(); 134 135 // `path` names a file when it's an exact tree entry: list its parent 136 // directory and start that file selected. otherwise `path` is the directory. 137 var dir = path; 138 var selected_file: ?[]const u8 = null; 139 if (path.len != 0 and tree.entries.get(path) != null) { 140 const slash = std.mem.lastIndexOfScalar(u8, path, '/'); 141 selected_file = try aa.dupe(u8, if (slash) |s| path[s + 1 ..] else path); 142 dir = if (slash) |s| path[0..s] else ""; 143 } 144 145 // collect the immediate children of `dir`. each committed path is a full 146 // file path; a child is a directory when more path follows its first 147 // segment under `dir`. 148 var children: std.StringArrayHashMapUnmanaged(bool) = .empty; // name -> is_dir 149 defer children.deinit(gpa); 150 const prefix_len = if (dir.len == 0) 0 else dir.len + 1; // skip "dir/" 151 for (tree.entries.keys()) |entry_path| { 152 if (dir.len != 0) { 153 if (!std.mem.startsWith(u8, entry_path, dir) or entry_path.len <= dir.len or entry_path[dir.len] != '/') continue; 154 } 155 const rel = entry_path[prefix_len..]; 156 const slash = std.mem.indexOfScalar(u8, rel, '/'); 157 const name = if (slash) |s| rel[0..s] else rel; 158 const is_dir = slash != null; 159 // stop taking new entries once we hit the cap; the rest are ignored. 160 if (children.count() >= max_entries and !children.contains(name)) continue; 161 const gop = try children.getOrPut(gpa, name); 162 if (!gop.found_existing) gop.value_ptr.* = is_dir else if (is_dir) gop.value_ptr.* = true; 163 } 164 165 // trees hold no empty directories, so a non-root `dir` with no children 166 // doesn't exist in this ref — a bad url (404). 167 if (dir.len != 0 and children.count() == 0) return error.NotFound; 168 169 // committed paths come out sorted (tree objects are written in sorted 170 // order), so the deduped children are already in name order. just group 171 // directories before files, keeping that order within each group, and dupe 172 // into the page arena. each file's contents are read here (into the page 173 // arena) so the detail pane can show them without another lookup. 174 const entries = try aa.alloc(Entry, children.count()); 175 var i: usize = 0; 176 for ([_]bool{ true, false }) |want_dir| { 177 for (children.keys(), children.values()) |name, is_dir| { 178 if (is_dir != want_dir) continue; 179 var entry: Entry = .{ .name = try aa.dupe(u8, name), .is_dir = is_dir }; 180 if (!is_dir) { 181 const file_path = try childDir(aa, dir, name); 182 if (tree.entries.get(file_path)) |tree_entry| { 183 // only the route's selected file paginates; the rest show 184 // their first window (for the in-page detail when selected). 185 const is_selected = if (selected_file) |sf| std.mem.eql(u8, sf, name) else false; 186 const window_start = if (is_selected) after else 0; 187 const content = readFileContent(repo_opts, state, io, gpa, aa, file_path, tree_entry, window_start) catch 188 FileContent{ .lines = &.{} }; 189 entry.lines = content.lines; 190 entry.is_binary = content.is_binary; 191 entry.window_start = window_start; 192 entry.has_more = content.has_more; 193 } 194 } 195 entries[i] = entry; 196 i += 1; 197 } 198 } 199 200 return .{ 201 .identity = try aa.dupe(u8, identity), 202 .ref_or_oid = resolved.ref_or_oid, 203 .ref_or_oid_value = resolved.value, 204 .dir = try aa.dupe(u8, dir), 205 .selected_file = selected_file, 206 .entries = entries, 207 .sub_header = try SubHeader.init(aa, resolved.ref_or_oid, resolved.value), 208 }; 209 } 210 211 const FileContent = struct { 212 lines: []const []const u8, 213 is_binary: bool = false, 214 has_more: bool = false, 215 }; 216 217 // read the window [start, start+file_page) of a committed file's contents at 218 // `tree_entry` into `arena`-owned lines, flagging whether more follow. xit's 219 // line iterator flags binary files (its source becomes `.binary`), in which case 220 // we report no lines and let the view show a placeholder. 221 fn readFileContent( 222 comptime repo_opts: rp.RepoOpts(.xit), 223 state: rp.Repo(.xit, repo_opts).State(.read_only), 224 io: std.Io, 225 gpa: std.mem.Allocator, 226 arena: std.mem.Allocator, 227 path: []const u8, 228 tree_entry: tr.TreeEntry(repo_opts.hash), 229 start: usize, 230 ) !FileContent { 231 var line_iter = try df.LineIterator(.xit, repo_opts).initFromTree(state, io, gpa, path, tree_entry); 232 defer line_iter.deinit(); 233 if (line_iter.source == .binary) return .{ .lines = &.{}, .is_binary = true }; 234 235 // init reads through the file to validate it (and buffer it in memory), so 236 // the line count is known and a window can be sliced out of the buffer. 237 const total = line_iter.count(); 238 const end = @min(start + file_page, total); 239 240 var lines: std.ArrayList([]const u8) = .empty; 241 var i = start; 242 while (i < end) : (i += 1) { 243 const line = try line_iter.get(i); 244 defer line_iter.free(line); 245 try lines.append(arena, try arena.dupe(u8, line)); 246 } 247 return .{ 248 .lines = try lines.toOwnedSlice(arena), 249 .has_more = end < total, 250 }; 251 } 252 253 // an empty listing pinned to a ref, for the wasm / no-repo / unresolved paths. 254 fn emptyResult(aa: std.mem.Allocator, identity: []const u8, ref_or_oid: ui.RoutablePage.RefOrOid, ref_or_oid_value: []const u8, dir: []const u8) !Self { 255 return .{ 256 .identity = try aa.dupe(u8, identity), 257 .ref_or_oid = ref_or_oid, 258 .ref_or_oid_value = try aa.dupe(u8, ref_or_oid_value), 259 .dir = try aa.dupe(u8, dir), 260 .entries = &.{}, 261 .sub_header = try SubHeader.init(aa, ref_or_oid, ref_or_oid_value), 262 }; 263 } 264 265 pub const View = struct { 266 // a vertical stack: the "viewing <ref>" banner on top, then a horizontal 267 // split with the file/directory list on the left and a detail pane on the 268 // right showing the selected file's contents. 269 box: wgt.Box(ui.Widget), // vert: [sub_header_index] = banner, [content_index] = split 270 data: *const Self, 271 session: *ui.Session, 272 // the list row whose contents the detail pane currently shows. 273 shown_index: ?usize, 274 275 const sub_header_index: usize = 0; 276 const content_index: usize = 1; 277 // indices within the content box (the horizontal split). 278 const list_index: usize = 0; 279 const detail_index: usize = 1; 280 // indices within the detail pane frame (nav box above the content scroll). 281 const detail_nav_index: usize = 0; 282 const detail_scroll_index: usize = 1; 283 const list_max_width: usize = 40; 284 const detail_min_width: usize = 40; 285 286 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 287 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 288 errdefer outer.deinit(allocator); 289 290 // the ref banner at the top. 291 { 292 var sub_header = try SubHeader.View.init(allocator, &data.sub_header, session); 293 errdefer sub_header.deinit(allocator); 294 try outer.children.put(allocator, sub_header.getFocus().id, .{ .widget = .{ .repo_sub_header = sub_header }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 295 } 296 297 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 298 errdefer box.deinit(allocator); 299 300 // the directory listing on the left (one focusable row each). 301 { 302 var list_scroll = blk: { 303 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 304 errdefer list_box.deinit(allocator); 305 306 // labels and link kinds are borrowed by the rows, so they live in 307 // the page arena (as long as this page's widget tree). 308 const aa = session.page_arena.allocator(); 309 if (data.dir.len != 0) { 310 try addRow(allocator, &list_box, "..", try dirLink(session.page_arena, data, parentDir(data.dir))); 311 } 312 for (data.entries) |entry| { 313 const path = try childDir(aa, data.dir, entry.name); 314 const label = if (entry.is_dir) try std.fmt.allocPrint(aa, "{s}/", .{entry.name}) else entry.name; 315 const link = if (entry.is_dir) 316 try dirLink(session.page_arena, data, path) 317 else 318 try fileLink(session.page_arena, data, path); 319 try addRow(allocator, &list_box, label, link); 320 } 321 // select the file the route named, else prefer a README so its 322 // contents greet the visitor in the detail pane, else the first 323 // row. a non-root directory has a leading ".." row, so the entry 324 // index is offset by one there. 325 if (list_box.children.count() > 0) { 326 const base: usize = if (data.dir.len != 0) 1 else 0; 327 const entry_idx = selectedFileIndex(data) orelse readmeIndex(data.entries); 328 const sel = if (entry_idx) |i| i + base else 0; 329 list_box.getFocus().child_id = list_box.children.keys()[sel]; 330 } 331 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 332 }; 333 errdefer list_scroll.deinit(allocator); 334 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 335 } 336 337 // the detail pane on the right — a "next" nav box above a scroll of the 338 // selected file's content window. both are repopulated per selection by 339 // populateDetail. 340 { 341 var detail_outer = blk: { 342 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 343 errdefer frame.deinit(allocator); 344 345 { 346 var nav_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 347 errdefer nav_box.deinit(allocator); 348 try frame.children.put(allocator, nav_box.getFocus().id, .{ .widget = .{ .box = nav_box }, .rect = null, .min_size = null }); 349 } 350 { 351 // the content scroll wrapped in a bordered box; build sets the 352 // border single normally and double when the content is focused 353 // (the content box itself is borderless). 354 var scroll_frame = blk2: { 355 var detail_scroll = blk3: { 356 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 357 errdefer detail_inner.deinit(allocator); 358 break :blk3 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }); 359 }; 360 errdefer detail_scroll.deinit(allocator); 361 var sf = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .single, .rounded_corners = true, .direction = .vert }); 362 errdefer sf.deinit(allocator); 363 // the wrapper's selected child is its scroll, so the focus 364 // chain reaches the content box (populateDetail points the 365 // scroll's inner box at it). 366 sf.getFocus().child_id = detail_scroll.getFocus().id; 367 try sf.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 368 break :blk2 sf; 369 }; 370 errdefer scroll_frame.deinit(allocator); 371 // entering the detail pane lands on the content scroll (focus 372 // recovery descends the frame's selected child). the nav links 373 // above are reached by scrolling to the top, then up. 374 frame.getFocus().child_id = scroll_frame.getFocus().id; 375 try frame.children.put(allocator, scroll_frame.getFocus().id, .{ .widget = .{ .box = scroll_frame }, .rect = null, .min_size = null }); 376 } 377 break :blk frame; 378 }; 379 errdefer detail_outer.deinit(allocator); 380 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 381 } 382 383 box.getFocus().child_id = box.children.keys()[list_index]; 384 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 385 386 // focus lives in the split; the banner isn't focusable. 387 outer.getFocus().child_id = outer.children.keys()[content_index]; 388 return .{ .box = outer, .data = data, .session = session, .shown_index = null }; 389 } 390 391 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 392 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 393 errdefer row.deinit(allocator); 394 row.getFocus().focusable = true; 395 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 396 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 397 } 398 399 // the selected file's contents as a single focusable multi-line text box. 400 // it's borderless — the border lives on the surrounding scroll frame, which 401 // doubles when this content is focused. `text` lives in the page arena. 402 fn addContentBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8) !void { 403 _ = self; 404 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = null, .wrap_kind = .none }); 405 errdefer tb.deinit(allocator); 406 tb.getFocus().focusable = true; 407 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 408 } 409 410 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 411 self.box.deinit(allocator); 412 } 413 414 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 415 return &self.box.children.values()[content_index].widget.box; 416 } 417 418 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 419 return &self.contentBox().children.values()[list_index].widget.scroll; 420 } 421 422 fn listBox(self: *View) *wgt.Box(ui.Widget) { 423 return &self.listScroll().child.box; 424 } 425 426 fn detailOuter(self: *View) *wgt.Box(ui.Widget) { 427 return &self.contentBox().children.values()[detail_index].widget.box; 428 } 429 430 fn navBox(self: *View) *wgt.Box(ui.Widget) { 431 return &self.detailOuter().children.values()[detail_nav_index].widget.box; 432 } 433 434 // the bordered box wrapping the content scroll (border toggles with focus). 435 fn detailScrollFrame(self: *View) *wgt.Box(ui.Widget) { 436 return &self.detailOuter().children.values()[detail_scroll_index].widget.box; 437 } 438 439 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 440 return &self.detailScrollFrame().children.values()[0].widget.scroll; 441 } 442 443 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 444 return &self.detailScroll().child.box; 445 } 446 447 fn detailActive(self: *View) bool { 448 const content = self.contentBox(); 449 const cid = content.getFocus().child_id orelse return false; 450 return content.children.getIndex(cid) == detail_index; 451 } 452 453 // the selected list row's index, or null when nothing is focused. 454 fn selectedRowIndex(self: *View) ?usize { 455 const lb = self.listBox(); 456 const cid = lb.getFocus().child_id orelse return null; 457 return lb.children.getIndex(cid); 458 } 459 460 // the entry the selected row points at, or null for the ".." row (which 461 // isn't an entry). directory rows return their entry too, though they carry 462 // no contents. 463 fn selectedEntry(self: *View) ?Entry { 464 const idx = self.selectedRowIndex() orelse return null; 465 const base: usize = if (self.data.dir.len != 0) 1 else 0; // skip the ".." row 466 if (idx < base) return null; 467 const e = idx - base; 468 if (e >= self.data.entries.len) return null; 469 return self.data.entries[e]; 470 } 471 472 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 473 self.clearGrid(); 474 475 // swap the detail pane to the selected entry when the selection changes. 476 try self.refreshDetail(allocator); 477 478 // mirror the selected file into the url so it updates as the selection 479 // moves, but only while focus is inside this view (so landing on the tab 480 // keeps the page's base route). a file selection carries the file path and 481 // its content window offset; a directory / ".." row (or no selection) 482 // stays at the directory with no offset. the path is joined in a stack 483 // buffer since build runs every frame on the web. 484 if (root_focus.grandchild_id) |g| { 485 if (self.box.getFocus().children.contains(g)) { 486 var buf: [ui.RoutablePage.repo_route_max_len]u8 = undefined; 487 var sel_path = self.data.dir; 488 var sel_after: usize = 0; 489 if (self.selectedEntry()) |entry| { 490 if (!entry.is_dir) { 491 sel_path = if (self.data.dir.len == 0) 492 entry.name 493 else 494 std.fmt.bufPrint(&buf, "{s}/{s}", .{ self.data.dir, entry.name }) catch self.data.dir; 495 sel_after = entry.window_start; 496 } 497 } 498 if (ui.RoutablePage.repoFilesRoute(self.data.identity, self.data.ref_or_oid, self.data.ref_or_oid_value, sel_path, sel_after)) |route| 499 self.session.data.current_page = route; 500 } 501 } 502 503 // the selected list row shows a border (the focused TextBox upgrades it 504 // to a double border itself); the rest stay borderless. 505 const lb = self.listBox(); 506 for (lb.children.keys(), lb.children.values()) |id, *child| { 507 switch (child.widget) { 508 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 509 else => {}, 510 } 511 } 512 513 // the content scroll's border is single normally and double when the 514 // content is focused (the content box itself is borderless). 515 const has_content = self.detailInner().children.count() > 0; 516 const content_focused = if (root_focus.grandchild_id) |g| self.detailInner().children.contains(g) else false; 517 self.detailScrollFrame().options.border_style = if (!has_content) null else if (content_focused) .double else .single; 518 519 // same for the "next" link above the content. 520 const nav = self.navBox(); 521 for (nav.children.keys(), nav.children.values()) |id, *child| { 522 switch (child.widget) { 523 .text_box => |*tb| tb.options.border_style = if (nav.getFocus().child_id == id) .single else .hidden, 524 else => {}, 525 } 526 } 527 528 // cap the list at list_max_width only while the detail pane fits beside 529 // it. the box drops the detail pane when the width can't hold both 530 // minimums, so when it's that narrow we lift the cap and let the list 531 // fill the whole width. 532 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 533 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 534 535 // stretch the detail pane and its bordered scroll frame across the rest 536 // of the width so the pane fills the area rather than shrinking to its 537 // content, like the refs view's columns. the content scroll inside keeps 538 // its natural width: forcing it to the full inner width would make it one 539 // column too wide whenever the vertical scrollbar appears, leaving a 540 // phantom horizontal scroll. (height fills via the scroll clipping to the 541 // viewport.) 542 if (constraint.max_size.width) |w| { 543 const detail_w = if (both_panes_fit) w - list_max_width else w; 544 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_w, .height = null }; 545 self.detailOuter().children.values()[detail_scroll_index].min_size = .{ .width = detail_w, .height = null }; 546 } else { 547 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 548 self.detailOuter().children.values()[detail_scroll_index].min_size = null; 549 } 550 551 // crossing panes only re-selects the content box's child (focusList / 552 // focusDetail); when the window is too narrow to show both, the pane that 553 // was holding focus is dropped here. the framework recovers focus after 554 // the top-level build by re-deriving it down the selected-child chain, so 555 // there's nothing to fix up afterward. 556 try self.box.build(allocator, constraint, root_focus); 557 } 558 559 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 560 const cur = self.selectedRowIndex(); 561 if (std.meta.eql(cur, self.shown_index)) return; 562 try self.populateDetail(allocator); 563 self.shown_index = cur; 564 } 565 566 fn populateDetail(self: *View, allocator: std.mem.Allocator) !void { 567 const nav = self.navBox(); 568 const inner = self.detailInner(); 569 570 for (nav.children.values()) |*child| child.widget.deinit(allocator); 571 nav.children.clearAndFree(allocator); 572 nav.getFocus().child_id = null; 573 for (inner.children.values()) |*child| child.widget.deinit(allocator); 574 inner.children.clearAndFree(allocator); 575 inner.getFocus().child_id = null; 576 577 // only files have contents to show; directories and the ".." row leave 578 // the pane empty. 579 if (self.selectedEntry()) |entry| { 580 if (!entry.is_dir) { 581 // the "next" window link sits above the scroll, so it stays put 582 // while the content scrolls. 583 if (entry.has_more) try self.addNavLink(allocator, nav, "next lines →", entry, entry.window_start + file_page); 584 if (entry.is_binary) { 585 try self.addContentBox(allocator, inner, "(binary file)"); 586 } else { 587 const text = try numberedContent(self.session.page_arena.allocator(), entry.lines, entry.window_start); 588 try self.addContentBox(allocator, inner, text); 589 } 590 } 591 } 592 // point each box at its first child so focus can descend into it. 593 if (nav.children.count() > 0) nav.getFocus().child_id = nav.children.keys()[0]; 594 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 595 596 // reset the scroll to the top for the newly-shown file: directly on the 597 // terminal (the wasm offset), and via a version bump on the web (so the 598 // renderer's scroll id changes and JS drops the preserved position). 599 const sc = self.detailScroll(); 600 sc.x = 0; 601 sc.y = 0; 602 sc.getFocus().version +%= 1; 603 } 604 605 // a focusable "next" link above the content. it's an `a:` link to the 606 // selected file at `target_after`, so activating it (the host follows the 607 // link) reloads the page on the next window. 608 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_after: usize) !void { 609 const page_arena = self.session.page_arena; 610 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 611 const route = ui.RoutablePage.repoFilesRoute(self.data.identity, self.data.ref_or_oid, self.data.ref_or_oid_value, path, target_after) orelse return error.RouteTooLong; 612 const link = try std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.urlAlloc(page_arena)}); 613 var tb = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 614 errdefer tb.deinit(allocator); 615 tb.getFocus().focusable = true; 616 tb.getFocus().kind = .{ .custom = link }; 617 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 618 } 619 620 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 621 _ = allocator; 622 if (self.detailActive()) { 623 try self.detailInput(key, root_focus); 624 } else { 625 try self.listInput(key, root_focus); 626 } 627 } 628 629 fn listInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 630 // up/down (and the scroll wheel) move the selection; page up/down jump a 631 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 632 // directory (or "..") row are turned into navigation by the host 633 // (crossPageLink) before reaching here. 634 switch (key) { 635 .enter, .arrow_right => try self.focusDetail(root_focus), 636 .arrow_down => try self.moveSelection(root_focus, 1), 637 .arrow_up => try self.moveSelection(root_focus, -1), 638 .page_down => try self.moveSelection(root_focus, 10), 639 .page_up => try self.moveSelection(root_focus, -10), 640 .end => try self.moveSelection(root_focus, @intCast(self.listBox().children.count())), 641 .home => try self.moveSelection(root_focus, -@as(isize, @intCast(self.listBox().children.count()))), 642 .mouse => |mouse| switch (mouse.action) { 643 .scroll => |dir| try self.moveSelection(root_focus, if (dir == .up) -1 else 1), 644 else => {}, 645 }, 646 else => {}, 647 } 648 } 649 650 // whether focus is on the content box (vs. a "previous"/"next" nav link). 651 fn focusOnContent(self: *View, root_focus: *Focus) bool { 652 const g = root_focus.grandchild_id orelse return false; 653 return self.detailInner().children.contains(g); 654 } 655 656 fn detailInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 657 const sc = self.detailScroll(); 658 const on_content = self.focusOnContent(root_focus); 659 // the "next" link sits above the content scroll. on the link, left returns 660 // to the file list and down drops into the content. in the content, the 661 // terminal scrolls by offset and reaching the top then pressing up crosses 662 // back up to the link; on the web vertical scrolling is the browser's job 663 // so up just crosses to the link. 664 switch (key) { 665 .arrow_left => { 666 if (on_content) { 667 if (sc.x > 0) { 668 sc.x -= 1; 669 self.clampDetailScroll(); 670 } else try self.focusList(root_focus); 671 } else if (!self.moveNav(root_focus, -1)) { 672 try self.focusList(root_focus); 673 } 674 }, 675 .arrow_right => { 676 if (on_content) { 677 sc.x += 1; 678 self.clampDetailScroll(); 679 } else _ = self.moveNav(root_focus, 1); 680 }, 681 .arrow_up => { 682 if (on_content) { 683 if (self.session.is_terminal and sc.y > 0) { 684 sc.y -= 1; 685 self.clampDetailScroll(); 686 } else self.focusNav(root_focus); // cross up to the links 687 } 688 }, 689 .arrow_down => { 690 if (on_content) { 691 if (self.session.is_terminal) { 692 sc.y += 1; 693 self.clampDetailScroll(); 694 } 695 } else self.focusContent(root_focus); // links -> content 696 }, 697 .page_up => if (on_content and self.session.is_terminal) { 698 sc.y -= 10; 699 self.clampDetailScroll(); 700 }, 701 .page_down => if (on_content and self.session.is_terminal) { 702 sc.y += 10; 703 self.clampDetailScroll(); 704 }, 705 .home => if (on_content and self.session.is_terminal) { 706 sc.y = 0; 707 self.clampDetailScroll(); 708 }, 709 .end => if (on_content and self.session.is_terminal) { 710 sc.y = std.math.maxInt(isize); 711 self.clampDetailScroll(); 712 }, 713 .mouse => |mouse| switch (mouse.action) { 714 .scroll => |dir| if (on_content and self.session.is_terminal) { 715 sc.y += if (dir == .up) -5 else 5; 716 self.clampDetailScroll(); 717 }, 718 else => {}, 719 }, 720 else => {}, 721 } 722 } 723 724 // focus the first (left-most) nav link; a no-op when there are none, leaving 725 // focus on the content. 726 fn focusNav(self: *View, root_focus: *Focus) void { 727 const nav = self.navBox(); 728 if (nav.children.count() == 0) return; 729 root_focus.setFocus(nav.children.keys()[0]); 730 } 731 732 fn focusContent(self: *View, root_focus: *Focus) void { 733 const inner = self.detailInner(); 734 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 735 root_focus.setFocus(id); 736 } 737 738 // move focus `delta` rows within the nav box; returns false (without moving) 739 // when that would step off either end. 740 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 741 const nav = self.navBox(); 742 const keys = nav.children.keys(); 743 const cur_id = nav.getFocus().child_id orelse return false; 744 const cur: isize = @intCast(nav.children.getIndex(cur_id) orelse return false); 745 const target = cur + delta; 746 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 747 root_focus.setFocus(keys[@intCast(target)]); 748 return true; 749 } 750 751 // keep the detail scroll within its content, using the last build's grids. 752 // the scroll bar's reserved column/row isn't part of the content viewport, 753 // so exclude it or the last column/row stays unreachable. 754 fn clampDetailScroll(self: *View) void { 755 const sc = self.detailScroll(); 756 const vp = sc.grid orelse return; 757 const content = sc.child.box.grid orelse return; 758 const view_w = vp.size.width - sc.bar_w; 759 const view_h = vp.size.height - sc.bar_h; 760 const max_y: isize = if (content.size.height > view_h) @intCast(content.size.height - view_h) else 0; 761 const max_x: isize = if (content.size.width > view_w) @intCast(content.size.width - view_w) else 0; 762 sc.y = std.math.clamp(sc.y, 0, max_y); 763 sc.x = std.math.clamp(sc.x, 0, max_x); 764 } 765 766 // enter the detail pane, landing on the "next" link when there is one (so 767 // right-arrow reaches it), else on the content. a directory or ".." row has 768 // neither, so there's nothing to enter. selecting the frame's child (then 769 // focusing the frame) also handles the too-narrow case where the pane isn't 770 // laid out yet — focus lands inside it after the next build. 771 fn focusDetail(self: *View, root_focus: *Focus) !void { 772 const frame = self.detailOuter(); 773 if (self.navBox().children.count() > 0) { 774 frame.getFocus().child_id = self.navBox().getFocus().id; 775 } else if (self.detailInner().children.count() > 0) { 776 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 777 } else return; 778 root_focus.setFocus(frame.getFocus().id); 779 } 780 781 // return to the list. 782 fn focusList(self: *View, root_focus: *Focus) !void { 783 root_focus.setFocus(self.listScroll().getFocus().id); 784 } 785 786 fn moveSelection(self: *View, root_focus: *Focus, delta: isize) !void { 787 const lb = self.listBox(); 788 const keys = lb.children.keys(); 789 if (keys.len == 0) return; 790 const cur_id = lb.getFocus().child_id orelse return; 791 const cur: isize = @intCast(lb.children.getIndex(cur_id) orelse return); 792 const last: isize = @intCast(keys.len - 1); 793 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 794 if (next == @as(usize, @intCast(cur))) return; 795 root_focus.setFocus(keys[next]); 796 if (lb.children.values()[next].rect) |rect| self.listScroll().scrollToRect(rect); 797 } 798 799 pub fn clearGrid(self: *View) void { 800 self.box.clearGrid(); 801 } 802 803 pub fn getGrid(self: View) ?Grid { 804 return self.box.getGrid(); 805 } 806 807 pub fn getFocus(self: *View) *Focus { 808 return self.box.getFocus(); 809 } 810 811 // for the parent's "scroll up at the top jumps to the header" check. inside 812 // the detail pane up-arrow stays in the pane (scrolling); when the list holds 813 // focus, report its selection directly so up-arrow at the top row ascends. 814 pub fn getSelectedIndex(self: *View) ?usize { 815 if (self.detailActive()) return 1; 816 const lb = self.listBox(); 817 const cid = lb.getFocus().child_id orelse return null; 818 return lb.children.getIndex(cid); 819 } 820 }; 821 822 // join `lines` into one string, each prefixed with its 1-based file line number 823 // (right-aligned in a column wide enough for the last number, then a space), 824 // like the diffs view. `start` is the window's 0-based first line, so numbers 825 // continue across paginated windows. 826 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 827 if (lines.len == 0) return ""; 828 const width = std.fmt.count("{d}", .{start + lines.len}); 829 const indent = try arena.alloc(u8, width); 830 @memset(indent, ' '); 831 832 var out: std.ArrayList(u8) = .empty; 833 for (lines, 0..) |line, i| { 834 if (i != 0) try out.append(arena, '\n'); 835 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 836 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 837 try out.appendSlice(arena, num_str); 838 try out.append(arena, ' '); 839 try out.appendSlice(arena, line); 840 } 841 return out.toOwnedSlice(arena); 842 } 843 844 // the index of the "README"/"README.md" file entry (case-insensitive), or null 845 // if none. used to pick the default selection at the repo root. 846 fn readmeIndex(entries: []const Entry) ?usize { 847 for (entries, 0..) |entry, i| { 848 if (entry.is_dir) continue; 849 if (std.ascii.eqlIgnoreCase(entry.name, "README") or std.ascii.eqlIgnoreCase(entry.name, "README.md")) 850 return i; 851 } 852 return null; 853 } 854 855 // the index of the entry the route named (data.selected_file), or null if none. 856 fn selectedFileIndex(data: *const Self) ?usize { 857 const name = data.selected_file orelse return null; 858 for (data.entries, 0..) |entry, i| { 859 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 860 } 861 return null; 862 } 863 864 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 865 // following it navigates to that directory's listing. 866 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 867 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 868 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.urlAlloc(page_arena)}); 869 } 870 871 // an "ai:" link to the file's route: crossPageLink ignores it, so a wasm click 872 // selects the row in place and shows its contents in the detail pane. 873 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 874 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 875 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.urlAlloc(page_arena)}); 876 } 877 878 // `dir`/`name`, or just `name` at the root. 879 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 880 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 881 } 882 883 // `dir` with its last segment removed ("" when `dir` has a single segment). 884 fn parentDir(dir: []const u8) []const u8 { 885 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 886 return dir[0..slash]; 887 } 888