forked from BASLQC/kc-vita-translation
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
194831f
commit e0a3bbd
Showing
3 changed files
with
38 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package countdown; | ||
|
||
use Mu; | ||
use strictures; | ||
use Time::HiRes 'time'; | ||
|
||
ro "total"; | ||
ro _start_time => default => sub { time }; | ||
rw current => default => 0; | ||
rw _last_time_left => default => 0; | ||
lazy minimum_step => sub { 0.05 }; | ||
|
||
sub update_and_return { shift->update_count_and_return( undef, shift ) } | ||
|
||
sub update_count_and_return { shift->update(shift); return shift; } | ||
|
||
sub update { | ||
my ( $self, $done_count ) = @_; | ||
$self->current( $self->current + ( $done_count || 1 ) ); | ||
my $elapsed = time - $self->_start_time; | ||
my $total_time = $self->total * $elapsed / $self->current; | ||
my $left = $total_time - $elapsed; | ||
return if $self->_last_time_left and abs( $self->_last_time_left - $left ) < $self->minimum_step; | ||
printf "\r% 20s", sprintf "%.2f s", $left; | ||
$self->_last_time_left($left); | ||
return; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters