#!/usr/bin/env perl use strict; use warnings; use MIME::Base64 qw(encode_base64); my $b = "\\"; # 1 backslash, not 2 my $normal = sub { my $base64 = shift; print "\e]52;;", $base64, "\e$b"; }; my $tmux = sub { my $base64 = shift; print "\ePtmux;\e\e]52;;", $base64, "\e\e$b$b\e$b"; }; my $screen = sub { my $base64 = shift; my $len = 76; my $first = 1; while (1) { my $chunk = substr $base64, 0, $len, ""; last if $chunk eq ""; if ($first) { print "\eP\e]52;;", $chunk; undef $first; } else { print "\e$b\eP", $chunk; } } print "\a\e$b"; }; my $input = do { local $/; <> } || ""; $input =~ s/\n+\z//sm; length $input or exit; my $print = $normal; # default CHOOSE: { if (my $tmux_env = $ENV{TMUX}) { (undef, my $pid) = split /,/, $tmux_env, 3; if ($pid) { my $ps = `ps -p $pid -o command= 2>/dev/null` || ""; chomp $ps; (undef, my @option) = split /\s+/, $ps; last CHOOSE if grep { $_ eq "-CC" } @option; } $print = $tmux; } elsif ( ($ENV{TERM} || "") =~ /^screen/ ) { $print = $screen; } } $print->( encode_base64($input, "") ); __END__ =head1 NAME pbcopy - remote pbcopy for iTerm2 =head1 AUTHOR Shoichi Kaji =head1 LICENSE MIT =cut