╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
517 const diff_width: usize = if (constraint.max_size.width) |w| 518 (if (both_panes_fit) w - list_max_width else w) 519 else 520 diff_min_width; 521 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_width, .height = null }; 522 523 // the message is the pane's only wrapping row, and wrapping needs a 524 // bounded width, which the pane's scroll doesn't grant (it scrolls 525 // horizontally too). cap it to the pane, leaving room for the border 526 // and the scrollbar column. re-capped each build so it tracks resizes. 527 for (self.diffInner().children.values()) |*child| switch (child.widget) { 528 .text_box => |text_box| if (text_box.options.wrap_kind == .word) { 529 child.max_size = .{ .width = diff_width -| 3, .height = null }; 530 }, 531 else => {}, 532 }; 533 534 // the web bounds the layout to the browser viewport like the terminal; 535 // each Scroll's web-native mode hands its full content to a real 536 // scrollable element, so we no longer build unbounded here. 537 // 538 // crossing panes only re-selects the content box's child (focusDiff / 539 // focusList); when the window is too narrow to show both, the pane that 540 // held focus is dropped here. the framework recovers focus after the 541 // top-level build by re-deriving it down the selected-child chain, so 542 // there's nothing to fix up afterward. 543 try self.box.build(allocator, constraint, root_focus); 544 } 545 546 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 547 const sel = self.selectedCommitIndex() orelse return; 548 if (self.diffed_index) |d| if (d == sel) return; 549 try self.populateDiff(allocator, sel); 550 self.diffed_index = sel; 551 } 552 553 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 554 const commit = self.data.commits[sel]; 555 const inner = self.diffInner(); 556 557 for (inner.children.values()) |*child| child.widget.deinit(allocator); 558 inner.children.clearAndFree(allocator); 559 inner.getFocus().child_id = null; 560 561 switch (self.paneContent(sel)) { 562 .message => { 563 try self.addNavLink(allocator, inner, "← back to diff", commit.oid, 0, ""); 564 try self.addMessageBox(allocator, inner, commit, .whole); 565 }, 566 .diff => |d| { 567 if (d.path.len == 0) { 568 try self.addMessageBox(allocator, inner, commit, .preview); 569 if (commit.author != .unknown) { 570 var tb = try ui.authorBox(allocator, self.session.page_arena, commit.author); 571 errdefer tb.deinit(allocator); 572 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 573 } 574 if (commit.stats) |stats| { 575 var text: std.Io.Writer.Allocating = .init(allocator); 576 defer text.deinit(); 577 try text.writer.print("lines changed: {d}", .{stats.lines_added + stats.lines_changed}); 578 if (stats.lines_removed != 0) try text.writer.print("\nlines removed: {d}", .{stats.lines_removed}); 579 const bytes_added = stats.bytes_added >= stats.bytes_removed; 580 const bytes = if (bytes_added) stats.bytes_added - stats.bytes_removed else stats.bytes_removed - stats.bytes_added; 581 try text.writer.print("\nbytes {s}: {d}\nfiles changed: {d}", .{ 582 if (bytes_added) "added" else "removed", bytes, stats.files_added + stats.files_changed, 583 }); 584 if (stats.files_removed != 0) try text.writer.print("\nfiles removed: {d}", .{stats.files_removed}); 585 var tb = try wgt.TextBox.init(allocator, text.written(), .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " stats " }); 586 errdefer tb.deinit(allocator); 587 tb.getFocus().mode = .all; 588 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 589 } 590 try self.addViewFilesLink(allocator, inner, commit.oid); 591 } 592 593 try (Diff{ 594 .route = .{ .commit = .{ .location = self.data.location, .oid = commit.oid } }, 595 .path = d.path, 596 .window = commit.window, 597 }).appendWindow(allocator, self.session, inner); 598 }, 599 } 600 601 // point the pane at its first row so focus recovery can land here. 602 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 603 604 // reset the scroll to the top for the newly-shown commit: directly on the 605 // terminal (the wasm offset), and via a version bump on the web (so the 606 // renderer's scroll id changes and JS drops the preserved position). 607 const sc = self.diffScroll(); 608 sc.x = 0; 609 sc.y = 0; 610 sc.getFocus().version +%= 1; 611 } 612 613 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 614 if (self.headerActive()) { 615 if (key == .arrow_down) { 616 root_focus.setFocus(self.contentBox().getFocus().id); 617 } else { 618 try self.header().input(allocator, key, root_focus); 619 } 620 return; 621 } 622 if (key == .arrow_up and self.contentAtTop() and self.header().focusCloneUrl(root_focus)) return; 623 if (self.diffActive()) { 624 if (key == .arrow_left and self.diffScroll().x == 0) { 625 self.focusList(root_focus); 626 } else { 627 try self.diffOuter().children.values()[0].widget.diff_view.input(allocator, key, root_focus); 628 } 629 } else { 630 try self.listInput(key, root_focus); 631 } 632 } 633 634 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 635 // up/down (and the scroll wheel) move the selection a row; page up/down 636 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 637 // on the "next" row become navigation in the host before reaching here. 638 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 639 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 640 return; 641 } 642 switch (key) { 643 .enter => if (self.selectedCommitIndex() != null) 644 self.focusDiff(root_focus) 645 else if (self.data.next_start) |next| { 646 if (self.data.location.commitsRoute(.object, next, 0, "")) |route| 647 try self.session.navigate(route); 648 }, 649 .arrow_right => self.focusDiff(root_focus), 650 else => {}, 651 } 652 } 653 654 // enter the diff pane. the host arrives here on right-arrow or Enter from the 655 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 656 // case where the pane isn't laid out yet (it gets selected, then focused after 657 // the next build, landing on its remembered hunk). 658 fn focusDiff(self: *View, root_focus: *Focus) void { 659 if (self.diffInner().children.count() == 0) return; 660 root_focus.setFocus(self.diffOuter().getFocus().id); 661 } 662 663 // return to the list. 664 fn focusList(self: *View, root_focus: *Focus) void { 665 root_focus.setFocus(self.listScroll().getFocus().id); 666 } 667 668 pub fn clearGrid(self: *View) void { 669 self.box.clearGrid(); 670 } 671 672 pub fn getGrid(self: View) ?Grid { 673 return self.box.getGrid(); 674 } 675 676 pub fn getFocus(self: *View) *Focus { 677 return self.box.getFocus(); 678 } 679 680 fn contentAtTop(self: *View) bool { 681 if (self.diffActive()) { 682 const inner = self.diffInner(); 683 const cid = inner.getFocus().child_id orelse return true; 684 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 685 return first_focused and self.diffScroll().y == 0; 686 } 687 const lb = self.listBox(); 688 const cid = lb.getFocus().child_id orelse return true; 689 return lb.children.getIndex(cid) == 0; 690 } 691 692 // only the clone-url row sits directly below the repository header. 693 pub fn atTop(self: *View) bool { 694 return self.headerActive() or (!self.header().hasCloneUrl() and self.contentAtTop()); 695 } 696 697 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 698 return self.header().focusCloneUrl(root_focus); 699 } 700 }; 701 702 // the "a:" navigation link for the commits page walking from commit `oid` within 703 // `location`, windowing the selected commit's diff from hunk `start`, filtered 704 // to `path` ("" = every file). 705 fn commitsLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 706 const route = location.commitsRoute(.object, oid, start, path) orelse return error.RouteTooLong; 707 const url = try route.toUrl(page_arena); 708 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 709 } 710 711 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 712 // directory, within `location`. 713 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 714 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 715 const url = try route.toUrl(page_arena); 716 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 717 } 718 719 // the in-page "ai:" anchor for selecting a commit with its current pane content 720 // and window. the href is only followed with js off. 721 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, commit: Commit, content: Content) ![]const u8 { 722 const route = (switch (content) { 723 .message => location.commitMessageRoute(.object, commit.oid), 724 .diff => |diff| location.commitsRoute(.object, commit.oid, commit.window.start, diff.path), 725 }) orelse return error.RouteTooLong; 726 const url = try route.toUrl(page_arena); 727 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 728 } 729 730 // the "a:" link to the page showing `oid`'s message on its own. 731 fn messageLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 732 const route = location.commitMessageRoute(.object, oid) orelse return error.RouteTooLong; 733 const url = try route.toUrl(page_arena); 734 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 735 } 736 737 // the "<ref_or_oid> <value>" banner shown above the log. 738 pub const Header = struct { 739 content: []const u8, 740 741 // `value` arrives url-encoded, so decode it for display. 742 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 743 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 744 return .{ 745 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 746 }; 747 } 748 749 pub const View = struct { 750 box: wgt.Box(ui.Widget), 751 data: *const Header, 752 753 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !Header.View { 754 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 755 errdefer box.deinit(allocator); 756 757 if (!session.data.is_local) { 758 if (try ui.widget.CopyableText.initClone(allocator, session, location)) |value| { 759 var clone_url = value; 760 errdefer clone_url.deinit(allocator); 761 const min_width = clone_url.minWidth(); 762 box.getFocus().child_id = clone_url.getFocus().id; 763 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 764 } 765 } 766 767 var spacer = try ui.widget.Spacer.init(allocator); 768 errdefer spacer.deinit(allocator); 769 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 770 771 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 772 errdefer text_box.deinit(allocator); 773 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 774 775 return .{ .box = box, .data = data }; 776 } 777 778 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 779 self.box.deinit(allocator); 780 } 781 782 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 783 self.clearGrid(); 784 try self.box.build(allocator, constraint, root_focus); 785 } 786 787 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 788 const clone_url = self.cloneUrl() orelse return; 789 try clone_url.input(allocator, key, root_focus); 790 } 791 792 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 793 const clone_url = self.cloneUrl() orelse return false; 794 root_focus.setFocus(clone_url.getFocus().id); 795 return true; 796 } 797 798 pub fn hasCloneUrl(self: *Header.View) bool { 799 return self.cloneUrl() != null; 800 } 801 802 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 803 for (self.box.children.values()) |*child| switch (child.widget) { 804 .copyable_text => |*copyable_text| return copyable_text, 805 else => {}, 806 }; 807 return null; 808 } 809 810 pub fn clearGrid(self: *Header.View) void { 811 self.box.clearGrid(); 812 } 813 814 pub fn getGrid(self: Header.View) ?Grid { 815 return self.box.getGrid(); 816 } 817 818 pub fn getFocus(self: *Header.View) *Focus { 819 return self.box.getFocus(); 820 } 821 }; 822 }; 823