╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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