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.03 |
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 the first hash argument. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
rgb_to_brightness( { max => 255 }, 255, 255, 255); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Brightness is caliculated from RGB values by next 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
|
1
|
|
|
1
|
|
764
|
use v5.14; |
|
1
|
|
|
|
|
4
|
|
99
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
21
|
|
100
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
101
|
1
|
|
|
1
|
|
614
|
use Data::Dumper; |
|
1
|
|
|
|
|
6861
|
|
|
1
|
|
|
|
|
81
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
our $VERSION = "0.03"; |
104
|
|
|
|
|
|
|
|
105
|
1
|
|
|
1
|
|
9
|
use Exporter 'import'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
336
|
|
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
|
0
|
0
|
|
0
|
1
|
|
my %opt = ref $_[0] ? shift : {}; |
119
|
0
|
|
0
|
|
|
|
my $max = $opt{max} || 65535; |
120
|
0
|
|
|
|
|
|
my($r, $g, $b) = @_; |
121
|
0
|
|
|
|
|
|
int(($r * 30 + $g * 59 + $b * 11) / $max); # 0 .. 100 |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my %TERM_PROGRAM = qw( |
125
|
|
|
|
|
|
|
Apple_Terminal Apple_Terminal |
126
|
|
|
|
|
|
|
iTerm.app iTerm |
127
|
|
|
|
|
|
|
); |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
0
|
0
|
|
sub call(&@) { $_[0]->(@_[1..$#_]) } |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub finalize { |
132
|
0
|
|
|
0
|
0
|
|
my $mod = shift; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# default to do nothing. |
135
|
0
|
|
|
|
|
|
$mod->setopt($param{light} => '$'); |
136
|
0
|
|
|
|
|
|
$mod->setopt($param{dark} => '$'); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
my $brightness = call { |
139
|
0
|
|
|
0
|
|
|
my $v = $ENV{BRIGHTNESS}; |
140
|
0
|
0
|
0
|
|
|
|
if (defined $v && $v =~ /^\d+$/ |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
141
|
|
|
|
|
|
|
&& 0 <= $v && $v <= 100 |
142
|
|
|
|
|
|
|
) { |
143
|
0
|
|
|
|
|
|
return $v; |
144
|
|
|
|
|
|
|
} |
145
|
0
|
0
|
|
|
|
|
if (my $term_program = $ENV{TERM_PROGRAM}) { |
146
|
0
|
|
|
|
|
|
my $mod = __PACKAGE__ . "::$term_program"; |
147
|
0
|
|
|
|
|
|
my $brightness = "$mod\::brightness"; |
148
|
1
|
|
|
1
|
|
10
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
191
|
|
149
|
0
|
0
|
0
|
|
|
|
if (eval "require $mod" and defined &$brightness) { |
150
|
0
|
|
|
|
|
|
my $v = &$brightness; |
151
|
0
|
0
|
0
|
|
|
|
if (0 <= $v and $v <= 100) { |
152
|
0
|
|
|
|
|
|
return $v; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
} |
156
|
0
|
|
|
|
|
|
undef; |
157
|
0
|
|
|
|
|
|
}; |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
0
|
|
|
|
$brightness //= $param{default} // return; |
|
|
|
0
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
$mod->setopt(default => |
162
|
|
|
|
|
|
|
$brightness > $param{threshold} |
163
|
|
|
|
|
|
|
? $param{light} |
164
|
0
|
0
|
|
|
|
|
: $param{dark}); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
1
|
|
|
1
|
|
8
|
use List::Util qw(pairgrep); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
182
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub set { |
170
|
0
|
|
|
0
|
0
|
|
my %arg = @_; |
171
|
0
|
|
|
0
|
|
|
%param = (%param, pairgrep { exists $param{$a} } %arg); |
|
0
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
1; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
__DATA__ |