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