╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
607 const sel = next.index; 608 const commit = self.data.commits[sel]; 609 const inner = self.detailInner(); 610 611 for (inner.children.values()) |*child| child.widget.deinit(allocator); 612 inner.children.clearAndFree(allocator); 613 inner.getFocus().child_id = null; 614 self.back_id = null; 615 616 // with the list hidden beside it, the detail leads with the way back. 617 // an in-page link to this very page, so both hosts hand its press and 618 // enter to input instead of navigating. 619 if (next.narrow) { 620 const pa = self.session.page_arena; 621 const link = try pa.allocator().print("ai:{s}", .{try self.session.data.current_page.toUrl(pa)}); 622 self.back_id = try addLink(allocator, inner, "← back to commit list", link); 623 } 624 625 if (sel == 0 and self.data.message) { 626 _ = try addLink(allocator, inner, "← back to commit", try commitsLink(self.session.page_arena, self.data, commit.oid)); 627 try self.addMessageBox(allocator, inner, commit, .whole); 628 } else { 629 const pa = self.session.page_arena; 630 { 631 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 632 errdefer row.deinit(allocator); 633 const diff_route = self.data.handle.location.commitDiffRoute(commit.oid, commit.parent_oid) orelse return error.RouteTooLong; 634 _ = try addLink(allocator, &row, "view diff", try pa.allocator().print("a:{s}", .{try diff_route.toUrl(pa)})); 635 _ = try addLink(allocator, &row, "view files at this commit", try filesObjectLink(pa, self.data.handle.location, commit.oid)); 636 row.getFocus().child_id = row.children.keys()[0]; 637 try inner.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 638 } 639 if (commit.merge) if (self.data.handle.location.commitsMergeRoute(commit.oid)) |route| { 640 _ = try addLink(allocator, inner, "view commits from this merge", try pa.allocator().print("a:{s}", .{try route.toUrl(pa)})); 641 }; 642 { 643 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 644 errdefer row.deinit(allocator); 645 if (commit.author != .unknown) { 646 var tb = try ui.authorBox(allocator, pa, commit.author); 647 errdefer tb.deinit(allocator); 648 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 649 } 650 if (commit.committer != .unknown) { 651 var tb = try ui.authorBox(allocator, pa, commit.committer); 652 errdefer tb.deinit(allocator); 653 tb.options.top_label.text = " committer "; 654 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 655 } 656 const timestamp = try ui.timestamp.format(pa.allocator(), commit.timestamp, try self.session.nowSeconds()); 657 var tb = try wgt.TextBox.init(allocator, timestamp.relative, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = timestamp.exact } }); 658 errdefer tb.deinit(allocator); 659 tb.getFocus().mode = .all; 660 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 661 row.getFocus().child_id = row.children.keys()[0]; 662 try inner.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 663 } 664 if (commit.stats) |stats| { 665 var text: std.Io.Writer.Allocating = .init(allocator); 666 defer text.deinit(); 667 try text.writer.print("lines changed: {d}", .{stats.lines_added + stats.lines_changed}); 668 if (stats.lines_removed != 0) try text.writer.print("\nlines removed: {d}", .{stats.lines_removed}); 669 const bytes_added = stats.bytes_added >= stats.bytes_removed; 670 const bytes = if (bytes_added) stats.bytes_added - stats.bytes_removed else stats.bytes_removed - stats.bytes_added; 671 try text.writer.print("\nbytes {s}: {d}\nfiles changed: {d}", .{ 672 if (bytes_added) "added" else "removed", bytes, stats.files_added + stats.files_changed, 673 }); 674 if (stats.files_removed != 0) try text.writer.print("\nfiles removed: {d}", .{stats.files_removed}); 675 var tb = try wgt.TextBox.init(allocator, text.written(), .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " stats " } }); 676 errdefer tb.deinit(allocator); 677 tb.getFocus().mode = .all; 678 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 679 } 680 try self.addMessageBox(allocator, inner, commit, .preview); 681 } 682 683 // point the pane at its first row so focus recovery can land here. 684 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 685 686 // reset the scroll to the top for the newly-shown commit: directly on the 687 // terminal (the wasm offset), and via a version bump on the web (so the 688 // renderer's scroll id changes and JS drops the preserved position). 689 const sc = self.detailScroll(); 690 sc.x = 0; 691 sc.y = 0; 692 sc.getFocus().version +%= 1; 693 } 694 695 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 696 if (self.data.no_commits) return self.box.input(allocator, key, root_focus); 697 // scrolling crosses the sub header boundary the same way arrows do 698 const direction = inp.vertDirection(key); 699 if (self.headerActive()) { 700 // active means there is one 701 const header_view = self.header() orelse unreachable; 702 if (direction == .down) { 703 root_focus.setFocus(self.contentBox().getFocus().id); 704 return; 705 } 706 if (key == .enter) if (try header_view.submittedText(allocator)) |text| { 707 defer allocator.free(text); 708 return self.submit(text); 709 }; 710 return header_view.input(allocator, key, root_focus); 711 } 712 if (direction == .up and self.contentAtTop() and self.focusHeader(root_focus)) return; 713 if (self.detailActive()) { 714 // the way back to the list, by enter or a click 715 if (self.back_id) |id| { 716 if (inp.activated(root_focus, id, key)) return self.focusList(root_focus); 717 } 718 const inner = self.detailInner(); 719 if (inp.rowDelta(key, @intCast(inner.children.count()))) |delta| { 720 ui.widget.moveRowFocus(inner, self.detailScroll(), root_focus, delta); 721 } else if (key == .arrow_left) { 722 if (!ui.widget.moveInSelectedRow(inner, root_focus, false)) self.focusList(root_focus); 723 } else if (key == .arrow_right) { 724 _ = ui.widget.moveInSelectedRow(inner, root_focus, true); 725 } 726 } else { 727 try self.listInput(allocator, key, root_focus); 728 } 729 } 730 731 // navigate to the typed query's results, pinned to the list's ref. an 732 // empty query leaves the results for the plain log. 733 fn submit(self: *View, text: []const u8) !void { 734 if (text.len == 0 and self.data.search == null) return; 735 // an object view or a base-bounded list searches the default branch, so the url says so 736 const route = if (self.data.default_branch.len != 0) 737 self.data.handle.location.commitsRoute(.branch, self.data.default_branch, "") 738 else 739 self.data.handle.location.commitsRoute(self.data.ref_or_oid, self.data.ref_or_oid_value, self.data.base_oid); 740 try self.session.navigate((route orelse return).withSearch(text) orelse return); 741 } 742 743 fn listInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 744 // up/down (and the scroll wheel) move the selection a row; page up/down 745 // jump a fixed amount. right/Enter cross into the detail pane. Enter/clicks 746 // on the "next" row become navigation in the host before reaching here. 747 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 748 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 749 return; 750 } 751 switch (key) { 752 .enter => if (self.selectedCommitIndex() != null) 753 self.focusDetail(root_focus) 754 else if (self.data.next_start) |next| { 755 if (pageRoute(self.data, next)) |route| try self.session.navigate(route); 756 }, 757 .arrow_right => self.focusDetail(root_focus), 758 // when the window is too narrow to lay out the detail pane beside 759 // the list, a click on a row opens it like enter. the row was just 760 // selected, so the detail is swapped to it ahead of the build. 761 .mouse => |mouse| if (self.contentBox().children.values()[detail_index].rect == null and ui.widget.clickOnSelectedRow(self.listBox(), root_focus, mouse)) { 762 try self.refreshDetail(allocator, true); 763 self.focusDetail(root_focus); 764 }, 765 else => {}, 766 } 767 } 768 769 // enter the detail pane. the host arrives here on right-arrow or Enter from the 770 // list. a pane with no rows can't be entered. setFocus handles the too-narrow 771 // case where the pane isn't laid out yet (it gets selected, then focused after 772 // the next build, landing on its remembered row). 773 fn focusDetail(self: *View, root_focus: *Focus) void { 774 if (self.detailInner().children.count() == 0) return; 775 root_focus.setFocus(self.detailOuter().getFocus().id); 776 } 777 778 // return to the list. 779 fn focusList(self: *View, root_focus: *Focus) void { 780 root_focus.setFocus(self.listScroll().getFocus().id); 781 } 782 783 pub fn clearGrid(self: *View) void { 784 self.box.clearGrid(); 785 } 786 787 pub fn getGrid(self: View) ?Grid { 788 return self.box.getGrid(); 789 } 790 791 pub fn getFocus(self: *View) *Focus { 792 return self.box.getFocus(); 793 } 794 795 fn contentAtTop(self: *View) bool { 796 if (self.detailActive()) { 797 const inner = self.detailInner(); 798 const cid = inner.getFocus().child_id orelse return true; 799 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 800 return first_focused and self.detailScroll().y == 0; 801 } 802 const lb = self.listBox(); 803 const cid = lb.getFocus().child_id orelse return true; 804 return lb.children.getIndex(cid) == 0; 805 } 806 807 // only the header's own row sits directly below the repository header. 808 pub fn atTop(self: *View) bool { 809 if (self.data.no_commits) return true; 810 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 811 return self.headerActive() or (!focusable and self.contentAtTop()); 812 } 813 814 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 815 const header_view = self.header() orelse return false; 816 return header_view.focusHeader(root_focus); 817 } 818 }; 819 820 // the "a:" navigation link for the commits page walking from commit `oid` within 821 // `data.handle.location`. 822 fn commitsLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 823 const route = data.handle.location.commitsRoute(.object, oid, data.base_oid) orelse return error.RouteTooLong; 824 const url = try route.toUrl(page_arena); 825 return page_arena.allocator().print("a:{s}", .{url}); 826 } 827 828 // this list's page starting at `oid`: a repo's branch or tag keeps its ref (and 829 // its query) and moves `from` along, so the search box survives paging. a fork 830 // or an object view roots the page at the commit itself. 831 fn pageRoute(data: *const Self, oid: []const u8) ?ui.RoutablePage { 832 if (data.handle.location != .repo or data.ref_or_oid == .object) { 833 return data.handle.location.commitsRoute(.object, oid, data.base_oid); 834 } 835 const route = data.handle.location.commitsRoute(data.ref_or_oid, data.ref_or_oid_value, data.base_oid) orelse return null; 836 const paged = route.withFrom(oid) orelse return null; 837 return paged.withSearch(data.search orelse return paged); 838 } 839 840 fn nextPageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 841 const route = pageRoute(data, oid) orelse return error.RouteTooLong; 842 const url = try route.toUrl(page_arena); 843 return page_arena.allocator().print("a:{s}", .{url}); 844 } 845 846 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 847 // directory, within `location`. 848 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 849 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 850 const url = try route.toUrl(page_arena); 851 return page_arena.allocator().print("a:{s}", .{url}); 852 } 853 854 // the in-page "ai:" anchor for selecting a commit, showing its whole message 855 // when `message` is set. the href is only followed with js off. 856 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, commit: Commit, message: bool) ![]const u8 { 857 // a result roots the same results page at its commit, so the query 858 // survives following the row 859 if (data.search != null) { 860 const route = pageRoute(data, commit.oid) orelse return error.RouteTooLong; 861 return page_arena.allocator().print("ai:{s}", .{try route.toUrl(page_arena)}); 862 } 863 const route = (if (message) 864 data.handle.location.commitMessageRoute(.object, commit.oid, data.base_oid) 865 else 866 data.handle.location.commitsRoute(.object, commit.oid, data.base_oid)) orelse return error.RouteTooLong; 867 const url = try route.toUrl(page_arena); 868 return page_arena.allocator().print("ai:{s}", .{url}); 869 } 870 871 // the "a:" link to the page showing `oid`'s message on its own. 872 fn messageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 873 const route = data.handle.location.commitMessageRoute(.object, oid, data.base_oid) orelse return error.RouteTooLong; 874 const url = try route.toUrl(page_arena); 875 return page_arena.allocator().print("a:{s}", .{url}); 876 } 877