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