| 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' => | ||||||||||||||
| 29 | [ qw(â â â â â â â â â ² â ´ â ¤ â â â ¤ â ´ â ² â â â â â â â â ) ], | ||||||||||||||
| 30 | 'uni_dots8' => | ||||||||||||||
| 31 | [ qw(â â â â â â â â â â ¦ â ¤ â â â ¤ â ¦ â â â â â â â â â ) ], | ||||||||||||||
| 32 | 'uni_dots9' => | ||||||||||||||
| 33 | [ qw(â â â â â â â â â â ² â ´ â ¤ â â â ¤ â â â ¤ â ¦ â â â â â â â â â â ) ], | ||||||||||||||
| 34 | 'uni_dots10' => [ qw(⢹ ⢺ ⢼ ⣸ ⣠⡧ â¡ â¡) ], | ||||||||||||||
| 35 | 'uni_dots11' => [ qw(⢠⢠⢠⡠⡠⡠⡠) ], | ||||||||||||||
| 36 | 'uni_dots12' => [ qw(â â â ⡠⢠â â â ) ], | ||||||||||||||
| 37 | 'uni_bounce' => [ qw(â â â â ) ], | ||||||||||||||
| 38 | 'uni_pipes' => [ qw(⤠â â´ â â â ⬠â) ], | ||||||||||||||
| 39 | 'uni_hippie' => [ qw(â® â ⺠â¥) ], | ||||||||||||||
| 40 | 'uni_hands' => [ qw(â â â â) ], | ||||||||||||||
| 41 | 'uni_arrow_rot' => [ qw(â« â ⬠â) ], | ||||||||||||||
| 42 | 'uni_cards' => [ qw(⣠⤠⥠â¦) ], | ||||||||||||||
| 43 | 'uni_triangle' => [ qw(⢠⣠⤠â¥) ], | ||||||||||||||
| 44 | 'uni_square' => [ qw(â° â³ â² â±) ], | ||||||||||||||
| 45 | 'uni_pie' => [ qw(â´ â· â¶ âµ) ], | ||||||||||||||
| 46 | 'uni_circle' => [ qw(â â â â) ], | ||||||||||||||
| 47 | 'uni_qtr_circle' => [ qw(â â â â) ], | ||||||||||||||
| 48 | 'uni_three_lines' => [ qw(â â° â â°) ], | ||||||||||||||
| 49 | 'uni_trigram_down' => [ qw(â° â± â² â´) ], | ||||||||||||||
| 50 | 'uni_trigram_bounce' => [ qw(â° â± â² â´ â° â´ â² â±) ], | ||||||||||||||
| 51 | 'uni_count' => [ qw(â â â â â â â â â â) ], | ||||||||||||||
| 52 | 'uni_ellipsis_propeller' => [ qw(⮠Ⱐ⯠â±) ], | ||||||||||||||
| 53 | 'uni_earth' => [ qw(ð ð ð) ], | ||||||||||||||
| 54 | 'uni_moon' => [ qw(ð ð ð ð ð ð ð) ], | ||||||||||||||
| 55 | 'uni_junk_food' => [ qw(ð ð® ð¯ ð ð ð) ], | ||||||||||||||
| 56 | 'uni_clapping' => [ qw(ð ð) ], | ||||||||||||||
| 57 | 'uni_diamonds' => [ qw(ð¹ ð· ð¸ ð¶) ], | ||||||||||||||
| 58 | 'wide_ascii_prog' => | ||||||||||||||
| 59 | [ qw([>----] [=>---] [==>--] [===>-] [====>] [----<] [---<=] [--<==] [-<===] [<====]) ], | ||||||||||||||
| 60 | 'wide_ascii_propeller' => | ||||||||||||||
| 61 | [ qw([|----] [=/---] [==---] [===\-] [====|] [---\=] [---==] [-/===]) ], | ||||||||||||||
| 62 | 'wide_ascii_snek' => | ||||||||||||||
| 63 | [ qw([>----] [~>---] [~~>--] [~~~>-] [~~~~>] [----<] [---<~] [--<~~] [-<~~~] [<~~~~]) ], | ||||||||||||||
| 64 | 'wide_uni_greyscale' => | ||||||||||||||
| 65 | [ qw(âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ) ], | ||||||||||||||
| 66 | 'wide_uni_greyscale2' => | ||||||||||||||
| 67 | [ qw(âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ âââââââ ââââââ ââââââ) ], | ||||||||||||||
| 68 | ); | ||||||||||||||
| 69 | |||||||||||||||
| 70 | sub new { | ||||||||||||||
| 71 | 0 | ▶ | my ($class, %args) = @_; | ||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||
| 72 | 0 | my $self = {}; | |||||||||||||
| 73 | |||||||||||||||
| 74 | # seq can either be an array ref with a whole set of frames, or can be the | ||||||||||||||
| 75 | # name of a frame set. | ||||||||||||||
| 76 | 0 | ▶ | if ( ! defined($args{'seq'}) ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 77 | 0 | $self->{'seq'} = $frames{'wide_uni_greyscale2'}; | |||||||||||||
| 78 | } elsif ( ref($args{'seq'}) ne 'ARRAY' ) { | ||||||||||||||
| 79 | 0 | $self->{'seq'} = $frames{$args{'seq'}}; | |||||||||||||
| 80 | } else { | ||||||||||||||
| 81 | 0 | $self->{'seq'} = $args{'seq'}; | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | 0 | ▶ | $self->{'delay'} = $args{'delay'} || 0.2; | ||||||||||||
|
Condition0%0/2
$args{'delay'} || 0.2
MC/DC0%0/1
$args{'delay'} || 0.2
$args{'delay'}
Truth table0%0/2
A
A= $args{'delay'}
| |||||||||||||||
| 85 | 0 | ▶ | $self->{'color'} = $args{'color'} || 'cyan'; | ||||||||||||
|
Condition0%0/2
$args{'color'} || 'cyan'
MC/DC0%0/1
$args{'color'} || 'cyan'
$args{'color'}
Truth table0%0/2
A
A= $args{'color'}
| |||||||||||||||
| 86 | 0 | ▶ | $self->{'colorcycle'} = $args{'colorcycle'} || 0; | ||||||||||||
|
Condition0%0/2
$args{'colorcycle'} || 0
MC/DC0%0/1
$args{'colorcycle'} || 0
$args{'colorcycle'}
Truth table0%0/2
A
A= $args{'colorcycle'}
| |||||||||||||||
| 87 | 0 | $self->{'bksp'} = chr(0x08); | |||||||||||||
| 88 | 0 | $self->{'last_size'} = length($self->{'seq'}[0]); | |||||||||||||
| 89 | 0 | return bless $self, $class; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | sub start { | ||||||||||||||
| 93 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
start: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 94 | 0 | print "\x1b[?25l"; # Hide cursor | |||||||||||||
| 95 | 0 | $self->{'last_size'} = length($self->{'seq'}[0]); | |||||||||||||
| 96 | 0 | print colored("$self->{'seq'}[0]", $self->{'color'}); | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | sub next { | ||||||||||||||
| 100 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
next: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 101 | 0 | state $pos = 1; | |||||||||||||
| 102 | |||||||||||||||
| 103 | 0 | ▶ | if( $self->{'colorcycle'} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | 0 | push @colors, shift @colors; # rotate the colors list | |||||||||||||
| 105 | 0 | $self->{'color'} = $colors[0]; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 0 | print $self->{'bksp'} x $self->{'last_size'}; | |||||||||||||
| 109 | 0 | print colored("$self->{'seq'}[$pos]", $self->{'color'}); | |||||||||||||
| 110 | |||||||||||||||
| 111 | 0 | $pos = ++$pos % scalar @{ $self->{'seq'} }; | |||||||||||||
| 112 | 0 | $self->{'last_size'} = length($self->{'seq'}[$pos]); | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | sub done { | ||||||||||||||
| 116 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
done: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 117 | |||||||||||||||
| 118 | 0 | print $self->{'bksp'} x $self->{'last_size'}; | |||||||||||||
| 119 | 0 | print "\x1b[?25h"; # Show cursor | |||||||||||||
| 120 | } | ||||||||||||||
| 121 | |||||||||||||||
| 122 | # Fork and run spinner asynchronously, until signal received. | ||||||||||||||
| 123 | sub auto_start { | ||||||||||||||
| 124 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
auto_start: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 125 | |||||||||||||||
| 126 | 0 | my $pid = fork(); | |||||||||||||
| 127 | 0 | ▶ | die ("Failed to fork progress indicator.\n") unless defined $pid; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | |||||||||||||||
| 129 | 0 | ▶ | if ($pid) { # Parent | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 0 | $self->{'child'} = $pid; | |||||||||||||
| 131 | 0 | return; | |||||||||||||
| 132 | } else { # Kid stuff | ||||||||||||||
| 133 | 0 | $self->start(); | |||||||||||||
| 134 | 0 | while (1) { | |||||||||||||
| 135 | 0 | sleep $self->{'delay'}; | |||||||||||||
| 136 | 0 | $self->next(); | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | 0 | exit 0; # Should never get here? | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | sub auto_done { | ||||||||||||||
| 143 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
auto_done: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 144 | |||||||||||||||
| 145 | 0 | kill 'KILL', $self->{'child'}; | |||||||||||||
| 146 | 0 | my $pid = wait(); | |||||||||||||
| 147 | 0 | $self->done(); | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | # Run, OK? Does a thing, or a list of things. usually long-running things, | ||||||||||||||
| 151 | # runs a spinner, and prints a nice status message (check or X, whether | ||||||||||||||
| 152 | # success or err), when done. | ||||||||||||||
| 153 | sub run_ok { | ||||||||||||||
| 154 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
run_ok: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||
| 155 | 0 | my $exp = shift; # A list of functions to call and wait on. | |||||||||||||
| 156 | 0 | my $message = shift; # String to print before the spinner | |||||||||||||
| 157 | 0 | my $termwidth = `tput cols`; | |||||||||||||
| 158 | 0 | ▶ | $termwidth = 80 unless $termwidth <= 80; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | 0 | my $cols = $termwidth - length($message) - $self->{'last_size'} - 1; | |||||||||||||
| 160 | 0 | my ($ok, $nok); | |||||||||||||
| 161 | 0 | ▶ | if ($self->{'last_size'} == 1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 162 | 0 | $ok = colored("â", 'green'); | |||||||||||||
| 163 | 0 | $nok = colored("â", 'red'); | |||||||||||||
| 164 | } elsif ($self->{'last_size'} == 5) { | ||||||||||||||
| 165 | 0 | $ok = colored("[ â ]", 'white on_green'); | |||||||||||||
| 166 | 0 | $nok = colored("[ â ]", 'white on_red'); | |||||||||||||
| 167 | } else { # Better be 7, or it'll look goofy, but still work | ||||||||||||||
| 168 | 0 | $ok = colored("[ â ]", 'white on_green'); | |||||||||||||
| 169 | 0 | $nok = colored("[ â ]", 'white on_red'); | |||||||||||||
| 170 | } | ||||||||||||||
| 171 | |||||||||||||||
| 172 | 0 | print $message; | |||||||||||||
| 173 | 0 | print ' ' x $cols; | |||||||||||||
| 174 | 0 | ▶ | if ( ref($exp) eq 'ARRAY' ) { # List of expressions | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 175 | 0 | $self->start(); | |||||||||||||
| 176 | 0 | foreach my $exp (@{$exp}) { | |||||||||||||
| 177 | 0 | $self->next(); | |||||||||||||
| 178 | 0 | my $res = eval $exp; ## no critic | |||||||||||||
| 179 | 0 | ▶ | unless ($res) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 180 | 0 | $self->done(); | |||||||||||||
| 181 | 0 | say $nok; | |||||||||||||
| 182 | 0 | return 0; | |||||||||||||
| 183 | } | ||||||||||||||
| 184 | } | ||||||||||||||
| 185 | 0 | $self->done(); | |||||||||||||
| 186 | 0 | say $ok; | |||||||||||||
| 187 | } else { # Single expression | ||||||||||||||
| 188 | 0 | $self->auto_start(); | |||||||||||||
| 189 | 0 | my $res = eval $exp; ## no critic | |||||||||||||
| 190 | 0 | ▶ | unless ($res) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 191 | 0 | $self->auto_done(); | |||||||||||||
| 192 | 0 | say $nok; | |||||||||||||
| 193 | 0 | return 0; | |||||||||||||
| 194 | } | ||||||||||||||
| 195 | 0 | $self->auto_done(); | |||||||||||||
| 196 | 0 | say $ok; | |||||||||||||
| 197 | } | ||||||||||||||
| 198 | } | ||||||||||||||
| 199 | |||||||||||||||
| 200 | # Return list of available frames | ||||||||||||||
| 201 | sub available_frames { | ||||||||||||||
| 202 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
available_frames: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 203 | 0 | return \%frames; | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | |||||||||||||||
| 206 | sub available_colors { | ||||||||||||||
| 207 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
available_colors: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 208 | 0 | return @colors; | |||||||||||||
| 209 | } | ||||||||||||||
| 210 | |||||||||||||||
| 211 | # ok! | ||||||||||||||
| 212 | # Call this if you want a nice checkmark after you spinn | ||||||||||||||
| 213 | sub ok { | ||||||||||||||
| 214 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
ok: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 215 | 0 | my $ok; | |||||||||||||
| 216 | 0 | ▶ | if ($self->{'last_size'} == 1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 217 | 0 | $ok = colored("â", 'green'); | |||||||||||||
| 218 | } elsif ($self->{'last_size'} == 5) { | ||||||||||||||
| 219 | 0 | $ok = colored("[ â ]", 'white on_green'); | |||||||||||||
| 220 | } else { # Better be 7, or it'll look goofy, but still work | ||||||||||||||
| 221 | 0 | $ok = colored("[ â ]", 'white on_green'); | |||||||||||||
| 222 | } | ||||||||||||||
| 223 | 0 | say $ok; | |||||||||||||
| 224 | } | ||||||||||||||
| 225 | |||||||||||||||
| 226 | # nok! | ||||||||||||||
| 227 | # call this to tell user everything is not ok. prints a red x | ||||||||||||||
| 228 | sub nok { | ||||||||||||||
| 229 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
nok: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 230 | 0 | my $nok; | |||||||||||||
| 231 | 0 | ▶ | if ($self->{'last_size'} == 1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 232 | 0 | $nok = colored("â", 'red'); | |||||||||||||
| 233 | } elsif ($self->{'last_size'} == 5) { | ||||||||||||||
| 234 | 0 | $nok = colored("[ â ]", 'white on_red'); | |||||||||||||
| 235 | } else { # Better be 7, or it'll look goofy, but still work | ||||||||||||||
| 236 | 0 | $nok = colored("[ â ]", 'white on_red'); | |||||||||||||
| 237 | } | ||||||||||||||
| 238 | 0 | say $nok; | |||||||||||||
| 239 | } | ||||||||||||||
| 240 | |||||||||||||||
| 241 | # Frame length of the first frame of a seq...this won't work if | ||||||||||||||
| 242 | # if the frames change size, but the rest of the spinnner will | ||||||||||||||
| 243 | # probably also screw up in that case. | ||||||||||||||
| 244 | sub frame_length { | ||||||||||||||
| 245 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
frame_length: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 246 | 0 | return length($self->{'seq'}[0]); | |||||||||||||
| 247 | } | ||||||||||||||
| 248 | |||||||||||||||
| 249 | 1 | 1; | |||||||||||||
| 250 | |||||||||||||||
| 251 | __END__ | ||||||||||||||