follow (-f) option to output partial results
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 5 Sep 2019 15:07:05 +0000 (17:07 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Thu, 5 Sep 2019 21:56:49 +0000 (23:56 +0200)
Calculate and display on every SIGALRM, with scale and statistics upto that
point.  Named like equivalent tail -f.

graph

diff --git a/graph b/graph
index cb8a4061c10c5d5bcf68ca9f8154590eb667e0bc..423f5cbede3cb3881f3401dd1b93070aa236b97e 100755 (executable)
--- 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,6 +21,15 @@ GetOptions(\my %opt,
 $opt{width} ||= $ENV{COLUMNS} || 80;
 $opt{color} //= 1;
 
+if (defined $opt{follow}) {
+       $opt{follow} ||= 1;
+       $SIG{ALRM} = sub {
+               show_lines();
+               alarm $opt{follow};
+       };
+       alarm $opt{follow};
+}
+
 my (@lines, @values);
 while (readline) {
        chomp;
@@ -37,6 +47,8 @@ while (readline) {
 }
 @lines or exit;
 
+sub show_lines {
+
 my @order  = sort { $b <=> $a } grep { length } @values;
 my $maxval = $order[0];
 my $minval = min $order[-1], 0;
@@ -48,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) ] = '<';
@@ -57,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 :
@@ -71,7 +84,11 @@ for my $nr (0 .. $#lines) {
        printf '%-*s', $len, $lines[$nr];
        print $barmark[$_] // '-' for 1 .. $size && (($val || 0) - $minval) * $size;
        say '';
+       $nr++;
+}
+
 }
+show_lines();
 
 __END__
 
@@ -95,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[=<seconds>]
+
+Interval to output partial progress.
+
 =item -l, --length=[-]<size>
 
 Trim line contents (between number and bars)
@@ -182,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 <perl@shiar.org>