Data/Printer/Theme.pm

filter: Statementstmt 98.1% 103 / 105 Branchbran 90.9% 40 / 44 Conditioncond 81.2% 13 / 16 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 6 totaltotal 90.7% 177 / 195 CC 33 SCAR 17.0
CC
33
Cov
95%
_parse_color
12.1
__ANON__
5.0
_maybe_override_theme_colors
5.0
CRAP 5.5
« Data/Printer/Filter/Web.pm ↑ Summary Data/Printer/Filter/GLOB.pm »
1 package Data::Printer::Theme;
2 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 36 ▶use Data::Printer::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # the theme name
7 8 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
8 8 my ($self) = @_;
9 8 return $self->{name};
10 }
11  
12 # true if the theme has at least one color override
13 2 ▶sub customized {
Subroutine customized: called CC=2 SCAR=6.9
Pod undocumented
14 2 my ($self) = @_;
15 2 ▶ return exists $self->{is_custom} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'is_custom'} ? : 1 1 2
16 }
17  
18 # displays the color as-is
19 5 ▶sub color_for {
Subroutine color_for: called CC=2 SCAR=7.2
Pod undocumented
20 5 my ($self, $color_type) = @_;
21 5 ▶ return $self->{colors}{$color_type} || '';
Condition50%1/2
$self->{'colors'}{$color_type} || ''
l!l
50
MC/DC0%0/1
$self->{'colors'}{$color_type} || ''
$self->{'colors'}{$color_type}
Truth table50%1/2
A
A= $self->{'colors'}{$color_type}
Aresult
11
00
22 }
23  
24 # prints the SGR (terminal) color modifier
25 12 ▶sub sgr_color_for {
Subroutine sgr_color_for: called CC=3 SCAR=11.0
Pod undocumented
26 12 my ($self, $color_type) = @_;
27 12 ▶ return unless exists $self->{sgr_colors}{$color_type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"sgr_colors"}{$color_type} 3 9 12
28 9 ▶ return $self->{sgr_colors}{$color_type} || ''
Condition100%2/2
$self->{'sgr_colors'}{$color_type} || ''
l!l
72
MC/DC100%1/1
$self->{'sgr_colors'}{$color_type} || ''
$self->{'sgr_colors'}{$color_type}
Truth table100%2/2
A
A= $self->{'sgr_colors'}{$color_type}
Aresult
11
00
29 }
30  
31 # prints the SGR (terminal) color reset modifier
32 1 ▶sub color_reset { return "\e[m" }
Subroutine color_reset: called CC=1 SCAR=0.0
Pod undocumented
33  
34 272 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
35 272 my ($class, %params) = @_;
36  
37 272 my $color_level = $params{color_level};
38 272 my $colors_to_override = $params{color_overrides};
39 272 my $theme_name = $params{name};
40  
41 # before we put user info on string eval, make sure
42 # it's just a module name:
43 272 $theme_name =~ s/[^a-zA-Z0-9:]+//gsm;
44  
45 272 my $theme = bless {
46 name => $theme_name,
47 color_level => $color_level,
48 colors => {},
49 sgr_colors => {},
50 }, $class;
51 272 ▶ $theme->_load_theme($params{ddp}) or delete $theme->{name};
Branch: 1 branch
BranchTrueFalseTotal
$theme->_load_theme($params{"ddp"}) or delete $theme->{"name"} 270 2 272
52 272 $theme->_maybe_override_theme_colors($colors_to_override, $params{ddp});
53 272 return $theme;
54 }
55  
56 272 ▶sub _maybe_override_theme_colors {
Subroutine _maybe_override_theme_colors: called CC=5 SCAR=16.1
57 272 my ($self, $colors_to_override, $ddp) = @_;
58  
59 272 ▶ return unless $colors_to_override
Branch: 1 branch
BranchTrueFalseTotal
unless $colors_to_override and ref $colors_to_override eq "HASH" and keys %$colors_to_override 259 13 272
Condition66%4/6
$colors_to_override and ref $colors_to_override eq "HASH" and keys %$colors_to_override
!ll&&!rl&&r
259013
$colors_to_override and ref $colors_to_override eq "HASH"
!ll&&!rl&&r
259013
MC/DC33%1/3
$colors_to_override and ref $colors_to_override eq "HASH" and keys %$colors_to_override
$colors_to_override ref $colors_to_override eq "HASH" keys %$colors_to_override
Truth table50%2/4
A and B and C
A= $colors_to_overrideB= ref $colors_to_override eq "HASH"C= keys %$colors_to_override
ABCresult
0XX0
10X0
1100
1111
60 && ref $colors_to_override eq 'HASH'
61 && keys %$colors_to_override;
62  
63 13 ▶ my $error = Data::Printer::Common::_tryme(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
64 13 foreach my $kind (keys %$colors_to_override ) {
65 17 my $override = $colors_to_override->{$kind};
66 17 ▶ die "invalid color for '$kind': must be scalar not ref" if ref $override;
Branch: 1 branch
BranchTrueFalseTotal
if ref $override 1 16 17
67 16 my $parsed = $self->_parse_color($override, $ddp);
68 16 ▶ if (defined $parsed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parsed) 6 10 16
69 6 $self->{colors}{$kind} = $override;
70 6 $self->{sgr_colors}{$kind} = $parsed;
71 6 $self->{is_custom}{$kind} = 1;
72 }
73 }
74 13 });
75 13 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 1 12 13
76 1 Data::Printer::Common::_warn($ddp, "error overriding color: $error. Skipping!");
77 }
78 13 return;
79 }
80  
81 272 ▶sub _load_theme {
Subroutine _load_theme: called CC=3 SCAR=11.0
82 272 my ($self, $ddp) = @_;
83 272 my $theme_name = $self->{name};
84  
85 272 my $class = 'Data::Printer::Theme::' . $theme_name;
86 272 my $error = Data::Printer::Common::_tryme("use $class; 1;");
87 272 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 1 271 272
88 1 Data::Printer::Common::_warn($ddp, "error loading theme '$theme_name': $error.");
89 1 return;
90 }
91 271 my $loaded_colors = {};
92 271 my $loaded_colors_sgr = {};
93 271 ▶ $error = Data::Printer::Common::_tryme(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
94 271 my $class_colors;
95 36 ▶ { no strict 'refs'; $class_colors = &{ $class . '::colors'}(); }
Subroutine BEGIN: called CC=1 SCAR=0.0
96 271 ▶ die "${class}::colors() did not return a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $class_colors eq "HASH" 1 270 271
97 unless ref $class_colors eq 'HASH';
98  
99 270 foreach my $kind (keys %$class_colors) {
100 6750 my $loaded_color = $class_colors->{$kind};
101 6750 ▶ die "color for '$kind' must be a scalar in theme '$theme_name'"
Branch: 1 branch
BranchTrueFalseTotal
if ref $loaded_color 0 6750 6750
102 if ref $loaded_color;
103 6750 my $parsed_color = $self->_parse_color($loaded_color, $ddp);
104 6750 ▶ if (defined $parsed_color) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parsed_color) 6750 0 6750
105 6750 $loaded_colors->{$kind} = $loaded_color;
106 6750 $loaded_colors_sgr->{$kind} = $parsed_color;
107 }
108 }
109 271 });
110 271 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 1 270 271
111 1 Data::Printer::Common::_warn($ddp, "error loading theme '$theme_name': $error. Output will have no colors");
112 1 return;
113 }
114 270 $self->{colors} = $loaded_colors;
115 270 $self->{sgr_colors} = $loaded_colors_sgr;
116 270 return 1;
117 }
118  
119 6767 ▶sub _parse_color {
Subroutine _parse_color: called CC=12 SCAR=24.9
120 6767 my ($self, $color_label, $ddp) = @_;
121 6767 ▶ return unless defined $color_label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color_label 0 6767 6767
122 6767 ▶ return '' unless $color_label;
Branch: 1 branch
BranchTrueFalseTotal
unless $color_label 14 6753 6767
123  
124 6753 my $color_code;
125 6753 ▶ if ($color_label =~ /\Argb\((\d+),(\d+),(\d+)\)\z/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($color_label =~ /\Argb\((\d+),(\d+),(\d+)\)\z/) { } 4 6749 6753
elsif ($color_label =~ /\A#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})\z/i) { } 6720 29 6749
elsif ($color_label =~ /\A\e\[\d+(:?;\d+)*m\z/) { } 2 27 29
elsif ($color_label =~ /\A (?: \s* (?:on_)? (?:bright_)? (?:black|red|green|yellow|blue|magenta|cyan|white) )+ \s*\z/x) { } 20 7 27
126 4 my ($r, $g, $b) = ($1, $2, $3);
127 4 ▶ if ($r < 256 && $g < 256 && $b < 256) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < 256 and $g < 256 and $b < 256) { } 1 3 4
Condition100%6/6
$r < 256 and $g < 256 and $b < 256
!ll&&!rl&&r
211
$r < 256 and $g < 256
!ll&&!rl&&r
112
MC/DC100%3/3
$r < 256 and $g < 256 and $b < 256
$r < 256 $g < 256 $b < 256
Truth table100%4/4
A and B and C
A= $r < 256B= $g < 256C= $b < 256
ABCresult
0XX0
10X0
1100
1111
128 1 ▶ if ($self->{color_level} == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'color_level'} == 3) { } 1 0 1
129 1 $color_code = "\e[0;38;2;$r;$g;${b}m";
130 }
131 else {
132 0 my $reduced = _rgb2short($r,$g,$b);
133 0 $color_code = "\e[0;38;5;${reduced}m";
134 }
135 }
136 else {
137 3 Data::Printer::Common::_warn($ddp, "invalid color '$color_label': all colors must be between 0 and 255");
138 }
139 }
140 elsif ($color_label =~ /\A#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})\z/i) {
141 6720 my ($r, $g, $b) = map hex($_), ($1, $2, $3);
142 6720 ▶ if ($self->{color_level} == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'color_level'} == 3) { } 374 6346 6720
143 374 $color_code = "\e[0;38;2;$r;$g;${b}m";
144 }
145 else {
146 6346 my $reduced = _rgb2short($r,$g,$b);
147 6346 $color_code = "\e[0;38;5;${reduced}m";
148 }
149 }
150 elsif ($color_label =~ /\A\e\[\d+(:?;\d+)*m\z/) {
151 2 $color_code = $color_label;
152 }
153 elsif ($color_label =~ /\A
154 (?:
155 \s*
156 (?:on_)?
157 (?:bright_)?
158 (?:black|red|green|yellow|blue|magenta|cyan|white)
159 )+
160 \s*\z/x
161 ) {
162 20 my %ansi_colors = (
163 'black' => 30, 'on_black' => 40,
164 'red' => 31, 'on_red' => 41,
165 'green' => 32, 'on_green' => 42,
166 'yellow' => 33, 'on_yellow' => 43,
167 'blue' => 34, 'on_blue' => 44,
168 'magenta' => 35, 'on_magenta' => 45,
169 'cyan' => 36, 'on_cyan' => 46,
170 'white' => 37, 'on_white' => 47,
171 'bright_black' => 90, 'on_bright_black' => 100,
172 'bright_red' => 91, 'on_bright_red' => 101,
173 'bright_green' => 92, 'on_bright_green' => 102,
174 'bright_yellow' => 93, 'on_bright_yellow' => 103,
175 'bright_blue' => 94, 'on_bright_blue' => 104,
176 'bright_magenta' => 95, 'on_bright_magenta' => 105,
177 'bright_cyan' => 96, 'on_bright_cyan' => 106,
178 'bright_white' => 97, 'on_bright_white' => 107,
179 );
180 $color_code = "\e["
181 20 . join(';' => map $ansi_colors{$_}, split(/\s+/, $color_label))
182 . 'm'
183 ;
184 }
185 else {
186 7 Data::Printer::Common::_warn($ddp, "invalid color '$color_label'");
187 }
188 6753 return $color_code;
189 }
190  
191 6347 ▶sub _rgb2short {
Subroutine _rgb2short: called CC=4 SCAR=13.9
192 6347 my ($r,$g,$b) = @_;
193 6347 my @snaps = (47, 115, 155, 195, 235);
194 6347 my @new;
195 6347 foreach my $color ($r,$g,$b) {
196 19041 my $big = 0;
197 19041 foreach my $s (@snaps) {
198 95205 ▶ $big++ if $s < $color;
Branch: 1 branch
BranchTrueFalseTotal
if $s < $color 52030 43175 95205
199 }
200 19041 push @new, $big
201 }
202 6347 return $new[0]*36 + $new[1]*6 + $new[2] + 16
203 }
204  
205 36 1;
206 __END__
« Data/Printer/Filter/Web.pm ↑ Summary Data/Printer/Filter/GLOB.pm »