From 3499152841e851e26562164de5ec9d71081583c3 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 10 Nov 2018 17:12:57 -0500 Subject: simplify tmux-clipboard a bunch --- bin/tmux-clipboard | 96 +++++++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 45 deletions(-) (limited to 'bin/tmux-clipboard') diff --git a/bin/tmux-clipboard b/bin/tmux-clipboard index 338d2a2..2dc5a1a 100755 --- a/bin/tmux-clipboard +++ b/bin/tmux-clipboard @@ -1,68 +1,74 @@ #!/usr/bin/env perl use strict; use warnings; -use 5.014; +use 5.020; +use feature 'signatures'; +no warnings 'experimental::signatures'; use Config; -use Path::Class; -use POSIX qw(mkfifo); use constant { - FIFO_PATH => "$ENV{HOME}/.cache/tmux/clipboard/", COPY_CMD => ($Config{osname} eq 'linux' ? 'xclip -i' : 'pbcopy'), PASTE_CMD => ($Config{osname} eq 'linux' ? 'xclip -o' : 'pbpaste'), }; -my $fifo_file = file(FIFO_PATH, $$); -$fifo_file->dir->mkpath; -$fifo_file->remove; -END { $fifo_file->remove } -mkfifo("$fifo_file", 0700); - -if ($ARGV[0] eq 'copy') { - my $selection = $ARGV[1] || 'primary'; - - if (fork) { - exec("tmux save-buffer -a $fifo_file"); +sub main($cmd, $selection) { + if ($cmd eq 'copy') { + tmux_copy($selection); + } + elsif ($cmd eq 'paste') { + tmux_paste($selection); } else { - close STDOUT; - close STDERR; + die "usage: $0 [copy|paste]"; + } +} - my $copy_cmd = COPY_CMD; - if ($Config{osname} eq 'linux') { - $copy_cmd .= " -selection $selection"; - } +sub tmux_copy($selection='primary') { + set_clipboard_contents(get_tmux_buffer(), $selection); +} - open my $clipboard, '|-', $copy_cmd - or die "can't copy from clipboard using $copy_cmd: $!"; - $clipboard->print($fifo_file->slurp); - $clipboard->close; - } +sub tmux_paste($selection='primary') { + write_to_tmux(get_clipboard_contents($selection)); } -elsif ($ARGV[0] eq 'paste') { - my $selection = $ARGV[1] || 'primary'; - if (fork) { - system("tmux load-buffer -b tmux-clipboard $fifo_file"); - exec("tmux paste-buffer -b tmux-clipboard -dp"); +sub set_clipboard_contents($contents, $selection) { + my $copy_cmd = COPY_CMD; + if ($Config{osname} eq 'linux') { + $copy_cmd .= " -selection $selection"; } - else { - close STDOUT; - close STDERR; - my $paste_cmd = PASTE_CMD; - if ($Config{osname} eq 'linux') { - $paste_cmd .= " -selection $selection"; - } + open my $clipboard, '|-', $copy_cmd + or die "can't set clipboard contents using `$copy_cmd`: $!"; + print $clipboard $contents; + close $clipboard; +} - open my $clipboard, '-|', $paste_cmd - or die "can't paste from clipboard using $paste_cmd: $!"; - my $contents = do { local $/; <$clipboard> }; - $clipboard->close; - $fifo_file->spew(iomode => 'a', $contents); +sub get_clipboard_contents($selection) { + my $paste_cmd = PASTE_CMD; + if ($Config{osname} eq 'linux') { + $paste_cmd .= " -selection $selection"; } + + open my $clipboard, '-|', $paste_cmd + or die "can't get clipboard contents using `$paste_cmd`: $!"; + my $contents = do { local $/; <$clipboard> }; + close $clipboard; + $contents } -else { - die "usage: $0 [copy|paste]"; + +sub get_tmux_buffer { + `tmux show-buffer` } + +sub write_to_tmux($contents) { + my $tmux_cmd = "tmux load-buffer -b tmux-clipboard -"; + open my $tmux, '|-', $tmux_cmd + or die "can't set tmux buffer contents using `$tmux_cmd`: $!"; + print $tmux $contents; + close $tmux; + + system("tmux paste-buffer -b tmux-clipboard -dp"); +} + +main(@ARGV) -- cgit v1.2.3-54-g00ecf