diff options
author | dakkar <dakkar@thenautilus.net> | 2013-05-06 21:15:24 +0100 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2013-05-06 21:15:24 +0100 |
commit | 416fc1974ec50a7ab5b2501331a2341c7fac2318 (patch) | |
tree | f6ebae663b411f8dcadccb21bc0042935fb0b658 /lib | |
parent | fix timeout frequency (diff) | |
download | battery-416fc1974ec50a7ab5b2501331a2341c7fac2318.tar.gz battery-416fc1974ec50a7ab5b2501331a2341c7fac2318.tar.bz2 battery-416fc1974ec50a7ab5b2501331a2341c7fac2318.zip |
avoid division by zero
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Battery.pm | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/lib/Battery.pm b/lib/Battery.pm index cc2257a..5ae4b8d 100644 --- a/lib/Battery.pm +++ b/lib/Battery.pm @@ -58,7 +58,7 @@ sub _format_time { my ($t) = @_; my $h=int($t); my $m=int(($t-$h)*60); - return sprintf q{%dh%d},$h,$m; + return sprintf q{%dh%02d},$h,$m; } sub update { @@ -66,16 +66,21 @@ sub update { my $bs=get_battery_stats; my $e=$bs->{energy_now}/$bs->{energy_full}; - my $t=$bs->{energy_now}/$bs->{power_now}; - my $tot=$bs->{energy_full}/$bs->{power_now}; - - $self->{b}->set_text( - sprintf '%s/%s %.0f%%', - _format_time($t), - _format_time($tot), - 100*$e - ); - $self->{b}->set_fraction($t/$tot); + my $txt=''; + if ($bs->{power_now}) { + my $t=$bs->{energy_now}/$bs->{power_now}; + my $tot=$bs->{energy_full}/$bs->{power_now}; + $txt = sprintf '%s/%s ', + _format_time($t), + _format_time($tot); + $self->{b}->set_fraction($t/$tot); + } + else { + $self->{b}->set_fraction($e); + } + $txt .= sprintf '%.0f%%', 100*$e; + + $self->{b}->set_text($txt); return 1; } |