blib/lib/App/Greple/frame.pm

filter: Statementstmt 98.6% 73 / 74 Branchbran 67.8% 19 / 28 Conditioncond 86.0% 43 / 50 MC/DCmcdc 46.1% 12 / 26 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 8 totaltotal 80.6% 162 / 201 CC 35 SCAR 20.1
CC
35
Cov
89%
pane
15.1
rpn
8.3
opt_frame
5.2
CRAP 7.4
↑ Summary
1 package App::Greple::frame;
2  
3 87 our $VERSION = "1.04";
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 App::Greple::frame - Greple frame output module
10  
11 =head1 SYNOPSIS
12  
13 greple -Mframe --style=fold -- ...
14  
15 greple -Mframe --frame ...
16  
17 =head1 VERSION
18  
19 Version 1.04
20  
21 =head1 DESCRIPTION
22  
23 Greple -Mframe module provide a capability to put surrounding frames
24 for each blocks.
25  
26 C<top>, C<middle> and C<bottom> frames are printed for blocks.
27  
28 By default B<--join-blocks> option is enabled to collect consecutive
29 lines into a single block. If you don't like this, override it by
30 B<--no-join-blocks> option.
31  
32 =head1 MODULE OPTIONS
33  
34 Options placed between B<-Mframe> and B<--> belong to the module, so
35 their names do not conflict with the options of B<greple> or of other
36 modules.
37  
38 greple -Mframe --style=cols -- pattern file
39  
40 greple -Mframe --style=fold --width=80 -- pattern file
41  
42 When B<--> is given, everything before it must be a module option, so
43 C<< -Mframe --frame pattern -- file >> is an error. Options of
44 B<greple> and the pattern come after it.
45  
46 =over 7
47  
48 =item B<--style>=I<name>
49  
50 Select the output style. An unknown name is an error.
51  
52 =over 7
53  
54 =item B<fold>
55  
56 =begin html
57  
58 <p><img width="75%" src="https://raw.githubusercontent.com/kaz-utashiro/greple-frame/main/images/terminal-3.png">
59  
60 =end html
61  
62 Set frame and fold long lines with frame-friendly prefix string.
63 Folding width is taken from the terminal. Or you can specify the
64 width with the B<--width> option.
65  
66 =item B<cols>
67  
68 Output results in multi-column format to fit the width of the
69 terminal. The number of columns is automatically calculated from the
70 terminal width.
71  
72 =item B<pages>
73  
74 Output results in multi-column and paginated format.
75  
76 =begin html
77  
78 <p><img width="75%" src="https://raw.githubusercontent.com/kaz-utashiro/greple-frame/main/images/terminal-frame-pages.png">
79  
80 =end html
81  
82 =back
83  
84 =item B<--width>=I<n>
85  
86 Output width. It takes the place of the terminal width, and accepts
87 RPN as described in L</CONFIGURATION>.
88  
89 =back
90  
91 The next options mean the same as the options of the same name of
92 L<App::ansicolumn> and L<App::ansifold>.
93  
94 =over 7
95  
96 =item B<--pane>=I<n>
97  
98 Number of columns of B<cols> and B<pages> styles. By default, as many
99 columns as fit in the output width with text of 80 columns.
100  
101 =item B<--pane-width>=I<n>
102  
103 Width of a column of B<cols> and B<pages> styles, including the border.
104 The number of columns is calculated from it unless B<--pane> is given,
105 and the columns are not widened to the output width.
106  
107 B<--pane> and B<--pane-width> accept RPN, starting with the output
108 width.
109  
110 =item B<--boundary>=C<none>|C<word>|C<space>
111  
112 Where to fold long lines.
113  
114 =item B<--linebreak>=C<none>|C<all>|C<runin>|C<runout>
115  
116 Line-break mode.
117  
118 =item B<--runin>=I<n>, B<--runout>=I<n>, B<--runlen>=I<n>
119  
120 Run-in and run-out width. B<--runlen> sets both, and B<--runin> and
121 B<--runout> take priority over it. B<cols> and B<pages> styles keep the
122 run-in width free in the column, so the text area gets shorter.
123  
124 =back
125  
126 B<cols> and B<pages> styles use C<--boundary=none>,
127 C<--linebreak=all> and C<--runlen=0> by default. B<fold> style gives
128 them to B<ansifold> only when they are specified.
129  
130 =over 7
131  
132 =item B<--config> I<name>=I<value>,...
133  
134 Set parameters of L</CONFIGURATION>.
135  
136 =back
137  
138 =head1 COMMAND OPTIONS
139  
140 These options work as options of the B<greple> command, and do not need
141 B<-->. They are kept for compatibility and may be removed in the
142 future. Use L</MODULE OPTIONS>.
143  
144 =over 7
145  
146 =item B<--frame>
147  
148 =for comment
149 =item B<--frame-fold>
150  
151 Same as B<--style=fold>.
152  
153 =begin comment
154  
155 =item B<--frame-simple>
156  
157 Set frame without folding.
158  
159 =end comment
160  
161 =item B<--frame-cols>
162  
163 Same as B<--style=cols>.
164  
165 =item B<--frame-pages>
166  
167 Same as B<--style=pages>.
168  
169 =item B<--set-frame-width>=I<#>
170  
171 Same as B<--width>. You have to put this option before B<--frame>
172 option.
173  
174 =back
175  
176 =begin comment
177  
178 Put next line in your F<~/.greplerc> to autoload B<App::Greple::frame> module.
179  
180 autoload -Mframe --frame
181  
182 Then you can use B<--frame> option whenever you want.
183  
184 =end comment
185  
186 =head1 CONFIGURATION
187  
188 Module parameters are set with L<Getopt::EX::Config> by the B<config>
189 function. The B<--config> module option sets the same parameters.
190  
191 The B<set> function does the same as B<config>. It is kept only for
192 compatibility and will be removed in the future.
193  
194 =over 7
195  
196 =item B<config>(B<width>=I<n>)
197  
198 Set terminal width to I<n>. Use like this:
199  
200 greple -Mframe::config(width=80) ...
201  
202 greple -Mframe::config=width=80 ...
203  
204 If non-digit character is found in the value part, it is considered as
205 a Reverse Polish Notation, starting terminal width pushed on the
206 stack. RPN C<2/3-> means C<terminal-width / 2 - 3>.
207  
208 You can use like this:
209  
210 greple -Mframe::config=width=2/3- --frame --uc '(\w+::)+\w+' --git | ansicolumn -PC2
211  
212 With B<cols> and B<pages> styles, the whole output has this width, and
213 the width and the number of the columns are calculated from it.
214  
215 An unknown parameter is an error.
216  
217 =begin html
218  
219 <p><img width="75%" src="https://raw.githubusercontent.com/kaz-utashiro/greple-frame/main/images/terminal-column.png">
220  
221 =end html
222  
223 =item B<config>(B<style>=I<name>)
224  
225 =item B<config>(B<pane>=I<n>), B<config>(B<pane_width>=I<n>)
226  
227 =item B<config>(B<boundary>=I<name>), B<config>(B<linebreak>=I<name>)
228  
229 =item B<config>(B<runin>=I<n>), B<config>(B<runout>=I<n>), B<config>(B<runlen>=I<n>)
230  
231 Same as the module options of the same name.
232  
233 =back
234  
235 =head1 SEE ALSO
236  
237 L<App::ansifold>
238  
239 L<App::ansicolumn>
240  
241 L<Math::RPN>
242  
243 =head1 AUTHOR
244  
245 Kazumasa Utashiro
246  
247 =head1 LICENSE
248  
249 Copyright 2022-2026 Kazumasa Utashiro.
250  
251 This library is free software; you can redistribute it and/or modify
252 it under the same terms as Perl itself.
253  
254 =cut
255  
256 87 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
257 87 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
258 87 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
259 87 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
260 87 ▶use Getopt::EX::Config qw(config set);
Subroutine BEGIN: called CC=1 SCAR=0.0
261  
262 87 my($mod, $argv);
263 87 my($head, $blockend, $file_start, $file_end);
264 87 my $frame_width; # of a column, set by pane()
265  
266 # set() is kept only for compatibility and will be removed in the future.
267 # Values may be RPN, which is calculated when the value is used (see param).
268 87 my $config = Getopt::EX::Config->new(
269 style => undef,
270 width => undef,
271 pane => undef,
272 pane_width => undef,
273 boundary => undef,
274 linebreak => undef,
275 runin => undef,
276 runout => undef,
277 runlen => undef,
278 fold => '',
279 );
280  
281 23 ▶sub terminal_width {
Subroutine terminal_width: called CC=3 SCAR=11.4
Pod undocumented
282 87 ▶ use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
283 23 my $default = 80;
284 23 my @size;
285 23 ▶ if (open my $tty, ">", "/dev/tty") {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $tty, ">", "/dev/tty") 0 23 23
286 # Term::ReadKey 2.31 on macOS 10.15 has a bug in argument handling
287 # and the latest version 2.38 fails to install.
288 # This code should work on both versions.
289 0 @size = GetTerminalSize $tty, $tty;
290 }
291 23 ▶ $size[0] or $default;
Branch: 1 branch
BranchTrueFalseTotal
$size[0] or $default 0 23 23
292 }
293  
294 86 ▶sub finalize {
Subroutine finalize: called CC=4 SCAR=13.9
Pod undocumented
295 86 ($mod, $argv) = @_;
296 86 $config->deal_with($argv, "style=s", "width=s", "pane=s", "pane_width=s",
297 "boundary=s", "linebreak=s",
298 "runin=i", "runout=i", "runlen=i");
299 83 ▶ if (my @extra = $config->argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@extra) = $config->argv) 1 82 83
300 1 die "@extra: unexpected argument before --\n";
301 }
302 # The default gets the definition, not the name: an autoload of that
303 # name in the rc file would load the module again, which inserts the
304 # default once more and the frame comes out twice.
305 82 ▶ if (my $style = $config->{style}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $style = $config->{"style"}) 55 27 82
306 55 ▶ my @opt = $mod->getopt("--frame-$style")
Branch: 1 branch
BranchTrueFalseTotal
my(@opt) = $mod->getopt("--frame-$style") or die "${style}: unknown style\n" 54 1 55
307 or die "$style: unknown style\n";
308 54 $mod->setopt(default => [ @opt ]);
309 }
310 }
311  
312 87 my %frame_base = (
313 top => ' ┌─' ,
314 middle => ' ⋮ ├╶' ,
315 bottom => '──────┴─' ,
316 );
317  
318 85 ▶sub opt_frame {
Subroutine opt_frame: called CC=5 SCAR=16.4
Pod undocumented
319 85 my $pos = shift;
320 85 ▶ my $width = $frame_width // param('width') // ($config->{width} = terminal_width);
Condition83%5/6
($frame_width // param("width")) // ($config->{"width"} = terminal_width())
l!l&&r!l&&!r
8140
$frame_width // param("width")
l!l&&r!l&&!r
54274
MC/DC0%0/3
($frame_width // param("width")) // ($config->{"width"} = terminal_width())
$frame_width param("width") ($config->{"width"} = terminal_width())
Truth table75%3/4
A // B // C
A= $frame_widthB= param("width")C= ($config->{"width"} = terminal_width())
ABCresult
1XX1
01X1
0011
0000
321 85 ▶ local $_ = $frame_base{$pos} or die;
Branch: 1 branch
BranchTrueFalseTotal
local $_ = $frame_base{$pos} or die 85 0 85
322 85 ▶ if ((my $rest = $width - length) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $rest = $width - length($_)) > 0) 85 0 85
323 85 $_ .= (substr($_, -1, 1) x $rest);
324 }
325 85 $_;
326 }
327  
328 54 ▶sub whole_width { param('width') // terminal_width }
Subroutine whole_width: called CC=2 SCAR=7.2
Pod undocumented
Condition66%2/3
param("width") // terminal_width()
l!l&&r!l&&!r
41130
MC/DC0%0/2
param("width") // terminal_width()
param("width") terminal_width()
Truth table66%2/3
A // B
A= param("width")B= terminal_width()
ABresult
1X1
011
000
329  
330 my %rpn = (
331 6 ▶ width => { init => sub { terminal_width } },
Subroutine __ANON__: called CC=1 SCAR=0.0
332 87 pane => { init => \&whole_width },
333 pane_width => { init => \&whole_width },
334 );
335 7 ▶sub rpn {
Subroutine rpn: called CC=6 SCAR=21.2
Pod undocumented
336 7 my($k, $v) = @_;
337 require Getopt::EX::RPN
338 7 ▶ and Getopt::EX::RPN->import('rpn_calc');
Branch: 1 branch
BranchTrueFalseTotal
require Getopt::EX::RPN and "Getopt::EX::RPN"->import("rpn_calc") 7 0 7
339 7 ▶ my $init = $rpn{$k}->{init} // die;
Condition50%1/2
$rpn{$k}{"init"} // die()
l!l
70
MC/DC0%0/1
$rpn{$k}{"init"} // die()
$rpn{$k}{"init"}
Truth table50%1/2
A
A= $rpn{$k}{"init"}
Aresult
11
00
340 7 ▶ my @init = ref $init ? $init->() : $init ? $init : ();
Branch: 2 branches
BranchTrueFalseTotal
ref $init ? : 7 0 7
$init ? : 0 0 0
341 7 ▶ int(rpn_calc(@init, $v)) or die "$v: format error\n";
Branch: 1 branch
BranchTrueFalseTotal
int rpn_calc(@init, $v) or die "${v}: format error\n" 7 0 7
342 }
343  
344 # A value can be set at any time before it is used, by the module
345 # function or by an option, so the RPN is calculated here.
346 245 ▶sub param {
Subroutine param: called CC=4 SCAR=13.9
Pod undocumented
347 245 my $k = shift;
348 245 my $v = $config->{$k};
349 245 ▶ if (defined $v and $rpn{$k} and $v =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and $rpn{$k} and $v =~ /\D/u) 7 238 245
Condition100%6/6
defined $v and $rpn{$k} and $v =~ /\D/u
!ll&&!rl&&r
1381007
defined $v and $rpn{$k}
!ll&&!rl&&r
11127107
MC/DC100%3/3
defined $v and $rpn{$k} and $v =~ /\D/u
defined $v $rpn{$k} $v =~ /\D/u
Truth table100%4/4
A and B and C
A= defined $vB= $rpn{$k}C= $v =~ /\D/u
ABCresult
0XX0
10X0
1100
1111
350 7 $v = $config->{$k} = rpn($k, $v);
351 }
352 245 $v;
353 }
354  
355 # Width of the line number field and of the column border.
356 87 my($LINE_FIELD, $FRAME_GAP) = (8, 3);
357  
358 # For the multi-column options. The parameters mean the same as the options
359 # of ansicolumn, except that the width takes the place of the terminal width
360 # for all of them. The environment variables were the only way to give some
361 # values, and they still work as the default. Returns the values in the order
362 # of $<shift> in the definitions of $FOLD and $COLS or $PAGES.
363 53 ▶sub pane {
Subroutine pane: called CC=15 SCAR=27.2
Pod undocumented
364 53 my $whole = whole_width;
365 53 my $span = param('pane_width');
366 53 ▶ my $text = $ENV{GREPLE_FRAME_PAGES_WIDTH} // 80;
Condition100%2/2
$ENV{"GREPLE_FRAME_PAGES_WIDTH"} // 80
l!l
251
MC/DC100%1/1
$ENV{"GREPLE_FRAME_PAGES_WIDTH"} // 80
$ENV{"GREPLE_FRAME_PAGES_WIDTH"}
Truth table100%2/2
A
A= $ENV{"GREPLE_FRAME_PAGES_WIDTH"}
Aresult
11
00
367 53 ▶ my $panes = param('pane')
Condition87%7/8
param('pane') || int $whole / ($span || $text + $LINE_FIELD + $FRAME_GAP) || 1
l!l
1637
param("pane") or int $whole / ($span || $text + $LINE_FIELD + $FRAME_GAP)
l!l&&r!l&&!r
7937
$span || $text + $LINE_FIELD + $FRAME_GAP
l!l&&r!l&&!r
4420
MC/DC100%2/2
param('pane') || int $whole / ($span || $text + $LINE_FIELD + $FRAME_GAP) || 1
param("pane") int $whole / ($span || $text + $LINE_FIELD + $FRAME_GAP)
MC/DC0%0/2
$span || $text + $LINE_FIELD + $FRAME_GAP
$span $text + $LINE_FIELD + $FRAME_GAP
Truth table100%3/3
A or B
A= param("pane")B= int $whole / ($span || $text + $LINE_FIELD + $FRAME_GAP)
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $spanB= $text + $LINE_FIELD + $FRAME_GAP
ABresult
1X1
011
000
368 || int($whole / ($span || $text + $LINE_FIELD + $FRAME_GAP)) || 1;
369 53 ▶ my $run = $config->{runlen} // $ENV{GREPLE_FRAME_PAGES_MARGIN} // 0;
Condition100%5/5
($config->{"runlen"} // $ENV{"GREPLE_FRAME_PAGES_MARGIN"}) // 0
l!l
449
$config->{"runlen"} // $ENV{"GREPLE_FRAME_PAGES_MARGIN"}
l!l&&r!l&&!r
3149
MC/DC100%2/2
($config->{"runlen"} // $ENV{"GREPLE_FRAME_PAGES_MARGIN"}) // 0
$config->{"runlen"} $ENV{"GREPLE_FRAME_PAGES_MARGIN"}
Truth table100%3/3
A // B
A= $config->{"runlen"}B= $ENV{"GREPLE_FRAME_PAGES_MARGIN"}
ABresult
1X1
011
000
370 53 ▶ my $runin = $config->{runin} // $run;
Condition66%2/3
$config->{"runin"} // $run
l!l&&r!l&&!r
4490
MC/DC0%0/2
$config->{"runin"} // $run
$config->{"runin"} $run
Truth table66%2/3
A // B
A= $config->{"runin"}B= $run
ABresult
1X1
011
000
371 53 ▶ my $runout = $config->{runout} // $run;
Condition66%2/3
$config->{"runout"} // $run
l!l&&r!l&&!r
1520
MC/DC0%0/2
$config->{"runout"} // $run
$config->{"runout"} $run
Truth table66%2/3
A // B
A= $config->{"runout"}B= $run
ABresult
1X1
011
000
372 53 ▶ my $bound = $config->{boundary} // $ENV{GREPLE_FRAME_PAGES_BOUNDARY} // 'none';
Condition100%5/5
($config->{"boundary"} // $ENV{"GREPLE_FRAME_PAGES_BOUNDARY"}) // "none"
l!l
350
$config->{"boundary"} // $ENV{"GREPLE_FRAME_PAGES_BOUNDARY"}
l!l&&r!l&&!r
2150
MC/DC100%2/2
($config->{"boundary"} // $ENV{"GREPLE_FRAME_PAGES_BOUNDARY"}) // "none"
$config->{"boundary"} $ENV{"GREPLE_FRAME_PAGES_BOUNDARY"}
Truth table100%3/3
A // B
A= $config->{"boundary"}B= $ENV{"GREPLE_FRAME_PAGES_BOUNDARY"}
ABresult
1X1
011
000
373 53 ▶ my $break = $config->{linebreak} // 'all';
Condition100%2/2
$config->{"linebreak"} // "all"
l!l
152
MC/DC100%1/1
$config->{"linebreak"} // "all"
$config->{"linebreak"}
Truth table100%2/2
A
A= $config->{"linebreak"}
Aresult
11
00
374 53 ▶ my $width = int(($span || $whole / $panes) - $FRAME_GAP - $runin);
Condition66%2/3
$span || $whole / $panes
l!l&&r!l&&!r
5480
MC/DC0%0/2
$span || $whole / $panes
$span $whole / $panes
Truth table66%2/3
A || B
A= $spanB= $whole / $panes
ABresult
1X1
011
000
375 53 ▶ $width > 0 or die "width $whole is too small for $panes pane(s) and run-in $runin\n";
Branch: 1 branch
BranchTrueFalseTotal
$width > 0 or die "width $whole is too small for $panes pane(s) and run-in $runin\n" 52 1 53
376 52 $frame_width = $width;
377 52 ▶ ($width, $bound, $break, $runin, $runout, $whole, $span ? "-S $span" : '', $panes);
Branch: 1 branch
BranchTrueFalseTotal
$span ? : 5 47 52
378 }
379  
380 # For --frame: nothing is given unless specified, so that ~/.ansifoldrc works.
381 27 ▶sub fold_args {
Subroutine fold_args: called CC=3 SCAR=11.0
Pod undocumented
382 27 my @opt;
383 27 for my $name (qw(boundary linebreak runin runout runlen)) {
384 135 ▶ my $val = $config->{$name} // next;
Condition100%2/2
$config->{$name} // (next)
l!l
4131
MC/DC100%1/1
$config->{$name} // (next)
$config->{$name}
Truth table100%2/2
A
A= $config->{$name}
Aresult
11
00
385 4 push @opt, "--$name=$val";
386 }
387 27 (join(' ', @opt, param('fold')), param('width'));
388 }
389  
390 87 1;
391  
392 __DATA__
↑ Summary