blib/lib/Clipboard/Any.pm

filter: Statementstmt 15.0% 38 / 253 Branchbran 0.0% 0 / 154 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 46.6% 7 / 15 Podpod 100% 7 / 7 totaltotal 11.2% 52 / 462 CC 86 SCAR 51.8
CC
86
Cov
9%
get_clipboard_content
272.0
add_clipboard_content
240.0
list_clipboard_history
210.0
CRAP 177.4
↑ Summary
1 package Clipboard::Any;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Exporter::Rinci qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IPC::System::Options 'system', 'readpipe', 'run', -log=>1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 1 our $DATE = '2026-07-07'; # DATE
13 1 our $DIST = 'Clipboard-Any'; # DIST
14 1 our $VERSION = '0.017'; # VERSION
15  
16 0 ▶sub _x_selection {
Subroutine _x_selection: not called CC=2 SCAR=17.9
17 0 ▶ my $sel = $ENV{PERL_CLIPBOARD_ANY_XCLIP_SELECTION} // 'primary';
Condition0%0/2
$ENV{"PERL_CLIPBOARD_ANY_XCLIP_SELECTION"} // "primary"
l!l
00
MC/DC0%0/1
$ENV{"PERL_CLIPBOARD_ANY_XCLIP_SELECTION"} // "primary"
$ENV{"PERL_CLIPBOARD_ANY_XCLIP_SELECTION"}
Truth table0%0/2
A
A= $ENV{"PERL_CLIPBOARD_ANY_XCLIP_SELECTION"}
Aresult
11
00
18 0 $sel =~ s/\W+/_/g;
19 0 $sel;
20 }
21  
22 1 our $known_clipboard_managers = [qw/klipper parcellite clipit xclip/];
23 1 our $sch_clipboard_manager = ['str', in=>$known_clipboard_managers];
24 1 our %argspecopt_clipboard_manager = (
25 clipboard_manager => {
26 summary => 'Explicitly set clipboard manager to use',
27 schema => $sch_clipboard_manager,
28 description => <<'MARKDOWN',
29  
30 The default, when left undef, is to detect what clipboard manager is running.
31  
32 MARKDOWN
33 cmdline_aliases => {m=>{}},
34 },
35 );
36  
37 1 our %argspec0_index = (
38 index => {
39 summary => 'Index of item in history (0 means the current/latest, 1 the second latest, and so on)',
40 schema => 'int*',
41 description => <<'MARKDOWN',
42  
43 If the index exceeds the number of items in history, empty string or undef will
44 be returned instead.
45  
46 MARKDOWN
47 },
48 );
49  
50 1 our %SPEC;
51  
52 1 $SPEC{':package'} = {
53 v => 1.1,
54 summary => 'Common interface to clipboard manager functions',
55 description => <<'MARKDOWN',
56  
57 This module provides common functions related to clipboard manager.
58  
59 Supported clipboard manager: KDE Plasma's Klipper (`klipper`), `parcellite`,
60 `clipit`, `xclip`. Support for more clipboard managers, e.g. on Windows or other
61 Linux desktop environment is welcome.
62  
63 MARKDOWN
64 };
65  
66 1 $SPEC{'detect_clipboard_manager'} = {
67 v => 1.1,
68 summary => 'Detect which clipboard manager program is currently running',
69 description => <<'MARKDOWN',
70  
71 Will return a string containing name of clipboard manager program, e.g.
72 `klipper`. Will return undef if no known clipboard manager is detected.
73  
74 MARKDOWN
75 result_naked => 1,
76 args => {
77 detail => {
78 schema => 'bool*',
79 cmdline_aliases => {l=>{}},
80 },
81 },
82 #result => {
83 # schema => $sch_clipboard_manager,
84 #},
85 };
86 0 ▶sub detect_clipboard_manager {
Subroutine detect_clipboard_manager: not called CC=9 SCAR=43.8
Pod documented
87 0 my %args = @_;
88  
89 0 require File::Which;
90  
91 0 require Proc::Find;
92 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 0 local $Proc::Find::CACHE = 1;
94  
95 0 my $info = {};
96 DETECT: {
97  
98 0 DETECT_KLIPPER:
99 {
100 0 log_trace "Checking whether clipboard manager klipper is running ...";
101  
102 METHOD1: {
103 0 require Desktop::KDE::Util;
104 0 my $paths = Desktop::KDE::Util::which_qdbus();
105  
106 0 ▶ unless (@$paths) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$paths) 0 0 0
107 0 log_trace "qdbus not found, checking using qdbus";
108 0 last;
109 }
110  
111 0 for my $path (@$paths) {
112 0 my $out;
113 0 system({capture_merged=>\$out}, $path, "org.kde.klipper", "/klipper");
114 0 ▶ unless ($? == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? == 0) 0 0 0
115 # note, when klipper is disabled via System Tray Settings >
116 # General > Extra Items, the object path /klipper disappears.
117 0 log_trace "Failed listing org.kde.klipper /klipper methods (using qdus at $path)";
118 0 next;
119 }
120 0 log_trace "org.kde.klipper/klipper object active, concluding using klipper";
121 0 $info->{manager} = "klipper";
122 0 $info->{klipper_path} = $path;
123 0 last DETECT;
124 }
125 }
126  
127 # we need qdbus anyway
128 #METHOD2: {
129 # my $pids = Proc::Find::find_proc(name => "dbus-daemon");
130 # if (@$pids) {
131 # log_trace "There is dbus-daemon running, assuming we are using klipper";
132 # $info->{manager} = "klipper";
133 # last DETECT;
134 # } else {
135 # log_trace "dbus-daemon process does not seem to be running, probably not using klipper";
136 # }
137 #}
138 } # DETECT_KLIPPER
139  
140 DETECT_PARCELLITE:
141 {
142 0 log_trace "Checking whether clipboard manager parcellite is running ...";
143 0 my $pids = Proc::Find::find_proc(name => "parcellite");
144 0 ▶ if (@$pids) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pids) { } 0 0 0
145 0 log_trace "parcellite process is running, concluding using parcellite";
146 0 $info->{manager} = "parcellite";
147 0 last DETECT;
148 } else {
149 0 log_trace "parcellite process does not seem to be running, probably not using parcellite";
150 }
151 } # DETECT_PARCELLITE
152  
153 DETECT_CLIPIT:
154 {
155 # basically the same as parcellite
156 0 log_trace "Checking whether clipboard manager clipit is running ...";
157 0 my $pids = Proc::Find::find_proc(name => "clipit");
158 0 ▶ if (@$pids) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pids) { } 0 0 0
159 0 log_trace "clipit process is running, concluding using clipit";
160 0 $info->{manager} = "parcellite";
161 0 last DETECT;
162 } else {
163 0 log_trace "clipit process does not seem to be running, probably not using clipit";
164 }
165 } # DETECT_CLIPIT
166  
167 DETECT_XCLIP:
168 {
169 0 log_trace "Checking whether xclip is available ...";
170 0 my $path = File::Which::which("xclip");
171 0 ▶ unless ($path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 0 0
172 0 log_trace "xclip not found in PATH, skipping choosing xclip";
173 0 last;
174 }
175 0 log_trace "xclip found in PATH, concluding using xclip";
176 0 $info->{manager} = "xclip";
177 0 $info->{xclip_path} = $path;
178 } # DETECT_XCLIP
179  
180 log_trace "No known clipboard manager is detected"
181 0 ▶ unless $info->{manager};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"manager"} 0 0 0
182 } # DETECT
183  
184 0 ▶ if ($args{detail}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'detail'}) { } 0 0 0
185 0 $info;
186 } else {
187 0 $info->{manager};
188 }
189 }
190  
191 1 $SPEC{'clear_clipboard_history'} = {
192 v => 1.1,
193 summary => 'Delete all clipboard items',
194 description => <<'MARKDOWN',
195  
196 MARKDOWN
197 args => {
198 %argspecopt_clipboard_manager,
199 },
200 };
201 0 ▶sub clear_clipboard_history {
Subroutine clear_clipboard_history: not called CC=12 SCAR=50.5
Pod documented
202 0 my %args = @_;
203  
204 0 ▶ my $clipboard_manager = $args{clipboard_manager} // detect_clipboard_manager();
Condition0%0/3
$args{"clipboard_manager"} // detect_clipboard_manager()
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"clipboard_manager"} // detect_clipboard_manager()
$args{"clipboard_manager"} detect_clipboard_manager()
Truth table0%0/3
A // B
A= $args{"clipboard_manager"}B= detect_clipboard_manager()
ABresult
1X1
011
000
205 0 ▶ return [412, "Can't detect any known clipboard manager"]
Branch: 1 branch
BranchTrueFalseTotal
unless $clipboard_manager 0 0 0
206 unless $clipboard_manager;
207  
208 0 ▶ if ($clipboard_manager eq 'klipper') {
Branch: 4 branches
BranchTrueFalseTotal
if ($clipboard_manager eq 'klipper') { } 0 0 0
elsif ($clipboard_manager eq 'parcellite') { } 0 0 0
elsif ($clipboard_manager eq 'clipit') { } 0 0 0
elsif ($clipboard_manager eq 'xclip') { } 0 0 0
209 0 require Desktop::KDE::Util;
210 0 my $paths = Desktop::KDE::Util::which_qdbus();
211 0 ▶ die "Can't find qdbus" unless @$paths;
Branch: 1 branch
BranchTrueFalseTotal
unless @$paths 0 0 0
212 0 my ($stdout, $stderr);
213 # qdbus likes to emit an empty line
214 0 system({capture_stdout=>\$stdout, capture_stderr=>\$stderr},
215 $paths->[0], "org.kde.klipper", "/klipper", "clearClipboardHistory");
216 0 ▶ my $exit_code = $? < 0 ? $? : $?>>8;
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
217 0 ▶ return [500, "/klipper's clearClipboardHistory failed: $exit_code"] if $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code 0 0 0
218 0 return [200, "OK"];
219 } elsif ($clipboard_manager eq 'parcellite') {
220 0 return [501, "Not yet implemented"];
221 } elsif ($clipboard_manager eq 'clipit') {
222 0 return [501, "Not yet implemented"];
223 } elsif ($clipboard_manager eq 'xclip') {
224 0 my $fh;
225  
226 0 my $sel = _x_selection();
227 0 ▶ open $fh, "| xclip -i -selection $sel" ## no critic: InputOutput::ProhibitTwoArgOpen
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "| xclip -i -selection $sel" or return [500, "xclip -i -selection $sel failed (1): $!"] 0 0 0
228 or return [500, "xclip -i -selection $sel failed (1): $!"];
229 0 print $fh '';
230 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return [500, "xclip -i -selection $sel failed (2): $!"] 0 0 0
231 or return [500, "xclip -i -selection $sel failed (2): $!"];
232  
233 0 return [200, "OK"];
234 }
235  
236 0 [412, "Cannot clear clipboard history (clipboard manager=$clipboard_manager)"];
237 }
238  
239 1 $SPEC{'clear_clipboard_content'} = {
240 v => 1.1,
241 summary => 'Delete current clipboard content',
242 description => <<'MARKDOWN',
243  
244 MARKDOWN
245 args => {
246 %argspecopt_clipboard_manager,
247 },
248 };
249 0 ▶sub clear_clipboard_content {
Subroutine clear_clipboard_content: not called CC=12 SCAR=50.5
Pod documented
250 0 my %args = @_;
251  
252 0 ▶ my $clipboard_manager = $args{clipboard_manager} // detect_clipboard_manager();
Condition0%0/3
$args{"clipboard_manager"} // detect_clipboard_manager()
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"clipboard_manager"} // detect_clipboard_manager()
$args{"clipboard_manager"} detect_clipboard_manager()
Truth table0%0/3
A // B
A= $args{"clipboard_manager"}B= detect_clipboard_manager()
ABresult
1X1
011
000
253 0 ▶ return [412, "Can't detect any known clipboard manager"]
Branch: 1 branch
BranchTrueFalseTotal
unless $clipboard_manager 0 0 0
254 unless $clipboard_manager;
255  
256 0 ▶ if ($clipboard_manager eq 'klipper') {
Branch: 4 branches
BranchTrueFalseTotal
if ($clipboard_manager eq 'klipper') { } 0 0 0
elsif ($clipboard_manager eq 'parcellite') { } 0 0 0
elsif ($clipboard_manager eq 'clipit') { } 0 0 0
elsif ($clipboard_manager eq 'xclip') { } 0 0 0
257 0 require Desktop::KDE::Util;
258 0 my $paths = Desktop::KDE::Util::which_qdbus();
259 0 ▶ die "Can't find qdbus" unless @$paths;
Branch: 1 branch
BranchTrueFalseTotal
unless @$paths 0 0 0
260 0 my ($stdout, $stderr);
261 # qdbus likes to emit an empty line
262 0 system({capture_stdout=>\$stdout, capture_stderr=>\$stderr},
263 $paths->[0], "org.kde.klipper", "/klipper", "clearClipboardContents");
264 0 ▶ my $exit_code = $? < 0 ? $? : $?>>8;
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
265 0 ▶ return [500, "/klipper's clearClipboardContents failed: $exit_code"] if $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code 0 0 0
266 0 return [200, "OK"];
267 } elsif ($clipboard_manager eq 'parcellite') {
268 0 return [501, "Not yet implemented"];
269 } elsif ($clipboard_manager eq 'clipit') {
270 0 return [501, "Not yet implemented"];
271 } elsif ($clipboard_manager eq 'xclip') {
272 0 my $fh;
273 0 my $sel = _x_selection();
274 0 ▶ open $fh, "| xclip -i -selection $sel" ## no critic: InputOutput::ProhibitTwoArgOpen
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "| xclip -i -selection $sel" or return [500, "xclip -i -selection $sel failed (1): $!"] 0 0 0
275 or return [500, "xclip -i -selection $sel failed (1): $!"];
276 0 print $fh '';
277 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return [500, "xclip -i -selection $sel failed (2): $!"] 0 0 0
278 or return [500, "xclip -i -selection $sel failed (2): $!"];
279 0 return [200, "OK"];
280 }
281  
282 0 [412, "Cannot clear clipboard content (clipboard manager=$clipboard_manager)"];
283 }
284  
285 1 $SPEC{'get_clipboard_content'} = {
286 v => 1.1,
287 summary => 'Get the clipboard content (most recent, history index [0])',
288 description => <<'MARKDOWN',
289  
290 Caveats for klipper: Non-text item is not retrievable by getClipboardContents().
291 If the current item is e.g. an image, then the next text item from history will
292 be returned instead, or empty string if none exists.
293  
294 MARKDOWN
295 args => {
296 %argspecopt_clipboard_manager,
297 },
298 examples => [
299 {
300 summary => 'Munge text (remove duplicate spaces) in clipboard',
301 src_plang => 'bash',
302 src => q{[[prog]] | perl -lpe's/ {2,}/ /g' | clipadd},
303 test => 0,
304 'x.doc.show_result' => 0,
305 },
306 ],
307 };
308 0 ▶sub get_clipboard_content {
Subroutine get_clipboard_content: not called CC=16 SCAR=56.1
Pod documented
309 0 my %args = @_;
310  
311 0 ▶ my $clipboard_manager = $args{clipboard_manager} // detect_clipboard_manager();
Condition0%0/3
$args{"clipboard_manager"} // detect_clipboard_manager()
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"clipboard_manager"} // detect_clipboard_manager()
$args{"clipboard_manager"} detect_clipboard_manager()
Truth table0%0/3
A // B
A= $args{"clipboard_manager"}B= detect_clipboard_manager()
ABresult
1X1
011
000
312 0 ▶ return [412, "Can't detect any known clipboard manager"]
Branch: 1 branch
BranchTrueFalseTotal
unless $clipboard_manager 0 0 0
313 unless $clipboard_manager;
314  
315 0 ▶ if ($clipboard_manager eq 'klipper') {
Branch: 4 branches
BranchTrueFalseTotal
if ($clipboard_manager eq 'klipper') { } 0 0 0
elsif ($clipboard_manager eq 'parcellite') { } 0 0 0
elsif ($clipboard_manager eq 'clipit') { } 0 0 0
elsif ($clipboard_manager eq 'xclip') { } 0 0 0
316 0 require Desktop::KDE::Util;
317 0 my $paths = Desktop::KDE::Util::which_qdbus();
318 0 ▶ die "Can't find qdbus" unless @$paths;
Branch: 1 branch
BranchTrueFalseTotal
unless @$paths 0 0 0
319 0 my ($stdout, $stderr);
320 0 system({capture_stdout=>\$stdout, capture_stderr=>\$stderr},
321 $paths->[0], "org.kde.klipper", "/klipper", "getClipboardContents");
322 0 ▶ my $exit_code = $? < 0 ? $? : $?>>8;
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
323 0 ▶ return [500, "/klipper's getClipboardContents failed: $exit_code"] if $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code 0 0 0
324 0 chomp $stdout;
325 0 return [200, "OK", $stdout];
326 } elsif ($clipboard_manager eq 'parcellite') {
327 0 my ($stdout, $stderr);
328 0 system({capture_stdout=>\$stdout, capture_stderr=>\$stderr},
329 "parcellite", "-p");
330 0 ▶ my $exit_code = $? < 0 ? $? : $?>>8;
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
331 0 ▶ return [500, "parcellite command failed with exit code $exit_code"] if $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code 0 0 0
332 0 return [200, "OK", $stdout];
333 } elsif ($clipboard_manager eq 'clipit') {
334 0 my ($stdout, $stderr);
335 0 system({capture_stdout=>\$stdout, capture_stderr=>\$stderr},
336 "clipit", "-p");
337 0 ▶ my $exit_code = $? < 0 ? $? : $?>>8;
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
338 0 ▶ return [500, "clipit command failed with exit code $exit_code"] if $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code 0 0 0
339 0 return [200, "OK", $stdout];
340 } elsif ($clipboard_manager eq 'xclip') {
341 0 my ($stdout, $stderr);
342 0 my $sel = _x_selection();
343 0 system({capture_stdout=>\$stdout, capture_stderr=>\$stderr},
344 "xclip", "-o", "-selection", $sel);
345 0 ▶ my $exit_code = $? < 0 ? $? : $?>>8;
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
346 0 ▶ return [500, "xclip -o -selection $sel failed (exit code: $exit_code"] if $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code 0 0 0
347 0 return [200, "OK", $stdout];
348 }
349  
350 0 [412, "Cannot get clipboard content (clipboard manager=$clipboard_manager)"];
351 }
352  
353 1 $SPEC{'list_clipboard_history'} = {
354 v => 1.1,
355 summary => 'List the clipboard history',
356 description => <<'MARKDOWN',
357  
358 Caveats for klipper: 1) Klipper does not provide method to get the length of
359 history. So we retrieve history item one by one using getClipboardHistoryItem(i)
360 from i=0, i=1, and so on. And assume that if we get two consecutive empty
361 string, it means we reach the end of the clipboard history before the first
362 empty result.
363  
364 2) Non-text items are not retrievable by getClipboardHistoryItem().
365  
366 MARKDOWN
367 args => {
368 %argspecopt_clipboard_manager,
369 },
370 };
371 0 ▶sub list_clipboard_history {
Subroutine list_clipboard_history: not called CC=14 SCAR=53.5
Pod documented
372 0 my %args = @_;
373  
374 0 ▶ my $clipboard_manager = $args{clipboard_manager} // detect_clipboard_manager();
Condition0%0/3
$args{"clipboard_manager"} // detect_clipboard_manager()
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"clipboard_manager"} // detect_clipboard_manager()
$args{"clipboard_manager"} detect_clipboard_manager()
Truth table0%0/3
A // B
A= $args{"clipboard_manager"}B= detect_clipboard_manager()
ABresult
1X1
011
000
375 0 ▶ return [412, "Can't detect any known clipboard manager"]
Branch: 1 branch
BranchTrueFalseTotal
unless $clipboard_manager 0 0 0
376 unless $clipboard_manager;
377  
378 0 ▶ if ($clipboard_manager eq 'klipper') {
Branch: 4 branches
BranchTrueFalseTotal
if ($clipboard_manager eq 'klipper') { } 0 0 0
elsif ($clipboard_manager eq 'parcellite') { } 0 0 0
elsif ($clipboard_manager eq 'clipit') { } 0 0 0
elsif ($clipboard_manager eq 'xclip') { } 0 0 0
379 0 require Desktop::KDE::Util;
380 0 my $paths = Desktop::KDE::Util::which_qdbus();
381 0 ▶ die "Can't find qdbus" unless @$paths;
Branch: 1 branch
BranchTrueFalseTotal
unless @$paths 0 0 0
382 0 my @rows;
383 0 my $i = 0;
384 0 my $got_empty;
385 0 while (1) {
386 0 my ($stdout, $stderr);
387 0 system({capture_stdout=>\$stdout, capture_stderr=>\$stderr},
388 $paths->[0], "org.kde.klipper", "/klipper", "getClipboardHistoryItem", $i);
389 0 ▶ my $exit_code = $? < 0 ? $? : $?>>8;
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
390 0 ▶ return [500, "/klipper's getClipboardHistoryItem($i) failed: $exit_code"] if $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code 0 0 0
391 0 chomp $stdout;
392 0 ▶ if ($stdout eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout eq '') { } 0 0 0
393 0 log_trace "Got empty result";
394 0 ▶ if ($got_empty++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_empty++) { } 0 0 0
395 0 pop @rows;
396 0 last;
397 } else {
398 0 push @rows, $stdout;
399 }
400 } else {
401 0 log_trace "Got result '%s'", $stdout;
402 0 $got_empty = 0;
403 0 push @rows, $stdout;
404 }
405 0 $i++;
406 }
407 0 return [200, "OK", \@rows];
408 } elsif ($clipboard_manager eq 'parcellite') {
409 # parcellite -c usually just prints the same result as -p (primary)
410 0 return [501, "Not yet implemented"];
411 } elsif ($clipboard_manager eq 'clipit') {
412 # clipit -c usually just prints the same result as -p (primary)
413 0 return [501, "Not yet implemented"];
414 } elsif ($clipboard_manager eq 'xclip') {
415 0 my ($stdout, $stderr);
416  
417 0 my $sel = _x_selection();
418 0 system({capture_stdout=>\$stdout, capture_stderr=>\$stderr},
419 "xclip", "-o", "-selection", $sel);
420 0 ▶ my $exit_code = $? < 0 ? $? : $?>>8;
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
421 0 ▶ return [500, "xclip -o -selection $sel failed (exit code: $exit_code"] if $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code 0 0 0
422  
423 0 return [200, "OK", [$stdout]];
424 }
425  
426 0 [412, "Cannot list clipboard history (clipboard manager=$clipboard_manager)"];
427 }
428  
429 1 $SPEC{'get_clipboard_history_item'} = {
430 v => 1.1,
431 summary => 'Get a clipboard history item',
432 description => <<'MARKDOWN',
433  
434 MARKDOWN
435 args => {
436 %argspecopt_clipboard_manager,
437 %argspec0_index,
438 },
439 };
440 0 ▶sub get_clipboard_history_item {
Subroutine get_clipboard_history_item: not called CC=13 SCAR=52.0
Pod documented
441 0 my %args = @_;
442 0 my $index = $args{index};
443  
444 0 ▶ my $clipboard_manager = $args{clipboard_manager} // detect_clipboard_manager();
Condition0%0/3
$args{"clipboard_manager"} // detect_clipboard_manager()
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"clipboard_manager"} // detect_clipboard_manager()
$args{"clipboard_manager"} detect_clipboard_manager()
Truth table0%0/3
A // B
A= $args{"clipboard_manager"}B= detect_clipboard_manager()
ABresult
1X1
011
000
445 0 ▶ return [412, "Can't detect any known clipboard manager"]
Branch: 1 branch
BranchTrueFalseTotal
unless $clipboard_manager 0 0 0
446 unless $clipboard_manager;
447  
448 0 ▶ if ($clipboard_manager eq 'klipper') {
Branch: 4 branches
BranchTrueFalseTotal
if ($clipboard_manager eq 'klipper') { } 0 0 0
elsif ($clipboard_manager eq 'parcellite') { } 0 0 0
elsif ($clipboard_manager eq 'clipit') { } 0 0 0
elsif ($clipboard_manager eq 'xclip') { } 0 0 0
449 0 require Desktop::KDE::Util;
450 0 my $paths = Desktop::KDE::Util::which_qdbus();
451 0 ▶ die "Can't find qdbus" unless @$paths;
Branch: 1 branch
BranchTrueFalseTotal
unless @$paths 0 0 0
452 0 my ($stdout, $stderr);
453 0 system({capture_stdout=>\$stdout, capture_stderr=>\$stderr},
454 $paths->[0], "org.kde.klipper", "/klipper", "getClipboardHistoryItem", $index);
455 0 ▶ my $exit_code = $? < 0 ? $? : $?>>8;
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
456 0 ▶ return [500, "/klipper's getClipboardHistoryItem($index) failed: $exit_code"] if $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code 0 0 0
457 0 chomp $stdout;
458 0 return [200, "OK", $stdout];
459 } elsif ($clipboard_manager eq 'parcellite') {
460 # parcellite -c usually just prints the same result as -p (primary)
461 0 return [501, "Not yet implemented"];
462 } elsif ($clipboard_manager eq 'clipit') {
463 # clipit -c usually just prints the same result as -p (primary)
464 0 return [501, "Not yet implemented"];
465 } elsif ($clipboard_manager eq 'xclip') {
466 0 my ($stdout, $stderr, $exit_code);
467  
468 0 ▶ if ($index == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == 0) { } 0 0 0
469 0 my $sel = _x_selection();
470 0 system({capture_stdout=>\$stdout, capture_stderr=>\$stderr},
471 "xclip", "-o", "-selection", $sel);
472 0 ▶ my $exit_code = $? < 0 ? $? : $?>>8;
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
473 0 ▶ return [500, "xclip -o -selection $sel failed (exit code: $exit_code"] if $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code 0 0 0
474 0 return [200, "OK", $stdout];
475 } else {
476 0 return [200, "OK", undef];
477 }
478 }
479  
480 0 [412, "Cannot get clipboard history item (clipboard manager=$clipboard_manager)"];
481 }
482  
483 1 $SPEC{'add_clipboard_content'} = {
484 v => 1.1,
485 summary => 'Add a new content to the clipboard',
486 description => <<'MARKDOWN',
487  
488 For `xclip`: when adding content, the primary selection is set. The clipboard
489 content is unchanged.
490  
491 MARKDOWN
492 args => {
493 %argspecopt_clipboard_manager,
494 content => {
495 schema => 'str*',
496 pos=>0,
497 cmdline_src=>'stdin_or_args',
498 },
499 tee => {
500 summary => 'If set to true, will output content back to STDOUT',
501 schema => 'bool*',
502 cmdline_aliases => {t=>{}},
503 },
504 chomp_newline => {
505 summary => 'Remove trailing newlines before adding item to clipboard',
506 schema => 'bool*',
507 cmdline_aliases => {l=>{}},
508 },
509 },
510 examples => [
511 {
512 summary => 'Munge text (remove duplicate spaces) in clipboard',
513 src_plang => 'bash',
514 src => q{clipget | perl -lpe's/ {2,}/ /g' | [[prog]]},
515 test => 0,
516 'x.doc.show_result' => 0,
517 },
518 ],
519 };
520 0 ▶sub add_clipboard_content {
Subroutine add_clipboard_content: not called CC=15 SCAR=54.8
Pod documented
521 0 my %args = @_;
522  
523 0 ▶ my $clipboard_manager = $args{clipboard_manager} // detect_clipboard_manager();
Condition0%0/3
$args{"clipboard_manager"} // detect_clipboard_manager()
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"clipboard_manager"} // detect_clipboard_manager()
$args{"clipboard_manager"} detect_clipboard_manager()
Truth table0%0/3
A // B
A= $args{"clipboard_manager"}B= detect_clipboard_manager()
ABresult
1X1
011
000
524 0 ▶ return [412, "Can't detect any known clipboard manager"]
Branch: 1 branch
BranchTrueFalseTotal
unless $clipboard_manager 0 0 0
525 unless $clipboard_manager;
526  
527 defined $args{content} or
528 0 ▶ return [400, "Please specify content"];
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"content"} or return [400, "Please specify content"] 0 0 0
529  
530 0 my $content0 = $args{content};
531 0 my $content = $content0;
532 0 ▶ $content =~ s/\R+\z// if $args{chomp_newline};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"chomp_newline"} 0 0 0
533  
534 0 ▶ if ($clipboard_manager eq 'klipper') {
Branch: 4 branches
BranchTrueFalseTotal
if ($clipboard_manager eq 'klipper') { } 0 0 0
elsif ($clipboard_manager eq 'parcellite') { } 0 0 0
elsif ($clipboard_manager eq 'clipit') { } 0 0 0
elsif ($clipboard_manager eq 'xclip') { } 0 0 0
535 0 require Desktop::KDE::Util;
536 0 my $paths = Desktop::KDE::Util::which_qdbus();
537 0 ▶ die "Can't find qdbus" unless @$paths;
Branch: 1 branch
BranchTrueFalseTotal
unless @$paths 0 0 0
538 0 my ($stdout, $stderr);
539 # qdbus likes to emit an empty line
540 0 system({capture_stdout=>\$stdout, capture_stderr=>\$stderr},
541 $paths->[0], "org.kde.klipper", "/klipper", "setClipboardContents", $content);
542 0 ▶ my $exit_code = $? < 0 ? $? : $?>>8;
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
543 0 ▶ return [500, "/klipper's setClipboardContents failed: $exit_code"] if $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code 0 0 0
544 0 ▶ print $content0 if $args{tee};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"tee"} 0 0 0
545 0 return [200, "OK"];
546 } elsif ($clipboard_manager eq 'parcellite') {
547 # parcellite cli copies unknown options and stdin to clipboard history
548 # but not as the current one
549 0 return [501, "Not yet implemented"];
550 } elsif ($clipboard_manager eq 'clipit') {
551 # clipit cli copies unknown options and stdin to clipboard history but
552 # not as the current one
553 0 return [501, "Not yet implemented"];
554 } elsif ($clipboard_manager eq 'xclip') {
555 0 my $fh;
556  
557 0 my $sel = _x_selection();
558 0 ▶ open $fh, "| xclip -i -selection $sel" ## no critic: InputOutput::ProhibitTwoArgOpen
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "| xclip -i -selection $sel" or return [500, "xclip -i -selection $sel failed (1): $!"] 0 0 0
559 or return [500, "xclip -i -selection $sel failed (1): $!"];
560 0 print $fh $content;
561 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return [500, "xclip -i -selection $sel failed (2): $!"] 0 0 0
562 or return [500, "xclip -i -selection $sel failed (2): $!"];
563 0 return [200, "OK"];
564 }
565  
566 0 [412, "Cannot add clipboard content (clipboard manager=$clipboard_manager)"];
567 }
568  
569 1 1;
570 # ABSTRACT: Common interface to clipboard manager functions
571  
572 __END__
↑ Summary