blib/lib/App/optex/util/filter.pm

filter: Statementstmt 69.6% 133 / 191 Branchbran 45.0% 36 / 80 Conditioncond 27.5% 8 / 29 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 66.6% 28 / 42 Podpod 85.7% 12 / 14 totaltotal 58.8% 219 / 372 CC 59 SCAR 26.1
CC
59
Cov
59%
timestamp
56.0
set
20.1
interval
20.0
CRAP 13.6
« blib/lib/App/optex/debug.pm ↑ Summary blib/lib/App/optex/util.pm »
1 package App::optex::util::filter;
2  
3 16 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use open IO => 'utf8', ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Hash::Util qw(lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 16 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 our @EXPORT;
15 16 our @EXPORT_OK;
16  
17 16 my($mod, $argv);
18  
19 15 ▶sub initialize {
Subroutine initialize: called CC=1 SCAR=0.0
Pod undocumented
20 15 ($mod, $argv) = @_;
21 }
22  
23 =head1 NAME
24  
25 util::filter - optex filter utility module
26  
27 =head1 SYNOPSIS
28  
29 B<optex> -Mutil::filter [ --if/--of/--ef I<command> ] I<command>
30  
31 B<optex> -Mutil::filter [ --if/--of/--ef I<&function> ] I<command>
32  
33 B<optex> -Mutil::filter [ --isub/--osub/--esub/--psub I<function> ] I<command>
34  
35 =head1 OPTION
36  
37 =over 4
38  
39 =item B<--if> I<command>, B<--isub> I<function>
40  
41 Set STDIN filter. If the command starts with C<&>, module function
42 is called instead. C<--isub> is a shortcut for C<--if &function>.
43  
44 =item B<--of> I<command>, B<--osub> I<function>
45  
46 Set STDOUT filter.
47  
48 =item B<--ef> I<command>, B<--esub> I<function>
49  
50 Set STDERR filter.
51  
52 =item B<--yf> I<command>, B<--ysub> I<function>
53  
54 Set STDOUT filter and merge STDERR to STDOUT. The name comes from
55 the shape of Y, where two streams merge into one. Equivalent to
56 C<--of 'command' --ef 'E<gt>&1'>.
57  
58 =item B<--pf> I<command>, B<--psub> I<function>
59  
60 Set pre-fork filter. This function is called before executing the
61 target command process, and expected to return text data, that will
62 be poured into target process's STDIN. This allows you to share
63 information between pre-fork and output filter processes.
64  
65 See L<App::optex::xform> for actual use case.
66  
67 Command can be one of the following forms:
68  
69 command external command
70 &function module function
71 >file redirect to file
72 >>file append to file
73 >&1 dup to STDOUT (shell style)
74 >&2 dup to STDERR (shell style)
75 *STDOUT dup to STDOUT (Perl style)
76 *STDERR dup to STDERR (Perl style)
77  
78 To merge STDERR into STDOUT before filtering, use C<--yf> or
79 C<--ef 'E<gt>&1'> after C<--of>:
80  
81 --yf 'cat -n'
82 --of 'cat -n' --ef '>&1'
83  
84 =item B<--set-io-color> IO=I<color>
85  
86 Set color filter to filehandle. You can set color filter for STDERR
87 like this:
88  
89 --set-io-color STDERR=R
90  
91 Use comma to set multiple filehandles at once.
92  
93 --set-io-color STDIN=B,STDERR=R
94  
95 =item B<--io-color>
96  
97 Set default color to STDOUT and STDERR.
98  
99 =back
100  
101 =head1 DESCRIPTION
102  
103 This module is a collection of sample utility functions for command
104 B<optex>.
105  
106 Function can be called with option declaration. Parameters for the
107 function are passed by name and value list: I<name>=I<value>. Value 1
108 is assigned for the name without value.
109  
110 In this example,
111  
112 optex -Mutil::function(debug,message=hello,count=3)
113  
114 option I<debug> has value 1, I<message> has string "hello", and
115 I<count> also has string "3".
116  
117 =head1 FUNCTION
118  
119 =over 4
120  
121 =cut
122  
123 ######################################################################
124 ######################################################################
125 14 ▶sub io_filter (&@) {
Subroutine io_filter: called CC=9 SCAR=24.1
Pod undocumented
126 14 my $sub = shift;
127 14 my %opt = @_;
128 14 local @ARGV;
129 14 ▶ if ($opt{PREFORK}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"PREFORK"}) 0 14 14
130 0 my $stdin = $sub->();
131 0 ▶ $sub = sub { print $stdin };
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 $opt{STDIN} = 1;
133 }
134 14 ▶ my $pid = do {
Condition50%1/2
do { if ($opt{"STDIN"}) { open STDIN, "-|"; } elsif ($opt{"STDOUT"}) { open STDOUT, "|-"; } elsif ($opt{"STDERR"}) { open STDERR, "|-"; } else { croak("Missing option"); } } // die("fork: $!\n")
l!l
140
MC/DC0%0/1
do { if ($opt{"STDIN"}) { open STDIN, "-|"; } elsif ($opt{"STDOUT"}) { open STDOUT, "|-"; } elsif ($opt{"STDERR"}) { open STDERR, "|-"; } else { croak("Missing option"); } } // die("fork: $!\n")
do { if ($opt{"STDIN"}) { open STDIN, "-|"; } elsif ($opt{"STDOUT"}) { open STDOUT, "|-"; } elsif ($opt{"STDERR"}) { open STDERR, "|-"; } else { croak("Missing option"); } }
Truth table50%1/2
A
A= do { if ($opt{"STDIN"}) { open STDIN, "-|"; } elsif ($opt{"STDOUT"}) { open STDOUT, "|-"; } elsif ($opt{"STDERR"}) { open STDERR, "|-"; } else { croak("Missing option"); } }
Aresult
11
00
135 0 ▶ if ($opt{STDIN}) { open STDIN, '-|' }
Branch: 3 branches
BranchTrueFalseTotal
if ($opt{'STDIN'}) { } 0 14 14
elsif ($opt{'STDOUT'}) { } 10 4 14
elsif ($opt{'STDERR'}) { } 4 0 4
136 10 elsif ($opt{STDOUT}) { open STDOUT, '|-' }
137 4 elsif ($opt{STDERR}) { open STDERR, '|-' }
138 0 else { croak "Missing option" }
139 } // die "fork: $!\n";;
140 14 ▶ return $pid if $pid > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pid > 0 7 7 14
141 # child process (filter)
142 7 ▶ if ($opt{STDERR}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"STDERR"}) 2 5 7
143 2 ▶ open STDOUT, '>&', \*STDERR or die "dup: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", \*STDERR or die "dup: $!" 2 0 2
144 }
145 7 $sub->();
146 3 close STDOUT;
147 3 close STDERR;
148 3 exit 0;
149 }
150  
151 16 push @EXPORT_OK, 'io_filter';
152  
153 16 ▶sub set {
Subroutine set: called CC=17 SCAR=30.0
Pod documented
154 16 my %opt = @_;
155 16 our @waitpids;
156 16 for my $io (qw(PREFORK STDIN STDOUT STDERR)) {
157 59 ▶ my $filter = delete $opt{$io} // next;
Condition100%2/2
delete $opt{$io} // (next)
l!l
1841
MC/DC100%1/1
delete $opt{$io} // (next)
delete $opt{$io}
Truth table100%2/2
A
A= delete $opt{$io}
Aresult
11
00
158 18 my $pid;
159 # dup to another filehandle:
160 # *STDOUT, *STDERR : explicit
161 # >&1, >&2 : shell style
162 18 ▶ if ($filter =~ /^(\*STD(OUT|ERR)|>&([12]))$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($filter =~ /^(\*STD(OUT|ERR)|>&([12]))$/) { } 4 14 18
elsif ($filter =~ /^(>>?)(.+)/) { } 4 10 14
elsif ($filter =~ s/^&//) { } 2 8 10
163 4 ▶ my $dest = $2 // ($3 eq '1' ? 'OUT' : 'ERR');
Branch: 1 branch
BranchTrueFalseTotal
$3 eq '1' ? : 3 0 3
Condition100%2/2
$2 // ($3 eq 1 ? "OUT" : "ERR")
l!l
13
MC/DC100%1/1
$2 // ($3 eq 1 ? "OUT" : "ERR")
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
164 4 ▶ my $fh = $dest eq 'OUT' ? \*STDOUT : \*STDERR;
Branch: 1 branch
BranchTrueFalseTotal
$dest eq 'OUT' ? : 4 0 4
165 0 ▶ if ($io eq 'STDOUT') { open STDOUT, '>&', $fh or die "dup: $!" }
Branch: 3 branches
BranchTrueFalseTotal
if ($io eq 'STDOUT') { } 0 4 4
elsif ($io eq 'STDERR') { } 4 0 4
open STDOUT, ">&", $fh or die "dup: $!" 0 0 0
166 4 ▶ elsif ($io eq 'STDERR') { open STDERR, '>&', $fh or die "dup: $!" }
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $fh or die "dup: $!" 4 0 4
167 4 next;
168 }
169 elsif ($filter =~ /^(>>?)(.+)/) {
170 # >file or >>file - redirect to file
171 4 my($mode, $file) = ($1, $2);
172 2 ▶ $pid = io_filter {
Subroutine __ANON__: called CC=3 SCAR=11.1
173 2 ▶ open my $fh, $mode, $file or die "open: $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $mode, $file or die "open: ${file}: $!" 2 0 2
174 2 ▶ print $fh $_ while <>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline ARGV) and do { print $fh $_ } 4 2 6
175 2 close $fh;
176 4 } $io => 1;
177 }
178 elsif ($filter =~ s/^&//) {
179 2 ▶ if ($filter !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filter =~ /::/) 2 0 2
180 2 $filter = join '::', __PACKAGE__, $filter;
181 }
182 16 ▶ use Getopt::EX::Func qw(parse_func);
Subroutine BEGIN: called CC=1 SCAR=0.0
183 2 my $func = parse_func($filter);
184 2 ▶ $pid = io_filter { $func->call() } $io => 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
185 }
186 else {
187 8 ▶ $pid = io_filter { exec $filter or die "exec: $!\n" } $io => 1;
Subroutine __ANON__: called CC=2 SCAR=9.2
Branch: 1 branch
BranchTrueFalseTotal
exec $filter or die "exec: $!\n" 0 0 0
188 }
189 7 ▶ if ($io eq 'STDOUT' or $io eq 'STDERR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($io eq "STDOUT" or $io eq "STDERR") 7 0 7
Condition66%2/3
$io eq "STDOUT" or $io eq "STDERR"
l!l&&r!l&&!r
520
MC/DC0%0/2
$io eq "STDOUT" or $io eq "STDERR"
$io eq "STDOUT" $io eq "STDERR"
Truth table66%2/3
A or B
A= $io eq "STDOUT"B= $io eq "STDERR"
ABresult
1X1
011
000
190 7 push @waitpids, $pid;
191 }
192 }
193 9 ▶ %opt and die "Unknown parameter: " . Dumper \%opt;
Branch: 1 branch
BranchTrueFalseTotal
%opt and die 'Unknown parameter: ' . Dumper(\%opt) 0 9 9
194 9 ();
195 }
196  
197 =item B<set>(I<io>=I<command>)
198  
199 =item B<set>(I<io>=&I<function>)
200  
201 Primitive function to prepare input/output filter. All options are
202 implemented by this function. Takes C<STDIN>, C<STDOUT>, C<STDERR>,
203 C<PREFORK> as an I<io> name. Value can be command, &function, E<gt>file,
204 E<gt>&1, E<gt>&2, *STDOUT, or *STDERR.
205  
206 mode function
207 option --if &set(STDIN=$<shift>)
208 option --isub &set(STDIN=&$<shift>)
209 option --yf &set(STDOUT=$<shift>,STDERR=>&1)
210  
211 =cut
212  
213 ######################################################################
214  
215 0 ▶sub unctrl {
Subroutine unctrl: not called CC=2 SCAR=17.9
Pod documented
216 0 ▶ while (<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 0 0 0
217 0 s/([\000-\010\013-\037\177])/'^' . pack('c', ord($1)|0100)/ge;
218 0 print;
219 }
220 }
221  
222 16 push @EXPORT_OK, 'unctrl';
223  
224 =item B<unctrl>()
225  
226 Visualize control characters.
227  
228 =cut
229  
230 ######################################################################
231  
232 16 my %control = (
233 nul => [ 's', "\000", "\x{2400}" ], # ␀ SYMBOL FOR NULL
234 soh => [ 's', "\001", "\x{2401}" ], # ␁ SYMBOL FOR START OF HEADING
235 stx => [ 's', "\002", "\x{2402}" ], # ␂ SYMBOL FOR START OF TEXT
236 etx => [ 's', "\003", "\x{2403}" ], # ␃ SYMBOL FOR END OF TEXT
237 eot => [ 's', "\004", "\x{2404}" ], # ␄ SYMBOL FOR END OF TRANSMISSION
238 enq => [ 's', "\005", "\x{2405}" ], # ␅ SYMBOL FOR ENQUIRY
239 ack => [ 's', "\006", "\x{2406}" ], # ␆ SYMBOL FOR ACKNOWLEDGE
240 bel => [ 's', "\007", "\x{2407}" ], # ␇ SYMBOL FOR BELL
241 bs => [ 's', "\010", "\x{2408}" ], # ␈ SYMBOL FOR BACKSPACE
242 ht => [ 's', "\011", "\x{2409}" ], # ␉ SYMBOL FOR HORIZONTAL TABULATION
243 nl => [ '', "\012", "\x{240A}" ], # ␊ SYMBOL FOR LINE FEED
244 vt => [ 's', "\013", "\x{240B}" ], # ␋ SYMBOL FOR VERTICAL TABULATION
245 np => [ 's', "\014", "\x{240C}" ], # ␌ SYMBOL FOR FORM FEED
246 cr => [ 's', "\015", "\x{240D}" ], # ␍ SYMBOL FOR CARRIAGE RETURN
247 so => [ 's', "\016", "\x{240E}" ], # ␎ SYMBOL FOR SHIFT OUT
248 si => [ 's', "\017", "\x{240F}" ], # ␏ SYMBOL FOR SHIFT IN
249 dle => [ 's', "\020", "\x{2410}" ], # ␐ SYMBOL FOR DATA LINK ESCAPE
250 dc1 => [ 's', "\021", "\x{2411}" ], # ␑ SYMBOL FOR DEVICE CONTROL ONE
251 dc2 => [ 's', "\022", "\x{2412}" ], # ␒ SYMBOL FOR DEVICE CONTROL TWO
252 dc3 => [ 's', "\023", "\x{2413}" ], # ␓ SYMBOL FOR DEVICE CONTROL THREE
253 dc4 => [ 's', "\024", "\x{2414}" ], # ␔ SYMBOL FOR DEVICE CONTROL FOUR
254 nak => [ 's', "\025", "\x{2415}" ], # ␕ SYMBOL FOR NEGATIVE ACKNOWLEDGE
255 syn => [ 's', "\026", "\x{2416}" ], # ␖ SYMBOL FOR SYNCHRONOUS IDLE
256 etb => [ 's', "\027", "\x{2417}" ], # ␗ SYMBOL FOR END OF TRANSMISSION BLOCK
257 can => [ 's', "\030", "\x{2418}" ], # ␘ SYMBOL FOR CANCEL
258 em => [ 's', "\031", "\x{2419}" ], # ␙ SYMBOL FOR END OF MEDIUM
259 sub => [ 's', "\032", "\x{241A}" ], # ␚ SYMBOL FOR SUBSTITUTE
260 esc => [ '', "\033", "\x{241B}" ], # ␛ SYMBOL FOR ESCAPE
261 fs => [ 's', "\034", "\x{241C}" ], # ␜ SYMBOL FOR FILE SEPARATOR
262 gs => [ 's', "\035", "\x{241D}" ], # ␝ SYMBOL FOR GROUP SEPARATOR
263 rs => [ 's', "\036", "\x{241E}" ], # ␞ SYMBOL FOR RECORD SEPARATOR
264 us => [ 's', "\037", "\x{241F}" ], # ␟ SYMBOL FOR UNIT SEPARATOR
265 sp => [ 's', "\040", "\x{2420}" ], # ␠ SYMBOL FOR SPACE
266 del => [ 's', "\177", "\x{2421}" ], # ␡ SYMBOL FOR DELETE
267 nbsp => [ 's', "\240", "\x{2423}" ], # ␣ OPEN BOX
268 );
269  
270 16 ▶use List::Util qw(pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
271 560 ▶my %symbol = pairmap { $b->[1] => $b->[2] } %control;
Subroutine __ANON__: called CC=1 SCAR=0.0
272 560 ▶my %char = pairmap { $a => $b->[1] } %control;
Subroutine __ANON__: called CC=1 SCAR=0.0
273  
274 16 my $keep_after = qr/[\n]/;
275  
276 16 ▶use Text::ANSI::Tabs qw(ansi_expand);
Subroutine BEGIN: called CC=1 SCAR=0.0
277  
278 1 ▶sub visible {
Subroutine visible: called CC=12 SCAR=27.4
Pod documented
279 1 my %opt = @_;
280 1 ▶ my %flag = pairmap { $a => $b->[0] } %control;
Subroutine __ANON__: called CC=1 SCAR=0.0
281 1 lock_keys %flag;
282 1 ▶ if (defined(my $all = delete $opt{all})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $all = delete $opt{"all"})) 0 1 1
283 0 $flag{$_} = $all for keys %flag;
284 }
285 1 my($s_char, $c_char) = ('', '');
286 1 for my $name (qw(tabstyle tabstop)) {
287 2 ▶ if (exists $opt{$name} and my $value = delete $opt{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{$name} and my $value = delete $opt{$name}) 0 2 2
Condition33%1/3
exists $opt{$name} and my $value = delete $opt{$name}
!ll&&!rl&&r
200
MC/DC0%0/2
exists $opt{$name} and my $value = delete $opt{$name}
exists $opt{$name} my $value = delete $opt{$name}
Truth table33%1/3
A and B
A= exists $opt{$name}B= my $value = delete $opt{$name}
ABresult
0X0
100
111
288 0 Text::ANSI::Tabs->configure($name => $value);
289 }
290 }
291 1 %flag = (%flag, %opt);
292 1 for my $name (keys %flag) {
293 0 ▶ if ($flag{$name} eq 'c') { $c_char .= $char{$name} }
Branch: 2 branches
BranchTrueFalseTotal
if ($flag{$name} eq 'c') { } 0 35 35
elsif ($flag{$name}) { } 33 2 35
294 33 elsif ($flag{$name}) { $s_char .= $char{$name} }
295 }
296 1 ▶ while (<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 2 1 3
297 2 $_ = ansi_expand($_);
298 2 ▶ s{(?=(${keep_after}?))([$s_char]|(?#bug?)(?!))}{$symbol{$2}$1}g
Branch: 1 branch
BranchTrueFalseTotal
if $s_char ne "" 2 0 2
299 if $s_char ne '';
300 2 ▶ s{(?=(${keep_after}?))([$c_char]|(?#bug?)(?!))}{
Branch: 1 branch
BranchTrueFalseTotal
if $c_char ne "" 0 2 2
301 0 '^'.pack('c',ord($2)+64).$1
302 }ge if $c_char ne '';
303 2 print;
304 }
305 }
306  
307 16 push @EXPORT_OK, 'visible';
308  
309 =item B<visible>(I<name>=I<flag>)
310  
311 Make control and space characters visible.
312  
313 By default, ESCAPE and NEWLINE is not touched. Other control
314 characters and space are shown in unicode symbol. Tab character and
315 following space is visualized in unicode mark.
316  
317 When newline character is visualized, it is not deleted and shown with
318 visible representation.
319  
320 =over 7
321  
322 =item I<name>
323  
324 Name is C<tabstop>, C<tabstyle>, C<all>, or one of these:
325  
326 000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel
327 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si
328 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb
329 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us
330 040 sp
331 240 nbsp
332  
333 If the name is C<all>, the value is set for all characters.
334 Default is equivalent to:
335  
336 visible(tabstyle=bar,all=s,esc=0,nl=0)
337  
338 Tab width can be set by C<tabstop>. As for C<tabstyle>, use anything
339 defined in L<Text::ANSI::Fold>.
340  
341 =item I<flag>
342  
343 If the flag is empty or 0, the character is displayed as is. If flag
344 is C<c>, it is shown in C<^c> format. Otherwise shown in unicode
345 symbol.
346  
347 =back
348  
349 =cut
350  
351 ######################################################################
352  
353 0 ▶sub rev_line {
Subroutine rev_line: not called CC=1 SCAR=6.9
Pod documented
354 0 print reverse <STDIN>;
355 }
356  
357 16 push @EXPORT_OK, 'rev_line';
358  
359 =item B<rev_line>()
360  
361 Reverse output.
362  
363 =cut
364  
365 ######################################################################
366  
367 0 ▶sub rev_char {
Subroutine rev_char: not called CC=3 SCAR=24.8
Pod documented
368 0 ▶ while (<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 0 0 0
369 0 ▶ my $lb = s/(\R)\z// ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
s/(\R)\z// ? : 0 0 0
370 0 print reverse(/\X/g), $lb;
371 }
372 }
373  
374 16 push @EXPORT_OK, 'rev_char';
375  
376 =item B<rev_char>()
377  
378 Reverse characters in each line.
379  
380 =cut
381  
382 ######################################################################
383  
384 16 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
385  
386 0 ▶sub shuffle_line {
Subroutine shuffle_line: not called CC=1 SCAR=6.9
Pod documented
387 0 print shuffle <>;
388 }
389  
390 16 push @EXPORT_OK, 'shuffle';
391  
392 =item B<shuffle_line>()
393  
394 Shuffle lines.
395  
396 =cut
397  
398 ######################################################################
399  
400 16 ▶use Time::HiRes qw(usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
401  
402 0 ▶sub interval {
Subroutine interval: not called CC=4 SCAR=30.0
Pod documented
403 0 my %opt = @_;
404 0 ▶ $opt{time} //= 1;
Condition0%0/2
$opt{"time"} //= 1
l!l
00
MC/DC0%0/1
$opt{"time"} //= 1
$opt{"time"}
Truth table0%0/2
A
A= $opt{"time"}
Aresult
11
00
405 0 my $sleep = $opt{time} * 1000000;
406 0 ▶ while (<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 0 0 0
407 0 print;
408 0 ▶ usleep $sleep if $sleep > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sleep > 0 0 0 0
409 }
410 }
411  
412 16 push @EXPORT_OK, 'interval';
413  
414 =item B<interval>()
415  
416 Output each line at regular intervals of time.
417  
418 =cut
419  
420 ######################################################################
421  
422 16 ▶use Getopt::EX::Colormap qw(colorize);
Subroutine BEGIN: called CC=1 SCAR=0.0
423  
424 0 ▶sub io_color {
Subroutine io_color: not called CC=3 SCAR=24.8
Pod documented
425 0 my %opt = @_;
426 0 for my $io (qw(STDIN STDOUT STDERR)) {
427 0 ▶ my $color = $opt{$io} // next;
Condition0%0/2
$opt{$io} // (next)
l!l
00
MC/DC0%0/1
$opt{$io} // (next)
$opt{$io}
Truth table0%0/2
A
A= $opt{$io}
Aresult
11
00
428 0 ▶ io_filter {
Subroutine __ANON__: not called CC=2 SCAR=17.9
429 0 ▶ while (<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 0 0 0
430 0 print colorize($color, $_);
431 }
432 0 } $io => 1;
433 }
434 0 ();
435 }
436  
437 16 push @EXPORT_OK, 'io_color';
438  
439 =item B<io_color>( B<IO>=I<color> )
440  
441 Colorize text. B<IO> is either of C<STDOUT> or C<STDERR>. Use comma
442 to set both at a same time: C<STDOUT=C,STDERR=R>.
443  
444 =cut
445  
446 ######################################################################
447  
448 0 ▶sub splice_line {
Subroutine splice_line: not called CC=4 SCAR=30.0
Pod documented
449 0 my %opt = @_;
450 0 my @line = <>;
451 0 ▶ if (my $length = $opt{length}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $length = $opt{'length'}) { } 0 0 0
452 0 ▶ print splice @line, $opt{offset} // 0, $opt{length};
Condition0%0/2
$opt{"offset"} // 0
l!l
00
MC/DC0%0/1
$opt{"offset"} // 0
$opt{"offset"}
Truth table0%0/2
A
A= $opt{"offset"}
Aresult
11
00
453 } else {
454 0 ▶ print splice @line, $opt{offset} // 0;
Condition0%0/2
$opt{"offset"} // 0
l!l
00
MC/DC0%0/1
$opt{"offset"} // 0
$opt{"offset"}
Truth table0%0/2
A
A= $opt{"offset"}
Aresult
11
00
455 }
456 }
457  
458 =item B<splice_line>( offset=I<n>, [ length=I<m> ] )
459  
460 Splice lines.
461  
462 =cut
463  
464 ######################################################################
465  
466 16 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
467 16 ▶use Getopt::EX::Colormap qw(colorize);
Subroutine BEGIN: called CC=1 SCAR=0.0
468  
469 0 ▶sub timestamp {
Subroutine timestamp: not called CC=7 SCAR=40.3
Pod documented
470 0 my %opt = @_;
471 0 ▶ my $format = $opt{format} || "%T.%f";
Condition0%0/2
$opt{'format'} || '%T.%f'
l!l
00
MC/DC0%0/1
$opt{'format'} || '%T.%f'
$opt{'format'}
Truth table0%0/2
A
A= $opt{'format'}
Aresult
11
00
472 0 ▶ my $color = $opt{color} || 'Y';
Condition0%0/2
$opt{'color'} || 'Y'
l!l
00
MC/DC0%0/1
$opt{'color'} || 'Y'
$opt{'color'}
Truth table0%0/2
A
A= $opt{'color'}
Aresult
11
00
473  
474 0 my $sub = do {
475 0 my $re_subsec = qr/%f|(?<milli>%L)|%(?<prec>\d*)N/;
476 0 ▶ if ($format =~ /$re_subsec/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /$re_subsec/) { } 0 0 0
477 0 require Time::HiRes;
478 0 ▶ my $prec = $+{milli} ? 3 : $+{prec} || 6;
Branch: 1 branch
BranchTrueFalseTotal
$+{'milli'} ? : 0 0 0
Condition0%0/2
$+{"prec"} or 6
l!l
00
MC/DC0%0/1
$+{"prec"} or 6
$+{"prec"}
Truth table0%0/2
A
A= $+{"prec"}
Aresult
11
00
479 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
480 0 my($sec, $usec) = Time::HiRes::gettimeofday();
481 0 ▶ $usec /= (10 ** (6 - $prec)) if 0 < $prec and $prec < 6;
Branch: 1 branch
BranchTrueFalseTotal
if 0 < $prec and $prec < 6 0 0 0
Condition0%0/3
0 < $prec and $prec < 6
!ll&&!rl&&r
000
MC/DC0%0/2
0 < $prec and $prec < 6
0 < $prec $prec < 6
Truth table0%0/3
A and B
A= 0 < $precB= $prec < 6
ABresult
0X0
100
111
482 0 (my $time = $format)
483 0 =~ s/$re_subsec/sprintf("%0${prec}d", $usec)/ge;
484 0 localtime($sec)->strftime($time);
485 }
486 0 } else {
487 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
488 0 localtime(time)->strftime($format);
489 }
490 0 }
491 };
492  
493 0 ▶ while (<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 0 0 0
494 0 print colorize($color, $sub->()), " ", $_;
495 }
496 }
497  
498 16 push @EXPORT_OK, 'timestamp';
499  
500 =item B<timestamp>( [ format=I<strftime_format> ] )
501  
502 Put timestamp on each line of output.
503  
504 Format is interpreted by C<strftime> function. Default format is
505 C<"%T.%f"> where C<%T> is 24h style time C<%H:%M:%S>, and C<%f> is
506 microsecond. C<%L> means millisecond. C<%nN> can be used to specify
507 precision.
508  
509 =cut
510  
511 ######################################################################
512  
513 0 ▶sub gunzip { exec "gunzip -c" }
Subroutine gunzip: not called CC=1 SCAR=6.9
Pod documented
514  
515 0 ▶sub gzip { exec "gzip -c" }
Subroutine gzip: not called CC=1 SCAR=6.9
Pod documented
516  
517 16 push @EXPORT_OK, 'gunzip', 'gzip';
518  
519 =item B<gunzip>()
520  
521 Gunzip standard input.
522  
523 =item B<gzip>()
524  
525 Gzip standard input.
526  
527 =cut
528  
529 ######################################################################
530 ######################################################################
531  
532 =back
533  
534 =head1 EXAMPLE
535  
536 Next command print C<ping> command output with timestamp.
537  
538 optex -Mutil::filter --osub timestamp ping -c 10 localhost
539  
540 Put next line in your F<~/.optex.d/optex.rc>. Then for any command
541 executed by optex, standard error output will be shown in visible and
542 colored. This is convenient or debug.
543  
544 option default -Mutil::filter --io-color --esub visible
545  
546 Above setting is not effective for command executed through symbolic
547 link. You can set F<~/.optex.d/default.rc>, but it sometime calls
548 unexpected behavior. This is a future issue.
549  
550 =head1 SEE ALSO
551  
552 L<App::optex::xform>
553  
554 L<https://qiita.com/kaz-utashiro/items/2df8c7fbd2fcb880cee6>
555  
556 =cut
557  
558 16 1;
559  
560 __DATA__
« blib/lib/App/optex/debug.pm ↑ Summary blib/lib/App/optex/util.pm »