╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
545 var author = try ui.authorBox(allocator, self.session.page_arena, item.author); 546 errdefer author.deinit(allocator); 547 try box.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 548 } 549 550 if (box.children.count() > 0) box.getFocus().child_id = box.children.keys()[0]; 551 self.detailScroll().x = 0; 552 self.detailScroll().y = 0; 553 self.detailScroll().getFocus().version +%= 1; 554 detailed_index.* = index; 555 } 556 557 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 558 // scrolling crosses the sub header boundary the same way arrows do 559 const direction = inp.vertDirection(key); 560 if (self.headerActive()) { 561 if (direction == .down) root_focus.setFocus(self.content().getFocus().id) else try self.header().input(allocator, key, root_focus); 562 return; 563 } 564 if (direction == .up and self.contentAtTop()) { 565 self.header().focusCurrent(root_focus); 566 return; 567 } 568 if (self.detailActive()) { 569 if (key == .arrow_left) { 570 root_focus.setFocus(self.listScroll().getFocus().id); 571 } else if (inp.rowDelta(key, @intCast(self.detailBox().children.count()))) |delta| { 572 ui.widget.moveRowFocus(self.detailBox(), self.detailScroll(), root_focus, delta); 573 } 574 } else if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 575 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 576 } else switch (key) { 577 .arrow_right => if (self.detailBox().children.count() > 0) root_focus.setFocus(self.detailScroll().getFocus().id), 578 .enter => if (self.selectedEventIndex() != null) root_focus.setFocus(self.detailScroll().getFocus().id), 579 else => {}, 580 } 581 } 582 583 fn contentAtTop(self: *View) bool { 584 const box = if (self.detailActive()) self.detailBox() else self.listBox(); 585 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 586 const id = box.getFocus().child_id orelse return true; 587 return box.children.getIndex(id) == 0 and scroll.y == 0; 588 } 589 590 pub fn atTop(self: *View) bool { 591 return self.headerActive(); 592 } 593 594 // arriving from the page's tabs lands on the sub header, not the content 595 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 596 self.header().focusCurrent(root_focus); 597 return true; 598 } 599 600 pub fn clearGrid(self: *View) void { 601 self.box.clearGrid(); 602 } 603 604 pub fn getGrid(self: View) ?Grid { 605 return self.box.getGrid(); 606 } 607 608 pub fn getFocus(self: *View) *Focus { 609 return self.box.getFocus(); 610 } 611 }; 612 613 pub const Header = struct { 614 sync_status: ?[]const u8, 615 616 const nothing = "nothing to sync"; 617 const unsynced = "sync needed!"; 618 619 const Progress = struct { 620 message: *std.Io.Writer.Allocating, 621 622 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 623 switch (event) { 624 .text => |value| { 625 self.message.clearRetainingCapacity(); 626 try self.message.writer.writeAll(value); 627 }, 628 else => {}, 629 } 630 } 631 }; 632 633 pub fn local( 634 comptime repo_kind: rp.RepoKind, 635 comptime repo_opts: rp.RepoOpts(repo_kind), 636 aa: std.mem.Allocator, 637 repo: *rp.Repo(repo_kind, repo_opts), 638 io: std.Io, 639 failure: ?[]const u8, 640 ) !Header { 641 if (failure != null) return Header.empty(aa, failure); 642 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 643 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 644 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 645 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 646 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 647 } 648 649 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 650 return .{ .sync_status = if (failure) |name| try aa.print("error: {s}", .{name}) else nothing }; 651 } 652 653 pub fn remote() Header { 654 return .{ .sync_status = null }; 655 } 656 657 pub const View = struct { 658 box: wgt.Box(ui.Widget), 659 session: *ui.Session, 660 tab_ids: [2]usize, 661 // the tab stays selected while the sync button has focus 662 selected_tab: usize, 663 button_id: ?usize, 664 665 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 666 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 667 errdefer box.deinit(allocator); 668 var tab_ids: [2]usize = undefined; 669 const view_labels = [_][]const u8{ active_tab_label, removed_tab_label }; 670 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, view_labels, 0..) |view, view_label, i| { 671 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "", data.moment) orelse return error.RouteTooLong; 672 const count = data.window(view).count; 673 var label_buf: [64]u8 = undefined; 674 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ view_label, count }); 675 var tab = try wgt.TextBox.init(allocator, label, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 676 errdefer tab.deinit(allocator); 677 tab.getFocus().mode = .all; 678 const selected = std.meta.activeTag(session.data.current_page) == .repo_events and data.view == view; 679 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 680 tab_ids[i] = tab.getFocus().id; 681 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 682 } 683 684 // pushes the sync button and status to the right 685 { 686 var spacer = try ui.widget.Spacer.init(allocator); 687 errdefer spacer.deinit(allocator); 688 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null, .flex = .grow }); 689 } 690 691 var button_id: ?usize = null; 692 if (data.header.sync_status != null) { 693 box.getFocus().kind = .{ .custom = "form:/sync" }; 694 var button = try wgt.TextBox.init(allocator, "sync", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 695 errdefer button.deinit(allocator); 696 button.getFocus().mode = .all; 697 button.getFocus().kind = .{ .custom = "submit" }; 698 button_id = button.getFocus().id; 699 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 700 } 701 702 if (data.header.sync_status) |status_text| { 703 var status = try wgt.TextBox.init(allocator, status_text, .{ .border = .hidden, .wrap_kind = .none }); 704 errdefer status.deinit(allocator); 705 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 706 } 707 box.getFocus().child_id = tab_ids[@backingInt(data.view)]; 708 return .{ .box = box, .session = session, .tab_ids = tab_ids, .selected_tab = @backingInt(data.view), .button_id = button_id }; 709 } 710 711 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 712 self.box.deinit(allocator); 713 } 714 715 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 716 self.clearGrid(); 717 if (self.getSelectedIndex()) |index| self.selected_tab = index; 718 for (self.tab_ids, 0..) |id, index| { 719 const child = self.box.children.getPtr(id) orelse continue; 720 const tab = switch (child.widget) { 721 .text_box => |*text_box| text_box, 722 else => continue, 723 }; 724 ui.widget.markSelected(tab, index == self.selected_tab); 725 } 726 try self.box.build(allocator, constraint, root_focus); 727 } 728 729 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 730 const selected = self.box.getFocus().child_id orelse return; 731 const stops = [_]?usize{ self.tab_ids[0], self.tab_ids[1], self.button_id }; 732 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 733 const current = std.mem.indexOfScalar(?usize, stops[0..count], selected) orelse return; 734 if (inp.moveTab(key, current, count)) |next| { 735 root_focus.setFocus(stops[next] orelse unreachable); 736 return; 737 } 738 switch (key) { 739 .enter => if (self.syncFocused()) try self.requestSync(allocator), 740 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 741 else => {}, 742 } 743 } 744 745 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 746 if (!self.session.is_terminal) return; 747 if (self.session.host_request != null) return; 748 const button_id = self.button_id orelse return; 749 const child = self.box.children.getPtr(button_id) orelse return; 750 const button = switch (child.widget) { 751 .text_box => |*button| button, 752 else => return, 753 }; 754 try button.setContent(allocator, "syncing..."); 755 self.session.host_request = .sync_events; 756 } 757 758 fn syncFocused(self: *Header.View) bool { 759 const button_id = self.button_id orelse return false; 760 return self.box.getFocus().child_id == button_id; 761 } 762 763 pub fn getSelectedIndex(self: *Header.View) ?usize { 764 const id = self.box.getFocus().child_id orelse return null; 765 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 766 } 767 768 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 769 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 770 } 771 772 pub fn clearGrid(self: *Header.View) void { 773 self.box.clearGrid(); 774 } 775 776 pub fn getGrid(self: Header.View) ?Grid { 777 return self.box.getGrid(); 778 } 779 780 pub fn getFocus(self: *Header.View) *Focus { 781 return self.box.getFocus(); 782 } 783 }; 784 }; 785 786 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 787 if (comptime builtin.target.cpu.arch == .wasm32) return; 788 const io = session.io orelse return; 789 const source = session.local orelse return; 790 defer session.refresh_requested = true; 791 const failure = sync(io, allocator, source) catch |err| { 792 session.data.sync_failure = @errorName(err); 793 return; 794 }; 795 if (failure) |message| { 796 defer allocator.free(message); 797 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 798 return; 799 } 800 session.data.sync_failure = null; 801 switch (session.data.current_page) { 802 .repo_events => |route| { 803 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "", route.moment)) |first| 804 session.data.current_page = first; 805 }, 806 else => {}, 807 } 808 } 809 810 fn remoteName( 811 comptime repo_kind: rp.RepoKind, 812 comptime repo_opts: rp.RepoOpts(repo_kind), 813 repo: *rp.Repo(repo_kind, repo_opts), 814 io: std.Io, 815 allocator: std.mem.Allocator, 816 ) !?[]u8 { 817 var remotes = try repo.listRemotes(io, allocator); 818 defer remotes.deinit(); 819 const names = remotes.sections.keys(); 820 if (names.len == 0) return null; 821 for (names) |name| { 822 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 823 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 824 } 825 return try allocator.dupe(u8, names[0]); 826 } 827 828 fn isLoopbackSshUrl(url: []const u8) bool { 829 const uri = std.Uri.parse(url) catch return false; 830 const host = switch (uri.host orelse return false) { 831 .raw => |value| value, 832 .percent_encoded => |value| value, 833 }; 834 return std.mem.eql(u8, host, "localhost") or 835 std.mem.eql(u8, host, "127.0.0.1") or 836 std.mem.eql(u8, host, "::1") or 837 std.mem.eql(u8, host, "[::1]"); 838 } 839 840 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 841 var message: std.Io.Writer.Allocating = .init(allocator); 842 defer message.deinit(); 843 844 switch (source.repo_kind) { 845 inline else => |repo_kind| { 846 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 847 defer any_repo.deinit(io, allocator); 848 switch (any_repo) { 849 inline else => |*repo| { 850 const repo_opts = repo.self_repo_opts; 851 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 852 defer allocator.free(remote_name); 853 854 var config = try repo.listConfig(io, allocator); 855 defer config.deinit(); 856 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 857 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 858 const remote_section = try allocator.print("remote.{s}", .{remote_name}); 859 defer allocator.free(remote_section); 860 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 861 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 862 const push_url = remote_config.get("pushurl") orelse fetch_url; 863 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 864 const fetch_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(fetch_url)) loopback_opts else "" }); 865 defer allocator.free(fetch_ssh_command); 866 const push_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(push_url)) loopback_opts else "" }); 867 defer allocator.free(push_ssh_command); 868 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 869 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 870 871 const fetch_refspec = try allocator.print("+refs/heads/{s}:refs/remotes/{s}/{s}", .{ evt.events_ref.name, remote_name, evt.events_ref.name }); 872 defer allocator.free(fetch_refspec); 873 var fetch_opts = fetch_transport_opts; 874 fetch_opts.refspecs = &.{fetch_refspec}; 875 message.clearRetainingCapacity(); 876 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 877 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 878 return err; 879 }; 880 881 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 882 const local_oid = try repo.readRef(io, evt.events_ref); 883 const remote_oid = try repo.readRef(io, remote_ref); 884 if (local_oid == null and remote_oid == null) { 885 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 886 return null; 887 } 888 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 889 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 890 891 const push_refspec = try allocator.print("refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 892 defer allocator.free(push_refspec); 893 message.clearRetainingCapacity(); 894 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 895 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 896 return err; 897 }; 898 message.clearRetainingCapacity(); 899 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 900 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 901 return err; 902 }; 903 }, 904 } 905 }, 906 } 907 return null; 908 } 909