X-Git-Url: http://git.shiar.nl/barcat.git/blobdiff_plain/81dbf7f4a7d4c1e0f41716c57360635a1365910e..763e9e7e36fb056e87849dca56532b21e3c500cd:/graph diff --git a/graph b/graph index 501ac3c..423f5cb 100755 --- a/graph +++ b/graph @@ -5,12 +5,13 @@ use utf8; use List::Util qw( min max sum ); use open qw( :std :utf8 ); -our $VERSION = '1.01'; +our $VERSION = '1.02'; use Getopt::Long '2.33', qw( :config gnu_getopt ); sub podexit { require Pod::Usage; Pod::Usage::pod2usage(-exitval => 0, @_) } GetOptions(\my %opt, 'color|c!', + 'follow|f:i', 'trim|length|l=i', 'markers|m=s', 'width|w=i', @@ -20,16 +21,35 @@ GetOptions(\my %opt, $opt{width} ||= $ENV{COLUMNS} || 80; $opt{color} //= 1; -my @lines = readline or exit; -chomp for @lines; -my @values = map { s/^\h* ( -? [0-9]* (?:\.[0-9]+)? )//x and $1 } @lines; -my @order = sort { $b <=> $a } grep { length } @values; -if (defined $opt{trim}) { - my $trimpos = abs $opt{trim}; - $trimpos <= 1 ? ($_ = substr($_, 0, 1)) : - (length > $trimpos and substr($_, $trimpos - 1) = '…') for @lines; +if (defined $opt{follow}) { + $opt{follow} ||= 1; + $SIG{ALRM} = sub { + show_lines(); + alarm $opt{follow}; + }; + alarm $opt{follow}; +} + +my (@lines, @values); +while (readline) { + chomp; + push @values, s/^\h* ( -? [0-9]* (?:\.[0-9]+)? )//x && $1; + if (defined $opt{trim}) { + my $trimpos = abs $opt{trim}; + if ($trimpos <= 1) { + $_ = substr $_, 0, 1; + } + elsif (length > $trimpos) { + substr($_, $trimpos - 1) = '…'; + } + } + push @lines, $_; } +@lines or exit; + +sub show_lines { +my @order = sort { $b <=> $a } grep { length } @values; my $maxval = $order[0]; my $minval = min $order[-1], 0; my $lenval = max map { length } @order; @@ -40,7 +60,7 @@ my $size = ($maxval - $minval) && my @barmark; if ($opt{markers} // 1 and $size > 0) { - sub orderpos { (($order[$_[0]] + $order[$_[0] + .5]) / 2 - $minval) * $size } + 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) ] = '<'; @@ -49,7 +69,8 @@ if ($opt{markers} // 1 and $size > 0) { defined and $opt{color} and $_ = "\e[36m$_\e[0m" for @barmark; } -for my $nr (0 .. $#lines) { +state $nr = 0; +while ($nr <= $#lines) { my $val = $values[$nr]; if (length $val) { my $color = !$opt{color} ? 0 : @@ -63,8 +84,12 @@ for my $nr (0 .. $#lines) { printf '%-*s', $len, $lines[$nr]; print $barmark[$_] // '-' for 1 .. $size && (($val || 0) - $minval) * $size; say ''; + $nr++; } +} +show_lines(); + __END__ =head1 NAME @@ -87,6 +112,10 @@ Each line starting with a number is given a bar to visualise relative sizes. Disable colored output of values and bar markers. +=item -f, --follow[=] + +Interval to output partial progress. + =item -l, --length=[-] Trim line contents (between number and bars) @@ -174,6 +203,11 @@ Or the most frequent authors: git shortlog -sn | graph | head -3 +Latency history: + + ping google.com | + perl -pe '$|=1; print s/ time=(.*)// ? "$1 for " : "> "' | graph -f + =head1 AUTHOR Mischa POSLAWSKY