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