From: Mischa POSLAWSKY Date: Sun, 20 Nov 2022 17:17:14 +0000 (+0100) Subject: combined value and sum formatting, --reformat X-Git-Tag: v1.09~11 X-Git-Url: http://git.shiar.nl/barcat.git/commitdiff_plain/f066b3f08d4c03198c25b243206227e5801b6935 combined value and sum formatting, --reformat Apply value-format to all stat values; exception to keep unformatted input indicated by an experimental reformat option. --- diff --git a/barcat b/barcat index 061b256..e3d291b 100755 --- 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 ''; diff --git a/t/t1422-all_the_time_--sexagesimal_-H_-sC.out b/t/t1422-all_the_time_--sexagesimal_-H_-sC.out index cc1d16b..cba86f6 100644 --- a/t/t1422-all_the_time_--sexagesimal_-H_-sC.out +++ b/t/t1422-all_the_time_--sexagesimal_-H_-sC.out @@ -7,4 +7,4 @@ -0:00:01 - -0:00:02 - -2:02:02 -479345.21 total in 8 values over 9 lines (-2:02:02 min, 16:38:38 avg, 111:11:11 max) +133:09:05 total in 8 values over 9 lines (-2:02:02 min, 16:38:38 avg, 111:11:11 max)