blib/lib/Term/Spinner/Color.pm

filter: Statementstmt 19.6% 30 / 153 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 40.9% 9 / 22 Podpod 46.1% 6 / 13 totaltotal 18.9% 45 / 237 CC 25 SCAR 35.3
CC
25
Cov
15%
run_ok
110.0
new
42.0
auto_start
20.0
CRAP 34.2
↑ Summary
1 package Term::Spinner::Color;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Time::HiRes qw( sleep );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Term::Cap;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use open ':std', ':encoding(UTF-8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 $| = 1; # Disable buffering on STDOUT.
13  
14 # Couple of instance vars for colors and frame sets
15 1 my @colors = qw( red green yellow blue magenta cyan white );
16 1 my %frames = (
17 'ascii_propeller' => [qw(/ - \\ |)],
18 'ascii_plus' => [qw(x +)],
19 'ascii_blink' => [qw(o -)],
20 'ascii_v' => [qw(v < ^ >)],
21 'ascii_inflate' => [qw(. o O o)],
22 'uni_dots' => [qw(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏)],
23 'uni_dots2' => [qw(⣾ ⣽ ⣻ ⢿ ⡿ ⣟ ⣯ ⣷)],
24 'uni_dots3' => [qw(⣷ ⣯ ⣟ ⡿ ⢿ ⣻ ⣽ ⣾)],
25 'uni_dots4' => [qw(⠋ ⠙ ⠚ ⠞ ⠖ ⠦ ⠴ ⠲ ⠳ ⠓)],
26 'uni_dots5' => [qw(⠄ ⠆ ⠇ ⠋ ⠙ ⠸ ⠰ ⠠ ⠰ ⠸ ⠙ ⠋ ⠇ ⠆)],
27 'uni_dots6' => [qw(⠋ ⠙ ⠚ ⠒ ⠂ ⠂ ⠒ ⠲ ⠴ ⠦ ⠖ ⠒ ⠐ ⠐ ⠒ ⠓ ⠋')],
28 'uni_dots7' => [qw(⠁ ⠉ ⠙ ⠚ ⠒ ⠂ ⠂ ⠒ ⠲ ⠴ ⠤ ⠄ ⠄ ⠤ ⠴ ⠲ ⠒ ⠂ ⠂ ⠒ ⠚ ⠙ ⠉ ⠁)],
29 'uni_dots8' => [qw(⠈ ⠉ ⠋ ⠓ ⠒ ⠐ ⠐ ⠒ ⠖ ⠦ ⠤ ⠠ ⠠ ⠤ ⠦ ⠖ ⠒ ⠐ ⠐ ⠒ ⠓ ⠋ ⠉ ⠈)],
30 'uni_dots9' =>
31 [qw(⠁ ⠁ ⠉ ⠙ ⠚ ⠒ ⠂ ⠂ ⠒ ⠲ ⠴ ⠤ ⠄ ⠄ ⠤ ⠠ ⠠ ⠤ ⠦ ⠖ ⠒ ⠐ ⠐ ⠒ ⠓ ⠋ ⠉ ⠈ ⠈)],
32 'uni_dots10' => [qw(⢹ ⢺ ⢼ ⣸ ⣇ ⡧ ⡗ ⡏)],
33 'uni_dots11' => [qw(⢄ ⢂ ⢁ ⡁ ⡈ ⡐ ⡠)],
34 'uni_dots12' => [qw(⠁ ⠂ ⠄ ⡀ ⢀ ⠠ ⠐ ⠈)],
35 'uni_bounce' => [qw(⠁ ⠂ ⠄ ⠂)],
36 'uni_pipes' => [qw(┤ ┘ ┴ └ ├ ┌ ┬ ┐)],
37 'uni_hippie' => [qw(☮ ✌ ☺ ♥)],
38 'uni_hands' => [qw(☜ ☝ ☞ ☟)],
39 'uni_arrow_rot' => [qw(➫ ➭ ➬ ➭)],
40 'uni_cards' => [qw(♣ ♤ ♥ ♦)],
41 'uni_triangle' => [qw(◢ ◣ ◤ ◥)],
42 'uni_square' => [qw(◰ ◳ ◲ ◱)],
43 'uni_pie' => [qw(◴ ◷ ◶ ◵)],
44 'uni_circle' => [qw(◐ ◓ ◑ ◒)],
45 'uni_qtr_circle' => [qw(◜ ◝ ◞ ◟)],
46 'uni_three_lines' => [qw(⚞ ☰ ⚟ ☰)],
47 'uni_trigram_down' => [qw(☰ ☱ ☲ ☴)],
48 'uni_trigram_bounce' => [qw(☰ ☱ ☲ ☴ ☰ ☴ ☲ ☱)],
49 'uni_count' => [qw(➀ ➁ ➂ ➃ ➄ ➅ ➆ ➇ ➈ ➉)],
50 'uni_ellipsis_propeller' => [qw(⋮ ⋰ ⋯ ⋱)],
51 'uni_earth' => [qw(🌍 🌏 🌎)],
52 'uni_moon' => [qw(🌑 🌘 🌗 🌖 🌕 🌔 🌒)],
53 'uni_junk_food' => [qw(🌭 🌮 🌯 🍔 🍕 🍟)],
54 'uni_clapping' => [qw(👐 👏)],
55 'uni_diamonds' => [qw(🔹 🔷 🔸 🔶)],
56 'wide_ascii_prog' => [
57 qw([>----] [=>---] [==>--] [===>-] [====>] [----<] [---<=] [--<==] [-<===] [<====])
58 ],
59 'wide_ascii_propeller' =>
60 [qw([|----] [=/---] [==---] [===\-] [====|] [---\=] [---==] [-/===])],
61 'wide_ascii_snek' => [
62 qw([>----] [~>---] [~~>--] [~~~>-] [~~~~>] [----<] [---<~] [--<~~] [-<~~~] [<~~~~])
63 ],
64 'wide_uni_greyscale' => [
65 qw(▒▒▒▒▒▒▒ █▒▒▒▒▒▒ ██▒▒▒▒▒ ███▒▒▒▒ ████▒▒▒ █████▒▒ ██████▒ ███████ ██████▒ █████▒▒ ████▒▒▒ ███▒▒▒▒ ██▒▒▒▒▒ █▒▒▒▒▒▒ ▒▒▒▒▒▒▒)
66 ],
67 'wide_uni_greyscale2' => [
68 qw(▒▒▒▒▒▒▒ █▒▒▒▒▒▒ ██▒▒▒▒▒ ███▒▒▒▒ ████▒▒▒ █████▒▒ ██████▒ ███████ ▒██████ ▒▒█████ ▒▒▒████ ▒▒▒▒███ ▒▒▒▒▒██ ▒▒▒▒▒█ ▒▒▒▒▒▒)
69 ],
70 );
71  
72 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
73 0 my ($class, %args) = @_;
74 0 my $self = {};
75  
76 # seq can either be an array ref with a whole set of frames, or can be the
77 # name of a frame set.
78 0 ▶ if (!defined($args{'seq'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $args{'seq'}) { } 0 0 0
elsif (ref $args{'seq'} ne 'ARRAY') { } 0 0 0
79 0 $self->{'seq'} = $frames{'wide_uni_greyscale2'};
80 }
81 elsif (ref($args{'seq'}) ne 'ARRAY') {
82 0 $self->{'seq'} = $frames{$args{'seq'}};
83 }
84 else {
85 0 $self->{'seq'} = $args{'seq'};
86 }
87  
88 0 ▶ $self->{'delay'} = $args{'delay'} || 0.2;
Condition0%0/2
$args{'delay'} || 0.2
l!l
00
MC/DC0%0/1
$args{'delay'} || 0.2
$args{'delay'}
Truth table0%0/2
A
A= $args{'delay'}
Aresult
11
00
89 0 ▶ $self->{'color'} = $args{'color'} || 'cyan';
Condition0%0/2
$args{'color'} || 'cyan'
l!l
00
MC/DC0%0/1
$args{'color'} || 'cyan'
$args{'color'}
Truth table0%0/2
A
A= $args{'color'}
Aresult
11
00
90 0 ▶ $self->{'colorcycle'} = $args{'colorcycle'} || 0;
Condition0%0/2
$args{'colorcycle'} || 0
l!l
00
MC/DC0%0/1
$args{'colorcycle'} || 0
$args{'colorcycle'}
Truth table0%0/2
A
A= $args{'colorcycle'}
Aresult
11
00
91 0 $self->{'bksp'} = chr(0x08);
92 0 $self->{'last_size'} = length($self->{'seq'}[0]);
93 0 return bless $self, $class;
94 }
95  
96 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
97 0 my $self = shift;
98 0 print "\x1b[?25l"; # Hide cursor
99 0 $self->{'last_size'} = length($self->{'seq'}[0]);
100 0 print colored("$self->{'seq'}[0]", $self->{'color'});
101 }
102  
103 0 ▶sub next {
Subroutine next: not called CC=2 SCAR=17.9
Pod documented
104 0 my $self = shift;
105 0 state $pos = 1;
106  
107 0 ▶ if ($self->{'colorcycle'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"colorcycle"}) 0 0 0
108 0 push @colors, shift @colors; # rotate the colors list
109 0 $self->{'color'} = $colors[0];
110 }
111  
112 0 print $self->{'bksp'} x $self->{'last_size'};
113 0 print colored("$self->{'seq'}[$pos]", $self->{'color'});
114  
115 0 $pos = ++$pos % scalar @{$self->{'seq'}};
116 0 $self->{'last_size'} = length($self->{'seq'}[$pos]);
117 }
118  
119 0 ▶sub done {
Subroutine done: not called CC=1 SCAR=6.9
Pod documented
120 0 my $self = shift;
121  
122 0 print $self->{'bksp'} x $self->{'last_size'};
123 0 print "\x1b[?25h"; # Show cursor
124 }
125  
126 # Fork and run spinner asynchronously, until signal received.
127 0 ▶sub auto_start {
Subroutine auto_start: not called CC=4 SCAR=30.0
Pod documented
128 0 my $self = shift;
129  
130 0 my $ppid = $$;
131 0 my $pid = fork();
132 0 ▶ die("Failed to fork progress indicator.\n") unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
133  
134 0 ▶ if ($pid) { # Parent
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
135 0 $self->{'child'} = $pid;
136 0 return;
137 }
138 else { # Kid stuff
139 0 $self->start();
140 0 my $exists;
141 0 while (1) {
142 0 sleep $self->{'delay'};
143 0 $self->next();
144  
145 # Check to be sure parent is still running, if not, die
146 0 $exists = kill 0, $ppid;
147 0 ▶ unless ($exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exists) 0 0 0
148 0 $self->done();
149 0 exit 0;
150 }
151 0 $exists = "";
152 }
153 0 exit 0; # Should never get here?
154 }
155 }
156  
157 0 ▶sub auto_done {
Subroutine auto_done: not called CC=1 SCAR=6.9
Pod documented
158 0 my $self = shift;
159  
160 0 kill 'KILL', $self->{'child'};
161 0 my $pid = wait();
162 0 $self->done();
163 }
164  
165 # Run, OK? Does a thing, or a list of things. usually long-running things,
166 # runs a spinner, and prints a nice status message (check or X, whether
167 # success or err), when done.
168 0 ▶sub run_ok {
Subroutine run_ok: not called CC=10 SCAR=47.0
Pod documented
169 0 my $self = shift;
170 0 my $exp = shift; # A list of functions to call and wait on.
171 0 my $message = shift; # String to print before the spinner
172 0 my $termwidth = `tput cols`;
173 0 ▶ $termwidth = 80 unless $termwidth <= 80;
Branch: 1 branch
BranchTrueFalseTotal
unless $termwidth <= 80 0 0 0
174 0 my $cols = $termwidth - length($message) - $self->{'last_size'} - 1;
175 0 my ($ok, $nok, $meh);
176 0 ▶ if ($self->{'last_size'} == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'last_size'} == 1) { } 0 0 0
elsif ($self->{'last_size'} == 3) { } 0 0 0
elsif ($self->{'last_size'} == 5) { } 0 0 0
177 0 $ok = colored("✔", 'green');
178 0 $nok = colored("✘", 'red');
179 0 $meh = colored("⚠", 'yellow');
180 }
181 elsif ($self->{'last_size'} == 3) {
182 0 $ok = colored("[✔]", 'white on_green');
183 0 $nok = colored("[✘]", 'white on_red');
184 0 $meh = colored("[⚠]", 'white on_yellow');
185 }
186 elsif ($self->{'last_size'} == 5) {
187 0 $ok = colored("[ ✔ ]", 'white on_green');
188 0 $nok = colored("[ ✘ ]", 'white on_red');
189 0 $meh = colored("[ ⚠ ]", 'white on_yellow');
190 }
191 else { # Better be 7, or it'll look goofy, but still work
192 0 $ok = colored("[ ✔ ]", 'white on_green');
193 0 $nok = colored("[ ✘ ]", 'white on_red');
194 0 $meh = colored("[ ⚠ ]", 'white on_yellow');
195 }
196  
197 0 print $message;
198 0 print ' ' x $cols;
199 0 ▶ if (ref($exp) eq 'ARRAY') { # List of expressions
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exp eq 'ARRAY') { } 0 0 0
200 0 $self->start();
201 0 foreach my $exp (@{$exp}) {
202 0 $self->next();
203 0 my $res = eval $exp; ## no critic
204 0 ▶ if ($res == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res == 0) { } 0 0 0
elsif ($res == 2) { } 0 0 0
205 0 $self->done();
206 0 say $nok;
207 0 return 0;
208 }
209 elsif ($res == 2) { # Non-fatal error
210 0 $self->done();
211 0 say $meh;
212 0 return 2;
213 }
214 }
215 0 $self->done();
216 0 say $ok;
217 }
218 else { # Single expression
219 0 $self->auto_start();
220 0 my $res = eval $exp; ## no critic
221 0 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
222 0 $self->auto_done();
223 0 say $nok;
224 0 return 0;
225 }
226 0 $self->auto_done();
227 0 say $ok;
228 }
229 }
230  
231 # Return list of available frames
232 0 ▶sub available_frames {
Subroutine available_frames: not called CC=1 SCAR=6.9
Pod undocumented
233 0 my $self = shift;
234 0 return \%frames;
235 }
236  
237 0 ▶sub available_colors {
Subroutine available_colors: not called CC=1 SCAR=6.9
Pod undocumented
238 0 my $self = shift;
239 0 return @colors;
240 }
241  
242 # ok!
243 # Call this if you want a nice checkmark after you spinn
244 0 ▶sub ok {
Subroutine ok: not called CC=3 SCAR=24.8
Pod undocumented
245 0 my $self = shift;
246 0 my $ok;
247 0 ▶ if ($self->{'last_size'} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'last_size'} == 1) { } 0 0 0
elsif ($self->{'last_size'} == 5) { } 0 0 0
248 0 $ok = colored("✔", 'green');
249 }
250 elsif ($self->{'last_size'} == 5) {
251 0 $ok = colored("[ ✔ ]", 'white on_green');
252 }
253 else { # Better be 7, or it'll look goofy, but still work
254 0 $ok = colored("[ ✔ ]", 'white on_green');
255 }
256 0 say $ok;
257 }
258  
259 # meh
260 # call this to tell use that a non-fatal error occurred. prints a yellow ⚠
261 0 ▶sub meh {
Subroutine meh: not called CC=3 SCAR=24.8
Pod undocumented
262 0 my $self = shift;
263 0 my $meh;
264 0 ▶ if ($self->{'last_size'} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'last_size'} == 1) { } 0 0 0
elsif ($self->{'last_size'} == 5) { } 0 0 0
265 0 $meh = colored("⚠", 'yellow');
266 }
267 elsif ($self->{'last_size'} == 5) {
268 0 $meh = colored("[ ⚠ ]", 'white on_yellow');
269 }
270 else { # Better be 7, or it'll look goofy, but still work
271 0 $meh = colored("[ ⚠ ]", 'white on_yellow');
272 }
273 0 say $meh;
274 }
275  
276 # nok!
277 # call this to tell user everything is not ok. prints a red x
278 0 ▶sub nok {
Subroutine nok: not called CC=3 SCAR=24.8
Pod undocumented
279 0 my $self = shift;
280 0 my $nok;
281 0 ▶ if ($self->{'last_size'} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'last_size'} == 1) { } 0 0 0
elsif ($self->{'last_size'} == 5) { } 0 0 0
282 0 $nok = colored("✘", 'red');
283 }
284 elsif ($self->{'last_size'} == 5) {
285 0 $nok = colored("[ ✘ ]", 'white on_red');
286 }
287 else { # Better be 7, or it'll look goofy, but still work
288 0 $nok = colored("[ ✘ ]", 'white on_red');
289 }
290 0 say $nok;
291 }
292  
293 # Frame length of the first frame of a seq...this won't work if
294 # if the frames change size, but the rest of the spinnner will
295 # probably also screw up in that case.
296 0 ▶sub frame_length {
Subroutine frame_length: not called CC=1 SCAR=6.9
Pod undocumented
297 0 my $self = shift;
298 0 return length($self->{'seq'}[0]);
299 }
300  
301 1 1;
302  
303 __END__
↑ Summary