App-Glacier

 view release on metacpan or  search on metacpan

lib/App/Glacier/Progress.pm  view on Meta::CPAN

package App::Glacier::Progress;
use strict;
use warnings;
use Exporter;
use parent qw(Exporter);
use Term::ReadKey;
use POSIX qw(isatty);
use Carp;
use threads;
use threads::shared;

# new(NUMBER)
sub new {
    my ($class, $total, %opts) = @_;
    croak "argument can't be 0" unless $total > 0;
    my $self = bless {
	_total => $total,
	_digits => int(log($total) / log(10) + 1),
	_current => 0,
    }, $class;

    share($self->{_current});
    my $v;
    if ($v = delete $opts{prefix}) {
	$self->{_prefix} = $v;
    }
    my $show_default = 1;
    if ($v = delete $opts{show_current}) {
	$self->{_show_current} = $v;
	$show_default = 0;
    }
    if ($v = delete $opts{show_total}) {
	$self->{_show_total} = $v;
	$show_default = 0;
    }
    if ($v = delete $opts{show_percent}) {
	$self->{_show_percent} = $v;
	$show_default = 0;
    }
    if ($v = delete $opts{show_dots}) {
	$self->{_show_dots} = $v;
	$show_default = 0;
    }
    if ($v = delete $opts{show_none}) {
	$show_default = 0;
    }
    
    croak "extra arguments" if keys %opts;

    if ($show_default) {
	$self->{_show_current} = 1;
	$self->{_show_total} = 1;
	$self->{_show_percent} = 1;
    }

    if (-t STDOUT) {
	$self->{_sigwinch} = $SIG{WINCH};
	if (open($self->{_tty}, "+</dev/tty")) {
	    select((select($self->{_tty}), $|=1)[0]);
	} else {
	    $self->{_tty} = undef;
	} 
	$SIG{WINCH} = sub {
	    $self->{_width} = undef;
	    goto ${$self->{_sigwinch}} if defined $self->{_sigwinch};
	};
    }
    $self->display;
    return $self;
}



( run in 1.739 second using v1.01-cache-2.11-cpan-f52f0507bed )