X-Git-Url: http://git.shiar.nl/barcat.git/blobdiff_plain/7dfac791260c54c3369e895d030a91b21ebdda43..261e0bd8de1f84ceeaa417c9aed73c9862419416:/barcat diff --git a/barcat b/barcat index faabb3f..3eb004e 100755 --- a/barcat +++ b/barcat @@ -40,8 +40,6 @@ GetOptions(\%opt, $opt{trim} = $optval; }, 'value-length=i', - 'hidemin=i', - 'hidemax=i', 'minval=f', 'maxval=f', 'limit|L:s' => sub { @@ -49,12 +47,35 @@ GetOptions(\%opt, $optval ||= 0; $optval =~ /\A-[0-9]+\z/ and $optval .= '-'; # tail shorthand $optval =~ s/[+]/--/; - ($opt{hidemin}, $opt{hidemax}) = + my ($start, $end) = $optval =~ m/\A (?: (-? [0-9]+)? - )? (-? [0-9]+)? \z/ or die( "Value \"$optval\" invalid for option limit", " (range expected)\n" ); - s/\A-0*\z// and $_ ||= undef for $opt{hidemax} // (); + $start ||= 1; + $start--; + s/\A-0*\z// and $_ ||= undef for $end // (); + + $opt{hidemin} = sub { + my ($lines) = @_; + if ($start < 0) { + return max(0, $lines + $start + 2); + } + return $start; + } if $start; + $opt{hidemax} = sub { + my ($limit, $offset) = @_; + if ($end < 0) { + return $offset - $end - 1; # count + } + elsif ($start < 0) { + return $limit - $end + 1; # bottom + } + elsif ($end <= $limit) { + return $end - 1; # less + } + return $limit; + } if defined $end; }, 'log|e!', 'header!', @@ -125,16 +146,21 @@ $opt{'value-length'} = 4 if $opt{units}; $opt{'value-length'} = 1 if $opt{unmodified}; $opt{'signal-stat'} //= exists $SIG{INFO} ? 'INFO' : 'QUIT'; $opt{markers} //= '=avg >31.73v <68.27v +50v |0'; -$opt{report} //= join(', ', - '${min; color(31)} min', - '${avg; $opt{reformat} or $_ = sprintf "%0.2f", $_; color(36)} avg', - '${max; color(32)} max', +$opt{report} //= join('', + '${partsum+; $_ .= " of "}', + '${sum+; color(1); $_ .= " total in "}', + '${count#} values', + '${lines#; $_ = $_ != @order && " over $_ lines"}', + sprintf('${count: (%s)}', join ', ', + '${min; color(31)} min', + '${avg; $opt{reformat} or $_ = sprintf "%0.2f", $_; color(36)} avg', + '${max; color(32)} max', + ), ); $opt{palette} //= $opt{color} && [31, 90, 32]; $opt{indicators} = [split //, $opt{indicators} || ($opt{ascii} ? ' .oO' : $opt{spark} ? ' ▁▂▃▄▅▆▇█' : ' ▏▎▍▌▋▊▉█') ] if defined $opt{indicators} or $opt{spark}; -$opt{hidemin} = ($opt{hidemin} || 1) - 1; $opt{input} = (@ARGV && $ARGV[0] =~ m/\A[-0-9]/) ? \@ARGV : undef and undef $opt{interval}; @@ -216,23 +242,10 @@ sub color { sub show_lines { -state $nr = - $opt{hidemin} < 0 ? max(0, @lines + $opt{hidemin} + 1) : - $opt{hidemin}; +state $nr = $opt{hidemin} ? $opt{hidemin}->($#lines) : 0; @lines > $nr or return; -my $limit = $#lines; -if (defined $opt{hidemax}) { - if ($opt{hidemax} < 0) { - $limit = $nr - $opt{hidemax} - 1; - } - elsif ($opt{hidemin} and $opt{hidemin} < 0) { - $limit -= $opt{hidemax} - 1; - } - elsif ($opt{hidemax} <= $limit) { - $limit = $opt{hidemax} - 1; - } -} +my $limit = $opt{hidemax} ? $opt{hidemax}->($#lines, $nr) : $#lines; @order = sort { $b <=> $a } @order unless tied @order; my $maxval = $opt{maxval} // ( @@ -294,7 +307,7 @@ if ($opt{markers} and $size > 0) { state $lastmax = $maxval; if ($maxval > $lastmax) { print ' ' x ($lenval + $len); - printf color(90); + print color(90); printf '%-*s', ($lastmax - $minval) * $size / $range + .5, '-' x (($values[$nr - 1] - $minval) * $size / $range); @@ -366,54 +379,45 @@ say $opt{palette} ? color(0) : '' if $opt{spark}; } sub show_stat { - if ($opt{hidemin} or $opt{hidemax}) { - my $linemin = $opt{hidemin}; - my $linemax = ($opt{hidemax} || @lines) - 1; - if ($linemin < 0) { - $linemin += @lines; - $linemax = @lines - $linemax; - } - print varfmt('${sum+} of ', { - lines => $linemax - $linemin + 1, - sum => sum(0, grep {length} @values[$linemin .. $linemax]), - }); - } + my %vars = ( + count => int @order, + lines => int @lines, + ); + my $linemin = !$opt{hidemin} ? 0 : + ($vars{start} = $opt{hidemin}->($#lines)); + my $linemax = !$opt{hidemax} ? $#lines : + ($vars{end} = $opt{hidemax}->($#lines, $vars{start})); if (@order) { - my $total = sum @order; - 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, - count => int @order, - lines => int @lines, + $vars{partsum} = sum(0, grep {length} @values[$linemin .. $linemax]) + if $linemin <= $linemax and ($opt{hidemin} or $opt{hidemax}); + %vars = (%vars, + sum => sum(@order), min => $order[-1], max => $order[0], - avg => $total / @order, - }); + ); + $vars{avg} = $vars{sum} / @order; } - say ''; + say varfmt($opt{report}, \%vars); return 1; } sub varfmt { my ($fmt, $vars) = @_; $fmt =~ s[\$\{ \h*+ ((?: [^{}]++ | \{(?1)\} )+) \}]{ - my ($name, $cmd) = split /\s*;/, $1, 2; + my ($name, $op, $cmd) = split /\s*([;:])/, $1, 2; my $format = $name =~ s/\+// || $name !~ s/\#// && $opt{reformat}; local $_ = $vars->{$name}; - if (defined) { + defined && do { $_ = $opt{'value-format'}->($_) if $format; - if ($cmd) { + if ($cmd and $op eq ':') { + $_ = varfmt($cmd, $vars); + } + elsif ($cmd) { eval $cmd; warn "Error in \$$name report: $@" if $@; } $_; } - else { - warn "Unknown variable \$$name in report\n"; - "\$$name"; - } }eg; return $fmt; } @@ -644,6 +648,10 @@ Unspecified, block fill glyphs U+2581-2588 will be used. Total statistics after all data. +While processing (possibly a neverending pipe), +intermediate results are also shown on signal I if available (control+t on BSDs) +or I otherwise (ctrl+\ on linux). + =item -u, --unmodified Do not reformat values, keeping leading whitespace.