calculate length percentages of width
[barcat.git] / graph
diff --git a/graph b/graph
index e76ec645492b138a49ddfb11a52f1adf2e4b2088..0e07a6edcf70b32bef66252e21a312bb14cd8a72 100755 (executable)
--- a/graph
+++ b/graph
@@ -1,9 +1,10 @@
 #!/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';
 
@@ -12,10 +13,19 @@ sub podexit {
        require Pod::Usage;
        Pod::Usage::pod2usage(-exitval => 0, -perldocopt => '-oman', @_);
 }
-GetOptions(\my %opt,
+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',
@@ -25,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;
@@ -53,13 +64,13 @@ 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];
@@ -142,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.