-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpbcopy
executable file
·70 lines (51 loc) · 1.45 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
#!/usr/bin/env perl
# linux versio nof pbcopy
use strict;
use warnings;
use MIME::Base64 qw(encode_base64);
my $input = do { local $/; <> } || "";
$input =~ s/\n+\z//sm;
$input or exit;
my $b = "\\"; # 1 backslash, not 2
# escape sequences taken from http://qiita.com/kefir_/items/515ed5264fce40dec522
my %format = (
normal => "\e]52;;%s\e$b",
tmux => "\ePtmux;\e\e]52;;%s\e\e$b$b\e$b",
screen => "\eP\e]52;;%s\x07\e$b",
);
my $format = sub {
if (my $tmux = $ENV{TMUX}) {
# check tmux -CC
(undef, my $pid) = split /,/, $tmux, 3;
if ($pid) {
my $ps = `ps -p $pid -o command= 2>/dev/null` || "";
chomp $ps;
(undef, my @option) = split /\s+/, $ps;
if (grep { $_ eq "-CC" } @option) {
return $format{normal};
}
}
$format{tmux};
} elsif ( ($ENV{TERM} || "") eq "screen" ) {
$format{screen};
} else {
$format{normal};
}
}->();
printf $format, encode_base64($input, "");
__END__
=head1 NAME
pbcopy - remote pbcopy for iTerm2
=head1 SYNOPSIS
[remote] $ echo "copy to clipboard from remote!" | pbcopy
[local] $ pbcopy
copy to clipboard from remote!
=head1 SEE ALSO
=over 4
=item * http://doda.b.sourceforge.jp/2011/12/15/tmux-set-clipboard/
=item * http://qiita.com/kefir_/items/1f635fe66b778932e278
=item * http://qiita.com/kefir_/items/515ed5264fce40dec522
=back
=head1 AUTHOR
Shoichi Kaji
=cut