╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
493 return; 494 } 495 if (self.detailActive()) { 496 if (key == .arrow_left) { 497 root_focus.setFocus(self.listScroll().getFocus().id); 498 } else if (inp.rowDelta(key, @intCast(self.detailBox().children.count()))) |delta| { 499 ui.moveRowFocus(self.detailBox(), self.detailScroll(), root_focus, delta); 500 } 501 } else if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 502 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 503 } else switch (key) { 504 .arrow_right => if (self.detailBox().children.count() > 0) root_focus.setFocus(self.detailScroll().getFocus().id), 505 .enter => if (self.selectedEventIndex() != null) root_focus.setFocus(self.detailScroll().getFocus().id), 506 else => {}, 507 } 508 } 509 510 fn contentAtTop(self: *View) bool { 511 const box = if (self.detailActive()) self.detailBox() else self.listBox(); 512 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 513 const id = box.getFocus().child_id orelse return true; 514 return box.children.getIndex(id) == 0 and scroll.y == 0; 515 } 516 517 pub fn getSelectedIndex(self: *View) ?usize { 518 return if (self.headerActive()) 0 else 1; 519 } 520 521 pub fn clearGrid(self: *View) void { 522 self.box.clearGrid(); 523 } 524 525 pub fn getGrid(self: View) ?Grid { 526 return self.box.getGrid(); 527 } 528 529 pub fn getFocus(self: *View) *Focus { 530 return self.box.getFocus(); 531 } 532 }; 533 534 pub const Header = struct { 535 sync_status: ?[]const u8, 536 537 const nothing = "nothing to sync"; 538 const unsynced = "sync needed!"; 539 540 const Progress = struct { 541 message: *std.Io.Writer.Allocating, 542 543 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 544 switch (event) { 545 .text => |value| { 546 self.message.clearRetainingCapacity(); 547 try self.message.writer.writeAll(value); 548 }, 549 else => {}, 550 } 551 } 552 }; 553 554 pub fn local( 555 comptime repo_kind: rp.RepoKind, 556 comptime repo_opts: rp.RepoOpts(repo_kind), 557 aa: std.mem.Allocator, 558 repo: *rp.Repo(repo_kind, repo_opts), 559 io: std.Io, 560 failure: ?[]const u8, 561 ) !Header { 562 if (failure != null) return Header.empty(aa, failure); 563 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 564 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 565 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 566 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 567 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 568 } 569 570 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 571 return .{ .sync_status = if (failure) |name| try std.fmt.allocPrint(aa, "error: {s}", .{name}) else nothing }; 572 } 573 574 pub fn remote() Header { 575 return .{ .sync_status = null }; 576 } 577 578 pub const View = struct { 579 box: wgt.Box(ui.Widget), 580 session: *ui.Session, 581 tab_ids: [2]usize, 582 button_id: ?usize, 583 584 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 585 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 586 errdefer box.deinit(allocator); 587 var tab_ids: [2]usize = undefined; 588 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, 0..) |view, i| { 589 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "") orelse return error.RouteTooLong; 590 const count = data.window(view).count; 591 var label_buf: [64]u8 = undefined; 592 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(view), count }); 593 var tab = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 594 errdefer tab.deinit(allocator); 595 tab.getFocus().focusable = true; 596 tab.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "ai:{s}", .{try route.toUrl(session.page_arena)}) }; 597 tab_ids[i] = tab.getFocus().id; 598 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 599 } 600 601 var button_id: ?usize = null; 602 if (data.header.sync_status != null) { 603 box.getFocus().kind = .{ .custom = "form:/sync" }; 604 var button = try wgt.TextBox(ui.Widget).init(allocator, "sync", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 605 errdefer button.deinit(allocator); 606 button.getFocus().focusable = true; 607 button.getFocus().kind = .{ .custom = "submit" }; 608 button_id = button.getFocus().id; 609 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 610 } 611 612 if (data.header.sync_status) |status_text| { 613 var status = try wgt.TextBox(ui.Widget).init(allocator, status_text, .{ .border_style = .hidden, .wrap_kind = .none }); 614 errdefer status.deinit(allocator); 615 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 616 } 617 box.getFocus().child_id = tab_ids[@intFromEnum(data.view)]; 618 return .{ .box = box, .session = session, .tab_ids = tab_ids, .button_id = button_id }; 619 } 620 621 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 622 self.box.deinit(allocator); 623 } 624 625 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 626 self.clearGrid(); 627 for (self.tab_ids) |id| { 628 const child = self.box.children.getPtr(id) orelse continue; 629 const tab = switch (child.widget) { 630 .text_box => |*text_box| text_box, 631 else => continue, 632 }; 633 tab.options.border_style = if (self.box.getFocus().child_id == id) .single else .hidden; 634 } 635 try self.box.build(allocator, constraint, root_focus); 636 } 637 638 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 639 const selected = self.box.getFocus().child_id orelse return; 640 const current = self.box.children.getIndex(selected) orelse return; 641 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 642 if (inp.moveTab(key, current, count)) |next| { 643 root_focus.setFocus(self.box.children.keys()[next]); 644 return; 645 } 646 switch (key) { 647 .enter => if (self.syncFocused()) try self.requestSync(allocator), 648 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 649 else => {}, 650 } 651 } 652 653 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 654 if (!self.session.is_terminal) return; 655 if (self.session.host_request != null) return; 656 const button_id = self.button_id orelse return; 657 const child = self.box.children.getPtr(button_id) orelse return; 658 const button = switch (child.widget) { 659 .text_box => |*button| button, 660 else => return, 661 }; 662 try button.setContent(allocator, "syncing..."); 663 self.session.host_request = .sync_events; 664 } 665 666 fn syncFocused(self: *Header.View) bool { 667 const button_id = self.button_id orelse return false; 668 return self.box.getFocus().child_id == button_id; 669 } 670 671 pub fn getSelectedIndex(self: *Header.View) ?usize { 672 const id = self.box.getFocus().child_id orelse return null; 673 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 674 } 675 676 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 677 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 678 } 679 680 pub fn clearGrid(self: *Header.View) void { 681 self.box.clearGrid(); 682 } 683 684 pub fn getGrid(self: Header.View) ?Grid { 685 return self.box.getGrid(); 686 } 687 688 pub fn getFocus(self: *Header.View) *Focus { 689 return self.box.getFocus(); 690 } 691 }; 692 }; 693 694 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 695 if (comptime builtin.target.cpu.arch == .wasm32) return; 696 const io = session.io orelse return; 697 const source = session.local orelse return; 698 defer session.refresh_requested = true; 699 const failure = sync(io, allocator, source) catch |err| { 700 session.data.sync_failure = @errorName(err); 701 return; 702 }; 703 if (failure) |message| { 704 defer allocator.free(message); 705 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 706 return; 707 } 708 session.data.sync_failure = null; 709 switch (session.data.current_page) { 710 .repo_events => |route| { 711 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "")) |first| 712 session.data.current_page = first; 713 }, 714 else => {}, 715 } 716 } 717 718 fn remoteName( 719 comptime repo_kind: rp.RepoKind, 720 comptime repo_opts: rp.RepoOpts(repo_kind), 721 repo: *rp.Repo(repo_kind, repo_opts), 722 io: std.Io, 723 allocator: std.mem.Allocator, 724 ) !?[]u8 { 725 var remotes = try repo.listRemotes(io, allocator); 726 defer remotes.deinit(); 727 const names = remotes.sections.keys(); 728 if (names.len == 0) return null; 729 for (names) |name| { 730 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 731 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 732 } 733 return try allocator.dupe(u8, names[0]); 734 } 735 736 fn isLoopbackSshUrl(url: []const u8) bool { 737 const uri = std.Uri.parse(url) catch return false; 738 const host = switch (uri.host orelse return false) { 739 .raw => |value| value, 740 .percent_encoded => |value| value, 741 }; 742 return std.mem.eql(u8, host, "localhost") or 743 std.mem.eql(u8, host, "127.0.0.1") or 744 std.mem.eql(u8, host, "::1") or 745 std.mem.eql(u8, host, "[::1]"); 746 } 747 748 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 749 var message: std.Io.Writer.Allocating = .init(allocator); 750 defer message.deinit(); 751 752 switch (source.repo_kind) { 753 inline else => |repo_kind| { 754 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 755 defer any_repo.deinit(io, allocator); 756 switch (any_repo) { 757 inline else => |*repo| { 758 const repo_opts = repo.self_repo_opts; 759 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 760 defer allocator.free(remote_name); 761 762 var config = try repo.listConfig(io, allocator); 763 defer config.deinit(); 764 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 765 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 766 const remote_section = try std.fmt.allocPrint(allocator, "remote.{s}", .{remote_name}); 767 defer allocator.free(remote_section); 768 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 769 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 770 const push_url = remote_config.get("pushurl") orelse fetch_url; 771 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 772 const fetch_ssh_command = try std.fmt.allocPrint(allocator, "{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(fetch_url)) loopback_opts else "" }); 773 defer allocator.free(fetch_ssh_command); 774 const push_ssh_command = try std.fmt.allocPrint(allocator, "{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(push_url)) loopback_opts else "" }); 775 defer allocator.free(push_ssh_command); 776 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 777 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 778 779 const fetch_refspec = try std.fmt.allocPrint(allocator, "+refs/heads/{s}:refs/remotes/{s}/{s}", .{ evt.events_ref.name, remote_name, evt.events_ref.name }); 780 defer allocator.free(fetch_refspec); 781 var fetch_opts = fetch_transport_opts; 782 fetch_opts.refspecs = &.{fetch_refspec}; 783 message.clearRetainingCapacity(); 784 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 785 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 786 return err; 787 }; 788 789 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 790 const local_oid = try repo.readRef(io, evt.events_ref); 791 const remote_oid = try repo.readRef(io, remote_ref); 792 if (local_oid == null and remote_oid == null) { 793 try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 794 return null; 795 } 796 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 797 try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 798 799 const push_refspec = try std.fmt.allocPrint(allocator, "refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 800 defer allocator.free(push_refspec); 801 message.clearRetainingCapacity(); 802 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 803 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 804 return err; 805 }; 806 message.clearRetainingCapacity(); 807 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 808 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 809 return err; 810 }; 811 }, 812 } 813 }, 814 } 815 return null; 816 } 817