blib/lib/YAML/Tiny/Color.pm

filter: Statementstmt 22.9% 31 / 135 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 53.8% 7 / 13 Podpod 50.0% 1 / 2 totaltotal 18.6% 39 / 209 CC 32 SCAR 40.7
CC
32
Cov
16%
_write_string
90.0
_write_scalar
72.0
_write_array
72.0
CRAP 58.7
↑ Summary
1 package YAML::Tiny::Color;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util::LooksLikeNumber qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 require Exporter;
13 1 our @ISA = qw(Exporter);
14 1 our @EXPORT = qw(Dump);
15  
16 1 our $VERSION = '0.04'; # VERSION
17  
18 1 our $LineNumber = 0;
19  
20 # The character class of all characters we need to escape
21 # NOTE: Inlined, since it's only used once
22 # my $RE_ESCAPE = '[\\x00-\\x08\\x0b-\\x0d\\x0e-\\x1f\"\n]';
23  
24 # Printed form of the unprintable characters in the lowest range
25 # of ASCII characters, listed by ASCII ordinal position.
26 1 my @UNPRINTABLE = qw(
27 z x01 x02 x03 x04 x05 x06 a
28 x08 t n v f r x0e x0f
29 x10 x11 x12 x13 x14 x15 x16 x17
30 x18 x19 x1a e x1c x1d x1e x1f
31 );
32  
33 # Printable characters for escapes
34 1 my %UNESCAPES = (
35 z => "\x00", a => "\x07", t => "\x09",
36 n => "\x0a", v => "\x0b", f => "\x0c",
37 r => "\x0d", e => "\x1b", '\\' => '\\',
38 );
39  
40 # Special magic boolean words
41 1 my %QUOTE = map { $_ => 1 } qw{
42 null Null NULL
43 y Y yes Yes YES n N no No NO
44 true True TRUE false False FALSE
45 on On ON off Off OFF
46 };
47  
48 1 our %theme = (
49 start_quote => BOLD . BRIGHT_GREEN,
50 end_quote => RESET,
51 start_string => GREEN,
52 end_string => RESET,
53 # currently not highlighted
54 #start_string_escape => BOLD,
55 #end_string_escape => RESET . GREEN, # back to string
56 start_number => BOLD . BRIGHT_MAGENTA,
57 end_number => RESET,
58 start_bool => CYAN,
59 end_bool => RESET,
60 start_null => BOLD . CYAN,
61 end_null => RESET,
62 start_hash_key => MAGENTA,
63 end_hash_key => RESET,
64 #start_hash_key_escape => BOLD,
65 #end_hash_key_escape => RESET . MAGENTA, # back to object key
66 start_linum => REVERSE . WHITE,
67 end_linum => RESET,
68 );
69  
70 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
71 0 my $class = shift;
72 0 bless [ @_ ], $class;
73 }
74  
75 # Save an object to a string
76 0 ▶sub _write_string {
Subroutine _write_string: not called CC=9 SCAR=45.0
77 0 my $self = shift;
78 0 ▶ return '' unless @$self;
Branch: 1 branch
BranchTrueFalseTotal
unless @$self 0 0 0
79  
80 # Iterate over the documents
81 0 my $indent = 0;
82 0 my @lines = ();
83 0 foreach my $cursor ( @$self ) {
84 0 push @lines, '---';
85  
86 # An empty document
87 0 ▶ if ( ! defined $cursor ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $cursor) { } 0 0 0
elsif (not ref $cursor) { } 0 0 0
elsif (ref $cursor eq 'ARRAY') { } 0 0 0
elsif (ref $cursor eq 'HASH') { } 0 0 0
88 # Do nothing
89  
90 # A scalar document
91 } elsif ( ! ref $cursor ) {
92 0 $lines[-1] .= ' ' . $self->_write_scalar( $cursor, $indent );
93  
94 # A list at the root
95 } elsif ( ref $cursor eq 'ARRAY' ) {
96 0 ▶ unless ( @$cursor ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$cursor) 0 0 0
97 0 $lines[-1] .= ' []';
98 0 next;
99 }
100 0 push @lines, $self->_write_array( $cursor, $indent, {} );
101  
102 # A hash at the root
103 } elsif ( ref $cursor eq 'HASH' ) {
104 0 ▶ unless ( %$cursor ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$cursor) 0 0 0
105 0 $lines[-1] .= ' {}';
106 0 next;
107 }
108 0 push @lines, $self->_write_hash( $cursor, $indent, {} );
109  
110 } else {
111 0 Carp::croak("Cannot serialize " . ref($cursor));
112 }
113 }
114  
115 0 join '', map { "$_\n" } @lines;
116 }
117  
118 0 ▶sub _write_scalar {
Subroutine _write_scalar: not called CC=8 SCAR=42.8
119 0 my $string = $_[1];
120 #say "$string -> ", looks_like_number($string);
121 0 my $is_hkey = $_[3];
122  
123 0 my ($sq, $eq, $sn, $en, $ss, $es);
124 0 ▶ if ($is_hkey) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hkey) { } 0 0 0
125 0 $sq = $theme{start_hash_key};
126 0 $eq = $theme{end_hash_key};
127 0 $sn = $theme{start_hash_key};
128 0 $en = $theme{end_hash_key};
129 0 $ss = $theme{start_hash_key};
130 0 $es = $theme{end_hash_key};
131 } else {
132 0 $sq = $theme{start_quote};
133 0 $eq = $theme{end_quote};
134 0 $sn = $theme{start_number};
135 0 $en = $theme{end_number};
136 0 $ss = $theme{start_string};
137 0 $es = $theme{end_string};
138 }
139  
140 0 ▶ return $theme{start_null}.'~'.$theme{end_null} unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 0 0
141 0 ▶ return $theme{start_quote}."''".$theme{end_quote} unless length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless length $string 0 0 0
142 0 ▶ if ( $string =~ /[\x00-\x08\x0b-\x0d\x0e-\x1f\"\'\n]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /[\x00-\x08\x0b-\x0d\x0e-\x1f\"\'\n]/) 0 0 0
143 0 $string =~ s/\\/\\\\/g;
144 0 $string =~ s/"/\\"/g;
145 0 $string =~ s/\n/\\n/g;
146 0 $string =~ s/([\x00-\x1f])/\\$UNPRINTABLE[ord($1)]/g;
147 0 return join(
148 "",
149 $sq, '"', $eq,
150 $ss, $string, $es,
151 $sq, '"', $eq,
152 );
153 }
154 0 ▶ if ( $string =~ /(?:^\W|\s|:\z)/ or $QUOTE{$string} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /(?:^\W|\s|:\z)/ or $QUOTE{$string}) 0 0 0
Condition0%0/3
$string =~ /(?:^\W|\s|:\z)/ or $QUOTE{$string}
l!l&&r!l&&!r
000
MC/DC0%0/2
$string =~ /(?:^\W|\s|:\z)/ or $QUOTE{$string}
$string =~ /(?:^\W|\s|:\z)/ $QUOTE{$string}
Truth table0%0/3
A or B
A= $string =~ /(?:^\W|\s|:\z)/B= $QUOTE{$string}
ABresult
1X1
011
000
155 0 return join(
156 "",
157 $sq, "'", $eq,
158 $ss, $string, $es,
159 $sq, "'", $eq,
160 );
161 }
162 0 ▶ if (looks_like_number($string) =~ /^(4|12|4352|8704)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($string) =~ /^(4|12|4352|8704)$/o) { } 0 0 0
163 0 return join("", $sn, $string, $en);
164 } else {
165 0 return join("", $ss, $string, $es);
166 }
167 }
168  
169 0 ▶sub _write_array {
Subroutine _write_array: not called CC=8 SCAR=42.8
170 0 my ($self, $array, $indent, $seen) = @_;
171 0 ▶ if ( $seen->{refaddr($array)}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$seen{builtin::refaddr($array)}++) 0 0 0
172 0 die "YAML::Tiny::Color does not support circular references";
173 }
174 0 my @lines = ();
175 0 foreach my $el ( @$array ) {
176 0 my $line = (' ' x $indent) . '-';
177 0 my $type = ref $el;
178 0 ▶ if ( ! $type ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $type) { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
elsif ($type eq 'HASH') { } 0 0 0
179 0 $line .= ' ' . $self->_write_scalar( $el, $indent + 1 );
180 0 push @lines, $line;
181  
182 } elsif ( $type eq 'ARRAY' ) {
183 0 ▶ if ( @$el ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$el) { } 0 0 0
184 0 push @lines, $line;
185 0 push @lines, $self->_write_array( $el, $indent + 1, $seen );
186 } else {
187 0 $line .= ' []';
188 0 push @lines, $line;
189 }
190  
191 } elsif ( $type eq 'HASH' ) {
192 0 ▶ if ( keys %$el ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$el) { } 0 0 0
193 0 push @lines, $line;
194 0 push @lines, $self->_write_hash( $el, $indent + 1, $seen );
195 } else {
196 0 $line .= ' {}';
197 0 push @lines, $line;
198 }
199  
200 } else {
201 0 die "YAML::Tiny::Color does not support $type references";
202 }
203 }
204  
205 0 @lines;
206 }
207  
208 0 ▶sub _write_hash {
Subroutine _write_hash: not called CC=8 SCAR=42.8
209 0 my ($self, $hash, $indent, $seen) = @_;
210 0 ▶ if ( $seen->{refaddr($hash)}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$seen{builtin::refaddr($hash)}++) 0 0 0
211 0 die "YAML::Tiny::Color does not support circular references";
212 }
213 0 my @lines = ();
214 0 foreach my $name ( sort keys %$hash ) {
215 0 my $el = $hash->{$name};
216 0 my $line = (' ' x $indent) . $self->_write_scalar($name, 0, 1) . ":";
217 0 my $type = ref $el;
218 0 ▶ if ( ! $type ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $type) { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
elsif ($type eq 'HASH') { } 0 0 0
219 0 $line .= ' ' . $self->_write_scalar( $el, $indent + 1 );
220 0 push @lines, $line;
221  
222 } elsif ( $type eq 'ARRAY' ) {
223 0 ▶ if ( @$el ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$el) { } 0 0 0
224 0 push @lines, $line;
225 0 push @lines, $self->_write_array( $el, $indent + 1, $seen );
226 } else {
227 0 $line .= ' []';
228 0 push @lines, $line;
229 }
230  
231 } elsif ( $type eq 'HASH' ) {
232 0 ▶ if ( keys %$el ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$el) { } 0 0 0
233 0 push @lines, $line;
234 0 push @lines, $self->_write_hash( $el, $indent + 1, $seen );
235 } else {
236 0 $line .= ' {}';
237 0 push @lines, $line;
238 }
239  
240 } else {
241 0 die "YAML::Tiny::Color does not support $type references";
242 }
243 }
244  
245 0 @lines;
246 }
247  
248 0 ▶sub Dump {
Subroutine Dump: not called CC=3 SCAR=24.8
Pod documented
249 0 my $res = YAML::Tiny::Color->new(@_)->_write_string;
250  
251 0 ▶ if ($YAML::Tiny::Color::LineNumber) {
Branch: 1 branch
BranchTrueFalseTotal
if ($YAML::Tiny::Color::LineNumber) 0 0 0
252 0 my $lines = 0;
253 0 ▶ $lines++ while $res =~ /^/mog;
Branch: 1 branch
BranchTrueFalseTotal
$res =~ /^/gmo and do { ++$lines } 0 0 0
254 0 my $fmt = "%".length($lines)."d";
255 0 my $i = 0;
256 0 $res =~ s/^/
257 $theme{start_linum} . sprintf($fmt, ++$i) . $theme{end_linum}
258 0 /meg;
259 }
260 0 $res;
261 }
262  
263 1 1;
264 # ABSTRACT: Dump YAML with color
265  
266 __END__
↑ Summary