termcolor.pm

filter: Statementstmt 36.1% 39 / 108 Branchbran 6.2% 3 / 48 Conditioncond 11.1% 2 / 18 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 41.6% 10 / 24 Podpod 7.1% 1 / 14 totaltotal 25.3% 56 / 221 CC 33 SCAR 33.7
CC
33
Cov
25%
set_luminance
90.0
get_rgb
56.0
bg
30.0
CRAP 29.2
↑ Summary termcolor/XTerm.pm »
1 =encoding utf-8
2  
3 =head1 NAME
4  
5 Getopt::EX::termcolor - Getopt::EX termcolor module
6  
7 =head1 SYNOPSIS
8  
9 use Getopt::EX::Loader;
10 my $rcloader = Getopt::EX::Loader->new(
11 BASECLASS => [ 'App::command', 'Getopt::EX' ],
12 );
13  
14 or
15  
16 use Getopt::EX::Long qw(:DEFAULT ExConfigure);
17 ExConfigure BASECLASS => [ "App::command", "Getopt::EX" ];
18  
19 then
20  
21 $ command -Mtermcolor::bg=
22  
23 =head1 VERSION
24  
25 Version 1.10
26  
27 =head1 DESCRIPTION
28  
29 This is a common module for command using L<Getopt::EX> to manipulate
30 system dependent terminal color.
31  
32 Actual action is done by sub-module under L<Getopt::EX::termcolor>,
33 such as L<Getopt::EX::termcolor::Apple_Terminal>.
34  
35 Each sub-module is expected to have C<&get_color> function which
36 return the list of RGB values for requested name, but currently name
37 C<background> is only supported. Each RGB values are expected in a
38 range of 0 to 255 by default. If the first entry of the list is a
39 HASH reference, it may include maximum number indication like C<< {
40 max => 65535 } >>.
41  
42 Terminal luminance is calculated from RGB values by this equation and
43 produces decimal value from 0 to 100.
44  
45 ( 30 * R + 59 * G + 11 * B ) / MAX
46  
47 =begin comment
48  
49 If the environment variable C<TERM_LUMINANCE> is defined, its value is
50 used as a luminance without calling sub-modules. The value of
51 C<TERM_LUMINANCE> is expected in range of 0 to 100.
52  
53 =end comment
54  
55 If the environment variable C<TERM_BGCOLOR> is defined, it is used as
56 a background RGB value without calling sub-modules. RGB value is a
57 combination of integer described in 24bit/12bit hex, 24bit decimal or
58 6x6x6 216 color format. RGB color notation is compatible with
59 L<Getopt::EX::Colormap>.
60  
61 24bit hex #000000 .. #FFFFFF
62 12bit hex #000 .. #FFF
63 24bit decimal 0,0,0 .. 255,255,255
64 6x6x6 216 000 .. 555
65  
66 You can set C<TERM_BGCOLOR> in you start up file of shell. This
67 module has utility function C<bgcolor> which can be used like this:
68  
69 export TERM_BGCOLOR=`perl -MGetopt::EX::termcolor=bgcolor -e bgcolor`
70 : ${TERM_BGCOLOR:=#FFFFFF}
71  
72 =head1 MODULE FUNCTION
73  
74 =over 7
75  
76 =item B<bg>
77  
78 Call this function with module option:
79  
80 $ command -Mtermcolor::bg=
81  
82 If the terminal luminance is unknown, nothing happens. Otherwise, the
83 module insert B<--light-terminal> or B<--dark-terminal> option
84 according to the luminance value.
85  
86 You can change the behavior by optional parameters:
87  
88 threshold : threshold of light/dark (default 50)
89 default : default luminance value (default none)
90 light : light terminal option (default "--light-terminal")
91 dark : dark terminal option (default "--dark-terminal")
92  
93 Use like this:
94  
95 option default \
96 -Mtermcolor::bg(default=100,light=--light,dark=--dark)
97  
98 =back
99  
100 =head1 SEE ALSO
101  
102 L<Getopt::EX>
103  
104 L<Getopt::EX::termcolor::Apple_Terminal>
105  
106 L<Getopt::EX::termcolor::iTerm>
107  
108 L<Getopt::EX::termcolor::XTerm>
109  
110 =head1 AUTHOR
111  
112 Kazumasa Utashiro
113  
114 =head1 LICENSE
115  
116 Copyright 2020-2026 Kazumasa Utashiro.
117  
118 You can redistribute it and/or modify it under the same terms
119 as Perl itself.
120  
121 =cut
122  
123 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 package Getopt::EX::termcolor;
125  
126 2 our $VERSION = '1.10';
127  
128 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
133 2 our @EXPORT = qw();
134 2 our @EXPORT_OK = qw(rgb_to_luminance rgb_to_brightness luminance bgcolor);
135 2 our %EXPORT_TAGS = (all => [ @EXPORT_OK ]);
136  
137 #
138 # For backward compatibility.
139 #
140 0 ▶sub rgb_to_brightness {
Subroutine rgb_to_brightness: not called CC=1 SCAR=6.9
Pod undocumented
141 0 goto &rgb_to_luminance;
142 }
143  
144 15 ▶sub rgb_to_luminance {
Subroutine rgb_to_luminance: called CC=4 SCAR=13.9
Pod undocumented
145 15 ▶ @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return 15 0 15
146 15 ▶ my $opt = ref $_[0] ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 10 5 15
147 15 ▶ my $max = $opt->{max} || 255;
Condition100%2/2
$opt->{'max'} || 255
l!l
105
MC/DC100%1/1
$opt->{'max'} || 255
$opt->{'max'}
Truth table100%2/2
A
A= $opt->{'max'}
Aresult
11
00
148 15 my($r, $g, $b) = @_;
149 2 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 15 ($r * 30 + $g * 59 + $b * 11) / $max; # 0 .. 100
151 }
152  
153 2 my $mod;
154 my $argv;
155  
156 0 ▶sub initialize {
Subroutine initialize: not called CC=1 SCAR=6.9
Pod undocumented
157 0 ($mod, $argv) = @_;
158 0 set_luminance();
159 }
160  
161 2 our $debug = 0;
162  
163 0 ▶sub debug {
Subroutine debug: not called CC=1 SCAR=6.9
Pod undocumented
164 0 $debug ^= 1;
165 }
166  
167 0 ▶sub call_mod_sub {
Subroutine call_mod_sub: not called CC=4 SCAR=25.3
Pod undocumented
168 0 my($mod, $name, @arg) = @_;
169 0 my $call = "$mod\::$name";
170 0 ▶ if (eval "require $mod" and defined &$call) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require $mod" and defined &$call) { } 0 0 0
Condition0%0/3
eval "require $mod" and defined &$call
!ll&&!rl&&r
000
MC/DC0%0/2
eval "require $mod" and defined &$call
eval "require $mod" defined &$call
Truth table0%0/3
A and B
A= eval "require $mod"B= defined &$call
ABresult
0X0
100
111
171 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
172 0 $call->(@arg);
173 } else {
174 0 ▶ if ($@ !~ /^Can't locate /) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ =~ /^Can't locate /u) 0 0 0
175 0 croak $@;
176 }
177 }
178 }
179  
180 0 ▶sub rgb255 {
Subroutine rgb255: not called CC=3 SCAR=18.7
Pod undocumented
181 2 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
182 0 ▶ my $opt = ref $_[0] ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
183 0 ▶ my $max = $opt->{max} // 255;
Condition0%0/2
$opt->{"max"} // 255
l!l
00
MC/DC0%0/1
$opt->{"max"} // 255
$opt->{"max"}
Truth table0%0/2
A
A= $opt->{"max"}
Aresult
11
00
184 0 map { $_ * 255 / $max } @_;
185 }
186  
187 0 ▶sub get_rgb {
Subroutine get_rgb: not called CC=7 SCAR=40.3
Pod undocumented
188 0 my $cat = shift;
189 0 my @rgb;
190 RGB:
191 {
192 # TERM=xterm
193 0 ▶ if (($ENV{TERM} // '') =~ /^xterm/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($ENV{"TERM"} // "") =~ /^xterm/u) 0 0 0
Condition0%0/2
$ENV{"TERM"} // ""
l!l
00
MC/DC0%0/1
$ENV{"TERM"} // ""
$ENV{"TERM"}
Truth table0%0/2
A
A= $ENV{"TERM"}
Aresult
11
00
194 0 my $mod = __PACKAGE__ . "::XTerm";
195 0 @rgb = call_mod_sub $mod, 'get_color', $cat;
196 0 ▶ last if @rgb >= 3;
Branch: 1 branch
BranchTrueFalseTotal
if @rgb >= 3 0 0 0
197 }
198 # TERM_PROGRAM
199 0 ▶ if (my $term_program = $ENV{TERM_PROGRAM}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $term_program = $ENV{"TERM_PROGRAM"}) 0 0 0
200 0 ▶ warn "TERM_PROGRAM=$ENV{TERM_PROGRAM}\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Getopt::EX::termcolor::debug 0 0 0
201 0 my $submod = $term_program =~ s/\.app$//r;
202 0 my $mod = __PACKAGE__ . "::$submod";
203 0 @rgb = call_mod_sub $mod, 'get_color', $cat;
204 0 ▶ last if @rgb >= 3;
Branch: 1 branch
BranchTrueFalseTotal
if @rgb >= 3 0 0 0
205 }
206 0 return ();
207 }
208 GOTCHA:
209 0 rgb255 @rgb;
210 }
211  
212 0 ▶sub set_luminance {
Subroutine set_luminance: not called CC=9 SCAR=45.0
Pod undocumented
213 0 my $luminance;
214 0 ▶ if (defined $ENV{TERM_LUMINANCE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"TERM_LUMINANCE"}) 0 0 0
215 0 ▶ warn "TERM_LUMINANCE=$ENV{TERM_LUMINANCE}\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Getopt::EX::termcolor::debug 0 0 0
216 0 return;
217 }
218 0 if ("BACKWARD COMPATIBILITY") {
219 0 ▶ if (defined (my $env = $ENV{BRIGHTNESS})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $env = $ENV{"BRIGHTNESS"})) 0 0 0
220 0 ▶ warn "BRIGHTNESS=$env\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Getopt::EX::termcolor::debug 0 0 0
221 0 $ENV{TERM_LUMINANCE} = $env;
222 0 return;
223 }
224 }
225 0 ▶ if (my $bgcolor = $ENV{TERM_BGCOLOR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bgcolor = $ENV{'TERM_BGCOLOR'}) { } 0 0 0
226 0 ▶ warn "TERM_BGCOLOR=$bgcolor\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Getopt::EX::termcolor::debug 0 0 0
227 0 ▶ if (my @rgb = parse_rgb($bgcolor)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rgb) = parse_rgb($bgcolor)) { } 0 0 0
228 0 $luminance = rgb_to_luminance @rgb;
229 } else {
230 0 warn "Invalid format: TERM_BGCOLOR=$bgcolor\n";
231 }
232 } else {
233 0 $luminance = get_luminance();
234 }
235 0 ▶ $ENV{TERM_LUMINANCE} = $luminance // return;
Condition0%0/2
$luminance // (return)
l!l
00
MC/DC0%0/1
$luminance // (return)
$luminance
Truth table0%0/2
A
A= $luminance
Aresult
11
00
236 }
237  
238 0 ▶sub get_luminance {
Subroutine get_luminance: not called CC=1 SCAR=6.9
Pod undocumented
239 0 rgb_to_luminance get_rgb "background";
240 }
241  
242 2 ▶use List::Util qw(pairgrep);
Subroutine BEGIN: called CC=1 SCAR=0.0
243  
244 #
245 # FOR BACKWARD COMPATIBILITY
246 # DEPELICATED IN THE FUTURE
247 #
248 0 ▶sub set { goto &bg }
Subroutine set: not called CC=1 SCAR=6.9
Pod undocumented
249  
250 2 my %bg_param = (
251 light => "--light-terminal",
252 dark => "--dark-terminal",
253 default => undef,
254 threshold => 50,
255 );
256  
257 0 ▶sub bg {
Subroutine bg: not called CC=5 SCAR=34.0
Pod documented
258 my %param =
259 0 ▶ (%bg_param, pairgrep { exists $bg_param{$a} } @_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
260 my $luminance =
261 0 ▶ $ENV{TERM_LUMINANCE} // $param{default} // return;
Condition0%0/5
($ENV{"TERM_LUMINANCE"} // $param{"default"}) // (return)
l!l
00
$ENV{"TERM_LUMINANCE"} // $param{"default"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($ENV{"TERM_LUMINANCE"} // $param{"default"}) // (return)
$ENV{"TERM_LUMINANCE"} $param{"default"}
Truth table0%0/3
A // B
A= $ENV{"TERM_LUMINANCE"}B= $param{"default"}
ABresult
1X1
011
000
262 my $option = $luminance > $param{threshold} ?
263 $param{light} : $param{dark}
264 0 ▶ or return;
Branch: 2 branches
BranchTrueFalseTotal
my $option = $luminance > $param{"threshold"} ? $param{"light"} : $param{"dark"} or return 0 0 0
$luminance > $param{'threshold'} ? : 0 0 0
265  
266 # $mod->setopt($option => '$<ignore>');
267 0 $mod->setopt(default => $option);
268 }
269  
270 0 ▶sub parse_rgb {
Subroutine parse_rgb: not called CC=5 SCAR=34.0
Pod undocumented
271 0 my $rgb = shift;
272 0 my @rgb = do {
273 0 ▶ if ($rgb =~ /^\#?([\da-f]{2})([\da-f]{2})([\da-f]{2})$/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($rgb =~ /^\#?([\da-f]{2})([\da-f]{2})([\da-f]{2})$/iu) { } 0 0 0
elsif ($rgb =~ /^\#([\da-f])([\da-f])([\da-f])$/iu) { } 0 0 0
elsif ($rgb =~ /^([0-5])([0-5])([0-5])$/u) { } 0 0 0
elsif ($rgb =~ /^(\d+),(\d+),(\d+)$/u) { } 0 0 0
274 0 map { hex } $1, $2, $3;
275 }
276 elsif ($rgb =~ /^\#([\da-f])([\da-f])([\da-f])$/i) {
277 0 map { 0x11 * hex } $1, $2, $3;
278 }
279 elsif ($rgb =~ /^([0-5])([0-5])([0-5])$/) {
280 0 map { 0x33 * int } $1, $2, $3;
281 }
282 elsif ($rgb =~ /^(\d+),(\d+),(\d+)$/) {
283 0 map { int } $1, $2, $3;
284 }
285 else {
286 0 return ();
287 }
288 };
289 0 @rgb;
290 }
291  
292 0 ▶sub luminance {
Subroutine luminance: not called CC=2 SCAR=17.9
Pod undocumented
293 0 ▶ my $v = get_luminance() // return;
Condition0%0/2
get_luminance() // (return)
l!l
00
MC/DC0%0/1
get_luminance() // (return)
get_luminance()
Truth table0%0/2
A
A= get_luminance()
Aresult
11
00
294 0 say $v;
295 }
296  
297 0 ▶sub bgcolor {
Subroutine bgcolor: not called CC=2 SCAR=17.9
Pod undocumented
298 0 ▶ my @rgb = get_rgb "background" or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@rgb) = get_rgb("background") or return 0 0 0
299 0 printf "#%02X%02X%02X\n", @rgb;
300 }
301  
302 2 1;
303  
304 __DATA__
↑ Summary termcolor/XTerm.pm »