From ea4599f110cbf1c347535ac1add4299bb8a5e13e Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Thu, 5 Sep 2019 23:46:21 +0200 Subject: [PATCH] calculate length percentages of width Shorthand to facilitate relative sizes without manual preparation. Allow strings so trailing % can be accepted. --- graph | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/graph b/graph index 307c9e4..0e07a6e 100755 --- 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=[-] +=item -l, --length=[-][%] Trim line contents (between number and bars) to a maximum number of characters. -- 2.30.0