╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const xit = @import("xit"); 6 const rp = xit.repo; 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 commits a page shows before a "next" link appears. 17 const page_size = 20; 18 // how many diff hunks one window of a commit's diff shows; "next"/"previous" 19 // move to the adjacent window. 20 const diff_page = 10; 21 // the largest commit message the detail view will read in full. 22 const max_message_size = 64 * 1024; 23 24 pub const Hunk = struct { 25 path: ?[]const u8 = null, // the file this hunk belongs to 26 lines: []const []const u8, 27 }; 28 29 // one commit on the current page, with its diff against its first parent 30 // pre-rendered up to the budget so the web client can show it without a repo. 31 pub const Commit = struct { 32 oid: []const u8, 33 date: []const u8, // "YYYY-MM-DD" 34 message: []const u8, // trimmed, may be multi-line 35 hunks: []const Hunk, 36 // the hunk index this window starts at (0 = the first window). 37 window_start: usize, 38 // whether hunks exist before this window / after it. 39 has_prev: bool, 40 has_more: bool, 41 }; 42 43 // "owner/name", so the view can build /repo/owner/name/commits/... links. 44 identity: []const u8, 45 // the resolved ref/oid this log walks from (the default branch when the route 46 // didn't name one), so the page can canonicalize its url to it. 47 ref_or_oid: ui.RoutablePage.RefOrOid, 48 ref_or_oid_value: []const u8, 49 // the file the top commit's diff is filtered to ("" = every file). 50 path: []const u8 = "", 51 commits: []const Commit, 52 // the first oid of the next page, or null when this is the last page. 53 next_start: ?[]const u8, 54 // the "viewing <ref> <value>" banner shown above the log. 55 header: Header, 56 57 const Self = @This(); 58 59 // walk the log for an opened repo. generic over the repo's backend and hash 60 // kind so the oid buffers and diff types it threads through match the repo's 61 // opts. `start` is the hunk index the selected commit's (the first one, the 62 // walk root) diff window starts at; 0 is the first window, moved by 63 // "next"/"previous". walks with the arena's backing allocator (transient; the 64 // commits we keep are duped into the page arena so they outlive it). 65 pub fn init( 66 comptime repo_kind: rp.RepoKind, 67 comptime repo_opts: rp.RepoOpts(repo_kind), 68 arena: *std.heap.ArenaAllocator, 69 repo: *rp.Repo(repo_kind, repo_opts), 70 io: std.Io, 71 gpa: std.mem.Allocator, 72 identity: []const u8, 73 requested_ref_or_oid: ?ui.RoutablePage.RefOrOid, 74 requested_value: []const u8, 75 start: usize, 76 path: []const u8, 77 ) !Self { 78 const aa = arena.allocator(); 79 const hex_len = ui.ResolvedRefOrOid(repo_kind, repo_opts).hex_len; 80 81 // resolve the requested ref (or the default branch) to the commit oid to 82 // walk from. an explicitly named ref that doesn't resolve is a bad url 83 // (NotFound -> 404); the default-branch path falls through to empty. 84 const resolved = (try ui.ResolvedRefOrOid(repo_kind, repo_opts).init(repo, io, aa, requested_ref_or_oid, requested_value)) orelse { 85 if (requested_ref_or_oid != null) return error.NotFound; 86 return emptyResult(aa, identity, .branch, requested_value, path); 87 }; 88 var start_arr = [1][hex_len]u8{resolved.oid}; 89 const start_oids: []const [hex_len]u8 = start_arr[0..1]; 90 91 // collect this page's commit metadata, plus a peek at the one after it (its 92 // oid is the next page's start). the diff for each is rendered afterward, so 93 // the log iterator is closed before opening per-commit diff iterators. 94 var buf: [page_size]Commit = undefined; 95 var oids: [page_size][hex_len]u8 = undefined; 96 var count: usize = 0; 97 var next_start: ?[]const u8 = null; 98 { 99 var iter = repo.log(io, gpa, start_oids) catch return emptyResult(aa, identity, resolved.ref_or_oid, resolved.value, path); 100 defer iter.deinit(); 101 while (try iter.next(gpa)) |commit_object| { 102 defer commit_object.deinit(); 103 if (count == page_size) { 104 next_start = try aa.dupe(u8, &commit_object.oid); 105 break; 106 } 107 const md = commit_object.content.commit.metadata; 108 @memcpy(&oids[count], &commit_object.oid); 109 buf[count] = .{ 110 .oid = try aa.dupe(u8, &commit_object.oid), 111 .date = try formatDate(aa, md.timestamp), 112 .message = try readMessage(repo_kind, repo_opts, aa, commit_object), 113 .hunks = &.{}, 114 .window_start = 0, 115 .has_prev = false, 116 .has_more = false, 117 }; 118 count += 1; 119 } 120 } 121 122 // render each commit's diff (best effort: a failed diff leaves it empty). 123 // the diff machinery isn't wasm-clean and the wasm client never runs it (it 124 // renders from the snapshot), so gate it out of the wasm build. 125 if (!builtin.cpu.arch.isWasm()) { 126 for (buf[0..count], oids[0..count], 0..) |*commit, oid, i| { 127 // the selected commit (the first, == start_oid) shows the window the 128 // url asks for; the rest show their first window. 129 // the path filter only applies to the top commit (the walk root). 130 const window_start = if (i == 0) start else 0; 131 const rendered = renderCommitDiff(repo_kind, repo_opts, io, gpa, aa, repo, oid, window_start, diff_page, if (i == 0) path else "") catch 132 RenderedDiff{ .hunks = &.{}, .has_prev = false, .has_more = false }; 133 commit.hunks = rendered.hunks; 134 commit.window_start = window_start; 135 commit.has_prev = rendered.has_prev; 136 commit.has_more = rendered.has_more; 137 } 138 } 139 140 return .{ 141 .identity = try aa.dupe(u8, identity), 142 .ref_or_oid = resolved.ref_or_oid, 143 .ref_or_oid_value = resolved.value, 144 .path = try aa.dupe(u8, path), 145 .commits = try aa.dupe(Commit, buf[0..count]), 146 .next_start = next_start, 147 .header = try Header.init(aa, resolved.ref_or_oid, resolved.value), 148 }; 149 } 150 151 // an empty listing pinned to a ref, for the wasm / no-repo / unresolved paths. 152 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8, path: []const u8) !Self { 153 return .{ 154 .identity = try aa.dupe(u8, identity), 155 .ref_or_oid = ref_or_oid, 156 .ref_or_oid_value = try aa.dupe(u8, value), 157 .path = try aa.dupe(u8, path), 158 .commits = &.{}, 159 .next_start = null, 160 .header = try Header.init(aa, ref_or_oid, value), 161 }; 162 } 163 164 const RenderedDiff = struct { 165 hunks: []const Hunk, 166 has_prev: bool, 167 has_more: bool, 168 }; 169 170 // render the window [start, start+len) of a commit's diff against its first 171 // parent into `arena`-owned hunks. has_prev/has_more flag adjacent windows. 172 // a non-empty `path` filters to that file, so the window indexes its hunks. 173 fn renderCommitDiff( 174 comptime repo_kind: rp.RepoKind, 175 comptime repo_opts: rp.RepoOpts(repo_kind), 176 io: std.Io, 177 gpa: std.mem.Allocator, 178 arena: std.mem.Allocator, 179 repo: *rp.Repo(repo_kind, repo_opts), 180 oid: [xit.hash.hexLen(repo_opts.hash)]u8, 181 start: usize, 182 len: usize, 183 path: []const u8, 184 ) !RenderedDiff { 185 const empty = RenderedDiff{ .hunks = &.{}, .has_prev = false, .has_more = false }; 186 187 // load the commit so we can diff it against its first parent. 188 var start_oids = [_][xit.hash.hexLen(repo_opts.hash)]u8{oid}; 189 var commit_iter = repo.log(io, gpa, start_oids[0..1]) catch return empty; 190 defer commit_iter.deinit(); 191 const commit_object = (commit_iter.next(gpa) catch return empty) orelse return empty; 192 defer commit_object.deinit(); 193 194 const parent_maybe = commit_object.content.commit.metadata.firstParent(); 195 196 var tree_diff = repo.treeDiff(io, gpa, parent_maybe, &commit_object.oid) catch return empty; 197 defer tree_diff.deinit(); 198 199 var file_iter = repo.filePairs(io, gpa, .{ .tree = .{ .tree_diff = &tree_diff } }) catch return empty; 200 201 var hunks: std.ArrayList(Hunk) = .empty; 202 var index: usize = 0; // running hunk index across all files 203 var has_more = false; 204 205 file_loop: while (file_iter.next() catch null) |pair_val| { 206 var pair = pair_val; 207 defer pair.deinit(); 208 209 if (path.len != 0 and !std.mem.eql(u8, pair.path, path)) continue; 210 211 var hunk_iter = df.HunkIterator(repo_kind, repo_opts).init(gpa, &pair.a, &pair.b) catch continue; 212 defer hunk_iter.deinit(gpa); 213 214 // the path label rides on the first of this file's hunks we actually show. 215 var path_attached = false; 216 while (try hunk_iter.next(gpa)) |hunk_val| { 217 var hunk = hunk_val; 218 defer hunk.deinit(gpa); 219 220 const i = index; 221 index += 1; 222 if (i < start) continue; // before this window 223 if (hunks.items.len >= len) { 224 has_more = true; 225 break :file_loop; 226 } 227 var lines: std.ArrayList([]const u8) = .empty; 228 try appendHunkLines(repo_kind, repo_opts, arena, &lines, &hunk_iter, &hunk); 229 try hunks.append(arena, .{ 230 .path = if (path_attached) null else try arena.dupe(u8, pair.path), 231 .lines = try lines.toOwnedSlice(arena), 232 }); 233 path_attached = true; 234 } 235 } 236 237 return .{ .hunks = try hunks.toOwnedSlice(arena), .has_prev = start > 0, .has_more = has_more }; 238 } 239 240 // append a hunk's header and its edit lines, each prefixed with a right-aligned 241 // line number in a column wide enough for the hunk's largest number, then a space 242 fn appendHunkLines( 243 comptime repo_kind: rp.RepoKind, 244 comptime repo_opts: rp.RepoOpts(repo_kind), 245 arena: std.mem.Allocator, 246 lines: *std.ArrayList([]const u8), 247 hunk_iter: *df.HunkIterator(repo_kind, repo_opts), 248 hunk: *df.Hunk(repo_kind, repo_opts), 249 ) !void { 250 var max_num: usize = 1; 251 for (hunk.edits.items) |edit| { 252 const n = editLineNum(edit) + 1; 253 if (n > max_num) max_num = n; 254 } 255 const width = std.fmt.count("{d}", .{max_num}); 256 // a run of spaces sliced to each line's leading pad. 257 const indent = try arena.alloc(u8, width); 258 @memset(indent, ' '); 259 260 for (hunk.edits.items) |edit| { 261 const text = switch (edit) { 262 .eql => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 263 .ins => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 264 .del => |e| try hunk_iter.line_iter_a.get(e.old_line.num), 265 }; 266 defer switch (edit) { 267 .eql, .ins => hunk_iter.line_iter_b.free(text), 268 .del => hunk_iter.line_iter_a.free(text), 269 }; 270 const prefix: u8 = switch (edit) { 271 .eql => ' ', 272 .ins => '+', 273 .del => '-', 274 }; 275 const num_str = try std.fmt.allocPrint(arena, "{d}", .{editLineNum(edit) + 1}); 276 try lines.append(arena, try std.fmt.allocPrint(arena, "{s}{s} {c}{s}", .{ 277 indent[0 .. width - num_str.len], num_str, prefix, text, 278 })); 279 } 280 } 281 282 // the line number to show for an edit: the new-side number for kept and inserted 283 // lines, the old-side number for deletions (0-based in the diff machinery). 284 fn editLineNum(edit: df.Edit) usize { 285 return switch (edit) { 286 .eql => |e| e.new_line.num, 287 .ins => |e| e.new_line.num, 288 .del => |e| e.old_line.num, 289 }; 290 } 291 292 // "YYYY-MM-DD" for a unix timestamp. 293 fn formatDate(arena: std.mem.Allocator, timestamp: u64) ![]const u8 { 294 const epoch_secs = std.time.epoch.EpochSeconds{ .secs = timestamp }; 295 const year_day = epoch_secs.getEpochDay().calculateYearDay(); 296 const month_day = year_day.calculateMonthDay(); 297 return std.fmt.allocPrint(arena, "{d:0>4}-{d:0>2}-{d:0>2}", .{ 298 year_day.year, 299 month_day.month.numeric(), 300 month_day.day_index + 1, 301 }); 302 } 303 304 // the whole commit message, trimmed and duped into `aa`. an oversized 305 // message falls back to that first line. 306 fn readMessage( 307 comptime repo_kind: rp.RepoKind, 308 comptime repo_opts: rp.RepoOpts(repo_kind), 309 aa: std.mem.Allocator, 310 commit_object: *xit.object.Object(repo_kind, repo_opts), 311 ) ![]const u8 { 312 const md = commit_object.content.commit.metadata; 313 try commit_object.object_reader.seekTo(commit_object.content.commit.message_position); 314 const message = commit_object.object_reader.interface.allocRemaining(aa, .limited(max_message_size)) catch 315 return try aa.dupe(u8, std.mem.trim(u8, md.message orelse "", " \t\r\n")); 316 return std.mem.trim(u8, message, " \t\r\n"); 317 } 318 319 // the first line of a commit message, for the one-row list entries. 320 fn firstLine(message: []const u8) []const u8 { 321 const nl = std.mem.indexOfScalar(u8, message, '\n'); 322 return if (nl) |i| std.mem.trimEnd(u8, message[0..i], " \t\r") else message; 323 } 324 325 pub const View = struct { 326 // a vertical stack: the "viewing <ref>" banner on top, then a horizontal 327 // split with the commit list on the left and a diff pane on the right 328 // showing the selected commit's diff. 329 box: wgt.Box(ui.Widget), // vert: [header_index] = banner, [content_index] = split 330 data: *const Self, 331 session: *ui.Session, 332 // the commit whose diff the pane currently shows (index into data.commits). 333 diffed_index: ?usize, 334 // the diff pane's message row, capped to the pane width so it word-wraps. 335 message_id: ?usize, 336 337 const header_index: usize = 0; 338 const content_index: usize = 1; 339 // indices within the content box (the horizontal split). 340 const list_index: usize = 0; 341 const diff_index: usize = 1; 342 const list_max_width: usize = 40; 343 const diff_min_width: usize = 40; 344 345 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 346 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 347 errdefer outer.deinit(allocator); 348 349 // the ref banner at the top. 350 { 351 var header = try Header.View.init(allocator, &data.header, session); 352 errdefer header.deinit(allocator); 353 try outer.children.put(allocator, header.getFocus().id, .{ .widget = .{ .repo_commits_header = header }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 354 } 355 356 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 357 errdefer box.deinit(allocator); 358 359 // the commit list (one focusable row each), plus a "next" link 360 { 361 var list_scroll = blk: { 362 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 363 errdefer list_box.deinit(allocator); 364 for (data.commits) |commit| { 365 // an in-page "ai:" anchor so a commit row is clickable with 366 // js off (the browser follows it, rooting the list there); 367 // with wasm the click just selects it and swaps the diff pane. 368 try addRow(allocator, &list_box, firstLine(commit.message), try commitRowLink(session.page_arena, data.identity, commit.oid, "")); 369 } 370 if (data.next_start) |next| { 371 try addRow(allocator, &list_box, "next →", try commitsLink(session.page_arena, data.identity, next, 0, "")); 372 } 373 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 374 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 375 }; 376 errdefer list_scroll.deinit(allocator); 377 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 } }); 378 } 379 380 // the diff pane — a frame around a scroll of the hunks 381 { 382 var diff_outer = blk: { 383 var diff_scroll = blk2: { 384 var diff_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 385 errdefer diff_inner.deinit(allocator); 386 // fill the pane (content top-left, scroll bars pinned to the 387 // edges) rather than shrinking to the diff content. 388 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = diff_inner }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }); 389 }; 390 errdefer diff_scroll.deinit(allocator); 391 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 392 errdefer frame.deinit(allocator); 393 // the frame's selected child is its scroll, so the focus chain 394 // reaches a hunk (populateDiff points the scroll's inner box at 395 // one), letting focus recovery descend into the diff pane after 396 // it's laid out beside a too-narrow list. 397 frame.getFocus().child_id = diff_scroll.getFocus().id; 398 try frame.children.put(allocator, diff_scroll.getFocus().id, .{ .widget = .{ .scroll = diff_scroll }, .rect = null, .min_size = null }); 399 break :blk frame; 400 }; 401 errdefer diff_outer.deinit(allocator); 402 try box.children.put(allocator, diff_outer.getFocus().id, .{ .widget = .{ .box = diff_outer }, .rect = null, .min_size = .{ .width = diff_min_width, .height = null } }); 403 } 404 405 box.getFocus().child_id = box.children.keys()[list_index]; 406 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 407 408 // focus lives in the split; the banner isn't focusable. 409 outer.getFocus().child_id = outer.children.keys()[content_index]; 410 411 return .{ 412 .box = outer, 413 .data = data, 414 .session = session, 415 .diffed_index = null, 416 .message_id = null, 417 }; 418 } 419 420 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 421 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 422 errdefer row.deinit(allocator); 423 row.getFocus().focusable = true; 424 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 425 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 426 } 427 428 // one hunk as a focusable multi-line text box. its lines sit flush (the 429 // inner Text rows have no border). `lines` are joined into the page arena 430 // (text_box borrows it). 431 fn addHunkBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), lines: []const []const u8) !void { 432 if (lines.len == 0) return; 433 const text = try std.mem.join(self.session.page_arena.allocator(), "\n", lines); 434 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 435 errdefer tb.deinit(allocator); 436 tb.getFocus().focusable = true; 437 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 438 } 439 440 // a focusable file-path label shown above the first hunk of each file. it 441 // links to this page filtered to that file, so activating it navigates 442 // there. 443 fn addPathBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), path: []const u8, oid: []const u8) !void { 444 const link = try commitsLink(self.session.page_arena, self.data.identity, oid, 0, path); 445 var tb = try wgt.TextBox(ui.Widget).init(allocator, path, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 446 errdefer tb.deinit(allocator); 447 tb.getFocus().focusable = true; 448 tb.getFocus().kind = .{ .custom = link }; 449 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 450 } 451 452 // a focusable window-navigation row ("previous"/"next"). it's a link to this 453 // commit's route at `target_start`, so activating it (the host follows the 454 // "a:" link) reloads the page on the adjacent window — same on TUI and web. 455 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, oid: []const u8, target_start: usize, path: []const u8) !void { 456 const link = try commitsLink(self.session.page_arena, self.data.identity, oid, target_start, path); 457 var tb = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 458 errdefer tb.deinit(allocator); 459 tb.getFocus().focusable = true; 460 tb.getFocus().kind = .{ .custom = link }; 461 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 462 } 463 464 // a focusable row for the top of the diff pane linking to the files tab at 465 // this commit's tree, so its files are always viewable as of this object. 466 fn addViewFilesLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), oid: []const u8) !void { 467 const link = try filesObjectLink(self.session.page_arena, self.data.identity, oid); 468 var tb = try wgt.TextBox(ui.Widget).init(allocator, "view files at this commit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 469 errdefer tb.deinit(allocator); 470 tb.getFocus().focusable = true; 471 tb.getFocus().kind = .{ .custom = link }; 472 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 473 } 474 475 // the selected commit's full message, focusable like the rows around it. it 476 // word-wraps, which needs a bounded width: the diff scroll grants its rows 477 // an unbounded one, so build caps this row via `message_id`. 478 fn addMessageBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), message: []const u8) !void { 479 if (message.len == 0) return; 480 var tb = try wgt.TextBox(ui.Widget).init(allocator, message, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = "commit message" }); 481 errdefer tb.deinit(allocator); 482 tb.getFocus().focusable = true; 483 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 484 self.message_id = tb.getFocus().id; 485 } 486 487 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 488 self.box.deinit(allocator); 489 } 490 491 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 492 return &self.box.children.values()[content_index].widget.box; 493 } 494 495 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 496 return &self.contentBox().children.values()[list_index].widget.scroll; 497 } 498 499 fn listBox(self: *View) *wgt.Box(ui.Widget) { 500 return &self.listScroll().child.box; 501 } 502 503 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 504 return &self.contentBox().children.values()[diff_index].widget.box; 505 } 506 507 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 508 return &self.diffOuter().children.values()[0].widget.scroll; 509 } 510 511 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 512 return &self.diffScroll().child.box; 513 } 514 515 fn diffActive(self: *View) bool { 516 const content = self.contentBox(); 517 const cid = content.getFocus().child_id orelse return false; 518 return content.children.getIndex(cid) == diff_index; 519 } 520 521 // the selected commit's index, or null when the "next" row is selected. 522 fn selectedCommitIndex(self: *View) ?usize { 523 const lb = self.listBox(); 524 const cid = lb.getFocus().child_id orelse return null; 525 const idx = lb.children.getIndex(cid) orelse return null; 526 if (idx >= self.data.commits.len) return null; 527 return idx; 528 } 529 530 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 531 self.clearGrid(); 532 533 // swap the diff pane to the selected commit when it changes. 534 try self.refreshDiff(allocator); 535 536 // mirror the focused commit into the url so it updates as the selection 537 // moves, but only while focus is inside this view (the list or diff). when 538 // focus sits on the header tab, use the page's base /commits route. 539 if (root_focus.grandchild_id) |g| { 540 if (self.box.getFocus().children.contains(g)) { 541 if (self.selectedCommitIndex()) |sel| { 542 // mirror the commit's current diff window so the url stays 543 // linkable (0 for any commit but the windowed start one). 544 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, self.data.commits[sel].oid, self.data.commits[sel].window_start, if (sel == 0) self.data.path else "")) |route| 545 self.session.data.current_page = route; 546 } 547 } 548 } 549 550 // the selected list row shows a border (the focused TextBox upgrades it 551 // to a double border itself); the rest stay borderless. 552 const lb = self.listBox(); 553 for (lb.children.keys(), lb.children.values()) |id, *child| { 554 switch (child.widget) { 555 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 556 else => {}, 557 } 558 } 559 560 // cap the list at list_max_width only while the diff pane fits beside it. 561 // the box drops the diff when the width can't hold both minimums, so when 562 // it's that narrow we lift the cap and let the list fill the whole width. 563 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 564 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 565 566 // stretch the diff pane across the rest of the width so it fills the area 567 // rather than shrinking to its content; its scroll fills the pane. when 568 // too narrow for both, it fills the whole width. 569 const diff_width: usize = if (constraint.max_size.width) |w| 570 (if (both_panes_fit) w - list_max_width else w) 571 else 572 diff_min_width; 573 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_width, .height = null }; 574 575 // cap the message row so it wraps within the pane, leaving room for the 576 // pane's border and its scrollbar column. 577 if (self.message_id) |id| { 578 if (self.diffInner().children.getPtr(id)) |child| { 579 child.max_size = .{ .width = diff_width -| 3, .height = null }; 580 } 581 } 582 583 // the web bounds the layout to the browser viewport like the terminal; 584 // each Scroll's web-native mode hands its full content to a real 585 // scrollable element, so we no longer build unbounded here. 586 // 587 // crossing panes only re-selects the content box's child (focusDiff / 588 // focusList); when the window is too narrow to show both, the pane that 589 // held focus is dropped here. the framework recovers focus after the 590 // top-level build by re-deriving it down the selected-child chain, so 591 // there's nothing to fix up afterward. 592 try self.box.build(allocator, constraint, root_focus); 593 } 594 595 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 596 const sel = self.selectedCommitIndex() orelse return; 597 if (self.diffed_index) |d| if (d == sel) return; 598 try self.populateDiff(allocator, sel); 599 self.diffed_index = sel; 600 } 601 602 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 603 const commit = self.data.commits[sel]; 604 // the path filter only applies to the top commit (the walk root). 605 const path = if (sel == 0) self.data.path else ""; 606 const inner = self.diffInner(); 607 608 for (inner.children.values()) |*child| child.widget.deinit(allocator); 609 inner.children.clearAndFree(allocator); 610 inner.getFocus().child_id = null; 611 self.message_id = null; 612 613 if (path.len != 0) { 614 // the file's path, then a row returning to this commit's unfiltered diff. 615 var label = try wgt.TextBox(ui.Widget).init(allocator, path, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 616 errdefer label.deinit(allocator); 617 label.getFocus().focusable = true; 618 try inner.children.put(allocator, label.getFocus().id, .{ .widget = .{ .text_box = label }, .rect = null, .min_size = null }); 619 try self.addNavLink(allocator, inner, "← all files", commit.oid, 0, ""); 620 } else { 621 try self.addMessageBox(allocator, inner, commit.message); 622 try self.addViewFilesLink(allocator, inner, commit.oid); 623 } 624 625 // a "previous" row and a "next" row at the bottom reload the page on the 626 // adjacent diff window. 627 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page, path); 628 for (commit.hunks) |hunk| { 629 if (path.len == 0) if (hunk.path) |hunk_path| try self.addPathBox(allocator, inner, hunk_path, commit.oid); 630 try self.addHunkBox(allocator, inner, hunk.lines); 631 } 632 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page, path); 633 634 // point the pane at its first row so focus recovery can land here. 635 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 636 637 // reset the scroll to the top for the newly-shown commit: directly on the 638 // terminal (the wasm offset), and via a version bump on the web (so the 639 // renderer's scroll id changes and JS drops the preserved position). 640 const sc = self.diffScroll(); 641 sc.x = 0; 642 sc.y = 0; 643 sc.getFocus().version +%= 1; 644 } 645 646 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 647 _ = allocator; 648 if (self.diffActive()) { 649 try self.diffInput(key, root_focus); 650 } else { 651 try self.listInput(key, root_focus); 652 } 653 } 654 655 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 656 // up/down (and the scroll wheel) move the selection a row; page up/down 657 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 658 // on the "next" row become navigation in the host before reaching here. 659 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 660 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 661 return; 662 } 663 switch (key) { 664 .enter => if (self.selectedCommitIndex() != null) 665 try self.focusDiff(root_focus) 666 else if (self.data.next_start) |next| { 667 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, next, 0, "")) |route| 668 try self.session.navigate(route); 669 }, 670 .arrow_right => try self.focusDiff(root_focus), 671 else => {}, 672 } 673 } 674 675 fn diffInput(self: *View, key: Key, root_focus: *Focus) !void { 676 const sc = self.diffScroll(); 677 switch (key) { 678 // left scrolls horizontally, then leaves for the list once flush left. 679 .arrow_left => { 680 if (sc.x > 0) { 681 sc.x -= 1; 682 self.diffScroll().clampToContent(); 683 } else try self.focusList(root_focus); 684 }, 685 .arrow_right => { 686 sc.x += 1; 687 self.diffScroll().clampToContent(); 688 }, 689 .arrow_up => try self.moveDiff(root_focus, -1), 690 .arrow_down => try self.moveDiff(root_focus, 1), 691 .page_up => try self.pageDiff(root_focus, -10), 692 .page_down => try self.pageDiff(root_focus, 10), 693 .home => try self.jumpDiff(root_focus, false), 694 .end => try self.jumpDiff(root_focus, true), 695 // Enter / a click on a "previous"/"next" row follow its "a:" link in 696 // the host (it reloads the adjacent window), so they don't reach here. 697 .mouse => |mouse| switch (mouse.action) { 698 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 699 else => {}, 700 }, 701 else => {}, 702 } 703 } 704 705 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 706 // line count unless there is a new hunk visible (in which case it is a 707 // hunk count). on the web `delta` is always a hunk count. 708 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 709 const inner = self.diffInner(); 710 const keys = inner.children.keys(); 711 if (keys.len == 0) return; 712 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 713 const target = @as(isize, @intCast(cur)) + delta; 714 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 715 716 if (self.session.is_terminal) { 717 if (in_range and self.hunkVisible(@intCast(target))) { 718 root_focus.setFocus(keys[@intCast(target)]); 719 return; 720 } 721 722 const sc = self.diffScroll(); 723 sc.y += delta * 5; // magnify because it's a line count 724 self.diffScroll().clampToContent(); 725 if (in_range and self.hunkVisible(@intCast(target))) { 726 root_focus.setFocus(keys[@intCast(target)]); 727 } 728 } else { 729 if (in_range) { 730 root_focus.setFocus(keys[@intCast(target)]); 731 } 732 } 733 } 734 735 // page a fixed number of lines, then focus the leading visible hunk 736 // (bottom-most when paging down, top-most when paging up). in the 737 // terminal, `delta` is a line count, and on the web it's a hunk count. 738 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 739 if (self.session.is_terminal) { 740 const sc = self.diffScroll(); 741 sc.y += delta * 5; // magnify because it's a line count 742 self.diffScroll().clampToContent(); 743 try self.focusVisible(root_focus, delta > 0); 744 } else { 745 const inner = self.diffInner(); 746 const keys = inner.children.keys(); 747 if (keys.len == 0) return; 748 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 749 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 750 root_focus.setFocus(keys[target]); 751 } 752 } 753 754 // jump to the first or last hunk. on the web the browser scrolls to the 755 // focused hunk; on the terminal pin the scroll to the top/bottom too. 756 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 757 const inner = self.diffInner(); 758 const keys = inner.children.keys(); 759 if (keys.len == 0) return; 760 if (self.session.is_terminal) { 761 const sc = self.diffScroll(); 762 sc.y = if (to_end) std.math.maxInt(isize) else 0; 763 self.diffScroll().clampToContent(); 764 } 765 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 766 } 767 768 // whether hunk `index` is at least partly within the diff viewport, per the 769 // last build's layout rects (content space) and the current scroll offset. 770 fn hunkVisible(self: *View, index: usize) bool { 771 const inner = self.diffInner(); 772 const sc = self.diffScroll(); 773 const vp = sc.grid orelse return false; 774 const r = inner.children.values()[index].rect orelse return false; 775 const top = sc.y; 776 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 777 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 778 } 779 780 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 781 // the bottom-most visible (for downward motion), else the top-most. 782 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 783 const inner = self.diffInner(); 784 const sc = self.diffScroll(); 785 const vp = sc.grid orelse return; 786 const top = sc.y; 787 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 788 var chosen: ?usize = null; 789 for (inner.children.keys(), inner.children.values()) |id, *child| { 790 const r = child.rect orelse continue; // content-space layout rect 791 const r_top = r.y; 792 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 793 if (r_bot <= top or r_top >= bottom) continue; // not visible 794 chosen = id; 795 if (!prefer_last) break; // first visible 796 } 797 if (chosen) |id| root_focus.setFocus(id); 798 } 799 800 // enter the diff pane. the host arrives here on right-arrow or Enter from the 801 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 802 // case where the pane isn't laid out yet (it gets selected, then focused after 803 // the next build, landing on its remembered hunk). 804 fn focusDiff(self: *View, root_focus: *Focus) !void { 805 if (self.diffInner().children.count() == 0) return; 806 root_focus.setFocus(self.diffOuter().getFocus().id); 807 } 808 809 // return to the list. 810 fn focusList(self: *View, root_focus: *Focus) !void { 811 root_focus.setFocus(self.listScroll().getFocus().id); 812 } 813 814 pub fn clearGrid(self: *View) void { 815 self.box.clearGrid(); 816 } 817 818 pub fn getGrid(self: View) ?Grid { 819 return self.box.getGrid(); 820 } 821 822 pub fn getFocus(self: *View) *Focus { 823 return self.box.getFocus(); 824 } 825 826 // for the parent's "scroll up at the top jumps to the header" check. in the 827 // diff pane that means the first hunk is focused and the diff can't scroll up 828 // any further; otherwise up-arrow stays in the pane (scrolling or moving 829 // between hunks). when the list holds focus, report its selection directly. 830 pub fn getSelectedIndex(self: *View) ?usize { 831 if (self.diffActive()) { 832 const inner = self.diffInner(); 833 const cid = inner.getFocus().child_id orelse return 1; 834 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 835 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 836 } 837 const lb = self.listBox(); 838 const cid = lb.getFocus().child_id orelse return null; 839 return lb.children.getIndex(cid); 840 } 841 }; 842 843 // the "a:" navigation link for the commits page walking from commit `oid` within 844 // `identity`, windowing the selected commit's diff from hunk `start`, filtered 845 // to `path` ("" = every file). 846 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 847 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, start, path) orelse return error.RouteTooLong; 848 const url = try route.toUrl(page_arena); 849 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 850 } 851 852 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 853 // directory, within `identity` ("owner/name"). 854 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 855 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 856 const url = try route.toUrl(page_arena); 857 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 858 } 859 860 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 861 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 862 // in-page (crossPageLink ignores it); the href is only followed with js off. 863 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, path: []const u8) ![]const u8 { 864 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0, path) orelse return error.RouteTooLong; 865 const url = try route.toUrl(page_arena); 866 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 867 } 868 869 // the "viewing <ref_or_oid> <value>" banner shown above the log. 870 pub const Header = struct { 871 content: []const u8, 872 873 // `value` arrives url-encoded, so decode it for display. 874 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 875 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 876 return .{ 877 .content = try std.fmt.allocPrint(aa, "viewing {s} {s}", .{ @tagName(ref_or_oid), decoded }), 878 }; 879 } 880 881 pub const View = struct { 882 box: wgt.Box(ui.Widget), 883 data: *const Header, 884 885 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session) !Header.View { 886 _ = session; 887 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 888 errdefer box.deinit(allocator); 889 890 var text_box = try wgt.TextBox(ui.Widget).init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 891 errdefer text_box.deinit(allocator); 892 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 893 894 return .{ .box = box, .data = data }; 895 } 896 897 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 898 self.box.deinit(allocator); 899 } 900 901 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 902 self.clearGrid(); 903 try self.box.build(allocator, constraint, root_focus); 904 } 905 906 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 907 _ = self; 908 _ = allocator; 909 _ = key; 910 _ = root_focus; 911 } 912 913 pub fn clearGrid(self: *Header.View) void { 914 self.box.clearGrid(); 915 } 916 917 pub fn getGrid(self: Header.View) ?Grid { 918 return self.box.getGrid(); 919 } 920 921 pub fn getFocus(self: *Header.View) *Focus { 922 return self.box.getFocus(); 923 } 924 }; 925 }; 926