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