X-Git-Url: http://git.shiar.nl/barcat.git/blobdiff_plain/a0ff197f92a8630d87509509ad635c8f194fa29b..ca702a0a92ec32abe108dcdde6299f696362e3ab:/barcat diff --git a/barcat b/barcat index 650e406..48e47d1 100755 --- a/barcat +++ b/barcat @@ -6,7 +6,7 @@ use List::Util qw( min max sum ); use open qw( :std :utf8 ); use experimental qw( lexical_subs ); -our $VERSION = '1.05'; +our $VERSION = '1.06'; use Getopt::Long '2.33', qw( :config gnu_getopt ); my %opt; @@ -42,16 +42,25 @@ GetOptions(\%opt, " (range expected)\n" ); }, + 'header!', 'markers|m=s', 'graph-format=s' => sub { $opt{'graph-format'} = substr $_[1], 0, 1; }, 'spark:s' => sub { - $opt{spark} = [split //, $_[1] || '⎽▁▂▃▄▅▆▇█']; + $opt{spark} = [split //, $_[1] || '▁▂▃▄▅▆▇█']; + }, + 'palette:s' => sub { + $opt{palette} = [ split /\s/, $_[1] ]; }, 'stat|s!', + 'signal-stat=s', 'unmodified|u!', 'width|w=i', + 'version' => sub { + say "barcat version $VERSION"; + exit; + }, 'usage|h' => sub { local $/; my $pod = readline *DATA; @@ -84,14 +93,18 @@ $opt{units} = [split //, ' kMGTPEZYyzafpnμm'] if $opt{'human-readable'}; $opt{anchor} //= qr/\A/; $opt{'value-length'} = 6 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{palette} //= $opt{color} && [31, 90, 32]; my (@lines, @values, @order); -$SIG{QUIT} = \&show_stat; +$SIG{$_} = \&show_stat for $opt{'signal-stat'} || (); $SIG{ALRM} = sub { show_lines(); alarm $opt{interval} if defined $opt{interval} and $opt{interval} > 0; }; +$SIG{INT} = \&show_exit; if (defined $opt{interval}) { $opt{interval} ||= 1; @@ -103,12 +116,6 @@ if (defined $opt{interval}) { } or warn $@, "Expect slowdown with large datasets!\n"; } -$SIG{INT} = sub { - $SIG{INT} = 'DEFAULT'; # reset for subsequent attempts - exit if !$.; - 'IGNORE' # continue after assumed eof -}; - my $valmatch = qr/$opt{anchor} ( \h* -? [0-9]* \.? [0-9]+ (?: e[+-]?[0-9]+ )? |)/x; while (readline) { s/\r?\n\z//; @@ -156,14 +163,23 @@ my $size = ($maxval - $minval) && ($opt{width} - $lenval - $len) / ($maxval - $minval); # bar multiplication my @barmark; -if ($opt{markers} // 1 and $size > 0) { - my sub orderpos { (($order[$_[0]] + $order[$_[0] + .5]) / 2 - $minval) * $size } - $barmark[ (sum(@order) / @order - $minval) * $size ] = '='; # average - $barmark[ orderpos($#order * .31731) ] = '>'; - $barmark[ orderpos($#order * .68269) ] = '<'; - $barmark[ orderpos($#order / 2) ] = '+'; # mean - $barmark[ -$minval * $size ] = '|' if $minval < 0; # zero - color(36) for @barmark; +if ($opt{markers} and $size > 0) { + for my $markspec (split /\h/, $opt{markers}) { + my ($char, $func) = split //, $markspec, 2; + my $pos = eval { + if ($func eq 'avg') { + return sum(@order) / @order; + } + elsif ($func =~ /\A([0-9.]+)v\z/) { + my $index = $#order * $1 / 100; + return ($order[$index] + $order[$index + .5]) / 2; + } + else { + return $func; + } + }; + color(36) for $barmark[($pos - $minval) * $size] = $char; + } state $lastmax = $maxval; if ($maxval > $lastmax) { @@ -191,6 +207,14 @@ sub sival { ); } +say( + color(31), sprintf('%*s', $lenval, $minval), + color(90), '-', color(36), '+', + color(32), sprintf('%*s', $size * ($maxval - $minval) - 3, $maxval), + color(90), '-', color(36), '+', + color(0), +) if $opt{header}; + while ($nr <= $#lines) { $nr >= $opt{hidemax} and last if defined $opt{hidemax}; my $val = $values[$nr]; @@ -201,10 +225,10 @@ while ($nr <= $#lines) { } if (length $val) { - my $color = !$opt{color} ? undef : - $val == $order[0] ? 32 : # max - $val == $order[-1] ? 31 : # min - 90; + my $color = !$opt{palette} ? undef : + $val == $order[0] ? $opt{palette}->[-1] : # max + $val == $order[-1] ? $opt{palette}->[0] : # min + $opt{palette}->[1] // $opt{palette}->[0]; $val = $opt{units} ? sival($val) : sprintf "%*s", $lenval, $val; color($color) for $val; } @@ -219,7 +243,6 @@ continue { say '' if $opt{spark}; } -show_lines(); sub show_stat { if ($opt{hidemin} or $opt{hidemax}) { @@ -239,7 +262,15 @@ sub show_stat { } say ''; } -show_stat() if $opt{stat}; + +sub show_exit { + show_lines(); + show_stat() if $opt{stat}; + exit 130 if @_; # 0x80+signo + exit; +} + +show_exit(); __END__ =encoding utf8 @@ -286,6 +317,10 @@ A string can indicate the starting position of a value or capture the numbers itself, for example I<-f'(\d+)'> for the first digits anywhere. +=item --header + +Prepend a chart axis with minimum and maximum values labeled. + =item -H, --human-readable Format values using SI unit prefixes, @@ -319,38 +354,58 @@ but disregarded for padding and bar size. Glyph to repeat for the graph line. Defaults to a dash C<->. -=item -m, --markers= +=item -m, --markers= Statistical positions to indicate on bars. -Cannot be customized yet, -only disabled by providing an empty argument. - -Any value enables all marker characters: +A single indicator glyph precedes each position: =over 2 -=item B<=> +=item -Average: -the sum of all values divided by the number of counted lines. +Exact value to match on the axis. +A vertical bar at the zero crossing is displayed by I<|0> +for negative values. +For example I<:3.14> would show a colon at pi. -=item B<+> +=item I -Mean, median: +Ranked value at the given percentile. +The default shows I<+> at I<50v> for the mean or median; the middle value or average between middle values. +One standard deviation right of the mean is at about I<68.3v>. +The default includes I<< >31.73v <68.27v >> +to encompass all I results, or 68% of all entries, by B<< <--> >>. + +=item I -=item B<<> +Matches the average; +the sum of all values divided by the number of counted lines. +Indicated by default as I<=>. -Standard deviation left of the mean. -Only 16% of all values are lower. +=back -=item B<< > >> +=item --palette=... -Standard deviation right of the mean. -The part between B<< <--> >> encompass all I results, -or 68% of all entries. +Override colors of parsed numbers. +Can be any CSI escape, such as I<90> for default dark grey, +or alternatively I<1;30> for bold black. -=back +In case of additional colors, +the last is used for values equal to the maximum, the first for minima. +If unspecified, these are green and red respectively (I<31 90 32>). + +=item --spark[=] + +Replace lines by I, +single characters corresponding to input values. +A specified sequence of unicode characters will be used for +Of a specified sequence of unicode characters, +the first one will be used for non-values, +the last one for the maximum, +the second (if any) for the minimum, +and any remaining will be distributed over the range of values. +Unspecified, block fill glyphs U+2581-2588 will be used. =item -s, --stat