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