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