╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
711 if (commit.author != .unknown or commit.committer != .unknown) { 712 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 713 errdefer row.deinit(allocator); 714 if (commit.author != .unknown) { 715 var tb = try ui.authorBox(allocator, self.session.page_arena, commit.author); 716 errdefer tb.deinit(allocator); 717 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 718 } 719 if (commit.committer != .unknown) { 720 var tb = try ui.authorBox(allocator, self.session.page_arena, commit.committer); 721 errdefer tb.deinit(allocator); 722 tb.options.label = " committer "; 723 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 724 } 725 row.getFocus().child_id = row.children.keys()[0]; 726 try inner.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 727 } 728 { 729 var tb = try wgt.TextBox.init(allocator, commit.timestamp, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = " timestamp " }); 730 errdefer tb.deinit(allocator); 731 tb.getFocus().mode = .all; 732 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 733 } 734 if (commit.merge) if (self.data.location.commitsMergeRoute(commit.oid)) |route| { 735 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)})); 736 }; 737 if (commit.stats) |stats| { 738 var text: std.Io.Writer.Allocating = .init(allocator); 739 defer text.deinit(); 740 try text.writer.print("lines changed: {d}", .{stats.lines_added + stats.lines_changed}); 741 if (stats.lines_removed != 0) try text.writer.print("\nlines removed: {d}", .{stats.lines_removed}); 742 const bytes_added = stats.bytes_added >= stats.bytes_removed; 743 const bytes = if (bytes_added) stats.bytes_added - stats.bytes_removed else stats.bytes_removed - stats.bytes_added; 744 try text.writer.print("\nbytes {s}: {d}\nfiles changed: {d}", .{ 745 if (bytes_added) "added" else "removed", bytes, stats.files_added + stats.files_changed, 746 }); 747 if (stats.files_removed != 0) try text.writer.print("\nfiles removed: {d}", .{stats.files_removed}); 748 var tb = try wgt.TextBox.init(allocator, text.written(), .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = " stats " }); 749 errdefer tb.deinit(allocator); 750 tb.getFocus().mode = .all; 751 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 752 } 753 try addLink(allocator, inner, "view files at this commit", try filesObjectLink(self.session.page_arena, self.data.location, commit.oid)); 754 } 755 756 try (Diff{ 757 .route = .{ .commit = .{ .location = self.data.location, .oid = commit.oid, .base_oid = self.data.base_oid } }, 758 .path = d.path, 759 .window = commit.window, 760 }).appendWindow(allocator, self.session, inner); 761 }, 762 } 763 764 // point the pane at its first row so focus recovery can land here. 765 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 766 767 // reset the scroll to the top for the newly-shown commit: directly on the 768 // terminal (the wasm offset), and via a version bump on the web (so the 769 // renderer's scroll id changes and JS drops the preserved position). 770 const sc = self.diffScroll(); 771 sc.x = 0; 772 sc.y = 0; 773 sc.getFocus().version +%= 1; 774 } 775 776 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 777 // scrolling crosses the sub header boundary the same way arrows do 778 const direction = inp.vertDirection(key); 779 if (self.headerActive()) { 780 // active means there is one 781 const header_view = self.header() orelse unreachable; 782 if (direction == .down) { 783 root_focus.setFocus(self.contentBox().getFocus().id); 784 return; 785 } 786 if (key == .enter) if (try header_view.submittedText(allocator)) |text| { 787 defer allocator.free(text); 788 return self.submit(text); 789 }; 790 return header_view.input(allocator, key, root_focus); 791 } 792 if (direction == .up and self.contentAtTop() and self.focusHeader(root_focus)) return; 793 if (self.diffActive()) { 794 const diff_view = &self.diffOuter().children.values()[0].widget.diff_view; 795 if (key == .arrow_left and self.diffScroll().x == 0) { 796 if (!diff_view.moveInRow(root_focus, false)) self.focusList(root_focus); 797 } else { 798 try diff_view.input(allocator, key, root_focus); 799 } 800 } else { 801 try self.listInput(key, root_focus); 802 } 803 } 804 805 // navigate to the typed query's results, pinned to the list's ref. an 806 // empty query leaves the results for the plain log. 807 fn submit(self: *View, text: []const u8) !void { 808 if (text.len == 0 and self.data.search == null) return; 809 // an object view or a base-bounded list searches the default branch, so the url says so 810 const route = if (self.data.default_branch.len != 0) 811 self.data.location.commitsRoute(.branch, self.data.default_branch, 0, "", "") 812 else 813 self.data.location.commitsRoute(self.data.ref_or_oid, self.data.ref_or_oid_value, 0, "", self.data.base_oid); 814 try self.session.navigate((route orelse return).withSearch(text) orelse return); 815 } 816 817 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 818 // up/down (and the scroll wheel) move the selection a row; page up/down 819 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 820 // on the "next" row become navigation in the host before reaching here. 821 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 822 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 823 return; 824 } 825 switch (key) { 826 .enter => if (self.selectedCommitIndex() != null) 827 self.focusDiff(root_focus) 828 else if (self.data.next_start) |next| { 829 if (pageRoute(self.data, next)) |route| try self.session.navigate(route); 830 }, 831 .arrow_right => self.focusDiff(root_focus), 832 else => {}, 833 } 834 } 835 836 // enter the diff pane. the host arrives here on right-arrow or Enter from the 837 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 838 // case where the pane isn't laid out yet (it gets selected, then focused after 839 // the next build, landing on its remembered hunk). 840 fn focusDiff(self: *View, root_focus: *Focus) void { 841 if (self.diffInner().children.count() == 0) return; 842 root_focus.setFocus(self.diffOuter().getFocus().id); 843 } 844 845 // return to the list. 846 fn focusList(self: *View, root_focus: *Focus) void { 847 root_focus.setFocus(self.listScroll().getFocus().id); 848 } 849 850 pub fn clearGrid(self: *View) void { 851 self.box.clearGrid(); 852 } 853 854 pub fn getGrid(self: View) ?Grid { 855 return self.box.getGrid(); 856 } 857 858 pub fn getFocus(self: *View) *Focus { 859 return self.box.getFocus(); 860 } 861 862 fn contentAtTop(self: *View) bool { 863 if (self.diffActive()) { 864 const inner = self.diffInner(); 865 const cid = inner.getFocus().child_id orelse return true; 866 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 867 return first_focused and self.diffScroll().y == 0; 868 } 869 const lb = self.listBox(); 870 const cid = lb.getFocus().child_id orelse return true; 871 return lb.children.getIndex(cid) == 0; 872 } 873 874 // only the header's own row sits directly below the repository header. 875 pub fn atTop(self: *View) bool { 876 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 877 return self.headerActive() or (!focusable and self.contentAtTop()); 878 } 879 880 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 881 const header_view = self.header() orelse return false; 882 return header_view.focusHeader(root_focus); 883 } 884 }; 885 886 // the "a:" navigation link for the commits page walking from commit `oid` within 887 // `data.location`, windowing the selected commit's diff from hunk `start`, filtered 888 // to `path` ("" = every file). 889 fn commitsLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 890 const route = data.location.commitsRoute(.object, oid, start, path, data.base_oid) orelse return error.RouteTooLong; 891 const url = try route.toUrl(page_arena); 892 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 893 } 894 895 // this list's page starting at `oid`: a repo's branch or tag keeps its ref (and 896 // its query) and moves `from` along, so the search box survives paging. a fork 897 // or an object view roots the page at the commit itself. 898 fn pageRoute(data: *const Self, oid: []const u8) ?ui.RoutablePage { 899 if (data.location != .repo or data.ref_or_oid == .object) { 900 return data.location.commitsRoute(.object, oid, 0, "", data.base_oid); 901 } 902 const route = data.location.commitsRoute(data.ref_or_oid, data.ref_or_oid_value, 0, "", data.base_oid) orelse return null; 903 const paged = route.withFrom(oid) orelse return null; 904 return paged.withSearch(data.search orelse return paged); 905 } 906 907 fn nextPageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 908 const route = pageRoute(data, oid) orelse return error.RouteTooLong; 909 const url = try route.toUrl(page_arena); 910 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 911 } 912 913 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 914 // directory, within `location`. 915 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 916 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 917 const url = try route.toUrl(page_arena); 918 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 919 } 920 921 // the in-page "ai:" anchor for selecting a commit with its current pane content 922 // and window. the href is only followed with js off. 923 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, commit: Commit, content: Content) ![]const u8 { 924 // a result roots the same results page at its commit, so the query 925 // survives following the row 926 if (data.search != null) { 927 const route = pageRoute(data, commit.oid) orelse return error.RouteTooLong; 928 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 929 } 930 const route = (switch (content) { 931 .message => data.location.commitMessageRoute(.object, commit.oid, data.base_oid), 932 .diff => |diff| data.location.commitsRoute(.object, commit.oid, commit.window.start, diff.path, data.base_oid), 933 }) orelse return error.RouteTooLong; 934 const url = try route.toUrl(page_arena); 935 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 936 } 937 938 // the "a:" link to the page showing `oid`'s message on its own. 939 fn messageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 940 const route = data.location.commitMessageRoute(.object, oid, data.base_oid) orelse return error.RouteTooLong; 941 const url = try route.toUrl(page_arena); 942 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 943 } 944