ÿØÿà JFIF x x ÿÛ C
ÿÛ CÿÀ " ÿÄ
ÿÄ µ } !1AQa"q2‘¡#B±ÁRÑð$3br‚
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ
ÿÄ µ w !1AQaq"2B‘¡±Á #3RðbrÑpackage Term::Spinner::Color;
use strict;
use warnings;
use 5.010;
use POSIX;
use Time::HiRes qw( sleep );
use Term::ANSIColor;
use Term::Cap;
use utf8;
use open ':std', ':encoding(UTF-8)';
$| = 1; # Disable buffering on STDOUT.
# Couple of instance vars for colors and frame sets
my @colors = qw( red green yellow blue magenta cyan white );
my %frames = (
'ascii_propeller' => [qw(/ - \\ |)],
'ascii_plus' => [qw(x +)],
'ascii_blink' => [qw(o -)],
'ascii_v' => [qw(v < ^ >)],
'ascii_inflate' => [qw(. o O o)],
'uni_dots' => [qw(â ‹ â ™ â ¹ â ¸ â ¼ â ´ â ¦ â § â ‡ â )],
'uni_dots2' => [qw(⣾ ⣽ ⣻ ⢿ ⡿ ⣟ ⣯ ⣷)],
'uni_dots3' => [qw(⣷ ⣯ ⣟ ⡿ ⢿ ⣻ ⣽ ⣾)],
'uni_dots4' => [qw(â ‹ â ™ â š â ž â – â ¦ â ´ â ² â ³ â “)],
'uni_dots5' => [qw(â „ â † â ‡ â ‹ â ™ â ¸ â ° â â ° â ¸ â ™ â ‹ â ‡ â †)],
'uni_dots6' => [qw(â ‹ â ™ â š â ’ â ‚ â ‚ â ’ â ² â ´ â ¦ â – â ’ â â â ’ â “ â ‹')],
'uni_dots7' => [qw(â â ‰ â ™ â š â ’ â ‚ â ‚ â ’ â ² â ´ â ¤ â „ â „ â ¤ â ´ â ² â ’ â ‚ â ‚ â ’ â š â ™ â ‰ â )],
'uni_dots8' => [qw(â ˆ â ‰ â ‹ â “ â ’ â â â ’ â – â ¦ â ¤ â â â ¤ â ¦ â – â ’ â â â ’ â “ â ‹ â ‰ â ˆ)],
'uni_dots9' =>
[qw(â â â ‰ â ™ â š â ’ â ‚ â ‚ â ’ â ² â ´ â ¤ â „ â „ â ¤ â â â ¤ â ¦ â – â ’ â â â ’ â “ â ‹ â ‰ â ˆ â ˆ)],
'uni_dots10' => [qw(⢹ ⢺ ⢼ ⣸ ⣇ â¡§ â¡— â¡)],
'uni_dots11' => [qw(⢄ ⢂ ⢠⡠⡈ ⡠⡠)],
'uni_dots12' => [qw(â â ‚ â „ â¡€ ⢀ â â â ˆ)],
'uni_bounce' => [qw(â â ‚ â „ â ‚)],
'uni_pipes' => [qw(┤ ┘ â”´ â”” ├ ┌ ┬ â”)],
'uni_hippie' => [qw(☮ ✌ ☺ ♥)],
'uni_hands' => [qw(☜ ☠☞ ☟)],
'uni_arrow_rot' => [qw(âž« ➠➬ âž)],
'uni_cards' => [qw(♣ ♤ ♥ ♦)],
'uni_triangle' => [qw(â—¢ â—£ â—¤ â—¥)],
'uni_square' => [qw(â—° â—³ â—² â—±)],
'uni_pie' => [qw(â—´ â—· â—¶ â—µ)],
'uni_circle' => [qw(â— â—“ â—‘ â—’)],
'uni_qtr_circle' => [qw(◜ ◠◞ ◟)],
'uni_three_lines' => [qw(⚞ ☰ ⚟ ☰)],
'uni_trigram_down' => [qw(☰ ☱ ☲ ☴)],
'uni_trigram_bounce' => [qw(☰ ☱ ☲ ☴ ☰ ☴ ☲ ☱)],
'uni_count' => [qw(➀ ➠➂ ➃ ➄ ➅ ➆ ➇ ➈ ➉)],
'uni_ellipsis_propeller' => [qw(⋮ ⋰ ⋯ ⋱)],
'uni_earth' => [qw(🌠🌠🌎)],
'uni_moon' => [qw(🌑 🌘 🌗 🌖 🌕 🌔 🌒)],
'uni_junk_food' => [qw(🌠🌮 🌯 🔠🕠ðŸŸ)],
'uni_clapping' => [qw(👠ðŸ‘)],
'uni_diamonds' => [qw(🔹 🔷 🔸 🔶)],
'wide_ascii_prog' => [
qw([>----] [=>---] [==>--] [===>-] [====>] [----<] [---<=] [--<==] [-<===] [<====])
],
'wide_ascii_propeller' =>
[qw([|----] [=/---] [==---] [===\-] [====|] [---\=] [---==] [-/===])],
'wide_ascii_snek' => [
qw([>----] [~>---] [~~>--] [~~~>-] [~~~~>] [----<] [---<~] [--<~~] [-<~~~] [<~~~~])
],
'wide_uni_greyscale' => [
qw(▒▒▒▒▒▒▒ █▒▒▒▒▒▒ ██▒▒▒▒▒ ███▒▒▒▒ ████▒▒▒ █████▒▒ ██████▒ ███████ ██████▒ █████▒▒ ████▒▒▒ ███▒▒▒▒ ██▒▒▒▒▒ █▒▒▒▒▒▒ ▒▒▒▒▒▒▒)
],
'wide_uni_greyscale2' => [
qw(▒▒▒▒▒▒▒ █▒▒▒▒▒▒ ██▒▒▒▒▒ ███▒▒▒▒ ████▒▒▒ █████▒▒ ██████▒ ███████ ▒██████ ▒▒█████ ▒▒▒████ ▒▒▒▒███ ▒▒▒▒▒██ ▒▒▒▒▒█ ▒▒▒▒▒▒)
],
);
sub new {
my ($class, %args) = @_;
my $self = {};
# seq can either be an array ref with a whole set of frames, or can be the
# name of a frame set.
if (!defined($args{'seq'})) {
$self->{'seq'} = $frames{'wide_uni_greyscale2'};
}
elsif (ref($args{'seq'}) ne 'ARRAY') {
$self->{'seq'} = $frames{$args{'seq'}};
}
else {
$self->{'seq'} = $args{'seq'};
}
$self->{'delay'} = $args{'delay'} || 0.2;
$self->{'color'} = $args{'color'} || 'cyan';
$self->{'colorcycle'} = $args{'colorcycle'} || 0;
$self->{'bksp'} = chr(0x08);
$self->{'last_size'} = length($self->{'seq'}[0]);
return bless $self, $class;
}
sub start {
my $self = shift;
print "\x1b[?25l"; # Hide cursor
$self->{'last_size'} = length($self->{'seq'}[0]);
print colored("$self->{'seq'}[0]", $self->{'color'});
}
sub next {
my $self = shift;
state $pos = 1;
if ($self->{'colorcycle'}) {
push @colors, shift @colors; # rotate the colors list
$self->{'color'} = $colors[0];
}
print $self->{'bksp'} x $self->{'last_size'};
print colored("$self->{'seq'}[$pos]", $self->{'color'});
$pos = ++$pos % scalar @{$self->{'seq'}};
$self->{'last_size'} = length($self->{'seq'}[$pos]);
}
sub done {
my $self = shift;
print $self->{'bksp'} x $self->{'last_size'};
print "\x1b[?25h"; # Show cursor
}
# Fork and run spinner asynchronously, until signal received.
sub auto_start {
my $self = shift;
my $ppid = $$;
my $pid = fork();
die("Failed to fork progress indicator.\n") unless defined $pid;
if ($pid) { # Parent
$self->{'child'} = $pid;
return;
}
else { # Kid stuff
$self->start();
my $exists;
while (1) {
sleep $self->{'delay'};
$self->next();
# Check to be sure parent is still running, if not, die
$exists = kill 0, $ppid;
unless ($exists) {
$self->done();
exit 0;
}
$exists = "";
}
exit 0; # Should never get here?
}
}
sub auto_done {
my $self = shift;
kill 'KILL', $self->{'child'};
my $pid = wait();
$self->done();
}
# Run, OK? Does a thing, or a list of things. usually long-running things,
# runs a spinner, and prints a nice status message (check or X, whether
# success or err), when done.
sub run_ok {
my $self = shift;
my $exp = shift; # A list of functions to call and wait on.
my $message = shift; # String to print before the spinner
my $termwidth = `tput cols`;
$termwidth = 80 unless $termwidth <= 80;
my $cols = $termwidth - length($message) - $self->{'last_size'} - 1;
my ($ok, $nok, $meh);
if ($self->{'last_size'} == 1) {
$ok = colored("✔", 'green');
$nok = colored("✘", 'red');
$meh = colored("âš ", 'yellow');
}
elsif ($self->{'last_size'} == 3) {
$ok = colored("[✔]", 'white on_green');
$nok = colored("[✘]", 'white on_red');
$meh = colored("[âš ]", 'white on_yellow');
}
elsif ($self->{'last_size'} == 5) {
$ok = colored("[ ✔ ]", 'white on_green');
$nok = colored("[ ✘ ]", 'white on_red');
$meh = colored("[ âš ]", 'white on_yellow');
}
else { # Better be 7, or it'll look goofy, but still work
$ok = colored("[ ✔ ]", 'white on_green');
$nok = colored("[ ✘ ]", 'white on_red');
$meh = colored("[ âš ]", 'white on_yellow');
}
print $message;
print ' ' x $cols;
if (ref($exp) eq 'ARRAY') { # List of expressions
$self->start();
foreach my $exp (@{$exp}) {
$self->next();
my $res = eval $exp; ## no critic
if ($res == 0) {
$self->done();
say $nok;
return 0;
}
elsif ($res == 2) { # Non-fatal error
$self->done();
say $meh;
return 2;
}
}
$self->done();
say $ok;
}
else { # Single expression
$self->auto_start();
my $res = eval $exp; ## no critic
unless ($res) {
$self->auto_done();
say $nok;
return 0;
}
$self->auto_done();
say $ok;
}
}
# Return list of available frames
sub available_frames {
my $self = shift;
return \%frames;
}
sub available_colors {
my $self = shift;
return @colors;
}
# ok!
# Call this if you want a nice checkmark after you spinn
sub ok {
my $self = shift;
my $ok;
if ($self->{'last_size'} == 1) {
$ok = colored("✔", 'green');
}
elsif ($self->{'last_size'} == 5) {
$ok = colored("[ ✔ ]", 'white on_green');
}
else { # Better be 7, or it'll look goofy, but still work
$ok = colored("[ ✔ ]", 'white on_green');
}
say $ok;
}
# meh
# call this to tell use that a non-fatal error occurred. prints a yellow âš
sub meh {
my $self = shift;
my $meh;
if ($self->{'last_size'} == 1) {
$meh = colored("âš ", 'yellow');
}
elsif ($self->{'last_size'} == 5) {
$meh = colored("[ âš ]", 'white on_yellow');
}
else { # Better be 7, or it'll look goofy, but still work
$meh = colored("[ âš ]", 'white on_yellow');
}
say $meh;
}
# nok!
# call this to tell user everything is not ok. prints a red x
sub nok {
my $self = shift;
my $nok;
if ($self->{'last_size'} == 1) {
$nok = colored("✘", 'red');
}
elsif ($self->{'last_size'} == 5) {
$nok = colored("[ ✘ ]", 'white on_red');
}
else { # Better be 7, or it'll look goofy, but still work
$nok = colored("[ ✘ ]", 'white on_red');
}
say $nok;
}
# Frame length of the first frame of a seq...this won't work if
# if the frames change size, but the rest of the spinnner will
# probably also screw up in that case.
sub frame_length {
my $self = shift;
return length($self->{'seq'}[0]);
}
1;
__END__
=pod
=encoding utf8
=for html
=head1 NAME
Term::Spinner::Color - A terminal spinner/progress bar with
Unicode, color, and no non-core dependencies.
=head1 SYNOPSIS
=begin HTML