blib/lib/Clipboard/WaylandClipboard.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 100 CC 4 SCAR 13.1
CC
4
Cov
0%
copy_to_selection
6.0
paste
6.0
paste_from_selection
6.0
CRAP 3.7
1 package Clipboard::WaylandClipboard;
2 $Clipboard::WaylandClipboard::VERSION = '0.32';
3 # First written 04/11/2024 by Lester Hightower (PAUSE ID hightowe)
4  
5 use strict;
6 use warnings;
7  
8 use File::Spec ();
9  
10 sub copy {
11 my $self = shift;
12 my ($input) = @_;
13 return $self->copy_to_selection($self->favorite_selection, $input);
14 }
15  
16 sub copy_to_all_selections {
17 my $self = shift;
18 my ($input) = @_;
19 foreach my $sel ($self->all_selections) {
20 $self->copy_to_selection($sel, $input);
21 }
22 return;
23 }
24  
25 sub copy_to_selection {
26 my $self = shift;
27 my ($selection, $input) = @_;
28  
29 my @cmd_parts = qw(| wl-copy);
30 push(@cmd_parts, '--primary') if ($selection eq 'primary');
31 my $cmd = join(' ', @cmd_parts);
32 my $r = open my $exe, $cmd or die "Couldn't run `$cmd`: $!\n";
33 binmode $exe, ':encoding(UTF-8)';
34 print {$exe} $input;
35 close $exe or die "Error closing `$cmd`: $!";
36  
37 return;
38 }
39 sub paste {
40 my $self = shift;
41 for ($self->all_selections) {
42 my $data = $self->paste_from_selection($_);
43 return $data if length $data;
44 }
45 return undef;
46 }
47 sub paste_from_selection {
48 my $self = shift;
49 my ($selection) = @_;
50  
51 my @cmd_parts = qw(wl-paste --no-newline);
52 push(@cmd_parts, '--primary') if ($selection eq 'primary');
53 push(@cmd_parts, '|');
54 my $cmd = join(' ', @cmd_parts);
55 open my $exe, $cmd or die "Couldn't run `$cmd`: $!\n";
56 my $result = do { local $/; <$exe> };
57 close $exe or die "Error closing `$cmd`: $!";
58 return $result;
59 }
60 # "regular" isn't directly used in this code (it is the wl-clipboard default)
61 sub all_selections { qw( regular primary ) }
62 sub favorite_selection { my $self = shift; ($self->all_selections)[0] }
63  
64 sub available {
65 # close STDERR
66 open my $olderr, '>&', \*STDERR;
67 close STDERR;
68 open STDERR, '>', File::Spec->devnull;
69  
70 my $open_retval = open my $just_checking, 'wl-copy --version |';
71  
72 # restore STDERR
73 close STDERR;
74 open STDERR, '>&', $olderr;
75 close $olderr;
76  
77 return $open_retval;
78 }
79  
80 {
81 available() or warn <<'EPIGRAPH';
82  
83 Can't find the 'wl-copy' program from the wl‐clipboard package.
84  
85 Clipboard.pm's Wayland support depends on it.
86  
87 Here's the project homepage: https://github.com/bugaevc/wl-clipboard
88  
89 EPIGRAPH
90 }
91  
92 1;
93  
94 __END__