exponent fallback in case of unit overflow
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 7 Sep 2019 14:03:35 +0000 (16:03 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 7 Sep 2019 18:16:38 +0000 (20:16 +0200)
Guard against missing units.

barcat

diff --git a/barcat b/barcat
index 69f4516c0fbd766defefe578e9cb6e8eb4cd1efc..89b110fe2ca04ecbf8fb4421190346020687a02d 100755 (executable)
--- a/barcat
+++ b/barcat
@@ -51,7 +51,7 @@ GetOptions(\%opt,
 $opt{width} ||= $ENV{COLUMNS} || 80;
 $opt{color} //= -t *STDOUT;  # enable on tty
 $opt{trim}   *= $opt{width} / 100 if $opt{trimpct};
 $opt{width} ||= $ENV{COLUMNS} || 80;
 $opt{color} //= -t *STDOUT;  # enable on tty
 $opt{trim}   *= $opt{width} / 100 if $opt{trimpct};
-$opt{units}   = $opt{'human-readable'} && ['', qw( k M G T <> n μ m )];
+$opt{units}   = $opt{'human-readable'} && ['', qw( k M G T P E Z Y y z a f p n μ m )];
 
 if (defined $opt{interval}) {
        $opt{interval} ||= 1;
 
 if (defined $opt{interval}) {
        $opt{interval} ||= 1;
@@ -127,7 +127,8 @@ if ($opt{markers} // 1 and $size > 0) {
 
 sub sival {
        my $unit = int(log($_[0]) / log(1000) - ($_[0] < 1));
 
 sub sival {
        my $unit = int(log($_[0]) / log(1000) - ($_[0] < 1));
-       sprintf "%3.1f%1s", $_[0] / 1000 ** $unit, $opt{units}->[$unit];
+       sprintf "%3.1f%1s", $_[0] / 1000 ** $unit,
+               $#{$opt{units}} >> 1 < abs $unit ? "e$unit" : $opt{units}->[$unit];
 }
 
 while ($nr <= $#lines) {
 }
 
 while ($nr <= $#lines) {