| 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 ||= {}
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||||||
| 29 | my %colors = ( | ||||||||||||||||||||||||||||||||
| 30 | '-' => $options->{'-'} || 'on_red', | ||||||||||||||||||||||||||||||||
| 31 | '+' => $options->{'+'} || 'on_green', | ||||||||||||||||||||||||||||||||
| 32 | 16 | ▶ | 'u' => $options->{'u'} || 'reset', | ||||||||||||||||||||||||||||||
|
Condition100%6/6
$options->{'-'} || 'on_red'
$options->{'+'} || 'on_green'
$options->{'u'} || 'reset'
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->{'-'}
Truth table100%2/2
A
A= $options->{'+'}
Truth table100%2/2
A
A= $options->{'u'}
| |||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||
| 34 | 16 | ▶ | my $context_re = $options->{context} || qr/.*/; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'context'} || qr/.*/
MC/DC100%1/1
$options->{'context'} || qr/.*/
$options->{'context'}
Truth table100%2/2
A
A= $options->{'context'}
| |||||||||||||||||||||||||||||||||
| 35 | 16 | ▶ | my $gap = $options->{gap} || ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'gap'} || ''
MC/DC100%1/1
$options->{'gap'} || ''
$options->{'gap'}
Truth table100%2/2
A
A= $options->{'gap'}
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
@sdiff and () = @{shift @sdiff;}
MC/DC50%1/2
@sdiff and () = @{shift @sdiff;}
@sdiff
() = @{shift @sdiff;}
Truth table66%2/3
A and B
A= @sdiffB= () = @{shift @sdiff;}
| |||||||||||||||||||||||||||||||||
| 41 | 202 | ▶ | if ($mod =~ /[+-]/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 139 | ▶ | push @ansi, colored($s1 || $s2, $colors{$mod}); | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$s1 || $s2
MC/DC0%0/2
$s1 || $s2
$s1
$s2
Truth table66%2/3
A || B
A= $s1B= $s2
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@sdiff and $sdiff[0][0] eq $mod
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
$_ ||= ""
MC/DC100%1/1
$_ ||= ""
$_
Truth table100%2/2
A
A= $_
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 83 | ? $start_gap_end | ||||||||||||||||||||||||||||||||
| 84 | : $full_string; | ||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 3 | 1; | |||||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||||||