╭ 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 Key = xitui.input.Key; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 const inp = @import("../input.zig"); 15 16 // how many lines of a file's content one window shows before a "next" link. 17 const file_page = 5000; 18 19 // budget for the total bytes of file content preloaded (windowed) per page, 20 // so selecting a preloaded file shows its contents without a reload. 21 const max_preloaded_bytes = 2 * 1024 * 1024; 22 23 // one entry in the directory currently being viewed. 24 pub const Entry = struct { 25 name: []const u8, 26 is_dir: bool, 27 // a file's content window split into lines (each without its trailing 28 // newline); empty for directories and for binary or empty files. 29 lines: []const []const u8 = &.{}, 30 // true when xit's line iterator flagged the file as binary, so the detail 31 // pane shows a placeholder rather than its bytes. 32 is_binary: bool = false, 33 // the line index this window starts at (0 = the first window). non-zero only 34 // for the route's selected file, paginated by the url's `line`. 35 window_start: usize = 0, 36 // whether more lines exist after this window. 37 has_more: bool = false, 38 // whether this file's content was read; unloaded files load via their 39 // "a:" link. 40 loaded: bool = false, 41 }; 42 43 // where links and clone commands for this repository are rooted. 44 location: ui.RoutablePage.RepoLocation, 45 // the resolved ref this listing is read at (the default branch when the route 46 // didn't name one), so the view's directory links stay pinned to it. 47 ref_or_oid: ui.RoutablePage.RefOrOid, 48 ref_or_oid_value: []const u8, 49 patchrev_id: ?[]const u8 = null, 50 // the directory being viewed, relative to the repo root ("" at the root). 51 dir: []const u8, 52 // a file within `dir` to start selected (so its contents show and the url 53 // carries it), or null to fall back to a README / the first row. set when the 54 // route's path named a file rather than a directory. 55 selected_file: ?[]const u8 = null, 56 entries: []const Entry, 57 // the "viewing <ref> <value>" banner shown above the listing. 58 header: Header, 59 60 const Self = @This(); 61 62 pub fn initPatchRev( 63 comptime repo_kind: rp.RepoKind, 64 comptime repo_opts: rp.RepoOpts(repo_kind), 65 arena: *std.heap.ArenaAllocator, 66 repo: *rp.Repo(repo_kind, repo_opts), 67 io: std.Io, 68 gpa: std.mem.Allocator, 69 location: ui.RoutablePage.RepoLocation, 70 id_hex: []const u8, 71 path: []const u8, 72 line: usize, 73 ) !Self { 74 const id = evt.parseEventId(id_hex) catch return error.NotFound; 75 const record = (try evt.readFromRepo(evt.PatchRev, repo_kind, repo_opts, io, gpa, arena, repo, &id)) orelse return error.NotFound; 76 if (record.removed) return error.NotFound; 77 var data = try init(repo_kind, repo_opts, arena, repo, io, gpa, location, .object, record.head_tree_oid, path, line); 78 data.patchrev_id = try arena.allocator().dupe(u8, id_hex); 79 data.header.content = try std.fmt.allocPrint(arena.allocator(), "patchrev {s}", .{id_hex}); 80 return data; 81 } 82 83 pub fn filesRoute(self: *const Self, path: []const u8, line: usize) ?ui.RoutablePage { 84 if (self.patchrev_id) |id| return switch (self.location) { 85 .repo => |identity| ui.RoutablePage.repoPatchRevFilesRoute(identity, id, path, line), 86 .fork => null, 87 }; 88 return self.location.filesRoute(self.ref_or_oid, self.ref_or_oid_value, path, line); 89 } 90 91 // build the listing for an opened repo. generic over the repo's backend and 92 // hash kind so the tree/diff types it threads through match the repo's opts. 93 // reads with the arena's backing allocator (transient); the listing is built 94 // into the page arena so it outlives the repo handle. 95 pub fn init( 96 comptime repo_kind: rp.RepoKind, 97 comptime repo_opts: rp.RepoOpts(repo_kind), 98 arena: *std.heap.ArenaAllocator, 99 repo: *rp.Repo(repo_kind, repo_opts), 100 io: std.Io, 101 gpa: std.mem.Allocator, 102 location: ui.RoutablePage.RepoLocation, 103 requested_ref_or_oid: ?ui.RoutablePage.RefOrOid, 104 requested_value: []const u8, 105 path: []const u8, 106 line: usize, 107 ) !Self { 108 const aa = arena.allocator(); 109 110 // resolve the requested ref (or the default branch) to the object oid whose 111 // tree we list. a ref the route named explicitly that doesn't resolve is a 112 // bad url (NotFound -> 404); the default-branch path falls through to empty. 113 const resolved = (try ui.ResolvedRefOrOid(repo_kind, repo_opts).init(repo, io, aa, requested_ref_or_oid, requested_value)) orelse { 114 if (requested_ref_or_oid != null) return error.NotFound; 115 return emptyResult(aa, location, .branch, requested_value, path); 116 }; 117 118 // read just the viewed directory of that tree or commit. building the 119 // read-only state mirrors what repo.status does internally, but for an 120 // arbitrary commit rather than HEAD. a path that doesn't exist in the tree 121 // is a bad url (404). 122 var moment = repo.core.latestMoment() catch return emptyResult(aa, location, resolved.ref_or_oid, resolved.value, path); 123 const state = rp.Repo(repo_kind, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 124 var tree_dir = tr.TreeDir(repo_kind, repo_opts).init(state, io, gpa, &resolved.oid, path) catch |err| switch (err) { 125 error.TreeEntryNotFound => return error.NotFound, 126 else => return emptyResult(aa, location, resolved.ref_or_oid, resolved.value, path), 127 }; 128 defer tree_dir.deinit(); 129 130 // when `path` named a file, the listing is its parent directory with that 131 // file selected. 132 var dir = path; 133 var selected_file: ?[]const u8 = null; 134 if (tree_dir.file_name != null) { 135 const slash = std.mem.lastIndexOfScalar(u8, path, '/'); 136 selected_file = try aa.dupe(u8, if (slash) |s| path[s + 1 ..] else path); 137 dir = if (slash) |s| path[0..s] else ""; 138 } 139 140 // tree objects store their entries in name order, so the listing is 141 // already sorted. group directories before files, keeping that order 142 // within each group, and dupe into the page arena. file contents are read 143 // here (into the page arena) so the detail pane can show them without 144 // another lookup. 145 const entries = try aa.alloc(Entry, tree_dir.entries.count()); 146 var i: usize = 0; 147 var preloaded_bytes: usize = 0; 148 for ([_]bool{ true, false }) |want_dir| { 149 for (tree_dir.entries.keys(), tree_dir.entries.values()) |name, tree_entry| { 150 if (tree_entry.isTree() != want_dir) continue; 151 var entry: Entry = .{ .name = try aa.dupe(u8, name), .is_dir = want_dir }; 152 if (!want_dir) { 153 // only the route's selected file paginates; the rest show 154 // their first window (for the in-page detail when selected). 155 const is_selected = if (selected_file) |sf| std.mem.eql(u8, sf, name) else false; 156 // the README always loads because it's the default selection. 157 if (preloaded_bytes < max_preloaded_bytes or is_selected or isReadme(name)) { 158 const file_path = try childDir(aa, dir, name); 159 // the url's `line` is 1-based (0 = unset); the window 160 // index is 0-based. 161 const window_start = if (is_selected) line -| 1 else 0; 162 const content = readFileContent(repo_kind, repo_opts, state, io, gpa, aa, file_path, tree_entry, window_start) catch 163 FileContent{ .lines = &.{} }; 164 for (content.lines) |l| preloaded_bytes += l.len; 165 entry.lines = content.lines; 166 entry.is_binary = content.is_binary; 167 entry.window_start = window_start; 168 entry.has_more = content.has_more; 169 entry.loaded = true; 170 } 171 } 172 entries[i] = entry; 173 i += 1; 174 } 175 } 176 177 return .{ 178 .location = try location.dupe(aa), 179 .ref_or_oid = resolved.ref_or_oid, 180 .ref_or_oid_value = resolved.value, 181 .dir = try aa.dupe(u8, dir), 182 .selected_file = selected_file, 183 .entries = entries, 184 .header = try Header.init(aa, resolved.ref_or_oid, resolved.value), 185 }; 186 } 187 188 const FileContent = struct { 189 lines: []const []const u8, 190 is_binary: bool = false, 191 has_more: bool = false, 192 }; 193 194 // read the window [start, start+file_page) of a committed file's contents at 195 // `tree_entry` into `arena`-owned lines, flagging whether more follow. xit's 196 // line iterator flags binary files (its source becomes `.binary`), in which case 197 // we report no lines and let the view show a placeholder. 198 fn readFileContent( 199 comptime repo_kind: rp.RepoKind, 200 comptime repo_opts: rp.RepoOpts(repo_kind), 201 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 202 io: std.Io, 203 gpa: std.mem.Allocator, 204 arena: std.mem.Allocator, 205 path: []const u8, 206 tree_entry: tr.TreeEntry(repo_opts.hash), 207 start: usize, 208 ) !FileContent { 209 var line_iter = try df.LineIterator(repo_kind, repo_opts).initFromTree(state, io, gpa, path, tree_entry); 210 defer line_iter.deinit(); 211 if (line_iter.source == .binary) return .{ .lines = &.{}, .is_binary = true }; 212 213 // init reads through the file to validate it (and buffer it in memory), so 214 // the line count is known and a window can be sliced out of the buffer. 215 const total = line_iter.count(); 216 const end = @min(start +| file_page, total); 217 218 var lines: std.ArrayList([]const u8) = .empty; 219 var i = start; 220 while (i < end) : (i += 1) { 221 const line = try line_iter.get(i); 222 defer line_iter.free(line); 223 try lines.append(arena, try arena.dupe(u8, line)); 224 } 225 return .{ 226 .lines = try lines.toOwnedSlice(arena), 227 .has_more = end < total, 228 }; 229 } 230 231 // an empty listing pinned to a ref, for the wasm / no-repo / unresolved paths. 232 pub fn emptyResult(aa: std.mem.Allocator, location: ui.RoutablePage.RepoLocation, ref_or_oid: ui.RoutablePage.RefOrOid, ref_or_oid_value: []const u8, dir: []const u8) !Self { 233 return .{ 234 .location = try location.dupe(aa), 235 .ref_or_oid = ref_or_oid, 236 .ref_or_oid_value = try aa.dupe(u8, ref_or_oid_value), 237 .dir = try aa.dupe(u8, dir), 238 .entries = &.{}, 239 .header = try Header.init(aa, ref_or_oid, ref_or_oid_value), 240 }; 241 } 242 243 pub const View = struct { 244 // a vertical stack: the "viewing <ref>" banner on top, then a horizontal 245 // split with the file/directory list on the left and a detail pane on the 246 // right showing the selected file's contents. 247 box: wgt.Box(ui.Widget), // vert: [header_index] = banner, [content_index] = split 248 data: *const Self, 249 session: *ui.Session, 250 // the list row whose contents the detail pane currently shows. 251 shown_index: ?usize, 252 253 const header_index: usize = 0; 254 const content_index: usize = 1; 255 // indices within the content box (the horizontal split). 256 const list_index: usize = 0; 257 const detail_index: usize = 1; 258 // indices within the detail pane frame (nav box above the content scroll). 259 const detail_nav_index: usize = 0; 260 const detail_scroll_index: usize = 1; 261 const list_max_width: usize = 35; 262 const detail_min_width: usize = 40; 263 // a list row's height: one text line inside its (hidden) border. 264 const row_height: usize = 3; 265 266 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 267 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 268 errdefer outer.deinit(allocator); 269 270 // the ref banner at the top. 271 { 272 var header_view = try Header.View.init(allocator, &data.header, session, data.location); 273 errdefer header_view.deinit(allocator); 274 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_files_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 275 } 276 277 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 278 errdefer box.deinit(allocator); 279 280 // the directory listing on the left (one focusable row each). 281 { 282 var list_scroll = blk: { 283 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 284 errdefer list_box.deinit(allocator); 285 286 // link kinds are borrowed by the rows, so they live in the page 287 // arena (as long as this page's widget tree). row labels are 288 // copied by their text boxes. 289 const aa = session.page_arena.allocator(); 290 if (data.dir.len != 0) { 291 try addRow(allocator, &list_box, "..", try dirLink(session.page_arena, data, parentDir(data.dir))); 292 } 293 for (data.entries) |entry| { 294 const path = try childDir(aa, data.dir, entry.name); 295 const label = if (entry.is_dir) try std.fmt.allocPrint(aa, "{s}/", .{entry.name}) else entry.name; 296 const link = if (entry.is_dir) 297 try dirLink(session.page_arena, data, path) 298 else 299 try fileLink(session.page_arena, data, path, entry.loaded, lineNumber(entry.window_start)); 300 try addRow(allocator, &list_box, label, link); 301 } 302 // select the file the route named, else prefer a README so its 303 // contents greet the visitor in the detail pane, else the first 304 // row. a non-root directory has a leading ".." row, so the entry 305 // index is offset by one there. 306 var sel: usize = 0; 307 if (list_box.children.count() > 0) { 308 const base: usize = if (data.dir.len != 0) 1 else 0; 309 const entry_idx = selectedFileIndex(data) orelse readmeIndex(data.entries); 310 sel = if (entry_idx) |i| i + base else 0; 311 list_box.getFocus().child_id = list_box.children.keys()[sel]; 312 } 313 var sc = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 314 // start scrolled so the selected row begins at the top of the 315 // viewport. rows are a fixed height, so no layout pass is 316 // needed; a too-far offset near the end of the list is clamped 317 // in build (the browser clamps its own scrolling natively). 318 sc.y = @intCast(sel * row_height); 319 break :blk sc; 320 }; 321 errdefer list_scroll.deinit(allocator); 322 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 } }); 323 } 324 325 // the detail pane on the right — a "next" nav box above a scroll of the 326 // selected file's content window. both are repopulated per selection by 327 // populateDetail. 328 { 329 var detail_outer = blk: { 330 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 331 errdefer frame.deinit(allocator); 332 333 { 334 var nav_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 335 errdefer nav_box.deinit(allocator); 336 try frame.children.put(allocator, nav_box.getFocus().id, .{ .widget = .{ .box = nav_box }, .rect = null, .min_size = null }); 337 } 338 { 339 // the content scroll wrapped in a bordered box; build sets the 340 // border single normally and double when the content is focused 341 // (the content box itself is borderless). 342 var scroll_frame = blk2: { 343 var detail_scroll = blk3: { 344 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 345 errdefer detail_inner.deinit(allocator); 346 break :blk3 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }); 347 }; 348 errdefer detail_scroll.deinit(allocator); 349 var sf = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .single, .rounded_corners = true, .direction = .vert }); 350 errdefer sf.deinit(allocator); 351 // the wrapper's selected child is its scroll, so the focus 352 // chain reaches the content box (populateDetail points the 353 // scroll's inner box at it). 354 sf.getFocus().child_id = detail_scroll.getFocus().id; 355 try sf.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 356 break :blk2 sf; 357 }; 358 errdefer scroll_frame.deinit(allocator); 359 // entering the detail pane lands on the content scroll (focus 360 // recovery descends the frame's selected child). the nav links 361 // above are reached by scrolling to the top, then up. 362 frame.getFocus().child_id = scroll_frame.getFocus().id; 363 // shrink, so the fill scroll yields the nav box's row 364 // instead of consuming the frame's whole height. 365 try frame.children.put(allocator, scroll_frame.getFocus().id, .{ .widget = .{ .box = scroll_frame }, .rect = null, .min_size = null, .flex = .shrink }); 366 } 367 break :blk frame; 368 }; 369 errdefer detail_outer.deinit(allocator); 370 detail_outer.getFocus().mode = .mouse; 371 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 372 } 373 374 box.getFocus().child_id = box.children.keys()[list_index]; 375 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 376 377 // focus lives in the split; the banner isn't focusable. 378 outer.getFocus().child_id = outer.children.keys()[content_index]; 379 return .{ .box = outer, .data = data, .session = session, .shown_index = null }; 380 } 381 382 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 383 var row = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 384 errdefer row.deinit(allocator); 385 row.getFocus().mode = .all; 386 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 387 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 388 } 389 390 // the selected file's contents as a single focusable multi-line text box. 391 // it's borderless — the border lives on the surrounding scroll frame, which 392 // doubles when this content is focused. a non-empty `link` makes activating 393 // the box follow it, like addRow. 394 fn addContentBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 395 _ = self; 396 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = null, .wrap_kind = .none }); 397 errdefer tb.deinit(allocator); 398 tb.getFocus().mode = .all; 399 if (link.len != 0) tb.getFocus().kind = .{ .custom = link }; 400 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 401 } 402 403 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 404 self.box.deinit(allocator); 405 } 406 407 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 408 return &self.box.children.values()[content_index].widget.box; 409 } 410 411 fn header(self: *View) *Header.View { 412 return &self.box.children.values()[header_index].widget.repo_files_header; 413 } 414 415 fn headerActive(self: *View) bool { 416 return self.box.getFocus().child_id == self.header().getFocus().id; 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 // the selected list row shows a border (the focused TextBox upgrades it 480 // to a double border itself); the rest stay borderless. 481 const lb = self.listBox(); 482 for (lb.children.keys(), lb.children.values()) |id, *child| { 483 switch (child.widget) { 484 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 485 else => {}, 486 } 487 } 488 489 // the content scroll's border is single normally and double when the 490 // content is focused (the content box itself is borderless). 491 const has_content = self.detailInner().children.count() > 0; 492 const content_focused = if (root_focus.grandchild_id) |g| self.detailInner().children.contains(g) else false; 493 self.detailScrollFrame().options.border_style = if (!has_content) null else if (content_focused) .double else .single; 494 495 // cap the list at list_max_width only while the detail pane fits beside 496 // it. the box drops the detail pane when the width can't hold both 497 // minimums, so when it's that narrow we lift the cap and let the list 498 // fill the whole width. 499 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 500 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 501 502 // stretch the detail pane and its bordered scroll frame across the rest 503 // of the width so the pane fills the area rather than shrinking to its 504 // content, like the refs view's columns. the content scroll inside keeps 505 // its natural width: forcing it to the full inner width would make it one 506 // column too wide whenever the vertical scrollbar appears, leaving a 507 // phantom horizontal scroll. (height fills via the scroll clipping to the 508 // viewport.) 509 if (constraint.max_size.width) |w| { 510 const detail_w = if (both_panes_fit) w - list_max_width else w; 511 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_w, .height = null }; 512 self.detailOuter().children.values()[detail_scroll_index].min_size = .{ .width = detail_w, .height = null }; 513 } else { 514 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 515 self.detailOuter().children.values()[detail_scroll_index].min_size = null; 516 } 517 518 // crossing panes only re-selects the content box's child (focusList / 519 // focusDetail); when the window is too narrow to show both, the pane that 520 // was holding focus is dropped here. the framework recovers focus after 521 // the top-level build by re-deriving it down the selected-child chain, so 522 // there's nothing to fix up afterward. 523 try self.box.build(allocator, constraint, root_focus); 524 525 // clamp the list's initial offset (a selected row near the end of the 526 // list) against this build's grids, for the next frame. initRoot's 527 // pre-sizing build is unbounded — its full-content "viewport" would 528 // clamp the offset to zero, so only a bounded build counts. 529 if (constraint.max_size.height != null) self.listScroll().clampToContent(); 530 } 531 532 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 533 const cur = self.selectedRowIndex(); 534 if (std.meta.eql(cur, self.shown_index)) return; 535 try self.populateDetail(allocator); 536 self.shown_index = cur; 537 } 538 539 fn populateDetail(self: *View, allocator: std.mem.Allocator) !void { 540 const nav_box = self.navBox(); 541 const inner = self.detailInner(); 542 543 for (nav_box.children.values()) |*child| child.widget.deinit(allocator); 544 nav_box.children.clearAndFree(allocator); 545 nav_box.getFocus().child_id = null; 546 for (inner.children.values()) |*child| child.widget.deinit(allocator); 547 inner.children.clearAndFree(allocator); 548 inner.getFocus().child_id = null; 549 550 // only files have contents to show; directories and the ".." row leave 551 // the pane empty. 552 if (self.selectedEntry()) |entry| { 553 if (!entry.is_dir) { 554 // the window links sit above the scroll, so they stay put 555 // while the content scrolls. 556 if (entry.window_start > 0) try self.addNavLink(allocator, nav_box, "scroll to top", entry, 0); 557 if (entry.has_more) try self.addNavLink(allocator, nav_box, "next lines →", entry, lineNumber(entry.window_start + file_page)); 558 if (!entry.loaded) { 559 // the placeholder carries the file's "a:" link, so enter 560 // (or a click) here loads the file like on its list row. 561 const path = try childDir(self.session.page_arena.allocator(), self.data.dir, entry.name); 562 const link = try fileLink(self.session.page_arena, self.data, path, false, 0); 563 try self.addContentBox(allocator, inner, "(press enter to load this file)", link); 564 } else if (entry.is_binary) { 565 try self.addContentBox(allocator, inner, "(binary file)", ""); 566 } else { 567 const text = try numberedContent(self.session.page_arena.allocator(), entry.lines, entry.window_start); 568 try self.addContentBox(allocator, inner, text, ""); 569 } 570 } 571 } 572 // point each box at its first child so focus can descend into it. 573 if (nav_box.children.count() > 0) nav_box.getFocus().child_id = nav_box.children.keys()[0]; 574 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 575 576 // reserve the nav row's height (the shrink scroll frame yields it). 577 self.detailOuter().children.values()[detail_nav_index].min_size = 578 if (nav_box.children.count() > 0) .{ .width = null, .height = row_height } else null; 579 580 // reset the scroll to the top for the newly-shown file: directly on the 581 // terminal (the wasm offset), and via a version bump on the web (so the 582 // renderer's scroll id changes and JS drops the preserved position). 583 const sc = self.detailScroll(); 584 sc.x = 0; 585 sc.y = 0; 586 sc.getFocus().version +%= 1; 587 } 588 589 // a focusable "next" link above the content. it's an `a:` link to the 590 // selected file at `target_start`, so activating it (the host follows the 591 // link) reloads the page on the next window. 592 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_start: usize) !void { 593 const page_arena = self.session.page_arena; 594 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 595 const route = self.data.filesRoute(path, target_start) orelse return error.RouteTooLong; 596 const link = try std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 597 var tb = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 598 errdefer tb.deinit(allocator); 599 tb.getFocus().mode = .all; 600 tb.getFocus().kind = .{ .custom = link }; 601 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 602 } 603 604 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 605 if (self.headerActive()) { 606 if (key == .arrow_down) { 607 root_focus.setFocus(self.contentBox().getFocus().id); 608 } else { 609 try self.header().input(allocator, key, root_focus); 610 } 611 return; 612 } 613 if (key == .arrow_up and self.contentAtTop() and self.header().focusCloneUrl(root_focus)) return; 614 if (self.detailActive()) { 615 try self.detailInput(key, root_focus); 616 } else { 617 try self.listInput(key, root_focus); 618 } 619 } 620 621 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 622 // up/down (and the scroll wheel) move the selection; page up/down jump a 623 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 624 // directory (or "..") row are turned into navigation by the host 625 // (crossPageLink) before reaching here. 626 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 627 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 628 return; 629 } 630 switch (key) { 631 .enter, .arrow_right => try self.focusDetail(root_focus), 632 else => {}, 633 } 634 } 635 636 // whether focus is on the content box (vs. a "previous"/"next" nav link). 637 fn focusOnContent(self: *View, root_focus: *Focus) bool { 638 const g = root_focus.grandchild_id orelse return false; 639 return self.detailInner().children.contains(g); 640 } 641 642 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 643 const sc = self.detailScroll(); 644 const on_content = self.focusOnContent(root_focus); 645 // the "next" link sits above the content scroll. on the link, left returns 646 // to the file list and down drops into the content. in the content, the 647 // terminal scrolls by offset and reaching the top then pressing up crosses 648 // back up to the link; on the web vertical scrolling is the browser's job 649 // so up just crosses to the link. 650 switch (key) { 651 .arrow_left => { 652 if (on_content) { 653 if (sc.x > 0) { 654 sc.x -= 1; 655 self.detailScroll().clampToContent(); 656 } else try self.focusList(root_focus); 657 } else if (!self.moveNav(root_focus, -1)) { 658 try self.focusList(root_focus); 659 } 660 }, 661 .arrow_right => { 662 if (on_content) { 663 sc.x += 1; 664 self.detailScroll().clampToContent(); 665 } else _ = self.moveNav(root_focus, 1); 666 }, 667 .arrow_up => { 668 if (on_content) { 669 if (self.session.is_terminal and sc.y > 0) { 670 sc.y -= 1; 671 self.detailScroll().clampToContent(); 672 } else self.focusNav(root_focus); // cross up to the links 673 } 674 }, 675 .arrow_down => { 676 if (on_content) { 677 if (self.session.is_terminal) { 678 sc.y += 1; 679 self.detailScroll().clampToContent(); 680 } 681 } else self.focusContent(root_focus); // links -> content 682 }, 683 .page_up => if (on_content and self.session.is_terminal) { 684 sc.y -= 10; 685 self.detailScroll().clampToContent(); 686 }, 687 .page_down => if (on_content and self.session.is_terminal) { 688 sc.y += 10; 689 self.detailScroll().clampToContent(); 690 }, 691 .home => if (on_content and self.session.is_terminal) { 692 sc.y = 0; 693 self.detailScroll().clampToContent(); 694 }, 695 .end => if (on_content and self.session.is_terminal) { 696 sc.y = std.math.maxInt(isize); 697 self.detailScroll().clampToContent(); 698 }, 699 .mouse => |mouse| switch (mouse.action) { 700 .scroll => |dir| if (on_content and self.session.is_terminal) { 701 sc.y += if (dir == .up) -5 else 5; 702 self.detailScroll().clampToContent(); 703 }, 704 else => {}, 705 }, 706 else => {}, 707 } 708 } 709 710 // focus the first (left-most) nav link; a no-op when there are none, leaving 711 // focus on the content. 712 fn focusNav(self: *View, root_focus: *Focus) void { 713 const nav_box = self.navBox(); 714 if (nav_box.children.count() == 0) return; 715 root_focus.setFocus(nav_box.children.keys()[0]); 716 } 717 718 fn focusContent(self: *View, root_focus: *Focus) void { 719 const inner = self.detailInner(); 720 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 721 root_focus.setFocus(id); 722 } 723 724 // move focus `delta` rows within the nav box; returns false (without moving) 725 // when that would step off either end. 726 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 727 const nav_box = self.navBox(); 728 const keys = nav_box.children.keys(); 729 const cur_id = nav_box.getFocus().child_id orelse return false; 730 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 731 const target = cur + delta; 732 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 733 root_focus.setFocus(keys[@intCast(target)]); 734 return true; 735 } 736 737 // enter the detail pane, landing on the "next" link when there is one (so 738 // right-arrow reaches it), else on the content. a directory or ".." row has 739 // neither, so there's nothing to enter. selecting the frame's child (then 740 // focusing the frame) also handles the too-narrow case where the pane isn't 741 // laid out yet — focus lands inside it after the next build. 742 fn focusDetail(self: *View, root_focus: *Focus) !void { 743 const frame = self.detailOuter(); 744 if (self.navBox().children.count() > 0) { 745 frame.getFocus().child_id = self.navBox().getFocus().id; 746 } else if (self.detailInner().children.count() > 0) { 747 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 748 } else return; 749 root_focus.setFocus(frame.getFocus().id); 750 } 751 752 // return to the list. 753 fn focusList(self: *View, root_focus: *Focus) !void { 754 root_focus.setFocus(self.listScroll().getFocus().id); 755 } 756 757 pub fn clearGrid(self: *View) void { 758 self.box.clearGrid(); 759 } 760 761 pub fn getGrid(self: View) ?Grid { 762 return self.box.getGrid(); 763 } 764 765 pub fn getFocus(self: *View) *Focus { 766 return self.box.getFocus(); 767 } 768 769 fn contentAtTop(self: *View) bool { 770 if (self.detailActive()) { 771 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 772 return self.navBox().children.count() == 0 and 773 (!self.session.is_terminal or self.detailScroll().y == 0); 774 } 775 const lb = self.listBox(); 776 const cid = lb.getFocus().child_id orelse return true; 777 return lb.children.getIndex(cid) == 0; 778 } 779 780 // only the clone-url row sits directly below the repository header. 781 pub fn atTop(self: *View) bool { 782 return self.headerActive() or (!self.header().hasCloneUrl() and self.contentAtTop()); 783 } 784 785 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 786 return self.header().focusCloneUrl(root_focus); 787 } 788 }; 789 790 // join `lines` into one string, each prefixed with its 1-based file line number 791 // (right-aligned in a column wide enough for the last number, then a space), 792 // like the diffs view. `start` is the window's 0-based first line, so numbers 793 // continue across paginated windows. 794 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 795 if (lines.len == 0) return ""; 796 const width = std.fmt.count("{d}", .{start + lines.len}); 797 const indent = try arena.alloc(u8, width); 798 @memset(indent, ' '); 799 800 var out: std.ArrayList(u8) = .empty; 801 for (lines, 0..) |line, i| { 802 if (i != 0) try out.append(arena, '\n'); 803 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 804 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 805 try out.appendSlice(arena, num_str); 806 try out.append(arena, ' '); 807 try out.appendSlice(arena, line); 808 } 809 return out.toOwnedSlice(arena); 810 } 811 812 // the index of the "README"/"README.md" file entry (case-insensitive), or null 813 // if none. used to pick the default selection at the repo root. 814 fn readmeIndex(entries: []const Entry) ?usize { 815 for (entries, 0..) |entry, i| { 816 if (!entry.is_dir and isReadme(entry.name)) return i; 817 } 818 return null; 819 } 820 821 fn isReadme(name: []const u8) bool { 822 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 823 } 824 825 // the index of the entry the route named (data.selected_file), or null if none. 826 fn selectedFileIndex(data: *const Self) ?usize { 827 const name = data.selected_file orelse return null; 828 for (data.entries, 0..) |entry, i| { 829 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 830 } 831 return null; 832 } 833 834 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 835 // following it navigates to that directory's listing. 836 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 837 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 838 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 839 } 840 841 // a link to the file's route. a preloaded file gets an "ai:" link 842 // (crossPageLink ignores it, so a click selects the row in place and shows its 843 // contents); an unloaded file gets an "a:" link, so activating it reloads the 844 // page with that file selected and its content read. 845 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 846 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 847 const prefix = if (loaded) "ai:" else "a:"; 848 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 849 } 850 851 // the url `line` value for a content window: its first line's 1-based number, 852 // except the first window, which serializes as unset (0). 853 fn lineNumber(window_start: usize) usize { 854 return if (window_start == 0) 0 else window_start + 1; 855 } 856 857 // `dir`/`name`, or just `name` at the root. 858 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 859 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 860 } 861 862 // `dir` with its last segment removed ("" when `dir` has a single segment). 863 fn parentDir(dir: []const u8) []const u8 { 864 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 865 return dir[0..slash]; 866 } 867 868 // the "<ref_or_oid> <value>" banner shown above the listing. 869 pub const Header = struct { 870 content: []const u8, 871 872 // `value` arrives url-encoded, so decode it for display. 873 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 874 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 875 return .{ 876 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 877 }; 878 } 879 880 pub const View = struct { 881 box: wgt.Box(ui.Widget), 882 data: *const Header, 883 884 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !Header.View { 885 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 886 errdefer box.deinit(allocator); 887 888 if (session.data.host_kind == .server) { 889 if (try ui.widget.CopyableText.initClone(allocator, session, location)) |value| { 890 var clone_url = value; 891 errdefer clone_url.deinit(allocator); 892 const min_width = clone_url.minWidth(); 893 box.getFocus().child_id = clone_url.getFocus().id; 894 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 895 } 896 } 897 898 var spacer = try ui.widget.Spacer.init(allocator); 899 errdefer spacer.deinit(allocator); 900 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 901 902 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 903 errdefer text_box.deinit(allocator); 904 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 905 906 return .{ .box = box, .data = data }; 907 } 908 909 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 910 self.box.deinit(allocator); 911 } 912 913 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 914 self.clearGrid(); 915 try self.box.build(allocator, constraint, root_focus); 916 } 917 918 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 919 const clone_url = self.cloneUrl() orelse return; 920 try clone_url.input(allocator, key, root_focus); 921 } 922 923 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 924 const clone_url = self.cloneUrl() orelse return false; 925 root_focus.setFocus(clone_url.getFocus().id); 926 return true; 927 } 928 929 pub fn hasCloneUrl(self: *Header.View) bool { 930 return self.cloneUrl() != null; 931 } 932 933 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 934 for (self.box.children.values()) |*child| switch (child.widget) { 935 .copyable_text => |*copyable_text| return copyable_text, 936 else => {}, 937 }; 938 return null; 939 } 940 941 pub fn clearGrid(self: *Header.View) void { 942 self.box.clearGrid(); 943 } 944 945 pub fn getGrid(self: Header.View) ?Grid { 946 return self.box.getGrid(); 947 } 948 949 pub fn getFocus(self: *Header.View) *Focus { 950 return self.box.getFocus(); 951 } 952 }; 953 }; 954