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