File Coverage

blib/lib/Term/Spinner/Color.pm
Criterion Covered Total %
statement 26 149 17.4
branch 0 40 0.0
condition 0 6 0.0
subroutine 9 22 40.9
pod 6 13 46.1
total 41 230 17.8


line stmt bran cond sub pod time code
1             package Term::Spinner::Color;
2 1     1   46467 use strict;
  1         2  
  1         26  
3 1     1   4 use warnings;
  1         2  
  1         22  
4 1     1   20 use 5.010;
  1         7  
5 1     1   300 use POSIX;
  1         4481  
  1         4  
6 1     1   2518 use Time::HiRes qw( sleep );
  1         970  
  1         4  
7 1     1   547 use Term::ANSIColor;
  1         6358  
  1         80  
8 1     1   444 use Term::Cap;
  1         2306  
  1         30  
9 1     1   340 use utf8;
  1         13  
  1         4  
10 1     1   302 use open ':std', ':encoding(UTF-8)';
  1         962  
  1         5  
11              
12             $| = 1; # Disable buffering on STDOUT.
13              
14             # Couple of instance vars for colors and frame sets
15             my @colors = qw( red green yellow blue magenta cyan white );
16             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     0 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 0         if (!defined($args{'seq'})) {
    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   0       $self->{'delay'} = $args{'delay'} || 0.2;
89 0   0       $self->{'color'} = $args{'color'} || 'cyan';
90 0   0       $self->{'colorcycle'} = $args{'colorcycle'} || 0;
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     0 1   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             sub next {
104 0     0 1   my $self = shift;
105 0           state $pos = 1;
106              
107 0 0         if ($self->{'colorcycle'}) {
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'}};
  0            
116 0           $self->{'last_size'} = length($self->{'seq'}[$pos]);
117             }
118              
119             sub done {
120 0     0 1   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             sub auto_start {
128 0     0 1   my $self = shift;
129              
130 0           my $ppid = $$;
131 0           my $pid = fork();
132 0 0         die("Failed to fork progress indicator.\n") unless defined $pid;
133              
134 0 0         if ($pid) { # Parent
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 0         unless ($exists) {
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     0 1   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             sub run_ok {
169 0     0 1   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 0         $termwidth = 80 unless $termwidth <= 80;
174 0           my $cols = $termwidth - length($message) - $self->{'last_size'} - 1;
175 0           my ($ok, $nok, $meh);
176 0 0         if ($self->{'last_size'} == 1) {
    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 0         if (ref($exp) eq 'ARRAY') { # List of expressions
200 0           $self->start();
201 0           foreach my $exp (@{$exp}) {
  0            
202 0           $self->next();
203 0           my $res = eval $exp; ## no critic
204 0 0         if ($res == 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 0         unless ($res) {
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             sub available_frames {
233 0     0 0   my $self = shift;
234 0           return \%frames;
235             }
236              
237             sub available_colors {
238 0     0 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             sub ok {
245 0     0 0   my $self = shift;
246 0           my $ok;
247 0 0         if ($self->{'last_size'} == 1) {
    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             sub meh {
262 0     0 0   my $self = shift;
263 0           my $meh;
264 0 0         if ($self->{'last_size'} == 1) {
    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             sub nok {
279 0     0 0   my $self = shift;
280 0           my $nok;
281 0 0         if ($self->{'last_size'} == 1) {
    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             sub frame_length {
297 0     0 0   my $self = shift;
298 0           return length($self->{'seq'}[0]);
299             }
300              
301             1;
302              
303             __END__