╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
526 const header_view = self.header() orelse return false; 527 return self.box.getFocus().child_id == header_view.getFocus().id; 528 } 529 530 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 531 return &self.contentBox().children.values()[list_index].widget.scroll; 532 } 533 534 fn listBox(self: *View) *wgt.Box(ui.Widget) { 535 return &self.listScroll().child.box; 536 } 537 538 fn detailOuter(self: *View) *wgt.Box(ui.Widget) { 539 return &self.contentBox().children.values()[detail_index].widget.box; 540 } 541 542 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 543 return &self.detailOuter().children.values()[0].widget.scroll; 544 } 545 546 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 547 return &self.detailScroll().child.box; 548 } 549 550 fn detailActive(self: *View) bool { 551 const content = self.contentBox(); 552 const cid = content.getFocus().child_id orelse return false; 553 return content.children.getIndex(cid) == detail_index; 554 } 555 556 // the selected commit's index, or null when the "next" row is selected. 557 fn selectedCommitIndex(self: *View) ?usize { 558 const lb = self.listBox(); 559 const cid = lb.getFocus().child_id orelse return null; 560 const idx = lb.children.getIndex(cid) orelse return null; 561 if (idx >= self.data.commits.len) return null; 562 return idx; 563 } 564 565 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 566 self.clearGrid(); 567 if (self.data.no_commits) return self.box.build(allocator, constraint, root_focus); 568 569 // swap the detail pane to the selected commit when it changes. 570 try self.refreshDetail(allocator); 571 572 // the selected list row shows a border (the focused TextBox upgrades it 573 // to a double border itself); the rest stay borderless. 574 const lb = self.listBox(); 575 for (lb.children.keys(), lb.children.values()) |id, *child| { 576 switch (child.widget) { 577 .text_box => |*tb| ui.widget.markSelected(tb, lb.getFocus().child_id == id), 578 else => {}, 579 } 580 } 581 582 // cap the list at list_max_width only while the detail pane fits beside it. 583 // the box drops the pane when the width can't hold both minimums, so when 584 // it's that narrow we lift the cap and let the list fill the whole width. 585 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 586 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 587 588 // stretch the detail pane across the rest of the width so it fills the area 589 // rather than shrinking to its content; its scroll fills the pane. when 590 // too narrow for both, it fills the whole width. 591 const detail_width: usize = if (constraint.max_size.width) |w| 592 (if (both_panes_fit) w - list_max_width else w) 593 else 594 detail_min_width; 595 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 596 597 // the web bounds the layout to the browser viewport like the terminal; 598 // each Scroll's web-native mode hands its full content to a real 599 // scrollable element, so we no longer build unbounded here. 600 // 601 // crossing panes only re-selects the content box's child (focusDetail / 602 // focusList); when the window is too narrow to show both, the pane that 603 // held focus is dropped here. the framework recovers focus after the 604 // top-level build by re-deriving it down the selected-child chain, so 605 // there's nothing to fix up afterward. 606 try self.box.build(allocator, constraint, root_focus); 607 } 608 609 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 610 const sel = self.selectedCommitIndex() orelse return; 611 if (self.detailed_index) |d| if (d == sel) return; 612 try self.populateDetail(allocator, sel); 613 self.detailed_index = sel; 614 } 615 616 fn populateDetail(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 617 const commit = self.data.commits[sel]; 618 const inner = self.detailInner(); 619 620 for (inner.children.values()) |*child| child.widget.deinit(allocator); 621 inner.children.clearAndFree(allocator); 622 inner.getFocus().child_id = null; 623 624 if (sel == 0 and self.data.message) { 625 try addLink(allocator, inner, "← back to commit", try commitsLink(self.session.page_arena, self.data, commit.oid)); 626 try self.addMessageBox(allocator, inner, commit, .whole); 627 } else { 628 const pa = self.session.page_arena; 629 { 630 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 631 errdefer row.deinit(allocator); 632 const diff_route = self.data.handle.location.commitDiffRoute(commit.oid, commit.parent_oid) orelse return error.RouteTooLong; 633 try addLink(allocator, &row, "view diff", try std.fmt.allocPrint(pa.allocator(), "a:{s}", .{try diff_route.toUrl(pa)})); 634 try addLink(allocator, &row, "view files at this commit", try filesObjectLink(pa, self.data.handle.location, commit.oid)); 635 row.getFocus().child_id = row.children.keys()[0]; 636 try inner.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 637 } 638 if (commit.merge) if (self.data.handle.location.commitsMergeRoute(commit.oid)) |route| { 639 try addLink(allocator, inner, "view commits from this merge", try std.fmt.allocPrint(pa.allocator(), "a:{s}", .{try route.toUrl(pa)})); 640 }; 641 if (commit.author != .unknown or commit.committer != .unknown) { 642 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 643 errdefer row.deinit(allocator); 644 if (commit.author != .unknown) { 645 var tb = try ui.authorBox(allocator, pa, commit.author); 646 errdefer tb.deinit(allocator); 647 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 648 } 649 if (commit.committer != .unknown) { 650 var tb = try ui.authorBox(allocator, pa, commit.committer); 651 errdefer tb.deinit(allocator); 652 tb.options.label = " committer "; 653 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 654 } 655 row.getFocus().child_id = row.children.keys()[0]; 656 try inner.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 657 } 658 { 659 var tb = try wgt.TextBox.init(allocator, commit.timestamp, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = " timestamp " }); 660 errdefer tb.deinit(allocator); 661 tb.getFocus().mode = .all; 662 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .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_style = .single, .round_corners = true, .wrap_kind = .none, .label = " 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 const inner = self.detailInner(); 715 if (inp.rowDelta(key, @intCast(inner.children.count()))) |delta| { 716 ui.widget.moveRowFocus(inner, self.detailScroll(), root_focus, delta); 717 } else if (key == .arrow_left) { 718 if (!ui.widget.moveInSelectedRow(inner, root_focus, false)) self.focusList(root_focus); 719 } else if (key == .arrow_right) { 720 _ = ui.widget.moveInSelectedRow(inner, root_focus, true); 721 } 722 } else { 723 try self.listInput(key, root_focus); 724 } 725 } 726 727 // navigate to the typed query's results, pinned to the list's ref. an 728 // empty query leaves the results for the plain log. 729 fn submit(self: *View, text: []const u8) !void { 730 if (text.len == 0 and self.data.search == null) return; 731 // an object view or a base-bounded list searches the default branch, so the url says so 732 const route = if (self.data.default_branch.len != 0) 733 self.data.handle.location.commitsRoute(.branch, self.data.default_branch, "") 734 else 735 self.data.handle.location.commitsRoute(self.data.ref_or_oid, self.data.ref_or_oid_value, self.data.base_oid); 736 try self.session.navigate((route orelse return).withSearch(text) orelse return); 737 } 738 739 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 740 // up/down (and the scroll wheel) move the selection a row; page up/down 741 // jump a fixed amount. right/Enter cross into the detail pane. Enter/clicks 742 // on the "next" row become navigation in the host before reaching here. 743 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 744 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 745 return; 746 } 747 switch (key) { 748 .enter => if (self.selectedCommitIndex() != null) 749 self.focusDetail(root_focus) 750 else if (self.data.next_start) |next| { 751 if (pageRoute(self.data, next)) |route| try self.session.navigate(route); 752 }, 753 .arrow_right => self.focusDetail(root_focus), 754 else => {}, 755 } 756 } 757 758 // enter the detail pane. the host arrives here on right-arrow or Enter from the 759 // list. a pane with no rows can't be entered. setFocus handles the too-narrow 760 // case where the pane isn't laid out yet (it gets selected, then focused after 761 // the next build, landing on its remembered row). 762 fn focusDetail(self: *View, root_focus: *Focus) void { 763 if (self.detailInner().children.count() == 0) return; 764 root_focus.setFocus(self.detailOuter().getFocus().id); 765 } 766 767 // return to the list. 768 fn focusList(self: *View, root_focus: *Focus) void { 769 root_focus.setFocus(self.listScroll().getFocus().id); 770 } 771 772 pub fn clearGrid(self: *View) void { 773 self.box.clearGrid(); 774 } 775 776 pub fn getGrid(self: View) ?Grid { 777 return self.box.getGrid(); 778 } 779 780 pub fn getFocus(self: *View) *Focus { 781 return self.box.getFocus(); 782 } 783 784 fn contentAtTop(self: *View) bool { 785 if (self.detailActive()) { 786 const inner = self.detailInner(); 787 const cid = inner.getFocus().child_id orelse return true; 788 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 789 return first_focused and self.detailScroll().y == 0; 790 } 791 const lb = self.listBox(); 792 const cid = lb.getFocus().child_id orelse return true; 793 return lb.children.getIndex(cid) == 0; 794 } 795 796 // only the header's own row sits directly below the repository header. 797 pub fn atTop(self: *View) bool { 798 if (self.data.no_commits) return true; 799 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 800 return self.headerActive() or (!focusable and self.contentAtTop()); 801 } 802 803 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 804 const header_view = self.header() orelse return false; 805 return header_view.focusHeader(root_focus); 806 } 807 }; 808 809 // the "a:" navigation link for the commits page walking from commit `oid` within 810 // `data.handle.location`. 811 fn commitsLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 812 const route = data.handle.location.commitsRoute(.object, oid, data.base_oid) orelse return error.RouteTooLong; 813 const url = try route.toUrl(page_arena); 814 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 815 } 816 817 // this list's page starting at `oid`: a repo's branch or tag keeps its ref (and 818 // its query) and moves `from` along, so the search box survives paging. a fork 819 // or an object view roots the page at the commit itself. 820 fn pageRoute(data: *const Self, oid: []const u8) ?ui.RoutablePage { 821 if (data.handle.location != .repo or data.ref_or_oid == .object) { 822 return data.handle.location.commitsRoute(.object, oid, data.base_oid); 823 } 824 const route = data.handle.location.commitsRoute(data.ref_or_oid, data.ref_or_oid_value, data.base_oid) orelse return null; 825 const paged = route.withFrom(oid) orelse return null; 826 return paged.withSearch(data.search orelse return paged); 827 } 828 829 fn nextPageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 830 const route = pageRoute(data, oid) orelse return error.RouteTooLong; 831 const url = try route.toUrl(page_arena); 832 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 833 } 834 835 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 836 // directory, within `location`. 837 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 838 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 839 const url = try route.toUrl(page_arena); 840 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 841 } 842 843 // the in-page "ai:" anchor for selecting a commit, showing its whole message 844 // when `message` is set. the href is only followed with js off. 845 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, commit: Commit, message: bool) ![]const u8 { 846 // a result roots the same results page at its commit, so the query 847 // survives following the row 848 if (data.search != null) { 849 const route = pageRoute(data, commit.oid) orelse return error.RouteTooLong; 850 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 851 } 852 const route = (if (message) 853 data.handle.location.commitMessageRoute(.object, commit.oid, data.base_oid) 854 else 855 data.handle.location.commitsRoute(.object, commit.oid, data.base_oid)) orelse return error.RouteTooLong; 856 const url = try route.toUrl(page_arena); 857 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 858 } 859 860 // the "a:" link to the page showing `oid`'s message on its own. 861 fn messageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 862 const route = data.handle.location.commitMessageRoute(.object, oid, data.base_oid) orelse return error.RouteTooLong; 863 const url = try route.toUrl(page_arena); 864 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 865 } 866