blib/lib/App/watchdiff.pm

filter: Statementstmt 86.6% 149 / 172 Branchbran 46.1% 24 / 52 Conditioncond 39.1% 9 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 85.2% 29 / 34 Podpod 0.0% 0 / 7 totaltotal 69.8% 211 / 302 CC 37 SCAR 23.8
CC
37
Cov
74%
do_loop
34.8
execute
9.0
run
7.8
CRAP 10.8
« blib/lib/App/sdif/Util.pm ↑ Summary blib/lib/App/sdif.pm »
1 package App::watchdiff;
2  
3 ##
4 ## watchdiff: watch difference
5 ##
6 ## Copyright 2014- Kazumasa Utashiro
7 ##
8 ## Original version on Feb 15 2014
9 ##
10  
11 9 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use open ":std" => ":encoding(utf8)";
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 ▶use List::Util qw(pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 9 ▶use App::sdif;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 my $version = $App::sdif::VERSION;
23  
24 9 my $app;
25  
26 9 ▶use Getopt::EX::Hashed 'has'; {
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 9 Getopt::EX::Hashed->configure(DEFAULT => [ is => 'rw' ]);
29  
30 9 has help => ' h ' ;
31 9 has version => ' ' ;
32 9 has debug => ' d ' ;
33 9 has unit => ' by :s ' ;
34 9 has diff => ' =s ' ;
35 9 has exec => ' e =s@ ' , default => [] ;
36 9 has refresh => ' r :1 ' , default => 1 ;
37 9 has interval => ' i =i ' , default => 2 ;
38 9 has count => ' c =i ' , default => 1000 ;
39 9 has clear => ' ! ' , default => 1 ;
40 9 has silent => ' s ! ' , default => 0 ;
41 9 has mark => ' M ! ' , default => 0 ;
42 9 has verbose => ' V ! ' , default => undef ;
43 9 has old => ' O ! ' , default => 0 ;
44 9 has date => ' D ! ' , default => 1 ;
45 9 has newline => ' N ! ' , default => 1 ;
46 9 has context => ' C :2 ' , default => 999, alias => 'U';
47 9 has scroll => ' S ' , default => 1 ;
48 9 has colormap => ' cm =s@ ' , default => [] ;
49 has plain => ' p ' ,
50 8 ▶ action => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
51 8 $_->date = $_->newline = 0;
52 9 };
53  
54 0 ▶ has '+help' => action => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 pod2usage
56 -verbose => 99,
57 -sections => [ qw(SYNOPSIS VERSION) ];
58 9 };
59  
60 0 ▶ has '+version' => action => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 print "$version\n";
62 0 exit;
63 9 };
64  
65 9 ▶} no Getopt::EX::Hashed;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 9 my %colormap = qw(
68 APPEND K/544
69 DELETE K/544
70 OCHANGE K/445
71 NCHANGE K/445
72 OTEXT K/455E
73 NTEXT K/554E
74 );
75  
76 9 ▶use Term::ANSIColor::Concise qw(ansi_code csi_code);
Subroutine BEGIN: called CC=1 SCAR=0.0
77 54 ▶my %termcap = pairmap { $a => ansi_code($b) }
Subroutine __ANON__: called CC=1 SCAR=0.0
78 9 qw(
79 home {CUP}
80 clear {CUP}{ED2}
81 el {EL}
82 ed {ED}
83 decsc {DECSC}
84 decrc {DECRC}
85 );
86  
87 8 ▶sub run {
Subroutine run: called CC=7 SCAR=20.5
Pod undocumented
88 8 $app = my $opt = shift;
89 8 local @ARGV = @_;
90  
91 9 ▶ use Getopt::EX::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 8 Getopt::Long::Configure(qw(bundling require_order));
93 8 ▶ $opt->getopt or usage({status => 1});
Branch: 1 branch
BranchTrueFalseTotal
$opt->getopt or usage({"status", 1}) 8 0 8
94  
95 8 ▶ if ($opt->context and $opt->context < 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->context and $opt->context < 100) 0 8 8
Condition33%1/3
$opt->context and $opt->context < 100
!ll&&!rl&&r
080
MC/DC0%0/2
$opt->context and $opt->context < 100
$opt->context $opt->context < 100
Truth table33%1/3
A and B
A= $opt->contextB= $opt->context < 100
ABresult
0X0
100
111
96 0 ▶ $opt->verbose //= 1;
Condition0%0/2
$opt->verbose //= 1
l!l
00
MC/DC0%0/1
$opt->verbose //= 1
$opt->verbose
Truth table0%0/2
A
A= $opt->verbose
Aresult
11
00
97 }
98  
99 9 ▶ use Getopt::EX::Colormap;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 my $cm = Getopt::EX::Colormap
101 ->new(HASH => \%colormap)
102 8 ->load_params(@{$opt->colormap});
103  
104 8 ▶ if (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) { } 4 4 8
105 4 push @{$opt->exec}, [ @ARGV ];
106 } else {
107 4 ▶ @{$opt->exec} or pod2usage();
Branch: 1 branch
BranchTrueFalseTotal
@{$opt->exec;} or pod2usage() 4 0 4
108 }
109  
110 8 setup_terminal();
111 0 ▶ $SIG{INT} = sub { exit };
Subroutine __ANON__: not called CC=1 SCAR=1.2
112 8 $opt->do_loop();
113 }
114  
115 5 ▶END {
Subroutine END: called CC=1 SCAR=0.0
116 5 reset_terminal();
117 }
118  
119 12 ▶sub control_scroll {
Subroutine control_scroll: called CC=3 SCAR=13.1
Pod undocumented
120 12 my $opt = shift;
121 12 ▶ $opt->scroll && $opt->date && $opt->refresh == 1;
Branch: 1 branch
BranchTrueFalseTotal
$opt->scroll and $opt->date and $opt->refresh == 1 0 12 12
Condition33%1/3
$opt->scroll and $opt->date
!ll&&!rl&&r
0120
MC/DC0%0/2
$opt->scroll and $opt->date
$opt->scroll $opt->date
Truth table33%1/3
A and B
A= $opt->scrollB= $opt->date
ABresult
0X0
100
111
122 }
123  
124 8 ▶sub setup_terminal {
Subroutine setup_terminal: called CC=3 SCAR=15.4
Pod undocumented
125 8 ▶ if ($app and $app->control_scroll) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app and $app->control_scroll) 0 8 8
Condition33%1/3
$app and $app->control_scroll
!ll&&!rl&&r
080
MC/DC0%0/2
$app and $app->control_scroll
$app $app->control_scroll
Truth table33%1/3
A and B
A= $appB= $app->control_scroll
ABresult
0X0
100
111
126 0 STDOUT->printflush(csi_code(STBM => 3, 999));
127 }
128 }
129  
130 5 ▶sub reset_terminal {
Subroutine reset_terminal: called CC=3 SCAR=13.1
Pod undocumented
131 5 ▶ if ($app and $app->control_scroll) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app and $app->control_scroll) 0 5 5
Condition66%2/3
$app and $app->control_scroll
!ll&&!rl&&r
140
MC/DC0%0/2
$app and $app->control_scroll
$app $app->control_scroll
Truth table66%2/3
A and B
A= $appB= $app->control_scroll
ABresult
0X0
100
111
132 STDOUT->printflush($termcap{decsc},
133 csi_code(STBM =>),
134 0 $termcap{decrc});
135 }
136 }
137  
138 ##
139 ## Run all commands given by --exec and concatenate their output.
140 ##
141 9 package App::watchdiff::Command {
142 9 ▶ use parent 'Command::Run';
Subroutine BEGIN: called CC=1 SCAR=0.0
143 9 ▶ use Time::localtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
144 8 ▶ sub update {
Subroutine update: called CC=2 SCAR=7.0
145 8 my $obj = shift;
146 $obj->data(join "\n",
147 8 ▶ map { $obj->execute($_)->{data} // '' } $obj->command);
Condition50%1/2
$obj->execute($_)->{"data"} // ""
l!l
100
MC/DC0%0/1
$obj->execute($_)->{"data"} // ""
$obj->execute($_)->{"data"}
Truth table50%1/2
A
A= $obj->execute($_)->{"data"}
Aresult
11
00
148 8 $obj->date(ctime());
149 8 $obj;
150 }
151 }
152  
153 8 ▶sub do_loop {
Subroutine do_loop: called CC=16 SCAR=35.5
Pod undocumented
154 8 my $opt = shift;
155  
156 8 my $old = App::watchdiff::Command->new(command => $opt->exec);
157 8 my $new = App::watchdiff::Command->new(command => $opt->exec);
158  
159 my @default_diff = (
160 qw(cdif --no-unknown),
161 8 map { ('--cm', "$_=$colormap{$_}") } sort keys %colormap
162 );
163  
164 8 my @diffcmd = do {
165 8 ▶ if ($opt->diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->diff) { } 0 8 8
166 9 ▶ use Text::ParseWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
167 0 shellwords $opt->diff;
168 } else {
169 ( @default_diff,
170 32 ▶ map { ref $_->[1] eq 'CODE' ? $_->[1]->() : $_->[1] }
Branch: 1 branch
BranchTrueFalseTotal
ref $_->[1] eq 'CODE' ? : 8 24 32
171 40 grep { $_->[0] }
172 0 ▶ [ defined $opt->unit => sub { '--unit=' . $opt->unit } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 8 ▶ [ defined $opt->context => sub { '-U' . $opt->context } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
174 [ ! $opt->verbose => '--no-command' ],
175 [ ! $opt->mark => '--no-mark' ],
176 [ ! $opt->old => '--no-old' ],
177 );
178 }
179 };
180  
181 8 ▶ print $termcap{clear} if $opt->refresh;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->refresh 0 8 8
182 8 my $count = 0;
183 8 my $refresh_count = 0;
184 8 while (1) {
185 8 $old->rewind;
186 8 $new->update;
187 8 ▶ my $data = execute(@diffcmd, $old->path, $new->path) // die "diff: $!\n";
Condition50%1/2
execute(@diffcmd, $old->path, $new->path) // die("diff: $!\n")
l!l
40
MC/DC0%0/1
execute(@diffcmd, $old->path, $new->path) // die("diff: $!\n")
execute(@diffcmd, $old->path, $new->path)
Truth table50%1/2
A
A= execute(@diffcmd, $old->path, $new->path)
Aresult
11
00
188 4 ▶ if ($data eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq "") 0 4 4
189 0 ▶ if ($opt->silent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->silent) 0 0 0
190 0 flush($new->date, "\r");
191 0 next;
192 }
193 0 $data = $new->data;
194 0 ▶ $data =~ s/^/ /mg if $opt->mark;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->mark 0 0 0
195 }
196 4 ▶ $data .= "\n" if $opt->newline;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->newline 0 4 4
197 4 ▶ if ($opt->refresh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->refresh) 0 4 4
198 0 $data =~ s/^/$termcap{el}/mg;
199 0 ▶ if ($refresh_count++ % $opt->refresh == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refresh_count++ % $opt->refresh == 0) 0 0 0
200 0 print $termcap{clear};
201 }
202 }
203 4 ▶ print $new->date, "\n\n" if $opt->date;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->date 0 4 4
204 4 print $data;
205 4 ▶ if ($opt->refresh and $opt->clear) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->refresh and $opt->clear) 0 4 4
Condition33%1/3
$opt->refresh and $opt->clear
!ll&&!rl&&r
400
MC/DC0%0/2
$opt->refresh and $opt->clear
$opt->refresh $opt->clear
Truth table33%1/3
A and B
A= $opt->refreshB= $opt->clear
ABresult
0X0
100
111
206 0 flush($termcap{ed});
207 }
208 } continue {
209 4 ▶ last if ++$count == $opt->count;
Branch: 1 branch
BranchTrueFalseTotal
if ++$count == $opt->count 4 0 4
210 0 ($old, $new) = ($new, $old);
211 0 sleep $opt->interval;
212 }
213  
214 4 ▶ flush($termcap{el}) if $opt->refresh;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->refresh 0 4 4
215 4 return 0;
216 }
217  
218 0 ▶sub flush {
Subroutine flush: not called CC=2 SCAR=10.1
Pod undocumented
219 9 ▶ use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
220 0 ▶ state $stdout = IO::Handle->new->fdopen(fileno(STDOUT), "w") or die;
Branch: 1 branch
BranchTrueFalseTotal
state $stdout = "IO::Handle"->new->fdopen(fileno STDOUT, "w") or die 0 0 0
221 0 $stdout->printflush(@_);
222 }
223  
224 8 ▶sub execute {
Subroutine execute: called CC=8 SCAR=22.0
Pod undocumented
225 9 ▶ use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
226 8 ▶ my $pid = (my $fh = IO::File->new)->open('-|') // die "open: $@\n";
Condition50%1/2
(my $fh = "IO::File"->new)->open("-|") // die("open: $@\n")
l!l
80
MC/DC0%0/1
(my $fh = "IO::File"->new)->open("-|") // die("open: $@\n")
(my $fh = "IO::File"->new)->open("-|")
Truth table50%1/2
A
A= (my $fh = "IO::File"->new)->open("-|")
Aresult
11
00
227 8 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 4 4 8
228 4 ▶ open STDERR, ">&STDOUT" or die "dup: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or die "dup: $!" 4 0 4
229 4 close STDIN;
230 4 ▶ exec @_ or warn "$_[0]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
CORE::exec @_ or warn "$_[0]: $!\n" 0 0 0
231 0 exit 3;
232 }
233 4 binmode $fh, ':encoding(utf8)';
234 4 my $result = do { local $/; <$fh> };
235 4 for my $child (wait) {
236 4 ▶ $child != $pid and die "child = $child, pid = $pid";
Branch: 1 branch
BranchTrueFalseTotal
$child != $pid and die "child = $child, pid = $pid" 0 4 4
237 }
238 4 ▶ ($? >> 8) == 3 ? undef : $result;
Branch: 1 branch
BranchTrueFalseTotal
$? >> 8 == 3 ? : 0 4 4
239 }
240  
241 9 1;
242  
243 ######################################################################
244  
245 =pod
246  
247 =head1 NAME
248  
249 watchdiff - repeat command and watch differences
250  
251 =head1 VERSION
252  
253 Version 4.49
254  
255 =head1 DESCRIPTION
256  
257 Document is included in the executable. Use `man watchdiff` or
258 `perldoc watchdiff`.
259  
260 =head1 AUTHOR
261  
262 Kazumasa Utashiro
263  
264 L<https://github.com/kaz-utashiro/sdif-tools>
265  
266 =head1 LICENSE
267  
268 Copyright 2014- Kazumasa Utashiro
269  
270 This library is free software; you can redistribute it and/or modify
271 it under the same terms as Perl itself.
272  
273 =head1 SEE ALSO
274  
275 L<diff(1)>, L<cdif(1)>, L<sdif(1)>
276  
277 =cut
« blib/lib/App/sdif/Util.pm ↑ Summary blib/lib/App/sdif.pm »