╭ 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 pub const Diff = @import("Diff.zig"); 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 // the largest message read even when it has fewer than the preview's line 21 // limit. 22 const max_message_size = 100 * 1024; 23 24 // one commit on the current page, with its diff against its first parent 25 // pre-rendered up to the budget so the web client can show it without a repo. 26 pub const Commit = struct { 27 oid: []const u8, 28 date: []const u8, // "YYYY-MM-DD" 29 message: []const u8, // trimmed, may be multi-line 30 // whether `message` is a shortened preview. 31 message_truncated: bool = false, 32 author: ui.Author = .unknown, 33 stats: ?xit.patch.CommitStats = null, 34 window: Diff.Window = .{}, 35 }; 36 37 // where links and clone commands for this repository are rooted. 38 location: ui.RoutablePage.RepoLocation, 39 // the resolved ref/oid this log walks from (the default branch when the route 40 // didn't name one), so the page can canonicalize its url to it. 41 ref_or_oid: ui.RoutablePage.RefOrOid, 42 ref_or_oid_value: []const u8, 43 commit_count: ?u64 = null, 44 commits: []const Commit, 45 // the first oid of the next page, or null when this is the last page. 46 next_start: ?[]const u8, 47 // what the pane shows for the commit the log walks from. 48 content: Content = .{ .diff = .{} }, 49 // the "viewing <ref> <value>" banner shown above the log. 50 header: Header, 51 52 const Self = @This(); 53 54 // the diff and the message are alternatives, so a filtered diff can't also be 55 // a message. the message page reads up to the safety limit above. 56 pub const Content = union(enum) { 57 diff: struct { 58 // the file the top commit's diff is filtered to ("" = every file). 59 path: []const u8 = "", 60 }, 61 message, 62 }; 63 64 // walk the log for an opened repo. generic over the repo's backend and hash 65 // kind so the oid buffers and diff types it threads through match the repo's 66 // opts. `content` is what the walk root's pane shows, the only commit any of 67 // it applies to. walks with the arena's backing allocator (transient; the 68 // commits we keep are duped into the page arena so they outlive it). 69 pub fn init( 70 comptime repo_kind: rp.RepoKind, 71 comptime repo_opts: rp.RepoOpts(repo_kind), 72 arena: *std.heap.ArenaAllocator, 73 repo: *rp.Repo(repo_kind, repo_opts), 74 io: std.Io, 75 gpa: std.mem.Allocator, 76 // the admin db's moment, for resolving author emails to user names (null 77 // in local mode, which has no users) 78 admin_moment: ?evt.AdminDB.HashMap(.read_only), 79 location: ui.RoutablePage.RepoLocation, 80 requested_ref_or_oid: ?ui.RoutablePage.RefOrOid, 81 requested_value: []const u8, 82 content: ui.RoutablePage.RepoCommitsRoute.Content, 83 base_oid_maybe: ?[xit.hash.hexLen(repo_opts.hash)]u8, 84 ) !Self { 85 const aa = arena.allocator(); 86 const hex_len = ui.ResolvedRefOrOid(repo_kind, repo_opts).hex_len; 87 88 // the walk root's message replaces its diff, so it's read whole and its 89 // hunks aren't rendered. the window and the filter are its diff's. 90 const root_message = std.meta.activeTag(content) == .message; 91 const root_start: usize = switch (content) { 92 .diff => |d| d.start, 93 .message => 0, 94 }; 95 const root_path: []const u8 = switch (content) { 96 .diff => |*d| d.path.slice(), 97 .message => "", 98 }; 99 100 // resolve the requested ref (or the default branch) to the commit oid to 101 // walk from. an explicitly named ref that doesn't resolve is a bad url 102 // (NotFound -> 404); the default-branch path falls through to empty. 103 const resolved = (try ui.ResolvedRefOrOid(repo_kind, repo_opts).init(repo, io, aa, requested_ref_or_oid, requested_value)) orelse { 104 if (requested_ref_or_oid != null) return error.NotFound; 105 return emptyResult(aa, location, .branch, requested_value, content); 106 }; 107 var start_arr = [1][hex_len]u8{resolved.oid}; 108 const start_oids: []const [hex_len]u8 = start_arr[0..1]; 109 110 // collect this page's commit metadata, plus a peek at the one after it (its 111 // oid is the next page's start). the diff for each is rendered afterward, so 112 // the log iterator is closed before opening per-commit diff iterators. 113 var buf: [page_size]Commit = undefined; 114 var oids: [page_size][hex_len]u8 = undefined; 115 var count: usize = 0; 116 var next_start: ?[]const u8 = null; 117 { 118 var iter = repo.log(io, gpa, .{ .start_oids = start_oids, .first_parent = true }) catch return emptyResult(aa, location, resolved.ref_or_oid, resolved.value, content); 119 defer iter.deinit(); 120 // omit the base and its ancestors 121 if (base_oid_maybe) |base_oid| try iter.exclude(&base_oid); 122 while (try iter.next(gpa)) |commit_object| { 123 defer commit_object.deinit(); 124 if (count == page_size) { 125 next_start = try aa.dupe(u8, &commit_object.oid); 126 break; 127 } 128 const md = commit_object.content.commit.metadata; 129 @memcpy(&oids[count], &commit_object.oid); 130 const text, const truncated = try readMessage(repo_kind, repo_opts, aa, commit_object, root_message and count == 0); 131 buf[count] = .{ 132 .oid = try aa.dupe(u8, &commit_object.oid), 133 .date = try formatDate(aa, md.timestamp), 134 .message = text, 135 .message_truncated = truncated, 136 .author = try ui.Author.init(admin_moment, arena, md.author orelse ""), 137 .stats = if (repo_kind == .xit) try repo.commitStats(io, gpa, .{ .oid = &commit_object.oid }) else null, 138 }; 139 count += 1; 140 } 141 } 142 143 // render each commit's diff (best effort: a failed diff leaves it empty). 144 // the diff machinery isn't wasm-clean and the wasm client never runs it (it 145 // renders from the snapshot), so gate it out of the wasm build. 146 if (!builtin.cpu.arch.isWasm()) { 147 for (buf[0..count], oids[0..count], 0..) |*commit, oid, i| { 148 if (root_message and i == 0) continue; 149 // the walk root shows the window the url asks for and its filter; 150 // the rest show their first window, unfiltered. 151 const window_start = if (i == 0) root_start else 0; 152 commit.window = renderCommitDiff(repo_kind, repo_opts, io, gpa, aa, repo, oid, window_start, if (i == 0) root_path else "") catch .{ .start = window_start }; 153 } 154 } 155 156 const commit_count: ?u64 = switch (repo_kind) { 157 .git => null, 158 .xit => blk: { 159 const head_count = repo.commitCount(io, gpa, .{ .oid = &resolved.oid }) catch break :blk null; 160 const base_oid = base_oid_maybe orelse break :blk head_count; 161 const base_count = repo.commitCount(io, gpa, .{ .oid = &base_oid }) catch break :blk null; 162 break :blk if (head_count >= base_count) head_count - base_count else null; 163 }, 164 }; 165 166 return .{ 167 .location = try location.dupe(aa), 168 .ref_or_oid = resolved.ref_or_oid, 169 .ref_or_oid_value = resolved.value, 170 .commit_count = commit_count, 171 .commits = try aa.dupe(Commit, buf[0..count]), 172 .next_start = next_start, 173 .content = try pageContent(aa, content), 174 .header = try Header.init(aa, resolved.ref_or_oid, resolved.value), 175 }; 176 } 177 178 // an empty listing pinned to a ref, for the wasm / no-repo / unresolved paths. 179 pub fn emptyResult(aa: std.mem.Allocator, location: ui.RoutablePage.RepoLocation, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8, content: ui.RoutablePage.RepoCommitsRoute.Content) !Self { 180 return .{ 181 .location = try location.dupe(aa), 182 .ref_or_oid = ref_or_oid, 183 .ref_or_oid_value = try aa.dupe(u8, value), 184 .commit_count = null, 185 .commits = &.{}, 186 .next_start = null, 187 .content = try pageContent(aa, content), 188 .header = try Header.init(aa, ref_or_oid, value), 189 }; 190 } 191 192 // the page's content for a route's, duped into `aa`. the diff window doesn't 193 // carry over: each commit holds the one its pane shows as `window.start`. 194 fn pageContent(aa: std.mem.Allocator, content: ui.RoutablePage.RepoCommitsRoute.Content) !Content { 195 return switch (content) { 196 .diff => |d| .{ .diff = .{ .path = try aa.dupe(u8, d.path.slice()) } }, 197 .message => .message, 198 }; 199 } 200 201 // render the window [start, start+len) of a commit's diff against its first 202 // parent into `arena`-owned hunks. start/has_more identify adjacent windows. 203 // a non-empty `path` filters to that file, so the window indexes its hunks. 204 fn renderCommitDiff( 205 comptime repo_kind: rp.RepoKind, 206 comptime repo_opts: rp.RepoOpts(repo_kind), 207 io: std.Io, 208 gpa: std.mem.Allocator, 209 arena: std.mem.Allocator, 210 repo: *rp.Repo(repo_kind, repo_opts), 211 oid: [xit.hash.hexLen(repo_opts.hash)]u8, 212 start: usize, 213 path: []const u8, 214 ) !Diff.Window { 215 const empty = Diff.Window{ .start = start }; 216 217 // load the commit so we can diff it against its first parent. 218 var start_oids = [_][xit.hash.hexLen(repo_opts.hash)]u8{oid}; 219 var commit_iter = repo.log(io, gpa, .{ .start_oids = start_oids[0..1] }) catch return empty; 220 defer commit_iter.deinit(); 221 const commit_object = (commit_iter.next(gpa) catch return empty) orelse return empty; 222 defer commit_object.deinit(); 223 224 const parent_maybe = commit_object.content.commit.metadata.firstParent(); 225 226 return Diff.render(repo_kind, repo_opts, io, gpa, arena, repo, parent_maybe, oid, start, path); 227 } 228 229 // "YYYY-MM-DD" for a unix timestamp. 230 fn formatDate(arena: std.mem.Allocator, timestamp: u64) ![]const u8 { 231 const epoch_secs = std.time.epoch.EpochSeconds{ .secs = timestamp }; 232 const year_day = epoch_secs.getEpochDay().calculateYearDay(); 233 const month_day = year_day.calculateMonthDay(); 234 return std.fmt.allocPrint(arena, "{d:0>4}-{d:0>2}-{d:0>2}", .{ 235 year_day.year, 236 month_day.month.numeric(), 237 month_day.day_index + 1, 238 }); 239 } 240 241 // the commit message, trimmed and read into `aa`, plus whether its preview was 242 // cut short. 243 fn readMessage( 244 comptime repo_kind: rp.RepoKind, 245 comptime repo_opts: rp.RepoOpts(repo_kind), 246 aa: std.mem.Allocator, 247 commit_object: *xit.object.Object(repo_kind, repo_opts), 248 full_message: bool, 249 ) !struct { []const u8, bool } { 250 var message: std.ArrayList(u8) = .empty; 251 const byte_truncated = if (commit_object.readMessage(aa, &message, .limited(max_message_size))) |_| 252 false 253 else |err| switch (err) { 254 error.StreamTooLong => true, 255 else => |e| return e, 256 }; 257 const preview_end = if (full_message) null else ui.detailPreviewEnd(message.items); 258 const text = if (preview_end) |end| 259 message.items[0..end] 260 else if (byte_truncated) 261 trimIncompleteCodepoint(message.items) 262 else 263 message.items; 264 return .{ std.mem.trim(u8, text, " \t\r\n"), byte_truncated or preview_end != null }; 265 } 266 267 /// drop any incomplete UTF data from the end of a byte array 268 fn trimIncompleteCodepoint(bytes: []const u8) []const u8 { 269 var i = bytes.len; 270 while (i > 0 and bytes.len - i < 4) { 271 i -= 1; 272 const byte = bytes[i]; 273 if (byte & 0b1100_0000 == 0b1000_0000) continue; // continuation byte 274 const seq_len = std.unicode.utf8ByteSequenceLength(byte) catch return bytes; 275 return if (i + seq_len > bytes.len) bytes[0..i] else bytes; 276 } 277 return bytes; 278 } 279 280 // a focusable, word-wrapping box holding a commit message. `bottom_label` 281 // marks a message the page only shows part of ("" when it's whole). 282 fn messageBox(allocator: std.mem.Allocator, message: []const u8, bottom_label: []const u8) !wgt.TextBox { 283 var tb = try wgt.TextBox.init(allocator, message, .{ 284 .border_style = .single, 285 .rounded_corners = true, 286 .wrap_kind = .word, 287 .label = " message ", 288 .bottom_label = bottom_label, 289 }); 290 tb.getFocus().mode = .all; 291 return tb; 292 } 293 294 // the first line of a commit message, for the one-row list entries. 295 fn firstLine(message: []const u8) []const u8 { 296 const nl = std.mem.indexOfScalar(u8, message, '\n'); 297 return if (nl) |i| std.mem.trimEnd(u8, message[0..i], " \t\r") else message; 298 } 299 300 pub const View = struct { 301 // a vertical stack: the "viewing <ref>" banner on top, then a horizontal 302 // split with the commit list on the left and a diff pane on the right 303 // showing the selected commit's diff. 304 box: wgt.Box(ui.Widget), // vert: [header_index] = banner, [content_index] = split 305 data: *const Self, 306 session: *ui.Session, 307 // the commit whose diff the pane currently shows (index into data.commits). 308 diffed_index: ?usize, 309 310 const header_index: usize = 0; 311 const content_index: usize = 1; 312 // indices within the content box (the horizontal split). 313 const list_index: usize = 0; 314 const diff_index: usize = 1; 315 const list_max_width: usize = 35; 316 const diff_min_width: usize = 40; 317 318 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 319 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 320 errdefer outer.deinit(allocator); 321 322 // the ref banner at the top. 323 { 324 var header_view = try Header.View.init(allocator, &data.header, session, data.location); 325 errdefer header_view.deinit(allocator); 326 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_commits_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 327 } 328 329 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 330 errdefer box.deinit(allocator); 331 332 // the commit list (one focusable row each), plus a "next" link 333 { 334 var list_scroll = blk: { 335 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 336 errdefer list_box.deinit(allocator); 337 for (data.commits, 0..) |commit, index| { 338 // an in-page "ai:" anchor so a commit row is clickable with 339 // js off (the browser follows it, rooting the list there); 340 // with wasm the click just selects it and swaps the diff pane. 341 const content = if (index == 0) data.content else Content{ .diff = .{} }; 342 try addRow(allocator, &list_box, firstLine(commit.message), try commitRowLink(session.page_arena, data.location, commit, content)); 343 } 344 if (data.next_start) |next| { 345 try addRow(allocator, &list_box, "next →", try commitsLink(session.page_arena, data.location, next, 0, "")); 346 } 347 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 348 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 349 }; 350 errdefer list_scroll.deinit(allocator); 351 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 } }); 352 } 353 354 // the diff pane — a frame around a scroll of the hunks 355 { 356 var diff_outer = blk: { 357 var diff_scroll = try Diff.View.initEmpty(allocator, session); 358 errdefer diff_scroll.deinit(allocator); 359 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 360 errdefer frame.deinit(allocator); 361 // the frame's selected child is its scroll, so the focus chain 362 // reaches a hunk (populateDiff points the scroll's inner box at 363 // one), letting focus recovery descend into the diff pane after 364 // it's laid out beside a too-narrow list. 365 frame.getFocus().child_id = diff_scroll.getFocus().id; 366 try frame.children.put(allocator, diff_scroll.getFocus().id, .{ .widget = .{ .diff_view = diff_scroll }, .rect = null, .min_size = null }); 367 break :blk frame; 368 }; 369 errdefer diff_outer.deinit(allocator); 370 diff_outer.getFocus().mode = .mouse; 371 try box.children.put(allocator, diff_outer.getFocus().id, .{ .widget = .{ .box = diff_outer }, .rect = null, .min_size = .{ .width = diff_min_width, .height = null } }); 372 } 373 374 box.getFocus().child_id = box.children.keys()[list_index]; 375 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 376 377 // focus lives in the split; the banner isn't focusable. 378 outer.getFocus().child_id = outer.children.keys()[content_index]; 379 380 return .{ 381 .box = outer, 382 .data = data, 383 .session = session, 384 .diffed_index = null, 385 }; 386 } 387 388 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 389 var row = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 390 errdefer row.deinit(allocator); 391 row.getFocus().mode = .all; 392 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 393 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 394 } 395 396 // a focusable window-navigation row ("previous"/"next"). it's a link to this 397 // commit's route at `target_start`, so activating it (the host follows the 398 // "a:" link) reloads the page on the adjacent window — same on TUI and web. 399 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 { 400 const link = try commitsLink(self.session.page_arena, self.data.location, oid, target_start, path); 401 var tb = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 402 errdefer tb.deinit(allocator); 403 tb.getFocus().mode = .all; 404 tb.getFocus().kind = .{ .custom = link }; 405 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 406 } 407 408 // a focusable row for the top of the diff pane linking to the files tab at 409 // this commit's tree, so its files are always viewable as of this object. 410 fn addViewFilesLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), oid: []const u8) !void { 411 const link = try filesObjectLink(self.session.page_arena, self.data.location, oid); 412 var tb = try wgt.TextBox.init(allocator, "view files at this commit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 413 errdefer tb.deinit(allocator); 414 tb.getFocus().mode = .all; 415 tb.getFocus().kind = .{ .custom = link }; 416 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 417 } 418 419 // how much of the message a pane's box holds: a truncated preview links to 420 // the pane holding the whole thing, which is that link's destination. 421 const MessageBox = enum { preview, whole }; 422 423 // the selected commit's message. 424 fn addMessageBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), commit: Commit, kind: MessageBox) !void { 425 // a truncated message keeps its box even with no whole line to show, 426 // since the box is what links to the whole thing. 427 if (commit.message.len == 0 and !commit.message_truncated) return; 428 const cut_short = commit.message_truncated and kind == .preview; 429 var tb = try messageBox(allocator, commit.message, if (cut_short) " click or press enter to see more " else ""); 430 errdefer tb.deinit(allocator); 431 if (cut_short) tb.getFocus().kind = .{ .custom = try messageLink(self.session.page_arena, self.data.location, commit.oid) }; 432 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 433 } 434 435 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 436 self.box.deinit(allocator); 437 } 438 439 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 440 return &self.box.children.values()[content_index].widget.box; 441 } 442 443 fn header(self: *View) *Header.View { 444 return &self.box.children.values()[header_index].widget.repo_commits_header; 445 } 446 447 fn headerActive(self: *View) bool { 448 return self.box.getFocus().child_id == self.header().getFocus().id; 449 } 450 451 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 452 return &self.contentBox().children.values()[list_index].widget.scroll; 453 } 454 455 fn listBox(self: *View) *wgt.Box(ui.Widget) { 456 return &self.listScroll().child.box; 457 } 458 459 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 460 return &self.contentBox().children.values()[diff_index].widget.box; 461 } 462 463 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 464 return &self.diffOuter().children.values()[0].widget.diff_view.scroll; 465 } 466 467 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 468 return &self.diffScroll().child.box; 469 } 470 471 fn diffActive(self: *View) bool { 472 const content = self.contentBox(); 473 const cid = content.getFocus().child_id orelse return false; 474 return content.children.getIndex(cid) == diff_index; 475 } 476 477 // what the pane shows for the commit at `sel`. the page's content only 478 // applies to the commit it walks from; the rest show their plain diff. 479 fn paneContent(self: *View, sel: usize) Self.Content { 480 return if (sel == 0) self.data.content else .{ .diff = .{} }; 481 } 482 483 // the selected commit's index, or null when the "next" row is selected. 484 fn selectedCommitIndex(self: *View) ?usize { 485 const lb = self.listBox(); 486 const cid = lb.getFocus().child_id orelse return null; 487 const idx = lb.children.getIndex(cid) orelse return null; 488 if (idx >= self.data.commits.len) return null; 489 return idx; 490 } 491 492 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 493 self.clearGrid(); 494 495 // swap the diff pane to the selected commit when it changes. 496 try self.refreshDiff(allocator); 497 498 // the selected list row shows a border (the focused TextBox upgrades it 499 // to a double border itself); the rest stay borderless. 500 const lb = self.listBox(); 501 for (lb.children.keys(), lb.children.values()) |id, *child| { 502 switch (child.widget) { 503 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 504 else => {}, 505 } 506 } 507 508 // cap the list at list_max_width only while the diff pane fits beside it. 509 // the box drops the diff when the width can't hold both minimums, so when 510 // it's that narrow we lift the cap and let the list fill the whole width. 511 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 512 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 513 514 // stretch the diff pane across the rest of the width so it fills the area 515 // rather than shrinking to its content; its scroll fills the pane. when 516 // too narrow for both, it fills the whole width. 517 const diff_width: usize = if (constraint.max_size.width) |w| 518 (if (both_panes_fit) w - list_max_width else w) 519 else 520 diff_min_width; 521 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_width, .height = null }; 522 523 // the message is the pane's only wrapping row, and wrapping needs a 524 // bounded width, which the pane's scroll doesn't grant (it scrolls 525 // horizontally too). cap it to the pane, leaving room for the border 526 // and the scrollbar column. re-capped each build so it tracks resizes. 527 for (self.diffInner().children.values()) |*child| switch (child.widget) { 528 .text_box => |text_box| if (text_box.options.wrap_kind == .word) { 529 child.max_size = .{ .width = diff_width -| 3, .height = null }; 530 }, 531 else => {}, 532 }; 533 534 // the web bounds the layout to the browser viewport like the terminal; 535 // each Scroll's web-native mode hands its full content to a real 536 // scrollable element, so we no longer build unbounded here. 537 // 538 // crossing panes only re-selects the content box's child (focusDiff / 539 // focusList); when the window is too narrow to show both, the pane that 540 // held focus is dropped here. the framework recovers focus after the 541 // top-level build by re-deriving it down the selected-child chain, so 542 // there's nothing to fix up afterward. 543 try self.box.build(allocator, constraint, root_focus); 544 } 545 546 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 547 const sel = self.selectedCommitIndex() orelse return; 548 if (self.diffed_index) |d| if (d == sel) return; 549 try self.populateDiff(allocator, sel); 550 self.diffed_index = sel; 551 } 552 553 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 554 const commit = self.data.commits[sel]; 555 const inner = self.diffInner(); 556 557 for (inner.children.values()) |*child| child.widget.deinit(allocator); 558 inner.children.clearAndFree(allocator); 559 inner.getFocus().child_id = null; 560 561 switch (self.paneContent(sel)) { 562 .message => { 563 try self.addNavLink(allocator, inner, "← back to diff", commit.oid, 0, ""); 564 try self.addMessageBox(allocator, inner, commit, .whole); 565 }, 566 .diff => |d| { 567 if (d.path.len == 0) { 568 try self.addMessageBox(allocator, inner, commit, .preview); 569 if (commit.author != .unknown) { 570 var tb = try ui.authorBox(allocator, self.session.page_arena, commit.author); 571 errdefer tb.deinit(allocator); 572 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 573 } 574 if (commit.stats) |stats| { 575 var text: std.Io.Writer.Allocating = .init(allocator); 576 defer text.deinit(); 577 try text.writer.print("lines changed: {d}", .{stats.lines_added + stats.lines_changed}); 578 if (stats.lines_removed != 0) try text.writer.print("\nlines removed: {d}", .{stats.lines_removed}); 579 const bytes_added = stats.bytes_added >= stats.bytes_removed; 580 const bytes = if (bytes_added) stats.bytes_added - stats.bytes_removed else stats.bytes_removed - stats.bytes_added; 581 try text.writer.print("\nbytes {s}: {d}\nfiles changed: {d}", .{ 582 if (bytes_added) "added" else "removed", bytes, stats.files_added + stats.files_changed, 583 }); 584 if (stats.files_removed != 0) try text.writer.print("\nfiles removed: {d}", .{stats.files_removed}); 585 var tb = try wgt.TextBox.init(allocator, text.written(), .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " stats " }); 586 errdefer tb.deinit(allocator); 587 tb.getFocus().mode = .all; 588 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 589 } 590 try self.addViewFilesLink(allocator, inner, commit.oid); 591 } 592 593 try (Diff{ 594 .route = .{ .commit = .{ .location = self.data.location, .oid = commit.oid } }, 595 .path = d.path, 596 .window = commit.window, 597 }).appendWindow(allocator, self.session, inner); 598 }, 599 } 600 601 // point the pane at its first row so focus recovery can land here. 602 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 603 604 // reset the scroll to the top for the newly-shown commit: directly on the 605 // terminal (the wasm offset), and via a version bump on the web (so the 606 // renderer's scroll id changes and JS drops the preserved position). 607 const sc = self.diffScroll(); 608 sc.x = 0; 609 sc.y = 0; 610 sc.getFocus().version +%= 1; 611 } 612 613 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 614 if (self.headerActive()) { 615 if (key == .arrow_down) { 616 root_focus.setFocus(self.contentBox().getFocus().id); 617 } else { 618 try self.header().input(allocator, key, root_focus); 619 } 620 return; 621 } 622 if (key == .arrow_up and self.contentAtTop() and self.header().focusCloneUrl(root_focus)) return; 623 if (self.diffActive()) { 624 if (key == .arrow_left and self.diffScroll().x == 0) { 625 self.focusList(root_focus); 626 } else { 627 try self.diffOuter().children.values()[0].widget.diff_view.input(allocator, key, root_focus); 628 } 629 } else { 630 try self.listInput(key, root_focus); 631 } 632 } 633 634 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 635 // up/down (and the scroll wheel) move the selection a row; page up/down 636 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 637 // on the "next" row become navigation in the host before reaching here. 638 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 639 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 640 return; 641 } 642 switch (key) { 643 .enter => if (self.selectedCommitIndex() != null) 644 self.focusDiff(root_focus) 645 else if (self.data.next_start) |next| { 646 if (self.data.location.commitsRoute(.object, next, 0, "")) |route| 647 try self.session.navigate(route); 648 }, 649 .arrow_right => self.focusDiff(root_focus), 650 else => {}, 651 } 652 } 653 654 // enter the diff pane. the host arrives here on right-arrow or Enter from the 655 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 656 // case where the pane isn't laid out yet (it gets selected, then focused after 657 // the next build, landing on its remembered hunk). 658 fn focusDiff(self: *View, root_focus: *Focus) void { 659 if (self.diffInner().children.count() == 0) return; 660 root_focus.setFocus(self.diffOuter().getFocus().id); 661 } 662 663 // return to the list. 664 fn focusList(self: *View, root_focus: *Focus) void { 665 root_focus.setFocus(self.listScroll().getFocus().id); 666 } 667 668 pub fn clearGrid(self: *View) void { 669 self.box.clearGrid(); 670 } 671 672 pub fn getGrid(self: View) ?Grid { 673 return self.box.getGrid(); 674 } 675 676 pub fn getFocus(self: *View) *Focus { 677 return self.box.getFocus(); 678 } 679 680 fn contentAtTop(self: *View) bool { 681 if (self.diffActive()) { 682 const inner = self.diffInner(); 683 const cid = inner.getFocus().child_id orelse return true; 684 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 685 return first_focused and self.diffScroll().y == 0; 686 } 687 const lb = self.listBox(); 688 const cid = lb.getFocus().child_id orelse return true; 689 return lb.children.getIndex(cid) == 0; 690 } 691 692 // only the clone-url row sits directly below the repository header. 693 pub fn atTop(self: *View) bool { 694 return self.headerActive() or (!self.header().hasCloneUrl() and self.contentAtTop()); 695 } 696 697 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 698 return self.header().focusCloneUrl(root_focus); 699 } 700 }; 701 702 // the "a:" navigation link for the commits page walking from commit `oid` within 703 // `location`, windowing the selected commit's diff from hunk `start`, filtered 704 // to `path` ("" = every file). 705 fn commitsLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 706 const route = location.commitsRoute(.object, oid, start, path) orelse return error.RouteTooLong; 707 const url = try route.toUrl(page_arena); 708 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 709 } 710 711 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 712 // directory, within `location`. 713 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 714 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 715 const url = try route.toUrl(page_arena); 716 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 717 } 718 719 // the in-page "ai:" anchor for selecting a commit with its current pane content 720 // and window. the href is only followed with js off. 721 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, commit: Commit, content: Content) ![]const u8 { 722 const route = (switch (content) { 723 .message => location.commitMessageRoute(.object, commit.oid), 724 .diff => |diff| location.commitsRoute(.object, commit.oid, commit.window.start, diff.path), 725 }) orelse return error.RouteTooLong; 726 const url = try route.toUrl(page_arena); 727 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 728 } 729 730 // the "a:" link to the page showing `oid`'s message on its own. 731 fn messageLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 732 const route = location.commitMessageRoute(.object, oid) orelse return error.RouteTooLong; 733 const url = try route.toUrl(page_arena); 734 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 735 } 736 737 // the "<ref_or_oid> <value>" banner shown above the log. 738 pub const Header = struct { 739 content: []const u8, 740 741 // `value` arrives url-encoded, so decode it for display. 742 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 743 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 744 return .{ 745 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 746 }; 747 } 748 749 pub const View = struct { 750 box: wgt.Box(ui.Widget), 751 data: *const Header, 752 753 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !Header.View { 754 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 755 errdefer box.deinit(allocator); 756 757 if (session.data.host_kind == .server) { 758 if (try ui.widget.CopyableText.initClone(allocator, session, location)) |value| { 759 var clone_url = value; 760 errdefer clone_url.deinit(allocator); 761 const min_width = clone_url.minWidth(); 762 box.getFocus().child_id = clone_url.getFocus().id; 763 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 764 } 765 } 766 767 var spacer = try ui.widget.Spacer.init(allocator); 768 errdefer spacer.deinit(allocator); 769 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 770 771 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 772 errdefer text_box.deinit(allocator); 773 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 774 775 return .{ .box = box, .data = data }; 776 } 777 778 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 779 self.box.deinit(allocator); 780 } 781 782 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 783 self.clearGrid(); 784 try self.box.build(allocator, constraint, root_focus); 785 } 786 787 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 788 const clone_url = self.cloneUrl() orelse return; 789 try clone_url.input(allocator, key, root_focus); 790 } 791 792 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 793 const clone_url = self.cloneUrl() orelse return false; 794 root_focus.setFocus(clone_url.getFocus().id); 795 return true; 796 } 797 798 pub fn hasCloneUrl(self: *Header.View) bool { 799 return self.cloneUrl() != null; 800 } 801 802 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 803 for (self.box.children.values()) |*child| switch (child.widget) { 804 .copyable_text => |*copyable_text| return copyable_text, 805 else => {}, 806 }; 807 return null; 808 } 809 810 pub fn clearGrid(self: *Header.View) void { 811 self.box.clearGrid(); 812 } 813 814 pub fn getGrid(self: Header.View) ?Grid { 815 return self.box.getGrid(); 816 } 817 818 pub fn getFocus(self: *Header.View) *Focus { 819 return self.box.getFocus(); 820 } 821 }; 822 }; 823