t: strip options from input file names
[barcat.git] / barcat
diff --git a/barcat b/barcat
index 15a2269c9829079d81e0b86368e9a398e3d566f4..9cfe3e3df3330b69a2f762372e76ee2af606db54 100755 (executable)
--- a/barcat
+++ b/barcat
@@ -35,7 +35,7 @@ GetOptions(\%opt,
        'hidemax=i',
        'minval=f',
        'maxval=f',
        'hidemax=i',
        'minval=f',
        'maxval=f',
-       'limit|L=s' => sub {
+       'limit|L:s' => sub {
                my ($optname, $optval) = @_;
                $optval ||= 0;
                ($opt{hidemin}, $opt{hidemax}) =
                my ($optname, $optval) = @_;
                $optval ||= 0;
                ($opt{hidemin}, $opt{hidemax}) =
@@ -50,10 +50,24 @@ GetOptions(\%opt,
                $opt{'graph-format'} = substr $_[1], 0, 1;
        },
        'spark:s' => sub {
                $opt{'graph-format'} = substr $_[1], 0, 1;
        },
        'spark:s' => sub {
-               $opt{spark} = [split //, $_[1] || '▁▂▃▄▅▆▇█'];
+               $opt{spark} = [split //, $_[1] || ' ▁▂▃▄▅▆▇█'];
        },
        },
-       'palette:s' => sub {
-               $opt{palette} = [ split /\s/, $_[1] ];
+       'palette=s' => sub {
+               $opt{palette} = {
+                       fire   => [qw( 90 31 91 33 93 97 96 )],
+                       fire88 => [map {"38;5;$_"} qw(
+                               80  32 48 64  68 72 76  77 78 79  47
+                       )],
+                       fire256=> [map {"38;5;$_"} qw(
+                               235  52 88 124 160 196
+                               202 208 214 220 226  227 228 229 230 231  159
+                       )],
+                       ramp88 => [map {"38;5;$_"} qw(
+                               64 65 66 67 51 35 39 23 22 26 25 28
+                       )],
+                       whites => [qw( 1;30 0;37 1;37 )],
+                       greys  => [map {"38;5;$_"} 52, 235..255, 47],
+               }->{$_[1]} // [ split /[^0-9;]/, $_[1] ];
        },
        'stat|s!',
        'signal-stat=s',
        },
        'stat|s!',
        'signal-stat=s',
@@ -87,7 +101,7 @@ GetOptions(\%opt,
        },
 ) or exit 64;  # EX_USAGE
 
        },
 ) or exit 64;  # EX_USAGE
 
-$opt{width} ||= $ENV{COLUMNS} || 80;
+$opt{width} ||= $ENV{COLUMNS} || qx(tput cols) || 80;
 $opt{color} //= -t *STDOUT;  # enable on tty
 $opt{'graph-format'} //= '-';
 $opt{trim}   *= $opt{width} / 100 if $opt{trimpct};
 $opt{color} //= -t *STDOUT;  # enable on tty
 $opt{'graph-format'} //= '-';
 $opt{trim}   *= $opt{width} / 100 if $opt{trimpct};
@@ -222,18 +236,23 @@ while ($nr <= $#lines) {
        $nr >= $opt{hidemax} and last if defined $opt{hidemax};
        my $val = $values[$nr];
        my $rel = length $val && ($val - $minval) / ($maxval - $minval);
        $nr >= $opt{hidemax} and last if defined $opt{hidemax};
        my $val = $values[$nr];
        my $rel = length $val && ($val - $minval) / ($maxval - $minval);
+       my $color = !length $val || !$opt{palette} ? undef :
+               $val == $order[0] ? $opt{palette}->[-1] : # max
+               $val == $order[-1] ? $opt{palette}->[0] : # min
+               $opt{palette}->[ $rel * ($#{$opt{palette}} - 1) + 1 ];
 
        if ($opt{spark}) {
 
        if ($opt{spark}) {
-               print color($opt{palette}->[ $rel * $#{$opt{palette}} ]) if $opt{palette};
-               print $opt{spark}->[ $rel * $#{$opt{spark}} ];
+               print color($color), $opt{spark}->[
+                       !$val ? 0 : # blank
+                       $val == $order[0] ? -1 : # max
+                       $val == $order[-1] ? 1 : # min
+                       $#{$opt{spark}} < 3 ? 1 :
+                       $rel * ($#{$opt{spark}} - 3) + 2.5
+               ];
                next;
        }
 
        if (length $val) {
                next;
        }
 
        if (length $val) {
-               my $color = !$opt{palette} ? undef :
-                       $val == $order[0] ? $opt{palette}->[-1] : # max
-                       $val == $order[-1] ? $opt{palette}->[0] : # min
-                       $opt{palette}->[ $rel * ($#{$opt{palette}} - 1) + 1 ];
                $val = $opt{units} ? sival($val) : sprintf "%*s", $lenval, $val;
                color($color) for $val;
        }
                $val = $opt{units} ? sival($val) : sprintf "%*s", $lenval, $val;
                color($color) for $val;
        }
@@ -348,7 +367,7 @@ unless C<--length=0>.
 Prepend a dash (i.e. make negative) to enforce padding
 regardless of encountered contents.
 
 Prepend a dash (i.e. make negative) to enforce padding
 regardless of encountered contents.
 
-=item -L, --limit=(<count>|<start>-[<end>])
+=item -L, --limit[=(<count> | <start>-[<end>])]
 
 Stop output after a number of lines.
 All input is still counted and analyzed for statistics,
 
 Stop output after a number of lines.
 All input is still counted and analyzed for statistics,
@@ -396,7 +415,7 @@ Bars extend from 0 or the minimum value if lower,
 to the largest value encountered.
 These options can be set to customize this range.
 
 to the largest value encountered.
 These options can be set to customize this range.
 
-=item --palette=<color>...
+=item --palette=(<preset> | <color>...)
 
 Override colors of parsed numbers.
 Can be any CSI escape, such as I<90> for default dark grey,
 
 Override colors of parsed numbers.
 Can be any CSI escape, such as I<90> for default dark grey,