transition bar to visualise changed scale
[barcat.git] / graph
diff --git a/graph b/graph
index 423f5cbede3cb3881f3401dd1b93070aa236b97e..cdcb80c90c98064ffedf65b5d6c6ae8b8d2332e8 100755 (executable)
--- a/graph
+++ b/graph
@@ -18,8 +18,9 @@ GetOptions(\my %opt,
        'usage|h' => sub { podexit() },
        'help'    => sub { podexit(-verbose => 2) },
 ) or exit 64;  # EX_USAGE
+
 $opt{width} ||= $ENV{COLUMNS} || 80;
-$opt{color} //= 1;
+$opt{color} //= -t *STDOUT;  # enable on tty
 
 if (defined $opt{follow}) {
        $opt{follow} ||= 1;
@@ -30,6 +31,8 @@ if (defined $opt{follow}) {
        alarm $opt{follow};
 }
 
+$SIG{INT} = 'IGNORE';  # continue after assumed eof
+
 my (@lines, @values);
 while (readline) {
        chomp;
@@ -47,8 +50,12 @@ while (readline) {
 }
 @lines or exit;
 
+$SIG{INT} = 'DEFAULT';
+
 sub show_lines {
 
+state $nr = 0;
+
 my @order  = sort { $b <=> $a } grep { length } @values;
 my $maxval = $order[0];
 my $minval = min $order[-1], 0;
@@ -67,9 +74,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) {
@@ -108,9 +127,11 @@ Each line starting with a number is given a bar to visualise relative sizes.
 
 =over
 
-=item --no-color
+=item -c, --[no-]color
 
-Disable colored output of values and bar markers.
+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, --follow[=<seconds>]