calculate length percentages of width v1.02
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 5 Sep 2019 21:46:21 +0000 (23:46 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Thu, 5 Sep 2019 21:56:49 +0000 (23:56 +0200)
Shorthand to facilitate relative sizes without manual preparation.
Allow strings so trailing % can be accepted.

graph

diff --git a/graph b/graph
index 307c9e41d47bfc031ad0e897ee120d4c9c3a19f4..0e07a6edcf70b32bef66252e21a312bb14cd8a72 100755 (executable)
--- a/graph
+++ b/graph
@@ -13,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',
@@ -26,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;
@@ -143,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.