╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
173 // parent into `arena`-owned hunks. has_prev/has_more flag adjacent windows. 174 fn renderCommitDiff( 175 io: std.Io, 176 gpa: std.mem.Allocator, 177 arena: std.mem.Allocator, 178 repo: *rp.Repo(.xit, .{}), 179 oid: [xit.hash.hexLen(.sha1)]u8, 180 start: usize, 181 len: usize, 182 ) !RenderedDiff { 183 const empty = RenderedDiff{ .hunks = &.{}, .has_prev = false, .has_more = false }; 184 185 // load the commit so we can diff it against its first parent. 186 var start_oids = [_][xit.hash.hexLen(.sha1)]u8{oid}; 187 var commit_iter = repo.log(io, gpa, start_oids[0..1]) catch return empty; 188 defer commit_iter.deinit(); 189 const commit_object = (commit_iter.next(gpa) catch return empty) orelse return empty; 190 defer commit_object.deinit(); 191 192 const parent_maybe = commit_object.content.commit.metadata.firstParent(); 193 194 var tree_diff = repo.treeDiff(io, gpa, parent_maybe, &commit_object.oid) catch return empty; 195 defer tree_diff.deinit(); 196 197 var file_iter = repo.filePairs(io, gpa, .{ .tree = .{ .tree_diff = &tree_diff } }) catch return empty; 198 199 var hunks: std.ArrayList(Hunk) = .empty; 200 var index: usize = 0; // running hunk index across all files 201 var has_more = false; 202 203 file_loop: while (file_iter.next() catch null) |pair_val| { 204 var pair = pair_val; 205 defer pair.deinit(); 206 207 var hunk_iter = df.HunkIterator(.xit, .{}).init(gpa, &pair.a, &pair.b) catch continue; 208 defer hunk_iter.deinit(gpa); 209 210 // the path label rides on the first of this file's hunks we actually show. 211 var path_attached = false; 212 while (try hunk_iter.next(gpa)) |hunk_val| { 213 var hunk = hunk_val; 214 defer hunk.deinit(gpa); 215 216 const i = index; 217 index += 1; 218 if (i < start) continue; // before this window 219 if (hunks.items.len >= len) { 220 has_more = true; 221 break :file_loop; 222 } 223 var lines: std.ArrayList([]const u8) = .empty; 224 try appendHunkLines(arena, &lines, &hunk_iter, &hunk); 225 try hunks.append(arena, .{ 226 .path = if (path_attached) null else try arena.dupe(u8, pair.path), 227 .lines = try lines.toOwnedSlice(arena), 228 }); 229 path_attached = true; 230 } 231 } 232 233 return .{ .hunks = try hunks.toOwnedSlice(arena), .has_prev = start > 0, .has_more = has_more }; 234 } 235 236 // append a hunk's header and its edit lines, each prefixed with a right-aligned 237 // line number in a column wide enough for the hunk's largest number, then a space 238 fn appendHunkLines( 239 arena: std.mem.Allocator, 240 lines: *std.ArrayList([]const u8), 241 hunk_iter: *df.HunkIterator(.xit, .{}), 242 hunk: *df.Hunk(.xit, .{}), 243 ) !void { 244 var max_num: usize = 1; 245 for (hunk.edits.items) |edit| { 246 const n = editLineNum(edit) + 1; 247 if (n > max_num) max_num = n; 248 } 249 const width = std.fmt.count("{d}", .{max_num}); 250 // a run of spaces sliced to each line's leading pad. 251 const indent = try arena.alloc(u8, width); 252 @memset(indent, ' '); 253 254 for (hunk.edits.items) |edit| { 255 const text = switch (edit) { 256 .eql => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 257 .ins => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 258 .del => |e| try hunk_iter.line_iter_a.get(e.old_line.num), 259 }; 260 defer switch (edit) { 261 .eql, .ins => hunk_iter.line_iter_b.free(text), 262 .del => hunk_iter.line_iter_a.free(text), 263 }; 264 const prefix: u8 = switch (edit) { 265 .eql => ' ', 266 .ins => '+', 267 .del => '-', 268 }; 269 const num_str = try std.fmt.allocPrint(arena, "{d}", .{editLineNum(edit) + 1}); 270 try lines.append(arena, try std.fmt.allocPrint(arena, "{s}{s} {c}{s}", .{ 271 indent[0 .. width - num_str.len], num_str, prefix, text, 272 })); 273 } 274 } 275 276 // the line number to show for an edit: the new-side number for kept and inserted 277 // lines, the old-side number for deletions (0-based in the diff machinery). 278 fn editLineNum(edit: df.Edit) usize { 279 return switch (edit) { 280 .eql => |e| e.new_line.num, 281 .ins => |e| e.new_line.num, 282 .del => |e| e.old_line.num, 283 }; 284 } 285 286 // "YYYY-MM-DD" for a unix timestamp. 287 fn formatDate(arena: std.mem.Allocator, timestamp: u64) ![]const u8 { 288 const epoch_secs = std.time.epoch.EpochSeconds{ .secs = timestamp }; 289 const year_day = epoch_secs.getEpochDay().calculateYearDay(); 290 const month_day = year_day.calculateMonthDay(); 291 return std.fmt.allocPrint(arena, "{d:0>4}-{d:0>2}-{d:0>2}", .{ 292 year_day.year, 293 month_day.month.numeric(), 294 month_day.day_index + 1, 295 }); 296 } 297 298 // the first line of a commit message, with surrounding whitespace trimmed. 299 fn firstLine(message: []const u8) []const u8 { 300 const trimmed = std.mem.trim(u8, message, " \t\r\n"); 301 const nl = std.mem.indexOfScalar(u8, trimmed, '\n'); 302 return if (nl) |i| trimmed[0..i] else trimmed; 303 } 304 305 pub const View = struct { 306 // a vertical stack: the "viewing <ref>" banner on top, then a horizontal 307 // split with the commit list on the left and a diff pane on the right 308 // showing the selected commit's diff. 309 box: wgt.Box(ui.Widget), // vert: [sub_header_index] = banner, [content_index] = split 310 data: *const Self, 311 session: *ui.Session, 312 // the commit whose diff the pane currently shows (index into data.commits). 313 diffed_index: ?usize, 314 315 const sub_header_index: usize = 0; 316 const content_index: usize = 1; 317 // indices within the content box (the horizontal split). 318 const list_index: usize = 0; 319 const diff_index: usize = 1; 320 const list_max_width: usize = 40; 321 const diff_min_width: usize = 40; 322 323 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 324 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 325 errdefer outer.deinit(allocator); 326 327 // the ref banner at the top. 328 { 329 var sub_header = try SubHeader.View.init(allocator, &data.sub_header, session); 330 errdefer sub_header.deinit(allocator); 331 try outer.children.put(allocator, sub_header.getFocus().id, .{ .widget = .{ .repo_sub_header = sub_header }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 332 } 333 334 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 335 errdefer box.deinit(allocator); 336 337 // the commit list (one focusable row each), plus a "next" link 338 { 339 var list_scroll = blk: { 340 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 341 errdefer list_box.deinit(allocator); 342 for (data.commits) |commit| { 343 // an in-page "ai:" anchor so a commit row is clickable with 344 // js off (the browser follows it, rooting the list there); 345 // with wasm the click just selects it and swaps the diff pane. 346 try addRow(allocator, &list_box, commit.message, try commitRowLink(session.page_arena, data.identity, commit.oid)); 347 } 348 if (data.next_start) |next| { 349 try addRow(allocator, &list_box, "next →", try commitsLink(session.page_arena, data.identity, next, 0)); 350 } 351 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 352 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 353 }; 354 errdefer list_scroll.deinit(allocator); 355 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 } }); 356 } 357 358 // the diff pane — a frame around a scroll of the hunks 359 { 360 var diff_outer = blk: { 361 var diff_scroll = blk2: { 362 var diff_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 363 errdefer diff_inner.deinit(allocator); 364 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = diff_inner }, .{ .direction = .both, .web_native = !session.is_terminal }); 365 }; 366 errdefer diff_scroll.deinit(allocator); 367 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 368 errdefer frame.deinit(allocator); 369 try frame.children.put(allocator, diff_scroll.getFocus().id, .{ .widget = .{ .scroll = diff_scroll }, .rect = null, .min_size = null }); 370 break :blk frame; 371 }; 372 errdefer diff_outer.deinit(allocator); 373 try box.children.put(allocator, diff_outer.getFocus().id, .{ .widget = .{ .box = diff_outer }, .rect = null, .min_size = .{ .width = diff_min_width, .height = null } }); 374 } 375 376 box.getFocus().child_id = box.children.keys()[list_index]; 377 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 378 379 // focus lives in the split; the banner isn't focusable. 380 outer.getFocus().child_id = outer.children.keys()[content_index]; 381 382 return .{ 383 .box = outer, 384 .data = data, 385 .session = session, 386 .diffed_index = null, 387 }; 388 } 389 390 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 391 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 392 errdefer row.deinit(allocator); 393 row.getFocus().focusable = true; 394 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 395 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 396 } 397 398 // one hunk as a focusable multi-line text box. its lines sit flush (the 399 // inner Text rows have no border); the box's hidden border reserves the 400 // space the double border occupies when focused, so focusing doesn't shift 401 // layout. `lines` are joined into the page arena (text_box borrows it). 402 fn addHunkBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), lines: []const []const u8) !void { 403 if (lines.len == 0) return; 404 const text = try std.mem.join(self.session.page_arena.allocator(), "\n", lines); 405 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 406 errdefer tb.deinit(allocator); 407 tb.getFocus().focusable = true; 408 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 409 } 410 411 // a focusable file-path label shown above the first hunk of each file. 412 fn addPathBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), path: []const u8) !void { 413 _ = self; 414 var tb = try wgt.TextBox(ui.Widget).init(allocator, path, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 415 errdefer tb.deinit(allocator); 416 tb.getFocus().focusable = true; 417 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 418 } 419 420 // a focusable window-navigation row ("previous"/"next"). it's a link to this 421 // commit's route at `target_after`, so activating it (the host follows the 422 // "a:" link) reloads the page on the adjacent window — same on TUI and web. 423 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, oid: []const u8, target_after: usize) !void { 424 const link = try commitsLink(self.session.page_arena, self.data.identity, oid, target_after); 425 var tb = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 426 errdefer tb.deinit(allocator); 427 tb.getFocus().focusable = true; 428 tb.getFocus().kind = .{ .custom = link }; 429 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 430 } 431 432 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 433 self.box.deinit(allocator); 434 } 435 436 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 437 return &self.box.children.values()[content_index].widget.box; 438 } 439 440 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 441 return &self.contentBox().children.values()[list_index].widget.scroll; 442 } 443 444 fn listBox(self: *View) *wgt.Box(ui.Widget) { 445 return &self.listScroll().child.box; 446 } 447 448 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 449 return &self.contentBox().children.values()[diff_index].widget.box; 450 } 451 452 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 453 return &self.diffOuter().children.values()[0].widget.scroll; 454 } 455 456 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 457 return &self.diffScroll().child.box; 458 } 459 460 fn diffActive(self: *View) bool { 461 const content = self.contentBox(); 462 const cid = content.getFocus().child_id orelse return false; 463 return content.children.getIndex(cid) == diff_index; 464 } 465 466 // the selected commit's index, or null when the "next" row is selected. 467 fn selectedCommitIndex(self: *View) ?usize { 468 const lb = self.listBox(); 469 const cid = lb.getFocus().child_id orelse return null; 470 const idx = lb.children.getIndex(cid) orelse return null; 471 if (idx >= self.data.commits.len) return null; 472 return idx; 473 } 474 475 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 476 self.clearGrid(); 477 478 // swap the diff pane to the selected commit when it changes. 479 try self.refreshDiff(allocator); 480 481 // mirror the focused commit into the url so it updates as the selection 482 // moves, but only while focus is inside this view (the list or diff). when 483 // focus sits on the header tab, use the page's base /commits route. 484 if (root_focus.grandchild_id) |g| { 485 if (self.box.getFocus().children.contains(g)) { 486 if (self.selectedCommitIndex()) |sel| { 487 // mirror the commit's current diff window so the url stays 488 // linkable (0 for any commit but the windowed start one). 489 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, self.data.commits[sel].oid, self.data.commits[sel].window_start)) |route| 490 self.session.data.current_page = route; 491 } 492 } 493 } 494 495 // the selected list row shows a border (the focused TextBox upgrades it 496 // to a double border itself); the rest stay borderless. 497 const lb = self.listBox(); 498 for (lb.children.keys(), lb.children.values()) |id, *child| { 499 switch (child.widget) { 500 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 501 else => {}, 502 } 503 } 504 505 // the selected hunk (or the "load more" row) shows a single border (the 506 // focused TextBox upgrades it to a double border itself, so it stays 507 // single when focus is on the commit list); the rest keep their 508 // space-reserving hidden border. 509 const inner = self.diffInner(); 510 for (inner.children.keys(), inner.children.values()) |id, *child| { 511 switch (child.widget) { 512 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 513 else => {}, 514 } 515 } 516 517 // cap the list at list_max_width only while the diff pane fits beside it. 518 // the box drops the diff when the width can't hold both minimums, so when 519 // it's that narrow we lift the cap and let the list fill the whole width. 520 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 521 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 522 523 // the web bounds the layout to the browser viewport like the terminal; 524 // each Scroll's web-native mode hands its full content to a real 525 // scrollable element, so we no longer build unbounded here. 526 try self.box.build(allocator, constraint, root_focus); 527 528 // if the diff pane is selected but focus is still elsewhere in this view, 529 // the pane was too narrow to lay out when focus crossed over. it's laid 530 // out now, so focus its hunk. 531 if (self.diffActive() and inner.children.count() > 0) { 532 if (root_focus.grandchild_id) |g| { 533 const in_view = self.box.getFocus().children.contains(g); 534 const in_diff = inner.children.contains(g); 535 if (in_view and !in_diff) 536 try root_focus.setFocus(inner.getFocus().child_id orelse inner.children.keys()[0]); 537 } 538 } 539 } 540 541 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 542 const sel = self.selectedCommitIndex() orelse return; 543 if (self.diffed_index) |d| if (d == sel) return; 544 try self.populateDiff(allocator, sel); 545 self.diffed_index = sel; 546 } 547 548 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 549 const commit = self.data.commits[sel]; 550 const inner = self.diffInner(); 551 552 for (inner.children.values()) |*child| child.widget.deinit(allocator); 553 inner.children.clearAndFree(allocator); 554 inner.getFocus().child_id = null; 555 556 // a "previous" row at the top and a "next" row at the bottom reload the 557 // page on the adjacent diff window. 558 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page); 559 for (commit.hunks) |hunk| { 560 if (hunk.path) |path| try self.addPathBox(allocator, inner, path); 561 try self.addHunkBox(allocator, inner, hunk.lines); 562 } 563 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page); 564 565 // reset the scroll to the top for the newly-shown commit: directly on the 566 // terminal (the wasm offset), and via a version bump on the web (so the 567 // renderer's scroll id changes and JS drops the preserved position). 568 const sc = self.diffScroll(); 569 sc.x = 0; 570 sc.y = 0; 571 sc.getFocus().version +%= 1; 572 } 573 574 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 575 _ = allocator; 576 if (self.diffActive()) { 577 try self.diffInput(key, root_focus); 578 } else { 579 try self.listInput(key, root_focus); 580 } 581 } 582 583 fn listInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 584 // up/down (and the scroll wheel) move the selection a row; page up/down 585 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 586 // on the "next" row become navigation in the host before reaching here. 587 switch (key) { 588 .enter => if (self.selectedCommitIndex() != null) 589 try self.focusDiff(root_focus) 590 else if (self.data.next_start) |next| { 591 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, next, 0)) |route| 592 try self.session.navigate(route); 593 }, 594 .arrow_right => try self.focusDiff(root_focus), 595 .arrow_down => try self.moveSelection(root_focus, 1), 596 .arrow_up => try self.moveSelection(root_focus, -1), 597 .page_down => try self.moveSelection(root_focus, 10), 598 .page_up => try self.moveSelection(root_focus, -10), 599 .end => try self.moveSelection(root_focus, @intCast(self.listBox().children.count())), 600 .home => try self.moveSelection(root_focus, -@as(isize, @intCast(self.listBox().children.count()))), 601 .mouse => |mouse| switch (mouse.action) { 602 .scroll => |dir| try self.moveSelection(root_focus, if (dir == .up) -1 else 1), 603 else => {}, 604 }, 605 else => {}, 606 } 607 } 608 609 fn diffInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 610 const sc = self.diffScroll(); 611 switch (key) { 612 // left scrolls horizontally, then leaves for the list once flush left. 613 .arrow_left => { 614 if (sc.x > 0) { 615 sc.x -= 1; 616 self.clampDiffScroll(); 617 } else try self.focusList(root_focus); 618 }, 619 .arrow_right => { 620 sc.x += 1; 621 self.clampDiffScroll(); 622 }, 623 .arrow_up => try self.moveDiff(root_focus, -1), 624 .arrow_down => try self.moveDiff(root_focus, 1), 625 .page_up => try self.pageDiff(root_focus, -10), 626 .page_down => try self.pageDiff(root_focus, 10), 627 .home => try self.jumpDiff(root_focus, false), 628 .end => try self.jumpDiff(root_focus, true), 629 // Enter / a click on a "previous"/"next" row follow its "a:" link in 630 // the host (it reloads the adjacent window), so they don't reach here. 631 .mouse => |mouse| switch (mouse.action) { 632 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 633 else => {}, 634 }, 635 else => {}, 636 } 637 } 638 639 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 640 // line count unless there is a new hunk visible (in which case it is a 641 // hunk count). on the web `delta` is always a hunk count. 642 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 643 const inner = self.diffInner(); 644 const keys = inner.children.keys(); 645 if (keys.len == 0) return; 646 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 647 const target = @as(isize, @intCast(cur)) + delta; 648 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 649 650 if (self.session.is_terminal) { 651 if (in_range and self.hunkVisible(@intCast(target))) { 652 try root_focus.setFocus(keys[@intCast(target)]); 653 return; 654 } 655 656 const sc = self.diffScroll(); 657 sc.y += delta * 5; // magnify because it's a line count 658 self.clampDiffScroll(); 659 if (in_range and self.hunkVisible(@intCast(target))) { 660 try root_focus.setFocus(keys[@intCast(target)]); 661 } 662 } else { 663 if (in_range) { 664 try root_focus.setFocus(keys[@intCast(target)]); 665 } 666 } 667 } 668 669 // page a fixed number of lines, then focus the leading visible hunk 670 // (bottom-most when paging down, top-most when paging up). in the 671 // terminal, `delta` is a line count, and on the web it's a hunk count. 672 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 673 if (self.session.is_terminal) { 674 const sc = self.diffScroll(); 675 sc.y += delta * 5; // magnify because it's a line count 676 self.clampDiffScroll(); 677 try self.focusVisible(root_focus, delta > 0); 678 } else { 679 const inner = self.diffInner(); 680 const keys = inner.children.keys(); 681 if (keys.len == 0) return; 682 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 683 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 684 try root_focus.setFocus(keys[target]); 685 } 686 } 687 688 // jump to the first or last hunk. on the web the browser scrolls to the 689 // focused hunk; on the terminal pin the scroll to the top/bottom too. 690 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 691 const inner = self.diffInner(); 692 const keys = inner.children.keys(); 693 if (keys.len == 0) return; 694 if (self.session.is_terminal) { 695 const sc = self.diffScroll(); 696 sc.y = if (to_end) std.math.maxInt(isize) else 0; 697 self.clampDiffScroll(); 698 } 699 try root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 700 } 701 702 // whether hunk `index` is at least partly within the diff viewport, per the 703 // last build's layout rects (content space) and the current scroll offset. 704 fn hunkVisible(self: *View, index: usize) bool { 705 const inner = self.diffInner(); 706 const sc = self.diffScroll(); 707 const vp = sc.grid orelse return false; 708 const r = inner.children.values()[index].rect orelse return false; 709 const top = sc.y; 710 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 711 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 712 } 713 714 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 715 // the bottom-most visible (for downward motion), else the top-most. 716 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 717 const inner = self.diffInner(); 718 const sc = self.diffScroll(); 719 const vp = sc.grid orelse return; 720 const top = sc.y; 721 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 722 var chosen: ?usize = null; 723 for (inner.children.keys(), inner.children.values()) |id, *child| { 724 const r = child.rect orelse continue; // content-space layout rect 725 const r_top = r.y; 726 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 727 if (r_bot <= top or r_top >= bottom) continue; // not visible 728 chosen = id; 729 if (!prefer_last) break; // first visible 730 } 731 if (chosen) |id| try root_focus.setFocus(id); 732 } 733 734 // keep the diff scroll within its content, using the last build's grids. the 735 // scroll bar's reserved column/row isn't part of the content viewport, so 736 // exclude it (as scrollToRect does) or the last column/row stays unreachable. 737 fn clampDiffScroll(self: *View) void { 738 const sc = self.diffScroll(); 739 const vp = sc.grid orelse return; 740 const content = sc.child.box.grid orelse return; 741 const view_w = vp.size.width - sc.bar_w; 742 const view_h = vp.size.height - sc.bar_h; 743 const max_y: isize = if (content.size.height > view_h) @intCast(content.size.height - view_h) else 0; 744 const max_x: isize = if (content.size.width > view_w) @intCast(content.size.width - view_w) else 0; 745 sc.y = std.math.clamp(sc.y, 0, max_y); 746 sc.x = std.math.clamp(sc.x, 0, max_x); 747 } 748 749 // enter the diff pane by focusing the hunk that was focused last (the box's 750 // child_id remembers it across the trip to the commit list); fall back to the 751 // first hunk on a fresh diff. the host arrives here on right-arrow or Enter 752 // from the list. a diff with no hunks can't be entered. 753 fn focusDiff(self: *View, root_focus: *Focus) !void { 754 const inner = self.diffInner(); 755 if (inner.children.count() == 0) return; 756 const target = inner.getFocus().child_id orelse inner.children.keys()[0]; 757 try root_focus.setFocus(target); 758 if (root_focus.grandchild_id == target) return; 759 // the diff pane wasn't laid out last build (too narrow to show beside 760 // the list), so its hunks aren't in the focus tree yet. select the pane 761 // at the box level, and the hunk will be focused after the next build. 762 self.contentBox().getFocus().child_id = self.diffOuter().getFocus().id; 763 } 764 765 fn focusList(self: *View, root_focus: *Focus) !void { 766 const lb = self.listBox(); 767 const id = lb.getFocus().child_id orelse (if (lb.children.count() > 0) lb.children.keys()[0] else return); 768 try root_focus.setFocus(id); 769 } 770 771 fn moveSelection(self: *View, root_focus: *Focus, delta: isize) !void { 772 const lb = self.listBox(); 773 const keys = lb.children.keys(); 774 if (keys.len == 0) return; 775 const cur_id = lb.getFocus().child_id orelse return; 776 const cur: isize = @intCast(lb.children.getIndex(cur_id) orelse return); 777 const last: isize = @intCast(keys.len - 1); 778 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 779 if (next == @as(usize, @intCast(cur))) return; 780 try root_focus.setFocus(keys[next]); 781 if (lb.children.values()[next].rect) |rect| self.listScroll().scrollToRect(rect); 782 } 783 784 pub fn clearGrid(self: *View) void { 785 self.box.clearGrid(); 786 } 787 788 pub fn getGrid(self: View) ?Grid { 789 return self.box.getGrid(); 790 } 791 792 pub fn getFocus(self: *View) *Focus { 793 return self.box.getFocus(); 794 } 795 796 // for the parent's "scroll up at the top jumps to the header" check. in the 797 // diff pane that means the first hunk is focused and the diff can't scroll up 798 // any further; otherwise up-arrow stays in the pane (scrolling or moving 799 // between hunks). when the list holds focus, report its selection directly. 800 pub fn getSelectedIndex(self: *View) ?usize { 801 if (self.diffActive()) { 802 const inner = self.diffInner(); 803 const cid = inner.getFocus().child_id orelse return 1; 804 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 805 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 806 } 807 const lb = self.listBox(); 808 const cid = lb.getFocus().child_id orelse return null; 809 return lb.children.getIndex(cid); 810 } 811 }; 812 813 // the "a:" navigation link for the commits page walking from commit `oid` within 814 // `identity`, showing the selected commit's first `after` hunks (0 = default). 815 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, after: usize) ![]const u8 { 816 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, after) orelse return error.RouteTooLong; 817 const url = try route.urlAlloc(page_arena); 818 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 819 } 820 821 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 822 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 823 // in-page (crossPageLink ignores it); the href is only followed with js off. 824 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 825 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0) orelse return error.RouteTooLong; 826 const url = try route.urlAlloc(page_arena); 827 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 828 } 829