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