line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=encoding utf-8 |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Getopt::EX::autocolor - Getopt::EX autocolor module |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Getopt::EX::Loader; |
10
|
|
|
|
|
|
|
my $rcloader = new Getopt::EX::Loader |
11
|
|
|
|
|
|
|
BASECLASS => [ 'App::command', 'Getopt::EX' ]; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
$ command -Mautocolor |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 VERSION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Version 0.04 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 DESCRIPTION |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
This is a common module for command using L to set system |
22
|
|
|
|
|
|
|
dependent autocolor option. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Actual action is done by sub-module under L, |
25
|
|
|
|
|
|
|
such as L. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Each sub-module is expected to have C<&brightness> function which |
28
|
|
|
|
|
|
|
returns integer value between 0 and 100. If the sub-module was found |
29
|
|
|
|
|
|
|
and C<&brightness> function exists, its result is taken as a |
30
|
|
|
|
|
|
|
brightness of the terminal. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
However, if the environment variable C is defined, its |
33
|
|
|
|
|
|
|
value is used as a brightness without calling sub-modules. The value |
34
|
|
|
|
|
|
|
of C is expected in range of 0 to 100. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
If the brightness can not be taken, nothing happens. Otherwise, the |
37
|
|
|
|
|
|
|
module insert B<--light-terminal> or B<--dark-terminal> option |
38
|
|
|
|
|
|
|
according to the brightness value. These options are defined as |
39
|
|
|
|
|
|
|
C$ in this module and do nothing. They can be overridden |
40
|
|
|
|
|
|
|
by other module or user definition. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
You can change the behavior of this module by calling C<&set> function |
43
|
|
|
|
|
|
|
with module option. It takes some parameters and they override |
44
|
|
|
|
|
|
|
default values. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
threshold : threshold of light/dark (default 50) |
47
|
|
|
|
|
|
|
default : default brightness value (default none) |
48
|
|
|
|
|
|
|
light : light terminal option (default "--light-terminal") |
49
|
|
|
|
|
|
|
dark : dark terminal option (default "--dark-terminal") |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
For example, use like: |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
option default \ |
54
|
|
|
|
|
|
|
-Mautocolor::set(default=100,light=--light,dark=--dark) |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 FUNCTIONS |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=over 7 |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item B |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
This exportable function caliculates brightness (luminane) from RGB |
63
|
|
|
|
|
|
|
values. It accepts three parameters of 0 to 65535 integer. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Maximum value can be specified by optional hash argument. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
rgb_to_brightness( { max => 255 }, 255, 255, 255); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Brightness is caliculated from RGB values by this equation. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Y = 0.30 * R + 0.59 * G + 0.11 * B |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=back |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 SEE ALSO |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
L |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
L |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
L |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 AUTHOR |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Kazumasa Utashiro |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 LICENSE |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Copyright (C) 2020 Kazumasa Utashiro. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
You can redistribute it and/or modify it under the same terms |
92
|
|
|
|
|
|
|
as Perl itself. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=cut |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
package Getopt::EX::autocolor; |
97
|
|
|
|
|
|
|
|
98
|
2
|
|
|
2
|
|
69738
|
use v5.14; |
|
2
|
|
|
|
|
18
|
|
99
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
40
|
|
100
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
47
|
|
101
|
2
|
|
|
2
|
|
1324
|
use Data::Dumper; |
|
2
|
|
|
|
|
13760
|
|
|
2
|
|
|
|
|
153
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
our $VERSION = "0.04"; |
104
|
|
|
|
|
|
|
|
105
|
2
|
|
|
2
|
|
16
|
use Exporter 'import'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
732
|
|
106
|
|
|
|
|
|
|
our @EXPORT = qw(); |
107
|
|
|
|
|
|
|
our %EXPORT_TAGS = (); |
108
|
|
|
|
|
|
|
our @EXPORT_OK = qw(rgb_to_brightness); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
our %param = ( |
111
|
|
|
|
|
|
|
light => "--light-terminal", |
112
|
|
|
|
|
|
|
dark => "--dark-terminal", |
113
|
|
|
|
|
|
|
default => undef, |
114
|
|
|
|
|
|
|
threshold => 50, |
115
|
|
|
|
|
|
|
); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub rgb_to_brightness { |
118
|
10
|
100
|
|
10
|
1
|
120
|
my $opt = ref $_[0] ? shift : {}; |
119
|
10
|
|
100
|
|
|
36
|
my $max = $opt->{max} || 65535; |
120
|
10
|
|
|
|
|
24
|
my($r, $g, $b) = @_; |
121
|
10
|
|
|
|
|
55
|
int(($r * 30 + $g * 59 + $b * 11) / $max); # 0 .. 100 |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
0
|
0
|
|
sub call(&@) { $_[0]->(@_[1..$#_]) } |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub finalize { |
127
|
0
|
|
|
0
|
0
|
|
my $mod = shift; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# default to do nothing. |
130
|
0
|
|
|
|
|
|
$mod->setopt($param{light} => '$'); |
131
|
0
|
|
|
|
|
|
$mod->setopt($param{dark} => '$'); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
my $brightness = call { |
134
|
0
|
|
|
0
|
|
|
my $v = $ENV{BRIGHTNESS}; |
135
|
0
|
0
|
0
|
|
|
|
if (defined $v && $v =~ /^\d+$/ |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
136
|
|
|
|
|
|
|
&& 0 <= $v && $v <= 100 |
137
|
|
|
|
|
|
|
) { |
138
|
0
|
|
|
|
|
|
return $v; |
139
|
|
|
|
|
|
|
} |
140
|
0
|
0
|
|
|
|
|
if (my $term_program = $ENV{TERM_PROGRAM}) { |
141
|
0
|
|
|
|
|
|
my $submod = $term_program =~ s/\.app$//r; |
142
|
0
|
|
|
|
|
|
my $mod = __PACKAGE__ . "::$submod"; |
143
|
0
|
|
|
|
|
|
my $brightness = "$mod\::brightness"; |
144
|
2
|
|
|
2
|
|
16
|
no strict 'refs'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
396
|
|
145
|
0
|
0
|
0
|
|
|
|
if (eval "require $mod" and defined &$brightness) { |
146
|
0
|
|
|
|
|
|
my $v = &$brightness; |
147
|
0
|
0
|
0
|
|
|
|
if (0 <= $v and $v <= 100) { |
148
|
0
|
|
|
|
|
|
return $v; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
0
|
|
|
|
|
|
undef; |
153
|
0
|
|
|
|
|
|
}; |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
0
|
|
|
|
$brightness //= $param{default} // return; |
|
|
|
0
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
$mod->setopt(default => |
158
|
|
|
|
|
|
|
$brightness > $param{threshold} |
159
|
|
|
|
|
|
|
? $param{light} |
160
|
0
|
0
|
|
|
|
|
: $param{dark}); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
2
|
|
|
2
|
|
17
|
use List::Util qw(pairgrep); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
421
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub set { |
166
|
0
|
|
|
0
|
0
|
|
my %arg = @_; |
167
|
0
|
|
|
0
|
|
|
%param = (%param, pairgrep { exists $param{$a} } %arg); |
|
0
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
1; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
__DATA__ |