value-length option to override $lenval
[barcat.git] / graph
diff --git a/graph b/graph
index 7118dc198a373b799c99065a147d75c8f5604e91..ea17d4538eafe629e45323dae754ca4d3cea40e7 100755 (executable)
--- a/graph
+++ b/graph
 #!/usr/bin/env perl
-use 5.014;
+use 5.018;
 use warnings;
 use utf8;
 use List::Util qw( min max sum );
 use open qw( :std :utf8 );
+use experimental qw( lexical_subs );
 
-our $VERSION = '1.00';
+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,
+sub podexit {
+       require Pod::Usage;
+       Pod::Usage::pod2usage(-exitval => 0, -perldocopt => '-oman', @_);
+}
+my %opt;
+GetOptions(\%opt,
        'color|c!',
-       'trim|length|l=i',
+       'C' => sub { $opt{color} = 0 },
+       'field|f=s',
+       'interval|t:i',
+       'trim|length|l=s' => sub {
+               my ($optname, $optval) = @_;
+               $optval =~ s/%$// and $opt{trimpct}++;
+               $optval =~ m/^-?[0-9]+$/ or die(
+                       "Value \"$optval\" invalid for option $optname",
+                       " (number or percentage expected)\n"
+               );
+               $opt{trim} = $optval;
+       },
+       'value-length=i',
        'markers|m=s',
+       'unmodified|u!',
        'width|w=i',
        'usage|h' => sub { podexit() },
        'help'    => sub { podexit(-verbose => 2) },
 ) or exit 64;  # EX_USAGE
+
 $opt{width} ||= $ENV{COLUMNS} || 80;
-$opt{color} //= 1;
-$opt{trim} //= 0;
+$opt{color} //= -t *STDOUT;  # enable on tty
+$opt{trim}   *= $opt{width} / 100 if $opt{trimpct};
+
+if (defined $opt{interval}) {
+       $opt{interval} ||= 1;
+       $SIG{ALRM} = sub {
+               show_lines();
+               alarm $opt{interval};
+       };
+       alarm $opt{interval};
+}
 
-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;
-length > $opt{trim} + 1 and substr($_, $opt{trim}) = '…' for @lines;
+$SIG{INT} = 'IGNORE';  # continue after assumed eof
+
+my (@lines, @values);
+my $anchor = !defined $opt{field} ? qr/\A/ :
+       $opt{field} =~ /^[0-9]+$/ ? qr/(?:\S*\h+){$opt{field}}\K/ :
+       $opt{field};
+while (readline) {
+       s/\r?\n\z//;
+       s/^\h*// unless $opt{unmodified};
+       push @values, s/$anchor ( \h* -? [0-9]* \.? [0-9]+ |)/\n/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, $_;
+}
+
+$SIG{INT} = 'DEFAULT';
+
+sub show_lines {
 
+state $nr = 0;
+@lines and @lines > $nr or return;
+
+my @order  = sort { $b <=> $a } grep { length } @values;
 my $maxval = $order[0];
 my $minval = min $order[-1], 0;
-my $lenval = max map { length } @order;
-my $len    = $opt{trim} ? $opt{trim} + 2 :
-       1 + max map { length } @lines;  # left padding
+my $lenval = $opt{'value-length'} // max map { length } @order;
+my $len    = defined $opt{trim} && $opt{trim} <= 0 ? -$opt{trim} + 1 :
+       max map { length $values[$_] && length $lines[$_] } 0 .. $#lines;  # left padding
 my $size   = ($maxval - $minval) &&
        ($opt{width} - $lenval - $len) / ($maxval - $minval);  # bar multiplication
 
 my @barmark;
-if ($opt{markers} // 1) {
-       sub orderpos { (($order[$_[0]] + $order[$_[0] + .5]) / 2 - $minval) * $size }
+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
        defined and $opt{color} and $_ = "\e[36m$_\e[0m" for @barmark;
+
+       state $lastmax = $maxval;
+       if ($maxval > $lastmax) {
+               print ' ' x ($lenval + $len);
+               printf "\e[90m" if $opt{color};
+               printf '%-*s',
+                       ($lastmax - $minval) * $size + .5,
+                       '-' x (($values[$nr - 1] - $minval) * $size);
+               print "\e[92m" if $opt{color};
+               say '+' x (($maxval - $lastmax - $minval) * $size + .5);
+               print "\e[0m" if $opt{color};
+               $lastmax = $maxval;
+       }
 }
 
-for my $nr (0 .. $#lines) {
+while ($nr <= $#lines) {
        my $val = $values[$nr];
        if (length $val) {
                my $color = !$opt{color} ? 0 :
                        $val == $order[0] ? 32 : # max
                        $val == $order[-1] ? 31 : # min
                        90;
-               printf "\e[%sm", $color if $color;
-               printf "%*s", $lenval, $val;
-               print "\e[0m" if $color;
+               $val = sprintf "%*s", $lenval, $val;
+               $val = "\e[${color}m$val\e[0m" if $color;
        }
-       printf '%-*s', $len, $lines[$nr];
-       print $barmark[$_] // '-' for 1 .. (($val || 0) - $minval) * $size;
+       my $line = $lines[$nr] =~ s/\n/$val/r;
+       printf '%-*s', $len + length($val), $line;
+       print $barmark[$_] // '-' for 1 .. $size && (($values[$nr] || 0) - $minval) * $size;
        say '';
+       $nr++;
 }
 
+}
+show_lines();
+
 __END__
 
 =head1 NAME
@@ -72,21 +143,45 @@ B<graph> [<options>] [<input>]
 
 =head1 DESCRIPTION
 
-Each line starting with a number is given a bar to visualise relative sizes.
+Visualizes relative sizes of values read from input (file(s) or STDIN).
+Contents are concatenated similar to I<cat>,
+but numbers are reformatted and a bar graph is appended to each line.
 
 =head1 OPTIONS
 
 =over
 
-=item --no-color
+=item -c, --[no-]color
+
+Force colored output of values and bar markers.
+Defaults on if output is a tty,
+disabled otherwise such as when piped or redirected.
+
+=item -f, --field=(<number>|<regexp>)
+
+Compare values after a given number of whitespace separators,
+or matching a regular expression.
+
+Unspecified or I<-f0> means values are at the start of each line.
+With I<-f1> the second word is taken instead.
+A string can indicate the starting position of a value
+(such as I<-f:> if preceded by colons),
+or capture the numbers itself,
+for example I<-f'(\d+)'> for the first digits anywhere.
+
+=item -t, --interval[=<seconds>]
 
-Disable colored output of values and bar markers.
+Interval time to output partial progress.
 
-=item -l, --length=<size>
+=item -l, --length=[-]<size>[%]
 
 Trim line contents (between number and bars)
 to a maximum number of characters.
-The exceeding part is replaced by an abbreviation sign.
+The exceeding part is replaced by an abbreviation sign,
+unless C<--length=0>.
+
+Prepend a dash (i.e. make negative) to enforce padding
+regardless of encountered contents.
 
 =item -m, --markers=
 
@@ -105,11 +200,31 @@ the sum of all values divided by the number of counted lines.
 
 =item B<+>
 
-Median:
+Mean, median:
 the middle value or average between middle values.
 
+=item B<<>
+
+Standard deviation left of the mean.
+Only 16% of all values are lower.
+
+=item B<< > >>
+
+Standard deviation right of the mean.
+The part between B<< <--> >> encompass all I<normal> results,
+or 68% of all entries.
+
 =back
 
+=item -u, --unmodified
+
+Do not strip leading whitespace.
+Keep original value alignment, which may be significant in some programs.
+
+=item --value-length=<size>
+
+Reserved space for numbers.
+
 =item -w, --width=<columns>
 
 Override the maximum number of columns to use.
@@ -144,7 +259,29 @@ Number of HTTP requests per day:
 Any kind of database query with leading counts:
 
     echo 'SELECT count(*),schemaname FROM pg_tables GROUP BY 2' |
-    psql -t | graph
+    psql -t | graph -u
+
+Exchange rate USD/EUR history from CSV download provided by ECB:
+
+    curl https://sdw.ecb.europa.eu/export.do \
+         -Gd 'node=SEARCHRESULTS&q=EXR.D.USD.EUR.SP00.A&exportType=csv' |
+    grep '^[12]' | graph -f',\K' --value-length=7
+
+Total population history from the World Bank dataset (XML):
+
+    curl http://api.worldbank.org/v2/country/1W/indicator/SP.POP.TOTL |
+    xmllint --xpath '//*[local-name()="date" or local-name()="value"]' - |
+    sed -r 's,</wb:value>,\n,g; s,(<[^>]+>)+, ,g' | graph -f1
+
+Movies per year from prepared JSON data:
+
+    curl https://github.com/prust/wikipedia-movie-data/raw/master/movies.json |
+    jq '.[].year' | uniq -c | graph
+
+Pokémon height comparison:
+
+       curl https://github.com/Biuni/PokemonGO-Pokedex/raw/master/pokedex.json |
+       jq -r '.pokemon[] | [.height,.num,.name] | join(" ")' | graph
 
 Git statistics, such commit count by year:
 
@@ -154,6 +291,10 @@ Or the most frequent authors:
 
     git shortlog -sn | graph | head -3
 
+Latency history:
+
+    ping google.com | graph -f'time=\K' -t
+
 =head1 AUTHOR
 
 Mischa POSLAWSKY <perl@shiar.org>