JSON/Color.pm

filter: Statementstmt 74.5% 85 / 114 Branchbran 51.5% 34 / 66 Conditioncond 51.4% 18 / 35 MC/DCmcdc 17.6% 3 / 17 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 62.6% 154 / 246 CC 49 SCAR 29.6
CC
49
Cov
64%
_hash
69.9
_encode
17.8
encode_json
15.7
CRAP 19.3
↑ Summary ColorTheme/JSON/Color/default_ansi.pm »
1 package JSON::Color;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 2 our $DATE = '2021-11-24'; # DATE
9 2 our $DIST = 'JSON-Color'; # DIST
10 2 our $VERSION = '0.133'; # VERSION
11  
12 2 ▶our $sul_available = eval { require Scalar::Util::LooksLikeNumber; 1 } ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Scalar::Util::LooksLikeNumber; 1 } } ? : 2 0 2
13  
14 2 require Exporter;
15 2 our @ISA = qw(Exporter);
16 2 our @EXPORT_OK = qw(encode_json);
17  
18 2 ▶use Color::ANSI::Util qw(ansi_reset);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use ColorThemeRole::ANSI (); # for scan_prereqs
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 my %esc = (
22 "\n" => '\n',
23 "\r" => '\r',
24 "\t" => '\t',
25 "\f" => '\f',
26 "\b" => '\b',
27 "\"" => '\"',
28 "\\" => '\\\\',
29 "\'" => '\\\'',
30 );
31 sub _string {
32 2 ▶ my ($value, $opts) = @_;
Subroutine _string: called CC=9 SCAR=24.1
33  
34 2 my $ct = $opts->{_color_theme_obj};
35 2 my $c_reset = ansi_reset(1);
36 2 my ($c_q, $c_s, $c_e);
37 2 ▶ if ($opts->{obj_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'obj_key'}) { } 0 2 2
38 0 $c_s = $ct->get_item_color_as_ansi('object_key');
39 0 $c_q = $ct->get_item_color_as_ansi('object_key_quote');
40 0 $c_e = $ct->get_item_color_as_ansi('object_key_escape');
41 } else {
42 2 $c_s = $ct->get_item_color_as_ansi('string');
43 2 $c_q = $ct->get_item_color_as_ansi('string_quote');
44 2 $c_e = $ct->get_item_color_as_ansi('string_escape');
45 }
46  
47 2 ▶ for ($c_q, $c_s, $c_e) { $_ //= "" }
Condition100%2/2
$_ //= ""
l!l
33
MC/DC100%1/1
$_ //= ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
48  
49 2 $value =~ s/([\x22\x5c\n\r\t\f\b])|([\x00-\x08\x0b\x0e-\x1f])/
50 join("",
51 $c_e,
52 0 ▶ $1 ? $esc{$1} : '\\u00' . unpack('H2', $2),
Branch: 2 branches
BranchTrueFalseTotal
$1 ? : 0 0 0
$c_e ? : 0 0 0
53 ($c_e ? $c_reset : ""), $c_s,
54 )
55 /eg;
56  
57 2 ▶ return join(
Branch: 3 branches
BranchTrueFalseTotal
$c_q ? : 1 1 2
$c_s ? : 1 1 2
$c_q ? : 1 1 2
58 "",
59 $c_q, '"', ($c_q ? $c_reset : ""),
60 $c_s, $value, ($c_s ? $c_reset : ""),
61 $c_q, '"', ($c_q ? $c_reset : ""),
62 );
63 }
64  
65 sub _number {
66 1 ▶ my ($value, $opts) = @_;
Subroutine _number: called CC=3 SCAR=11.3
67  
68 1 my $ct = $opts->{_color_theme_obj};
69 1 my $c_reset = ansi_reset(1);
70 1 ▶ my $c_s = $ct->get_item_color_as_ansi('number') // "";
Condition50%1/2
$ct->get_item_color_as_ansi("number") // ""
l!l
01
MC/DC0%0/1
$ct->get_item_color_as_ansi("number") // ""
$ct->get_item_color_as_ansi("number")
Truth table50%1/2
A
A= $ct->get_item_color_as_ansi("number")
Aresult
11
00
71 1 ▶ return join(
Branch: 1 branch
BranchTrueFalseTotal
$c_s ? : 0 1 1
72 "",
73 $c_s,
74 $value,
75 ($c_s ? $c_reset : ""),
76 );
77 }
78  
79 sub _null {
80 1 ▶ my ($value, $opts) = @_;
Subroutine _null: called CC=3 SCAR=11.3
81  
82 1 my $ct = $opts->{_color_theme_obj};
83 1 my $c_reset = ansi_reset(1);
84 1 ▶ my $c_s = $ct->get_item_color_as_ansi('null') // "";
Condition50%1/2
$ct->get_item_color_as_ansi("null") // ""
l!l
01
MC/DC0%0/1
$ct->get_item_color_as_ansi("null") // ""
$ct->get_item_color_as_ansi("null")
Truth table50%1/2
A
A= $ct->get_item_color_as_ansi("null")
Aresult
11
00
85 1 ▶ return join(
Branch: 1 branch
BranchTrueFalseTotal
$c_s ? : 0 1 1
86 "",
87 $c_s,
88 "null",
89 ($c_s ? $c_reset : ""),
90 );
91 }
92  
93 sub _bool {
94 1 ▶ my ($value, $opts) = @_;
Subroutine _bool: called CC=4 SCAR=14.6
95  
96 1 my $ct = $opts->{_color_theme_obj};
97 1 my $c_reset = ansi_reset(1);
98 1 ▶ my $c_s = $ct->get_item_color_as_ansi($value ? 'true' : 'false') // "";
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1 0 1
Condition50%1/2
$ct->get_item_color_as_ansi($value ? "true" : "false") // ""
l!l
01
MC/DC0%0/1
$ct->get_item_color_as_ansi($value ? "true" : "false") // ""
$ct->get_item_color_as_ansi($value ? "true" : "false")
Truth table50%1/2
A
A= $ct->get_item_color_as_ansi($value ? "true" : "false")
Aresult
11
00
99 1 ▶ return join(
Branch: 1 branch
BranchTrueFalseTotal
$c_s ? : 0 1 1
100 "",
101 $c_s,
102 "$value",
103 ($c_s ? $c_reset : ""),
104 );
105 }
106  
107 sub _array {
108 2 ▶ my ($value, $opts) = @_;
Subroutine _array: called CC=6 SCAR=18.2
109  
110 #my $ct = $opts->{_color_theme_obj};
111 2 ▶ return "[]" unless @$value;
Branch: 1 branch
BranchTrueFalseTotal
unless @$value 1 1 2
112 1 ▶ my $indent = $opts->{pretty} ? " " x $opts->{_indent} : "";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pretty'} ? : 0 1 1
113 1 ▶ my $indent2 = $opts->{pretty} ? " " x ($opts->{_indent}+1) : "";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pretty'} ? : 0 1 1
114 1 ▶ my $nl = $opts->{pretty} ? "\n" : "";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pretty'} ? : 0 1 1
115 1 local $opts->{_indent} = $opts->{_indent}+1;
116 return join(
117 "",
118 "[$nl",
119 1 (map {(
120 6 ▶ $indent2,
Branch: 1 branch
BranchTrueFalseTotal
$_ == @$value - 1 ? : 1 5 6
121 _encode($value->[$_], $opts),
122 $_ == @$value-1 ? $nl : ",$nl",)
123 } 0..@$value-1),
124 $indent, "]",
125 );
126 }
127  
128 sub _hash {
129 1 ▶ my ($value, $opts) = @_;
Subroutine _hash: called CC=9 SCAR=42.5
130  
131 #my $ct = $opts->{_color_theme_obj};
132 1 ▶ return "{}" unless keys %$value;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$value 1 0 1
133 0 ▶ my $indent = $opts->{pretty} ? " " x $opts->{_indent} : "";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pretty'} ? : 0 0 0
134 0 ▶ my $indent2 = $opts->{pretty} ? " " x ($opts->{_indent}+1) : "";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pretty'} ? : 0 0 0
135 0 ▶ my $nl = $opts->{pretty} ? "\n" : "";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pretty'} ? : 0 0 0
136 0 ▶ my $colon = $opts->{pretty} ? ": " : ":";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pretty'} ? : 0 0 0
137 0 my @res;
138  
139 0 push @res, "{$nl";
140 0 my @k;
141 0 ▶ if ($opts->{sort_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'sort_by'}) { } 0 0 0
142 0 @k = sort { $opts->{sort_by}->() } keys %$value;
143 } else {
144 0 @k = sort keys(%$value);
145 }
146 0 local $opts->{_indent} = $opts->{_indent}+1;
147 0 for (0..@k-1) {
148 0 my $k = $k[$_];
149 push @res, (
150 $indent2,
151 _string($k, {%$opts, obj_key=>1}),
152 $colon,
153 0 ▶ _encode($value->{$k}, $opts),
Branch: 1 branch
BranchTrueFalseTotal
$_ == @k - 1 ? : 0 0 0
154 $_ == @k-1 ? $nl : ",$nl",
155 );
156 }
157 0 push @res, $indent, "}";
158 0 join "", @res;
159 }
160  
161 sub _encode {
162 8 ▶ my ($data, $opts) = @_;
Subroutine _encode: called CC=12 SCAR=28.8
163  
164 8 my $ref = ref($data);
165  
166 8 ▶ if (!defined($data)) {
Branch: 6 branches
BranchTrueFalseTotal
if (not defined $data) { } 1 7 8
elsif ($ref eq 'ARRAY') { } 2 5 7
elsif ($ref eq 'HASH') { } 1 4 5
elsif ($ref eq 'JSON::XS::Boolean' or $ref eq 'JSON::PP::Boolean') { } 1 3 4
elsif (not $ref) { } 3 0 3
elsif ($JSON::Color::sul_available and builtin::blessed($data) and $data->can('TO_JSON')) { } 0 0 0
167 1 return _null($data, $opts);
168 } elsif ($ref eq 'ARRAY') {
169 2 return _array($data, $opts);
170 } elsif ($ref eq 'HASH') {
171 1 return _hash($data, $opts);
172 ▶ } elsif ($ref eq 'JSON::XS::Boolean' || $ref eq 'JSON::PP::Boolean') {
Condition66%2/3
$ref eq "JSON::XS::Boolean" or $ref eq "JSON::PP::Boolean"
l!l&&r!l&&!r
013
MC/DC50%1/2
$ref eq "JSON::XS::Boolean" or $ref eq "JSON::PP::Boolean"
$ref eq "JSON::XS::Boolean" $ref eq "JSON::PP::Boolean"
Truth table66%2/3
A or B
A= $ref eq "JSON::XS::Boolean"B= $ref eq "JSON::PP::Boolean"
ABresult
1X1
011
000
173 1 return _bool($data, $opts);
174 } elsif (!$ref) {
175 3 ▶ if ($sul_available &&
Branch: 1 branch
BranchTrueFalseTotal
if ($JSON::Color::sul_available and &Scalar::Util::LooksLikeNumber::looks_like_number($data) =~ /^(4|12|4352|8704)$/o) { } 1 2 3
Condition66%2/3
$JSON::Color::sul_available and &Scalar::Util::LooksLikeNumber::looks_like_number($data) =~ /^(4|12|4352|8704)$/o
!ll&&!rl&&r
021
MC/DC50%1/2
$JSON::Color::sul_available and &Scalar::Util::LooksLikeNumber::looks_like_number($data) =~ /^(4|12|4352|8704)$/o
$JSON::Color::sul_available &Scalar::Util::LooksLikeNumber::looks_like_number($data) =~ /^(4|12|4352|8704)$/o
Truth table66%2/3
A and B
A= $JSON::Color::sul_availableB= &Scalar::Util::LooksLikeNumber::looks_like_number($data) =~ /^(4|12|4352|8704)$/o
ABresult
0X0
100
111
176 Scalar::Util::LooksLikeNumber::looks_like_number($data) =~
177 /^(4|12|4352|8704)$/o) {
178 1 return _number($data, $opts);
179 } else {
180 2 return _string($data, $opts);
181 }
182 ▶ } elsif ($sul_available &&
Condition0%0/6
$JSON::Color::sul_available and builtin::blessed($data) and $data->can("TO_JSON")
!ll&&!rl&&r
000
$JSON::Color::sul_available and builtin::blessed($data)
!ll&&!rl&&r
000
MC/DC0%0/3
$JSON::Color::sul_available and builtin::blessed($data) and $data->can("TO_JSON")
$JSON::Color::sul_available builtin::blessed($data) $data->can("TO_JSON")
Truth table0%0/4
A and B and C
A= $JSON::Color::sul_availableB= builtin::blessed($data)C= $data->can("TO_JSON")
ABCresult
0XX0
10X0
1100
1111
183 Scalar::Util::blessed($data) && $data->can('TO_JSON')) {
184 0 return _encode($data->TO_JSON, $opts);
185 } else {
186 0 die "Can't encode $data";
187 }
188 }
189  
190 sub encode_json {
191 2 ▶ my ($value, $opts) = @_;
Subroutine encode_json: called CC=10 SCAR=27.5
Pod documented
192 2 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
02
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
193 2 ▶ $opts->{_indent} //= 0;
Condition50%1/2
$opts->{"_indent"} //= 0
l!l
02
MC/DC0%0/1
$opts->{"_indent"} //= 0
$opts->{"_indent"}
Truth table50%1/2
A
A= $opts->{"_indent"}
Aresult
11
00
194 $opts->{color_theme} //=
195 (defined $ENV{NO_COLOR} ? "NoColor" : undef) //
196 $ENV{JSON_COLOR_COLOR_THEME} //
197 $ENV{COLOR_THEME} //
198 2 ▶ "default_ansi";
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'NO_COLOR'} ? : 1 1 2
Condition63%7/11
$opts->{"color_theme"} //= (((defined $ENV{"NO_COLOR"} ? "NoColor" : undef) // $ENV{"JSON_COLOR_COLOR_THEME"}) // $ENV{"COLOR_THEME"}) // "default_ansi"
l!l&&r!l&&!r
020
(((defined $ENV{"NO_COLOR"} ? "NoColor" : undef) // $ENV{"JSON_COLOR_COLOR_THEME"}) // $ENV{"COLOR_THEME"}) // "default_ansi"
l!l
11
((defined $ENV{"NO_COLOR"} ? "NoColor" : undef) // $ENV{"JSON_COLOR_COLOR_THEME"}) // $ENV{"COLOR_THEME"}
l!l&&r!l&&!r
101
(defined $ENV{"NO_COLOR"} ? "NoColor" : undef) // $ENV{"JSON_COLOR_COLOR_THEME"}
l!l&&r!l&&!r
101
MC/DC0%0/4
$opts->{"color_theme"} //= (((defined $ENV{"NO_COLOR"} ? "NoColor" : undef) // $ENV{"JSON_COLOR_COLOR_THEME"}) // $ENV{"COLOR_THEME"}) // "default_ansi"
$opts->{"color_theme"} (defined $ENV{"NO_COLOR"} ? "NoColor" : undef) $ENV{"JSON_COLOR_COLOR_THEME"} $ENV{"COLOR_THEME"}
Truth table0%0/5
A //= B // C // D
A= $opts->{"color_theme"}B= (defined $ENV{"NO_COLOR"} ? "NoColor" : undef)C= $ENV{"JSON_COLOR_COLOR_THEME"}D= $ENV{"COLOR_THEME"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
199  
200 2 require Module::Load::Util;
201 my $ct = Module::Load::Util::instantiate_class_with_optional_args(
202 {ns_prefixes=>["ColorTheme::JSON::Color", "ColorTheme", ""]},
203 $opts->{color_theme},
204 2 );
205 2 require Role::Tiny;
206 2 Role::Tiny->apply_roles_to_object($ct, 'ColorThemeRole::ANSI');
207 2 $opts->{_color_theme_obj} = $ct;
208  
209 2 my $res = _encode($value , $opts);
210  
211 2 ▶ if ($opts->{linum}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"linum"}) 0 2 2
212 0 my $lines = 0;
213 0 ▶ $lines++ while $res =~ /^/mog;
Branch: 1 branch
BranchTrueFalseTotal
$res =~ /^/gmo and do { ++$lines } 0 0 0
214 0 my $fmt = "%".length($lines)."d";
215 0 my $i = 0;
216 0 $res =~ s/^/
217 0 $ct->get_item_color('linum') . sprintf($fmt, ++$i) . ansi_reset(1)
218 /meg;
219 }
220 2 $res;
221 }
222  
223 2 1;
224 # ABSTRACT: Encode to colored JSON
225  
226 __END__
↑ Summary ColorTheme/JSON/Color/default_ansi.pm »