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