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