blib/lib/App/ClipboardUtils.pm

filter: Statementstmt 34.6% 34 / 98 Branchbran 0.0% 0 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 5 / 7 Podpod 100% 2 / 2 totaltotal 27.1% 41 / 151 CC 24 SCAR 61.7
CC
24
Cov
24%
add_clipboard_content
600.0
tee_clipboard_content
2.0
BEGIN
1.0
CRAP 480.2
↑ Summary
1 package App::ClipboardUtils;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 1 our $DATE = '2026-09-20'; # DATE
9 1 our $DIST = 'App-ClipboardUtils'; # DIST
10 1 our $VERSION = '0.015'; # VERSION
11  
12 1 ▶use Clipboard::Any ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Clone::PP qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our %SPEC;
16  
17 {
18 1 $SPEC{add_clipboard_content} = clone $Clipboard::Any::SPEC{add_clipboard_content};
19  
20 # because we also have --command and do our own while(<>) { ... }
21 1 delete $SPEC{add_clipboard_content}{args}{content}{cmdline_src};
22  
23 $SPEC{add_clipboard_content}{args}{split_by} = {
24 1 summary => 'Split content by specified string/regex, add the split content as multiple clipboard entries',
25 schema => ['str_or_re*'],
26 description => <<'MARKDOWN',
27  
28 Cannot be used together with `--fragments` or `--split-by-org-headlines` or `--command-line` option.
29  
30 Note that if you supply a regex, you should not have any capture groups in the
31 regex.
32  
33 MARKDOWN
34 cmdline_aliases => {s=>{}},
35 };
36  
37 $SPEC{add_clipboard_content}{args}{fragments} = {
38 1 summary => 'Only add text fragments inside content',
39 schema => ['bool*'],
40 description => <<'MARKDOWN',
41  
42 Cannot be used together with `--split-by` or `--command-line` option.
43  
44 Example content:
45  
46 FAQS
47 ====
48  
49 Q: Will the Thingamagic explode under extreme pressure?
50 A: No doubt.
51  
52 # BEGIN clipadd id=2
53 Q: How much pressure should I apply to the Thingamagic initially?
54 A: Not that much, really. It will pressurize itself eventually.
55 # END clipadd id=2
56  
57 # BEGIN clipadd id=1
58 Q: What is the appropriate age to start using the Thingamagic?
59 A: It depends on whether your kid is smart (or stupid) enough.
60 # END clipadd id=1
61  
62 Q: Another question?
63 A: Another half-assed answer.
64  
65 Command:
66  
67 % cat faq.txt | clipadd --fragments
68  
69 Read <pm:Text::Fragment> for more details on text fragments.
70  
71 MARKDOWN
72 #cmdline_aliases => {f=>{}},
73 };
74  
75 $SPEC{add_clipboard_content}{args}{split_by_org_headlines} = {
76 1 summary => 'Only add contents inside Org headlines',
77 schema => ['bool*'],
78 description => <<'MARKDOWN',
79  
80 Cannot be used together with `--split-by` or `--command-line` option.
81  
82 Example content:
83  
84 * headline 1
85 Content 1
86 Second line
87 ** headline 1.1
88 Content 1.1
89 ** headline 1.2
90 Content 1.2
91 * headline 2
92 Content 2
93  
94 Command:
95  
96 % cat document.org | clipadd --split-by-org-headlines
97 % cat document.org | clipadd -o
98  
99 MARKDOWN
100 cmdline_aliases => {o=>{}},
101 };
102  
103 $SPEC{add_clipboard_content}{args}{tee} = {
104 1 summary => 'Pass stdin to stdout',
105 schema => ['true*'],
106 description => <<'MARKDOWN',
107  
108 MARKDOWN
109 cmdline_aliases => {t=>{}},
110 };
111  
112 $SPEC{add_clipboard_content}{args}{command_line} = {
113 1 summary => 'For every line of input in *stdin*, execute a command, feed it the input line, and add the output to clipboard',
114 schema => ['str*'],
115 description => <<'MARKDOWN',
116  
117 Note that when you use this option, the `--content` argument is ignored. Input
118 is taken from stdin. With `--tee`, each output will be printed to stdout. After
119 eof, the utility will return empty result.
120  
121 An example for using this option (<prog:safer> is a utility from <pm:App::safer>):
122  
123 % clipadd -c safer --tee
124 Foo Bar, Co., Ltd.
125 foo-bar-co-ltd
126 BaZZ, Co., Ltd.
127 bazz-co-ltd
128 _
129  
130 MARKDOWN
131 cmdline_aliases => {c=>{}},
132 };
133  
134 1 $SPEC{add_clipboard_content}{args_rels}{"choose_one&"} = [
135 [qw/command_line split_by split_by_org_headlines fragments/],
136 ];
137 }
138  
139 0 ▶sub add_clipboard_content {
Subroutine add_clipboard_content: not called CC=24 SCAR=64.0
Pod documented
140 0 my %args = @_;
141 0 my $split_by = delete $args{split_by};
142 0 my $tee = delete $args{tee};
143 0 my $command_line = $args{command_line};
144  
145 0 ▶ if (my $split_by_org_headlines = delete $args{split_by_org_headlines}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $split_by_org_headlines = delete $args{"split_by_org_headlines"}) 0 0 0
146 0 $split_by = qr/^\*+\s.*\R/m;
147 }
148  
149 0 ▶ if (defined $command_line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $command_line) { } 0 0 0
150  
151 0 require IPC::System::Options;
152  
153 0 ▶ while (defined(my $input_line = <>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $input_line = readline ARGV)) 0 0 0
154 0 my $stdout;
155 0 IPC::System::Options::run({log=>1, die=>1, stdin => $input_line, capture_stdout => \$stdout}, $command_line);
156  
157 0 ▶ if (defined $split_by) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $split_by) { } 0 0 0
158 0 my $content = delete $args{content};
159 0 my @split_parts = split /($split_by)/, $content;
160 0 log_debug "split_by=%s, num_parts=%d", $split_by, scalar(@split_parts);
161 0 log_trace "split_contents=%s", \@split_parts;
162  
163 0 my $i = 0;
164 0 ▶ while (my ($part, $separator) = splice @split_parts, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@split_parts, 0, 2)) 0 0 0
165 0 ▶ if ($tee) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tee) 0 0 0
166 0 print $part;
167 0 ▶ print $separator if defined $separator;
Branch: 1 branch
BranchTrueFalseTotal
if defined $separator 0 0 0
168 }
169  
170 # do not add empty part to clipboard
171 0 ▶ if (length $part) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $part) 0 0 0
172 0 my $res = Clipboard::Any::add_clipboard_content(
173 %args, content => $part,
174 );
175 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
176 }
177 }
178 } else {
179 0 ▶ print $stdout if $tee;
Branch: 1 branch
BranchTrueFalseTotal
if $tee 0 0 0
180 0 my $res = Clipboard::Any::add_clipboard_content(%args, content => $stdout);
181 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
182 }
183 } # while input
184 0 return [200, "OK"];
185  
186 } else {
187  
188 0 my $content = $args{content};
189 0 ▶ $content = do { local $/; scalar <> } unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 0 0
190 0 $args{content} = $content;
191  
192 0 ▶ if (defined $split_by) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $split_by) { } 0 0 0
elsif ($args{'fragments'}) { } 0 0 0
193 0 my @split_parts = split /($split_by)/, $content;
194 0 log_trace "split_by=%s, split_contents=%s", $split_by, \@split_parts;
195  
196 0 my $res = [204, "OK (no content)"];
197 0 my $i = 0;
198 0 ▶ while (my ($part, $separator) = splice @split_parts, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@split_parts, 0, 2)) 0 0 0
199 0 ▶ if ($tee) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tee) 0 0 0
200 0 print $part;
201 0 ▶ print $separator if defined $separator;
Branch: 1 branch
BranchTrueFalseTotal
if defined $separator 0 0 0
202 }
203  
204 # do not add empty part to clipboard
205 0 ▶ if (length $part) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $part) 0 0 0
206 0 $res = Clipboard::Any::add_clipboard_content(
207 %args, content => $part,
208 ); # currently we use the last add_clipboard_content status
209 }
210 }
211 0 $res->[3]{'func.parts'} = @split_parts;
212 0 $res;
213 } elsif ($args{fragments}) {
214 0 require Text::Fragment;
215 0 my $lfres = Text::Fragment::list_fragments(text => $content, label => "clipadd");
216 0 ▶ return [500, "Can't list fragments in content: $lfres->[0] - $lfres->[1]"]
Branch: 1 branch
BranchTrueFalseTotal
unless $lfres->[0] == 200 0 0 0
217 unless $lfres->[0] == 200;
218 0 my @parts = map { $_->{payload} } sort { $a->{id} <=> $b->{id} } @{ $lfres->[2] };
219  
220 0 my $res = [204, "OK (no content)"];
221 0 for my $part (@parts) {
222 0 ▶ if ($tee) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tee) 0 0 0
223 0 print $part;
224 }
225  
226 # do not add empty part to clipboard
227 0 ▶ if (length $part) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $part) 0 0 0
228 0 $res = Clipboard::Any::add_clipboard_content(
229 %args, content => $part,
230 ); # currently we use the last add_clipboard_content status
231 }
232 }
233 0 $res->[3]{'func.parts'} = @parts;
234 0 $res;
235 } else {
236 0 ▶ print $content if $tee;
Branch: 1 branch
BranchTrueFalseTotal
if $tee 0 0 0
237 0 Clipboard::Any::add_clipboard_content(%args);
238 }
239  
240 } # if command_line
241 }
242  
243 1 $SPEC{tee_clipboard_content} = clone $Clipboard::Any::SPEC{add_clipboard_content};
244 1 $SPEC{tee_clipboard_content}{summary} = 'Shortcut for add-clipboard-content --tee';
245 1 $SPEC{tee_clipboard_content}{description} = '';
246 1 delete $SPEC{tee_clipboard_content}{args}{tee};
247 0 ▶sub tee_clipboard_content {
Subroutine tee_clipboard_content: not called CC=1 SCAR=6.9
Pod documented
248 0 add_clipboard_content(@_, tee => 1);
249 }
250  
251 1 1;
252 # ABSTRACT: CLI utilities related to clipboard
253  
254 __END__
↑ Summary