combined value and sum formatting, --reformat
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 20 Nov 2022 17:17:14 +0000 (18:17 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 21 Nov 2022 16:02:50 +0000 (17:02 +0100)
Apply value-format to all stat values; exception to keep unformatted input
indicated by an experimental reformat option.

barcat
t/t1422-all_the_time_--sexagesimal_-H_-sC.out

diff --git a/barcat b/barcat
index 061b256a10b2cdeb997f7ecac8d56c8cad3ca6ef..e3d291b32e3b5ce5e46450a2df0372d20a5f5bb5 100755 (executable)
--- a/barcat
+++ b/barcat
@@ -28,6 +28,7 @@ GetOptions(\%opt,
        },
        'human-readable|H!',
        'sexagesimal!',
+       'reformat!',
        'interval|t:i',
        'trim|length|l=s' => sub {
                my ($optname, $optval) = @_;
@@ -128,7 +129,6 @@ $opt{hidemin} = ($opt{hidemin} || 1) - 1;
 $opt{input} = (@ARGV && $ARGV[0] =~ m/\A[-0-9]/) ? \@ARGV : undef
        and undef $opt{interval};
 
-$opt{'sum-format'} = sub { sprintf '%.8g', $_[0] };
 $opt{'calc-format'} = sub { sprintf '%*.*f', 0, 2, $_[0] };
 $opt{'value-format'} = $opt{sexagesimal} ? sub {
        my $s = abs($_[0]) + .5;
@@ -150,7 +150,8 @@ $opt{'value-format'} = $opt{sexagesimal} ? sub {
                $#{$opt{units}} * 1.5 < abs $unit ? sprintf('e%d', $unit) :
                        $opt{units}->[$unit/3]  # suffix
        );
-};
+} and $opt{reformat}++;
+$opt{'value-format'} ||= sub { sprintf '%.8g', $_[0] };
 
 
 my (@lines, @values, @order);
@@ -197,10 +198,6 @@ while (defined ($_ = $opt{input} ? shift @{ $opt{input} } : readline)) {
                and $. % $opt{interval} == 0;
 }
 
-if ($opt{'zero-missing'}) {
-       push @values, (0) x 10;
-}
-
 $SIG{INT} = 'DEFAULT';
 
 sub color {
@@ -314,7 +311,7 @@ while ($nr <= $limit) {
 
        if (length $val) {
                $val = sprintf("%*s", $lenval,
-                       $opt{'value-format'} ? $opt{'value-format'}->($val) : $val
+                       $opt{reformat} ? $opt{'value-format'}->($val) : $val
                );
                color($color) for $val;
        }
@@ -344,19 +341,19 @@ sub show_stat {
                        $linemin += @lines;
                        $linemax = @lines - $linemax;
                }
-               printf '%.8g of ', $opt{'sum-format'}->(
+               printf '%.8g of ', $opt{'value-format'}->(
                        sum(grep {length} @values[$linemin .. $linemax]) // 0
                );
        }
        if (@order) {
                my $total = sum @order;
-               printf '%s total', color(1) . $opt{'sum-format'}->($total) . color(0);
+               printf '%s total', color(1) . $opt{'value-format'}->($total) . color(0);
                printf ' in %d values', scalar @order;
                printf ' over %d lines', scalar @lines if @order != @lines;
                printf(' (%s min, %s avg, %s max)',
-                       color(31) . ($opt{'value-format'} || sub {$_[0]})->($order[-1]) . color(0),
-                       color(36) . ($opt{'value-format'} || $opt{'calc-format'})->($total / @order) . color(0),
-                       color(32) . ($opt{'value-format'} || sub {$_[0]})->($order[0]) . color(0),
+                       color(31) . ($opt{reformat} ? $opt{'value-format'} : sub {$_[0]})->($order[-1]) . color(0),
+                       color(36) . ($opt{reformat} ? $opt{'value-format'} : $opt{'calc-format'})->($total / @order) . color(0),
+                       color(32) . ($opt{reformat} ? $opt{'value-format'} : sub {$_[0]})->($order[0]) . color(0),
                );
        }
        say '';
index cc1d16b6fccb91a021952a31bc4e724232e2d211..cba86f6672b25d0068b6d4a0638d12dfa8f28416 100644 (file)
@@ -7,4 +7,4 @@
 \e[90m-0:00:01\e[0m      -
 \e[90m-0:00:02\e[0m      -
 \e[31m-2:02:02\e[0m      
-\e[1m479345.21\e[0m total in 8 values over 9 lines (\e[31m-2:02:02\e[0m min, \e[36m16:38:38\e[0m avg, \e[32m111:11:11\e[0m max)
+\e[1m133:09:05\e[0m total in 8 values over 9 lines (\e[31m-2:02:02\e[0m min, \e[36m16:38:38\e[0m avg, \e[32m111:11:11\e[0m max)