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