calculate length percentages of width
[barcat.git] / graph
diff --git a/graph b/graph
index 14b717310a819c4272fd1cf4911f5ff6432feea5..0e07a6edcf70b32bef66252e21a312bb14cd8a72 100755 (executable)
--- a/graph
+++ b/graph
@@ -1,19 +1,33 @@
 #!/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.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!',
        'follow|f:i',
-       'trim|length|l=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;
+       },
        'markers|m=s',
+       'unmodified|u!',
        'width|w=i',
        'usage|h' => sub { podexit() },
        'help'    => sub { podexit(-verbose => 2) },
@@ -21,6 +35,7 @@ GetOptions(\my %opt,
 
 $opt{width} ||= $ENV{COLUMNS} || 80;
 $opt{color} //= -t *STDOUT;  # enable on tty
+$opt{trim}   *= $opt{width} / 100 if $opt{trimpct};
 
 if (defined $opt{follow}) {
        $opt{follow} ||= 1;
@@ -31,10 +46,13 @@ if (defined $opt{follow}) {
        alarm $opt{follow};
 }
 
+$SIG{INT} = 'IGNORE';  # continue after assumed eof
+
 my (@lines, @values);
 while (readline) {
-       chomp;
-       push @values, s/^\h* ( -? [0-9]* (?:\.[0-9]+)? )//x && $1;
+       s/\r?\n\z//;
+       s/^\h*// unless $opt{unmodified};
+       push @values, s/^ ( \h* -? [0-9]* \.? [0-9]+ |)//x && $1;
        if (defined $opt{trim}) {
                my $trimpos = abs $opt{trim};
                if ($trimpos <= 1) {
@@ -46,10 +64,14 @@ while (readline) {
        }
        push @lines, $_;
 }
-@lines or exit;
+
+$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;
@@ -68,9 +90,21 @@ if ($opt{markers} // 1 and $size > 0) {
        $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;
+       }
 }
 
-state $nr = 0;
 while ($nr <= $#lines) {
        my $val = $values[$nr];
        if (length $val) {
@@ -119,7 +153,7 @@ disabled otherwise such as when piped or redirected.
 
 Interval 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.
@@ -162,6 +196,11 @@ 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 -w, --width=<columns>
 
 Override the maximum number of columns to use.
@@ -196,7 +235,7 @@ 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
 
 Git statistics, such commit count by year: