blib/lib/Term/Spinner/Color.pm

filter: Statementstmt 21.5% 30 / 139 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 42.8% 9 / 21 Podpod 50.0% 6 / 12 totaltotal 20.9% 45 / 215 CC 22 SCAR 33.9
CC
22
Cov
17%
run_ok
90.0
new
42.0
auto_start
20.0
CRAP 29.7
↑ 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 sub new {
73 0 ▶ my ( $class, %args ) = @_;
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
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 sub start {
97 0 ▶ my $self = shift;
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
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 sub next {
104 0 ▶ my $self = shift;
Subroutine next: not called CC=2 SCAR=17.9
Pod documented
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 sub done {
120 0 ▶ my $self = shift;
Subroutine done: not called CC=1 SCAR=6.9
Pod documented
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 sub auto_start {
128 0 ▶ my $self = shift;
Subroutine auto_start: not called CC=4 SCAR=30.0
Pod documented
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 sub auto_done {
158 0 ▶ my $self = shift;
Subroutine auto_done: not called CC=1 SCAR=6.9
Pod documented
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 sub run_ok {
169 0 ▶ my $self = shift;
Subroutine run_ok: not called CC=9 SCAR=45.0
Pod documented
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 );
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 }
180 elsif ( $self->{'last_size'} == 3 ) {
181 0 $ok = colored( "[✔]", 'white on_green' );
182 0 $nok = colored( "[✘]", 'white on_red' );
183 }
184 elsif ( $self->{'last_size'} == 5 ) {
185 0 $ok = colored( "[ ✔ ]", 'white on_green' );
186 0 $nok = colored( "[ ✘ ]", 'white on_red' );
187 }
188 else { # Better be 7, or it'll look goofy, but still work
189 0 $ok = colored( "[ ✔ ]", 'white on_green' );
190 0 $nok = colored( "[ ✘ ]", 'white on_red' );
191 }
192  
193 0 print $message;
194 0 print ' ' x $cols;
195 0 ▶ if ( ref($exp) eq 'ARRAY' ) { # List of expressions
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exp eq 'ARRAY') { } 0 0 0
196 0 $self->start();
197 0 foreach my $exp ( @{$exp} ) {
198 0 $self->next();
199 0 my $res = eval $exp; ## no critic
200 0 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
201 0 $self->done();
202 0 say $nok;
203 0 return 0;
204 }
205 }
206 0 $self->done();
207 0 say $ok;
208 }
209 else { # Single expression
210 0 $self->auto_start();
211 0 my $res = eval $exp; ## no critic
212 0 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
213 0 $self->auto_done();
214 0 say $nok;
215 0 return 0;
216 }
217 0 $self->auto_done();
218 0 say $ok;
219 }
220 }
221  
222 # Return list of available frames
223 sub available_frames {
224 0 ▶ my $self = shift;
Subroutine available_frames: not called CC=1 SCAR=6.9
Pod undocumented
225 0 return \%frames;
226 }
227  
228 sub available_colors {
229 0 ▶ my $self = shift;
Subroutine available_colors: not called CC=1 SCAR=6.9
Pod undocumented
230 0 return @colors;
231 }
232  
233 # ok!
234 # Call this if you want a nice checkmark after you spinn
235 sub ok {
236 0 ▶ my $self = shift;
Subroutine ok: not called CC=3 SCAR=24.8
Pod undocumented
237 0 my $ok;
238 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
239 0 $ok = colored( "✔", 'green' );
240 }
241 elsif ( $self->{'last_size'} == 5 ) {
242 0 $ok = colored( "[ ✔ ]", 'white on_green' );
243 }
244 else { # Better be 7, or it'll look goofy, but still work
245 0 $ok = colored( "[ ✔ ]", 'white on_green' );
246 }
247 0 say $ok;
248 }
249  
250 # nok!
251 # call this to tell user everything is not ok. prints a red x
252 sub nok {
253 0 ▶ my $self = shift;
Subroutine nok: not called CC=3 SCAR=24.8
Pod undocumented
254 0 my $nok;
255 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
256 0 $nok = colored( "✘", 'red' );
257 }
258 elsif ( $self->{'last_size'} == 5 ) {
259 0 $nok = colored( "[ ✘ ]", 'white on_red' );
260 }
261 else { # Better be 7, or it'll look goofy, but still work
262 0 $nok = colored( "[ ✘ ]", 'white on_red' );
263 }
264 0 say $nok;
265 }
266  
267 # Frame length of the first frame of a seq...this won't work if
268 # if the frames change size, but the rest of the spinnner will
269 # probably also screw up in that case.
270 sub frame_length {
271 0 ▶ my $self = shift;
Subroutine frame_length: not called CC=1 SCAR=6.9
Pod undocumented
272 0 return length( $self->{'seq'}[0] );
273 }
274  
275 1 1;
276  
277 __END__
↑ Summary