blib/lib/String/ShowDiff.pm

filter: Statementstmt 100% 51 / 51 Branchbran 100% 10 / 10 Conditioncond 91.3% 21 / 23 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 95.2% 100 / 105 CC 17 SCAR 22.5
CC
17
Cov
98%
ansi_colored_diff
14.0
_construct_glue
4.0
BEGIN
1.0
CRAP 9.4
↑ Summary
1 package String::ShowDiff;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 require Exporter;
6  
7 3 ▶use vars qw/@ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 @ISA = qw(Exporter);
10  
11 3 %EXPORT_TAGS = ( 'all' => [ qw(
12 ansi_colored_diff
13 ) ] );
14  
15 3 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
16  
17 3 @EXPORT = qw(
18
19 );
20  
21 3 $VERSION = '0.03';
22  
23 3 ▶use Algorithm::Diff qw/sdiff/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Term::ANSIColor qw/colored/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 16 ▶sub ansi_colored_diff {
Subroutine ansi_colored_diff: called CC=14 SCAR=26.4
Pod documented
27 16 my ($string, $changed_string, $options) = @_;
28 16 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
124
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
29 my %colors = (
30 '-' => $options->{'-'} || 'on_red',
31 '+' => $options->{'+'} || 'on_green',
32 16 ▶ 'u' => $options->{'u'} || 'reset',
Condition100%6/6
$options->{'-'} || 'on_red'
l!l
88
$options->{'+'} || 'on_green'
l!l
412
$options->{'u'} || 'reset'
l!l
412
MC/DC100%1/1
$options->{'-'} || 'on_red'
$options->{'-'}
MC/DC100%1/1
$options->{'+'} || 'on_green'
$options->{'+'}
MC/DC100%1/1
$options->{'u'} || 'reset'
$options->{'u'}
Truth table100%2/2
A
A= $options->{'-'}
Aresult
11
00
Truth table100%2/2
A
A= $options->{'+'}
Aresult
11
00
Truth table100%2/2
A
A= $options->{'u'}
Aresult
11
00
33 );
34 16 ▶ my $context_re = $options->{context} || qr/.*/;
Condition100%2/2
$options->{'context'} || qr/.*/
l!l
412
MC/DC100%1/1
$options->{'context'} || qr/.*/
$options->{'context'}
Truth table100%2/2
A
A= $options->{'context'}
Aresult
11
00
35 16 ▶ my $gap = $options->{gap} || '';
Condition100%2/2
$options->{'gap'} || ''
l!l
313
MC/DC100%1/1
$options->{'gap'} || ''
$options->{'gap'}
Truth table100%2/2
A
A= $options->{'gap'}
Aresult
11
00
36
37 16 my @sdiff = sdiff(map {[split //, $_]} $string, $changed_string);
38 16 my @ansi;
39 16 my $first_while_loop = 1;
40 16 ▶ while (@sdiff and my ($mod, $s1, $s2) = @{shift @sdiff}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sdiff and () = @{shift @sdiff;}) 202 16 218
Condition66%2/3
@sdiff and () = @{shift @sdiff;}
!ll&&!rl&&r
160202
MC/DC50%1/2
@sdiff and () = @{shift @sdiff;}
@sdiff () = @{shift @sdiff;}
Truth table66%2/3
A and B
A= @sdiffB= () = @{shift @sdiff;}
ABresult
0X0
100
111
41 202 ▶ if ($mod =~ /[+-]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /[+-]/) { } 139 63 202
42 139 ▶ push @ansi, colored($s1 || $s2, $colors{$mod});
Condition66%2/3
$s1 || $s2
l!l&&r!l&&!r
47920
MC/DC0%0/2
$s1 || $s2
$s1 $s2
Truth table66%2/3
A || B
A= $s1B= $s2
ABresult
1X1
011
000
43 } else { # Must be either a change or a part of unchanged characters
44 # So take a look, whether there are more chars that should be
45 # handled in a row
46 63 ▶ while (@sdiff && $sdiff[0]->[0] eq $mod) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sdiff and $sdiff[0][0] eq $mod) 230 63 293
Condition100%3/3
@sdiff and $sdiff[0][0] eq $mod
!ll&&!rl&&r
360230
MC/DC100%2/2
@sdiff and $sdiff[0][0] eq $mod
@sdiff $sdiff[0][0] eq $mod
Truth table100%3/3
A and B
A= @sdiffB= $sdiff[0][0] eq $mod
ABresult
0X0
100
111
47 230 $s1 .= $sdiff[0]->[1]; # if so, join all chars from the old
48 # string to $s1
49 230 $s2 .= $sdiff[0]->[2]; # and from the new to $s2
50 230 shift @sdiff; # The information of this element
51 # is already in $s1, $s2 and $mod
52 # and thus unnecessary now
53 }
54 63 ▶ if ($mod eq 'u') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod eq 'u') { } 47 16 63
55 47 my $unchanged_part = _construct_glue(
56 $s1, $context_re, $gap, $first_while_loop, @sdiff==0
57 );
58 47 push @ansi, _colorize_string($unchanged_part, $colors{'u'});
59 } else {
60 push @ansi,
61 _colorize_string($s1, $colors{'-'}),
62 16 _colorize_string($s2, $colors{'+'});
63 }
64 }
65 202 $first_while_loop = 0;
66 }
67 16 return join "", @ansi;
68 }
69  
70 # call with _colorize_string($string, $color)
71 79 ▶sub _colorize_string { join "", map {colored($_,$_[1])} split //, $_[0] }
Subroutine _colorize_string: called CC=1 SCAR=0.0
72  
73 47 ▶sub _construct_glue {
Subroutine _construct_glue: called CC=4 SCAR=13.9
74 47 my ($full_string, $context_re, $gap, $at_beginning, $at_end) = @_;
75 47 my ($start) = $full_string =~ /^($context_re)/;
76 47 my ($end) = $full_string =~ /($context_re)$/;
77 47 ▶ $_ ||= "" for ($start, $end);
Condition100%2/2
$_ ||= ""
l!l
922
MC/DC100%1/1
$_ ||= ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
78  
79 # Return now the shorter string of either a constructed context with a gap
80 # string or the normal string between the two differences
81 47 my $start_gap_end = $start . $gap . $end;
82 47 ▶ return length($start_gap_end) < length($full_string)
Branch: 1 branch
BranchTrueFalseTotal
length $start_gap_end < length $full_string ? : 4 43 47
83 ? $start_gap_end
84 : $full_string;
85 }
86  
87 3 1;
88 __END__
↑ Summary