| 1 | package MYDan::Util::Progress; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Term::Size; | ||
| 6 | |||
| 7 | $|++; | ||
| 8 | |||
| 9 | sub new | ||
| 10 | { | ||
| 11 | my $class = shift; | ||
| 12 | my ( $size, $cols ) = ( 66, Term::Size::chars *STDOUT{IO} ); | ||
| 13 | |||
| 14 | $size = $cols if $size > $cols; | ||
| 15 | my $rc = int ( $cols / $size ); | ||
| 16 | $size = int( $cols / $rc ) -1; | ||
| 17 | |||
| 18 | bless +{ rc => $rc, size => $size -1, d => [] }, ref $class || $class; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub load | ||
| 22 | { | ||
| 23 | my ( $this, %data ) = @_; | ||
| 24 | map{ | ||
| 25 | push @{$this->{d}}, $_ unless defined $this->{data}{$_}; | ||
| 26 | $this->{data}{$_} = $data{$_}; | ||
| 27 | }keys %data; | ||
| 28 | |||
| 29 | return $this; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub print | ||
| 33 | { | ||
| 34 | my ( $this, $prompt ) = @_; | ||
| 35 | |||
| 36 | system 'clear'; | ||
| 37 | |||
| 38 | my $i = 0; | ||
| 39 | |||
| 40 | for my $k ( @{$this->{d}} ) | ||
| 41 | { | ||
| 42 | $i ++; | ||
| 43 | |||
| 44 | my $v = $k.":". ( $this->{data}{$k} ||'' ); | ||
| 45 | |||
| 46 | if( length $v > $this->{size} ) | ||
| 47 | { | ||
| 48 | $v = substr $v, 0 ,$this->{size}; | ||
| 49 | } | ||
| 50 | else | ||
| 51 | { | ||
| 52 | $v .= ' ' x ( $this->{size} - length $v ); | ||
| 53 | } | ||
| 54 | |||
| 55 | print "$v|"; | ||
| 56 | print "\n" unless $i % $this->{rc}; | ||
| 57 | } | ||
| 58 | return $this; | ||
| 59 | } | ||
| 60 | |||
| 61 | 1; | ||
| 62 | __END__ |