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