use
5.10.1;
our
$VERSION
=
'0.169'
;
sub
new {
my
(
$class
,
$self
) =
@_
;
bless
$self
,
$class
;
$self
->{show_progress_bar} //= 1;
return
$self
;
}
sub
set_progress_bar {
my
(
$self
) =
@_
;
my
$term_w
= get_term_width() + EXTRA_W;
$self
->{fmt} =
"\rComputing: %3d%% [%s]"
;
$self
->{short_print} =
$term_w
< 25 ? 1 : 0;
if
(
$self
->{short_print} ) {
$self
->{bar_w} =
$term_w
}
else
{
$self
->{bar_w} =
$term_w
-
length
(
sprintf
$self
->{fmt}, 100,
''
) + 1;
}
$self
->{step} =
int
(
$self
->{total} /
$self
->{bar_w} || 1 );
$self
->{count} //= 0;
$self
->{next_update} ||=
$self
->{step};
if
( !
$self
->{count} ) {
print
clear_screen;
print
"\rComputing: "
;
}
return
;
}
sub
update_progress_bar {
my
(
$self
) =
@_
;
my
$multi
=
int
(
$self
->{count} / (
$self
->{total} /
$self
->{bar_w} ) ) || 1;
if
(
$self
->{short_print} ) {
print
"\r"
, clear_to_end_of_line;
print
( (
'='
x
$multi
) . (
' '
x (
$self
->{bar_w} -
$multi
) ) );
}
else
{
printf
$self
->{fmt}, (
$self
->{count} /
$self
->{total} * 100 ), (
'='
x
$multi
) . (
' '
x (
$self
->{bar_w} -
$multi
) );
}
$self
->{next_update} =
$self
->{next_update} +
$self
->{step};
}
1;