report variables always formatted on trailing +
authorMischa POSLAWSKY <perl@shiar.org>
Mon, 21 Nov 2022 17:21:29 +0000 (18:21 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 22 Nov 2022 23:06:47 +0000 (00:06 +0100)
Random character chosen to fix sum values (t1602-float_statistics &a)
without obscure code invocations.

barcat

diff --git a/barcat b/barcat
index a86f74f4343bf3676e8b771a1e7ea4804a55c831..e898b6c117e0604cc58fdf044180467a71c55206 100755 (executable)
--- a/barcat
+++ b/barcat
@@ -359,13 +359,14 @@ sub show_stat {
                        $linemin += @lines;
                        $linemax = @lines - $linemax;
                }
-               printf '%.8g of ', $opt{'value-format'}->(
-                       sum(grep {length} @values[$linemin .. $linemax]) // 0
-               );
+               print varfmt('${sum+} of ', {
+                       lines => $linemax - $linemin + 1,
+                       sum => sum(0, grep {length} @values[$linemin .. $linemax]),
+               });
        }
        if (@order) {
                my $total = sum @order;
-               my $fmt = '${sum;color(1)} total in ${count#} values';
+               my $fmt = '${sum+;color(1)} total in ${count#} values';
                $fmt .= ' over ${lines#} lines' if @order != @lines;
                $fmt .= " ($_)" for $opt{report} || ();
                print varfmt($fmt, {
@@ -383,9 +384,9 @@ sub show_stat {
 
 sub varfmt {
        my ($fmt, $vars) = @_;
-       $fmt =~ s[\$\{( (?: [^{}]++ | \{(?1)\} )+ )\}]{
+       $fmt =~ s[\$\{ \h*+ ((?: [^{}]++ | \{(?1)\} )+) \}]{
                my ($name, $cmd) = split /\s*;/, $1, 2;
-               my $format = $name !~ s/\h*\#// && $opt{reformat};
+               my $format = $name =~ s/\+// || $name !~ s/\#// && $opt{reformat};
                local $_ = $vars->{$name};
                if (defined) {
                        $_ = $opt{'value-format'}->($_) if $format;