blib/lib/App/FzfUtils.pm

filter: Statementstmt 20.1% 22 / 109 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 57.1% 4 / 7 Podpod 100% 3 / 3 totaltotal 20.2% 29 / 143 CC 15 SCAR 41.0
CC
15
Cov
17%
cs_select
110.0
fzf2clip
30.0
fzf2clip_loop
6.0
CRAP 60.3
↑ Summary
1 package App::FzfUtils;
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 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9 1 our $DATE = '2026-08-27'; # DATE
10 1 our $DIST = 'App-FzfUtils'; # DIST
11 1 our $VERSION = '0.004'; # VERSION
12  
13 1 our %SPEC;
14  
15 1 our %argspecsopt_fzf = (
16 no_hscroll => {
17 summary => 'Will pass --no-hscroll to fzf',
18 schema => 'true*',
19 },
20 );
21  
22 1 $SPEC{':package'} = {
23 v => 1.1,
24 summary => 'Utilities related to fzf',
25 };
26  
27 $SPEC{fzf2clip} = {
28 1 v => 1.1,
29 summary => "Select using fzf, then add selection to clipboard",
30 description => <<'MARKDOWN',
31  
32 This is basically a shortcut for:
33  
34 % fzf < input.txt | clipadd
35  
36 where <prog:clipadd> is a utility from <pm:App::ClipboardUtils>.
37  
38 MARKDOWN
39 args => {
40 input => {
41 schema => 'str*',
42 cmdline_src => 'stdin_or_files',
43 pos => 0,
44 },
45 tee => {
46 summary => 'In addition to adding to clipboard, also print to STDOUT',
47 schema => 'bool*',
48 cmdline_aliases => {t=>{}},
49 },
50 },
51 deps => {
52 all => [
53 {prog => 'fzf'},
54 #{prog => 'clipadd'},
55 ],
56 },
57 };
58 0 ▶sub fzf2clip {
Subroutine fzf2clip: not called CC=5 SCAR=34.0
Pod documented
59 0 require IPC::Open2;
60 0 require Clipboard::Any;
61  
62 0 my %args = @_;
63  
64 0 my $input = $args{input};
65  
66 0 my $pid = IPC::Open2::open2(my $out, my $in, "fzf");
67 0 for my $line (split /^/m, $input) {
68 0 print $in $line;
69 }
70 0 close $in;
71 0 my $result;
72 {
73 0 local $/;
74 0 $result = <$out>;
75 0 ▶ chomp($result) if defined $result;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 0 0 0
76 }
77 0 close $out;
78 0 waitpid($pid, 0);
79  
80 ADD_CLIPBOARD: {
81 0 my $res = Clipboard::Any::add_clipboard_content(content => $result);
82 0 ▶ return [500, "Can't add to clipboard: $res->[0] - $res->[1]"]
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
83 unless $res->[0] == 200;
84 }
85  
86 0 ▶ [200, "OK", $args{tee} ? $result : ""];
Branch: 1 branch
BranchTrueFalseTotal
$args{'tee'} ? : 0 0 0
87 }
88  
89 $SPEC{fzf2clip_loop} = {
90 1 v => 1.1,
91 summary => "Like fzf2clip, but loop/repeat",
92 description => <<'MARKDOWN',
93  
94 This is basically a shortcut for:
95  
96 % fzf --bind 'enter:execute(clipadd {})' < input.txt
97  
98 where <prog:clipadd> is a utility from <pm:App::ClipboardUtils>.
99  
100 MARKDOWN
101 args => {
102 input => {
103 schema => 'str*',
104 cmdline_src => 'stdin_or_files',
105 pos => 0,
106 },
107 },
108 deps => {
109 all => [
110 {prog => 'fzf'},
111 {prog => 'clipadd'},
112 ],
113 },
114 };
115 0 ▶sub fzf2clip_loop {
Subroutine fzf2clip_loop: not called CC=2 SCAR=17.9
Pod documented
116 0 require IPC::Open2;
117 0 require Clipboard::Any;
118  
119 0 my %args = @_;
120  
121 0 my $input = $args{input};
122  
123 0 my $pid = IPC::Open2::open2(my $out, my $in, q[fzf --bind 'enter:execute-silent(clipadd {})']);
124 0 for my $line (split /^/m, $input) {
125 0 print $in $line;
126 }
127 0 close $in;
128 0 close $out;
129 0 waitpid($pid, 0);
130  
131 0 [200, "OK"];
132 }
133  
134 $SPEC{cs_select} = {
135 1 v => 1.1,
136 summary => "Select entries from template to clipboard",
137 description => <<'MARKDOWN',
138  
139 This is basically similar to:
140  
141 % fzf --bind 'enter:execute(clipadd {})' < template.txt
142  
143 except that it does some pre-processing to let each template entry be a
144 multiple-line text, and later do post-processing so the original entry is added
145 to the clipboard.
146  
147 **Template format**
148  
149 Template is an Org file with a particular format, where each entry is put under
150 a level-2 heading. The level-1 heading can be used for grouping/categorizing.
151 For example:
152  
153 * Product > P1
154 ** Does P1 need to be replaced every 5 years? :replacement:clipadd_1:
155 Yes, ideally every 3-5 years.
156 ** How to maintain P1 so it is in a good condition and can last longer?
157 Wash after every use, then dry.
158 Keep it clean.
159 And of course replace after 3-5 years.
160 * Product > P2
161 ** Entry 1 :clipadd_2:
162 ...
163 ** Entry 2
164 ...
165 * General > Support
166 ** Entry 3
167 ...
168 ** Entry 4
169 ...
170  
171 Before feeding to `fzf`, this utility will convert each entry into a single
172 line:
173  
174 [id=1][title=Does P1 need to be replaced every 5 years?]Yes, ideally every 3-5 years.[category=Product > P1][tag=replacement][tag=clipadd_1]
175 [id=2][title=How to maintain P1 so it is in a good condition and can last longer?]Wash after every use, then dry. Keep it clean. And of course replace after 3-5 years.[category=Product > P1]
176 [id=3][title=Entry 1]...[tag=clipadd_2][category=Product > P2]
177 [id=4][title=Entry 2]...[category=Product > P2]
178 [id=5][title=Entry 3]...[category=General > Support]
179 [id=6][title=Entry 4]...[category=General > Support]
180  
181 after selection, another script (<prog:cs-select-helper>) will turn back the
182 single-line entry into the original.
183  
184 **Tags**
185  
186 Some Org tags are used to customize how the entry is displayed or copied to
187 clipboard.
188  
189 **no_copy_title**. Do not copy the title to clipboard, only the content.
190  
191 **exclude**. Do not include this entry (or any child entries).
192  
193 MARKDOWN
194 args => {
195 template => {
196 schema => 'str*',
197 cmdline_src => 'stdin_or_files',
198 pos => 0,
199 },
200 wrap => {
201 schema => 'posint*',
202 cmdline_aliases => {w=>{}},
203 },
204 %argspecsopt_fzf,
205 },
206 deps => {
207 all => [
208 {prog => 'fzf'},
209 {prog => 'clipadd'},
210 ],
211 },
212 };
213 0 ▶sub cs_select {
Subroutine cs_select: not called CC=10 SCAR=47.0
Pod documented
214 0 require Clipboard::Any;
215 0 require File::Temp;
216 0 require IPC::Open2;
217 0 require JSON::PP;
218 0 require Org::Parser::Tiny;
219 0 require Text::ANSI::Util;
220  
221 0 my %args = @_;
222  
223 0 my $template = $args{template};
224 0 my $doc = Org::Parser::Tiny->new->parse($template);
225  
226 0 my @lines;
227 my $jsonfile;
228 PREPROCESS_TEMPLATE: {
229 0 my $dict = {};
230 0 my $id = 0;
231 0 ▶ for my $h1 (grep { $_->isa("Org::Parser::Tiny::Node::Headline") && $_->level == 1 } @{ $doc->children }) {
Condition0%0/3
$_->isa('Org::Parser::Tiny::Node::Headline') && $_->level == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$_->isa('Org::Parser::Tiny::Node::Headline') && $_->level == 1
$_->isa('Org::Parser::Tiny::Node::Headline') $_->level == 1
Truth table0%0/3
A && B
A= $_->isa('Org::Parser::Tiny::Node::Headline')B= $_->level == 1
ABresult
0X0
100
111
232 0 my $category = $h1->title;
233 0 my @h1_tags = @{ $h1->tags };
234 0 ▶ for my $h2 (grep { $_->isa("Org::Parser::Tiny::Node::Headline") && $_->level == 2 } @{ $h1->children }) {
Condition0%0/3
$_->isa('Org::Parser::Tiny::Node::Headline') && $_->level == 2
!ll&&!rl&&r
000
MC/DC0%0/2
$_->isa('Org::Parser::Tiny::Node::Headline') && $_->level == 2
$_->isa('Org::Parser::Tiny::Node::Headline') $_->level == 2
Truth table0%0/3
A && B
A= $_->isa('Org::Parser::Tiny::Node::Headline')B= $_->level == 2
ABresult
0X0
100
111
235 0 $id++;
236 0 my $title = $h2->title;
237 0 my $content = $h2->as_string;
238 0 my @h2_tags = @{ $h2->tags };
239  
240 0 my $exclude = (grep {$_ eq 'exclude'} @h1_tags, @h2_tags);
241 0 ▶ next if $exclude;
Branch: 1 branch
BranchTrueFalseTotal
if $exclude 0 0 0
242  
243 0 $content =~ s/\A.+\R//; # dump the raw heading
244  
245 0 my $no_copy_title = (grep {$_ eq 'no_copy_title'} @h1_tags, @h2_tags);
246  
247 0 my $clip_content;
248 0 ▶ my $clip_title = ($no_copy_title ? "" : $title . ":\n");
Branch: 1 branch
BranchTrueFalseTotal
$no_copy_title ? : 0 0 0
249 0 ▶ if ($args{wrap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'wrap'}) { } 0 0 0
250 0 $clip_content = $clip_title . Text::ANSI::Util::ta_wrap($content, $args{wrap});
251 } else {
252 0 $clip_content = $clip_title . $content;
253 }
254  
255 0 (my $line_content = $content) =~ s/\R+/ /g;
256 0 my $line = join(
257 "",
258 "[id=$id][title=", $h2->title, "]",
259 $line_content,
260 "[category=$category]",
261 );
262 0 push @lines, "$line\n";
263 0 $dict->{$id} = $clip_content;
264 }
265 }
266  
267 0 (my $tempfh, $jsonfile) = File::Temp::tempfile('XXXXXXXX', TMPDIR=>1, SUFFIX=>'.json');
268 0 log_trace "JSON file is at %s", $jsonfile;
269 0 print $tempfh JSON::PP::encode_json($dict);
270 0 close $tempfh;
271 } # PREPROCESS_TEMPLATE
272  
273 0 my @fzf_opts;
274 0 ▶ if ($args{no_hscroll}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"no_hscroll"}) 0 0 0
275 0 push @fzf_opts, "--no-hscroll";
276 }
277 0 my $fzf_opts = join(" ", @fzf_opts);
278  
279 0 my $pid = IPC::Open2::open2(my $out, my $in, qq[fzf $fzf_opts --bind 'enter:execute-silent(cs-select-helper $jsonfile {})']);
280 0 for my $line (@lines) {
281 0 print $in $line;
282 }
283 0 close $in;
284 0 close $out;
285 0 waitpid($pid, 0);
286  
287 0 [200, "OK"];
288 }
289  
290 1 1;
291 # ABSTRACT: Utilities related to fzf
292  
293 __END__
↑ Summary