-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpbcopy
executable file
·71 lines (56 loc) · 1.33 KB
/
pbcopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/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