File Coverage

blib/lib/App/Greple/stripe.pm
Criterion Covered Total %
statement 40 47 85.1
branch 3 4 75.0
condition n/a
subroutine 9 10 90.0
pod 0 2 0.0
total 52 63 82.5


line stmt bran cond sub pod time code
1             package App::Greple::stripe;
2              
3 4     4   270969 use 5.024;
  4         16  
4 4     4   27 use warnings;
  4         8  
  4         465  
5              
6             our $VERSION = "1.02";
7              
8             =encoding utf-8
9              
10             =head1 NAME
11              
12             App::Greple::stripe - Greple zebra stripe module
13              
14             =head1 SYNOPSIS
15              
16             greple -Mstripe [ module options -- ] ...
17              
18             =head1 VERSION
19              
20             Version 1.02
21              
22             =head1 DESCRIPTION
23              
24             L is a module for L to show
25             matched text in zebra striping fashion.
26              
27             The following command matches two consecutive lines.
28              
29             greple -E '(.+\n){1,2}' --face +E
30              
31             =for html

32            
33            

34              
35             However, each matched block is colored by the same color, so it is not
36             clear where the block breaks. One way is to explicitly display the
37             blocks using the C<--blockend> option.
38              
39             greple -E '(.+\n){1,2}' --face +E --blockend=--
40              
41             =for html

42            
43            

44              
45             Using the stripe module, blocks matching the same pattern are colored
46             with different colors of the similar color series.
47              
48             greple -Mstripe -E '(.+\n){1,2}' --face +E
49              
50             =for html

51            
52            

53              
54             By default, two color series are prepared. Thus, when multiple
55             patterns are searched, an even-numbered pattern and an odd-numbered
56             pattern are assigned different color series.
57              
58             greple -Mstripe -E '.*[02468]$' -E '.*[13579]$' --need=1
59              
60             =for html

61            
62            

63              
64             When multiple patterns are specified as in the above example, only
65             lines matching all patterns will be output. So the C<--need=1> option
66             is required to relax this condition.
67              
68             If you want to use different color series for three or more patterns,
69             specify C count when calling the module. The number of series
70             can be increased up to 6.
71              
72             greple -Mstripe::config=step=3 --need=1 -E p1 -E p2 -E p3 ...
73              
74             =for html

75            
76            

77              
78             =head1 MODULE OPTIONS
79              
80             There are options specific to the B module. They can be
81             specified either at the time of module declaration or as options
82             following the module declaration and ending with C<-->.
83              
84             The following three commands have exactly the same effect.
85              
86             greple -Mstripe::config=step=3
87              
88             greple -Mstripe --config step=3 --
89              
90             greple -Mstripe --step=3 --
91              
92             Note that, C function can be used instead of C for
93             backward compatibility, at this point.
94              
95             =over 7
96              
97             =item B<-Mstripe::config>=B=I
98              
99             =item B<--step>=I
100              
101             Set the step count to I.
102              
103             =item B<-Mstripe::config>=B
104              
105             =item B<--darkmode>
106              
107             Use dark background colors.
108              
109             =for html

110            
111            

112              
113             Use C<--face> option to set foreground color for all colormap. The
114             following command sets the foreground color to white and fills the
115             entire line with the background color.
116              
117             greple -Mstripe --darkmode -- --face +WE
118              
119             =for html

120            
121            

122              
123             =back
124              
125             =head1 SEE ALSO
126              
127             L
128              
129             L
130              
131             L
132              
133             =head1 AUTHOR
134              
135             Kazumasa Utashiro
136              
137             =head1 LICENSE
138              
139             Copyright ©︎ 2024-2025 Kazumasa Utashiro.
140              
141             This library is free software; you can redistribute it and/or modify
142             it under the same terms as Perl itself.
143              
144             =cut
145              
146 4     4   114 use List::Util qw(max pairmap first);
  4         54  
  4         682  
147 4     4   661 use Hash::Util qw(lock_keys);
  4         3147  
  4         58  
148 4     4   295 use Scalar::Util;
  4         8  
  4         262  
149             *is_number = \&Scalar::Util::looks_like_number;
150 4     4   570 use Data::Dumper;
  4         7466  
  4         296  
151              
152 4     4   2395 use Getopt::EX::Config qw(config set);
  4         21102  
  4         34  
153             my $config = Getopt::EX::Config->new(
154             step => 2,
155             darkmode => undef,
156             );
157             lock_keys %{$config};
158              
159             my %series = (
160             light => [
161             [ qw(/544 /533) ],
162             [ qw(/454 /353) ],
163             [ qw(/445 /335) ],
164             [ qw(/455 /355) ],
165             [ qw(/545 /535) ],
166             [ qw(/554 /553) ],
167             ],
168             dark => [
169             [ qw(/200 /100) ],
170             [ qw(/020 /010) ],
171             [ qw(/004 /003) ],
172             [ qw(/022 /011) ],
173             [ qw(/202 /101) ],
174             [ qw(/220 /110) ],
175             ],
176             );
177              
178             sub finalize {
179 3     3 0 2238 our($mod, $argv) = @_;
180             Getopt::EX::Config->deal_with(
181             $argv,
182 3         9 map("$_:1", keys %{$config}),
  3         31  
183             );
184 3         904 my @default = qw(--stripe-postgrep);
185 3         8 my @cm = qw(@);
186 3 100       10 my $map = config('darkmode') ? $series{dark} : $series{light};
187 3         55 for my $i (0, 1) {
188 6         33 for my $s (0 .. config('step') - 1) {
189 12         107 push @cm, $map->[$s % @$map]->[$i];
190             }
191             }
192 3         8 local $" = ',';
193 3         24 $mod->setopt(default => join(' ', @default, "--cm=@cm"));
194             }
195              
196             #
197             # Increment each index by $step
198             #
199             sub stripe {
200 3     3 0 53 my $grep = shift;
201 3         14 my $step = config('step');
202 3 50       87 if ($step == 0) {
203 0         0 $step = _max_index($grep) + 1;
204             }
205 3         144 my @counter = (-$step .. -1);
206 3         66 for my $r ($grep->result) {
207 30         77 my($b, @match) = @$r;
208 30         46 for my $m (@match) {
209 30         44 my $mod = $m->[2] % $step;
210 30         67 $m->[2] = ($counter[$mod] += $step);
211             }
212             }
213             }
214              
215             sub _max_index {
216 0     0     my $grep = shift;
217 0           my $max = 0;
218 0           for my $r ($grep->result) {
219 0           my($b, @match) = @$r;
220 0           $max = max($max, map($_->[2], @match));
221             }
222 0           return $max;
223             }
224              
225             1;
226              
227             __DATA__