From d8b46e635b34f40972271b6bdf1ea2383e3bff2a Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Sat, 27 Mar 2021 23:57:35 +0100 Subject: [PATCH] negative limit counts from last line --- barcat | 22 ++++++++++++++----- ..._#TODO.out => t1504-bottom_limit_-L-3.out} | 0 2 files changed, 16 insertions(+), 6 deletions(-) rename t/{t1504-bottom_limit_-L-3_#TODO.out => t1504-bottom_limit_-L-3.out} (100%) diff --git a/barcat b/barcat index 82f0fff..5d0f85c 100755 --- a/barcat +++ b/barcat @@ -39,8 +39,9 @@ GetOptions(\%opt, 'limit|L:s' => sub { my ($optname, $optval) = @_; $optval ||= 0; + $optval =~ /\A-[0-9]+\z/ and $optval .= '-'; # tail shorthand ($opt{hidemin}, $opt{hidemax}) = - $optval =~ m/\A (?: ([0-9]+)? - )? ([0-9]+)? \z/ or die( + $optval =~ m/\A (?: (-? [0-9]+)? - )? ([0-9]+)? \z/ or die( "Value \"$optval\" invalid for option limit", " (range expected)\n" ); @@ -205,7 +206,9 @@ sub color { sub show_lines { -state $nr = $opt{hidemin}; +state $nr = + $opt{hidemin} < 0 ? @lines + $opt{hidemin} + 1 : + $opt{hidemin}; @lines or return; @lines > $nr or return; @@ -316,9 +319,12 @@ say $opt{palette} ? color(0) : '' if $opt{spark}; sub show_stat { if ($opt{hidemin} or $opt{hidemax}) { - printf '%.8g of ', $opt{'sum-format'}->(sum(grep { length } - @values[$opt{hidemin} .. ($opt{hidemax} || @lines) - 1] - ) // 0); + my $linemin = $opt{hidemin}; + $linemin += @lines if $linemin < 0; + my $linemax = ($opt{hidemax} || @lines) - 1; + printf '%.8g of ', $opt{'sum-format'}->( + sum(grep {length} @values[$linemin .. $linemax]) // 0 + ); } if (@order) { my $total = sum @order; @@ -422,9 +428,13 @@ unless C<--length=0>. Prepend a dash (i.e. make negative) to enforce padding regardless of encountered contents. -=item -L, --limit[=( | -[])] +=item -L, --limit[=( | - | -[])] Stop output after a number of lines. +A single value indicates the last line number (like C), +or first line counting from the bottom if negative (like C). +A specific range can be given by two values. + All input is still counted and analyzed for statistics, but disregarded for padding and bar size. diff --git a/t/t1504-bottom_limit_-L-3_#TODO.out b/t/t1504-bottom_limit_-L-3.out similarity index 100% rename from t/t1504-bottom_limit_-L-3_#TODO.out rename to t/t1504-bottom_limit_-L-3.out -- 2.30.0