line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Printer::Theme; |
2
|
35
|
|
|
35
|
|
54776
|
use strict; |
|
35
|
|
|
|
|
80
|
|
|
35
|
|
|
|
|
1036
|
|
3
|
35
|
|
|
35
|
|
172
|
use warnings; |
|
35
|
|
|
|
|
66
|
|
|
35
|
|
|
|
|
871
|
|
4
|
35
|
|
|
35
|
|
517
|
use Data::Printer::Common; |
|
35
|
|
|
|
|
108
|
|
|
35
|
|
|
|
|
22572
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# the theme name |
7
|
|
|
|
|
|
|
sub name { |
8
|
8
|
|
|
8
|
0
|
46
|
my ($self) = @_; |
9
|
8
|
|
|
|
|
37
|
return $self->{name}; |
10
|
|
|
|
|
|
|
} |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# true if the theme has at least one color override |
13
|
|
|
|
|
|
|
sub customized { |
14
|
2
|
|
|
2
|
0
|
4
|
my ($self) = @_; |
15
|
2
|
100
|
|
|
|
11
|
return exists $self->{is_custom} ? 1 : 0; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# displays the color as-is |
19
|
|
|
|
|
|
|
sub color_for { |
20
|
5
|
|
|
5
|
0
|
11
|
my ($self, $color_type) = @_; |
21
|
5
|
|
50
|
|
|
25
|
return $self->{colors}{$color_type} || ''; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# prints the SGR (terminal) color modifier |
25
|
|
|
|
|
|
|
sub sgr_color_for { |
26
|
12
|
|
|
12
|
0
|
2640
|
my ($self, $color_type) = @_; |
27
|
12
|
100
|
|
|
|
38
|
return unless exists $self->{sgr_colors}{$color_type}; |
28
|
9
|
|
100
|
|
|
34
|
return $self->{sgr_colors}{$color_type} || '' |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# prints the SGR (terminal) color reset modifier |
32
|
1
|
|
|
1
|
0
|
4
|
sub color_reset { return "\e[m" } |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub new { |
35
|
265
|
|
|
265
|
0
|
6040
|
my ($class, %params) = @_; |
36
|
|
|
|
|
|
|
|
37
|
265
|
|
|
|
|
523
|
my $color_level = $params{color_level}; |
38
|
265
|
|
|
|
|
439
|
my $colors_to_override = $params{color_overrides}; |
39
|
265
|
|
|
|
|
451
|
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
|
265
|
|
|
|
|
884
|
$theme_name =~ s/[^a-zA-Z0-9:]+//gsm; |
44
|
|
|
|
|
|
|
|
45
|
265
|
|
|
|
|
1085
|
my $theme = bless { |
46
|
|
|
|
|
|
|
name => $theme_name, |
47
|
|
|
|
|
|
|
color_level => $color_level, |
48
|
|
|
|
|
|
|
colors => {}, |
49
|
|
|
|
|
|
|
sgr_colors => {}, |
50
|
|
|
|
|
|
|
}, $class; |
51
|
265
|
100
|
|
|
|
845
|
$theme->_load_theme($params{ddp}) or delete $theme->{name}; |
52
|
265
|
|
|
|
|
875
|
$theme->_maybe_override_theme_colors($colors_to_override, $params{ddp}); |
53
|
265
|
|
|
|
|
973
|
return $theme; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub _maybe_override_theme_colors { |
57
|
265
|
|
|
265
|
|
571
|
my ($self, $colors_to_override, $ddp) = @_; |
58
|
|
|
|
|
|
|
|
59
|
265
|
50
|
66
|
|
|
820
|
return unless $colors_to_override |
|
|
|
66
|
|
|
|
|
60
|
|
|
|
|
|
|
&& ref $colors_to_override eq 'HASH' |
61
|
|
|
|
|
|
|
&& keys %$colors_to_override; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $error = Data::Printer::Common::_tryme(sub { |
64
|
13
|
|
|
13
|
|
31
|
foreach my $kind (keys %$colors_to_override ) { |
65
|
17
|
|
|
|
|
24
|
my $override = $colors_to_override->{$kind}; |
66
|
17
|
100
|
|
|
|
37
|
die "invalid color for '$kind': must be scalar not ref" if ref $override; |
67
|
16
|
|
|
|
|
30
|
my $parsed = $self->_parse_color($override, $ddp); |
68
|
16
|
100
|
|
|
|
62
|
if (defined $parsed) { |
69
|
6
|
|
|
|
|
10
|
$self->{colors}{$kind} = $override; |
70
|
6
|
|
|
|
|
9
|
$self->{sgr_colors}{$kind} = $parsed; |
71
|
6
|
|
|
|
|
15
|
$self->{is_custom}{$kind} = 1; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} |
74
|
13
|
|
|
|
|
72
|
}); |
75
|
13
|
100
|
|
|
|
64
|
if ($error) { |
76
|
1
|
|
|
|
|
4
|
Data::Printer::Common::_warn($ddp, "error overriding color: $error. Skipping!"); |
77
|
|
|
|
|
|
|
} |
78
|
13
|
|
|
|
|
575
|
return; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _load_theme { |
82
|
265
|
|
|
265
|
|
637
|
my ($self, $ddp) = @_; |
83
|
265
|
|
|
|
|
555
|
my $theme_name = $self->{name}; |
84
|
|
|
|
|
|
|
|
85
|
265
|
|
|
|
|
649
|
my $class = 'Data::Printer::Theme::' . $theme_name; |
86
|
265
|
|
|
|
|
940
|
my $error = Data::Printer::Common::_tryme("use $class; 1;"); |
87
|
265
|
100
|
|
|
|
707
|
if ($error) { |
88
|
1
|
|
|
|
|
5
|
Data::Printer::Common::_warn($ddp, "error loading theme '$theme_name': $error."); |
89
|
1
|
|
|
|
|
6
|
return; |
90
|
|
|
|
|
|
|
} |
91
|
264
|
|
|
|
|
631
|
my $loaded_colors = {}; |
92
|
264
|
|
|
|
|
453
|
my $loaded_colors_sgr = {}; |
93
|
|
|
|
|
|
|
$error = Data::Printer::Common::_tryme(sub { |
94
|
264
|
|
|
264
|
|
388
|
my $class_colors; |
95
|
35
|
|
|
35
|
|
281
|
{ no strict 'refs'; $class_colors = &{ $class . '::colors'}(); } |
|
35
|
|
|
|
|
76
|
|
|
35
|
|
|
|
|
30483
|
|
|
264
|
|
|
|
|
397
|
|
|
264
|
|
|
|
|
390
|
|
|
264
|
|
|
|
|
1456
|
|
96
|
264
|
100
|
|
|
|
911
|
die "${class}::colors() did not return a hash reference" |
97
|
|
|
|
|
|
|
unless ref $class_colors eq 'HASH'; |
98
|
|
|
|
|
|
|
|
99
|
263
|
|
|
|
|
1463
|
foreach my $kind (keys %$class_colors) { |
100
|
6049
|
|
|
|
|
8856
|
my $loaded_color = $class_colors->{$kind}; |
101
|
6049
|
50
|
|
|
|
10241
|
die "color for '$kind' must be a scalar in theme '$theme_name'" |
102
|
|
|
|
|
|
|
if ref $loaded_color; |
103
|
6049
|
|
|
|
|
10667
|
my $parsed_color = $self->_parse_color($loaded_color, $ddp); |
104
|
6049
|
50
|
|
|
|
12048
|
if (defined $parsed_color) { |
105
|
6049
|
|
|
|
|
10718
|
$loaded_colors->{$kind} = $loaded_color; |
106
|
6049
|
|
|
|
|
12429
|
$loaded_colors_sgr->{$kind} = $parsed_color; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
264
|
|
|
|
|
1741
|
}); |
110
|
264
|
100
|
|
|
|
1945
|
if ($error) { |
111
|
1
|
|
|
|
|
6
|
Data::Printer::Common::_warn($ddp, "error loading theme '$theme_name': $error. Output will have no colors"); |
112
|
1
|
|
|
|
|
5
|
return; |
113
|
|
|
|
|
|
|
} |
114
|
263
|
|
|
|
|
583
|
$self->{colors} = $loaded_colors; |
115
|
263
|
|
|
|
|
476
|
$self->{sgr_colors} = $loaded_colors_sgr; |
116
|
263
|
|
|
|
|
765
|
return 1; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub _parse_color { |
120
|
6066
|
|
|
6066
|
|
10096
|
my ($self, $color_label, $ddp) = @_; |
121
|
6066
|
50
|
|
|
|
10711
|
return unless defined $color_label; |
122
|
6066
|
100
|
|
|
|
9939
|
return '' unless $color_label; |
123
|
|
|
|
|
|
|
|
124
|
6052
|
|
|
|
|
7525
|
my $color_code; |
125
|
6052
|
100
|
|
|
|
19734
|
if ($color_label =~ /\Argb\((\d+),(\d+),(\d+)\)\z/) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
126
|
4
|
|
|
|
|
11
|
my ($r, $g, $b) = ($1, $2, $3); |
127
|
4
|
100
|
100
|
|
|
30
|
if ($r < 256 && $g < 256 && $b < 256) { |
|
|
|
100
|
|
|
|
|
128
|
1
|
50
|
|
|
|
4
|
if ($self->{color_level} == 3) { |
129
|
1
|
|
|
|
|
4
|
$color_code = "\e[0;38;2;$r;$g;${b}m"; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
else { |
132
|
0
|
|
|
|
|
0
|
my $reduced = _rgb2short($r,$g,$b); |
133
|
0
|
|
|
|
|
0
|
$color_code = "\e[0;38;5;${reduced}m"; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
else { |
137
|
3
|
|
|
|
|
9
|
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
|
6021
|
|
|
|
|
20555
|
my ($r, $g, $b) = map hex($_), ($1, $2, $3); |
142
|
6021
|
100
|
|
|
|
12332
|
if ($self->{color_level} == 3) { |
143
|
344
|
|
|
|
|
598
|
$color_code = "\e[0;38;2;$r;$g;${b}m"; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
else { |
146
|
5677
|
|
|
|
|
9726
|
my $reduced = _rgb2short($r,$g,$b); |
147
|
5677
|
|
|
|
|
12955
|
$color_code = "\e[0;38;5;${reduced}m"; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
elsif ($color_label =~ /\A\e\[\d+(:?;\d+)*m\z/) { |
151
|
2
|
|
|
|
|
5
|
$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
|
18
|
|
|
|
|
139
|
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
|
18
|
|
|
|
|
105
|
. join(';' => map $ansi_colors{$_}, split(/\s+/, $color_label)) |
182
|
|
|
|
|
|
|
. 'm' |
183
|
|
|
|
|
|
|
; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
else { |
186
|
7
|
|
|
|
|
22
|
Data::Printer::Common::_warn($ddp, "invalid color '$color_label'"); |
187
|
|
|
|
|
|
|
} |
188
|
6052
|
|
|
|
|
17391
|
return $color_code; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub _rgb2short { |
192
|
5678
|
|
|
5678
|
|
8696
|
my ($r,$g,$b) = @_; |
193
|
5678
|
|
|
|
|
9377
|
my @snaps = (47, 115, 155, 195, 235); |
194
|
5678
|
|
|
|
|
7216
|
my @new; |
195
|
5678
|
|
|
|
|
9401
|
foreach my $color ($r,$g,$b) { |
196
|
17034
|
|
|
|
|
21630
|
my $big = 0; |
197
|
17034
|
|
|
|
|
23707
|
foreach my $s (@snaps) { |
198
|
85170
|
100
|
|
|
|
150550
|
$big++ if $s < $color; |
199
|
|
|
|
|
|
|
} |
200
|
17034
|
|
|
|
|
26521
|
push @new, $big |
201
|
|
|
|
|
|
|
} |
202
|
5678
|
|
|
|
|
12393
|
return $new[0]*36 + $new[1]*6 + $new[2] + 16 |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
1; |
206
|
|
|
|
|
|
|
__END__ |