report variables always formatted on trailing +
[barcat.git] / barcat
diff --git a/barcat b/barcat
index e02ce4ed09e9c7f47f53e00a7eda3801cb8fdc47..e898b6c117e0604cc58fdf044180467a71c55206 100755 (executable)
--- a/barcat
+++ b/barcat
@@ -359,14 +359,15 @@ 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';
-               $fmt .= ' over ${lines} lines' if @order != @lines;
+               my $fmt = '${sum+;color(1)} total in ${count#} values';
+               $fmt .= ' over ${lines#} lines' if @order != @lines;
                $fmt .= " ($_)" for $opt{report} || ();
                print varfmt($fmt, {
                        sum => $total,
@@ -383,11 +384,12 @@ 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/\+// || $name !~ s/\#// && $opt{reformat};
                local $_ = $vars->{$name};
                if (defined) {
-                       $_ = $opt{'value-format'}->($_) if $opt{reformat};
+                       $_ = $opt{'value-format'}->($_) if $format;
                        if ($cmd) {
                                eval $cmd;
                                warn "Error in \$$name report: $@" if $@;