From: Mischa POSLAWSKY Date: Sat, 3 Dec 2022 01:32:56 +0000 (+0100) Subject: support count combined with trim, interval X-Git-Tag: v1.10~12 X-Git-Url: http://git.shiar.nl/barcat.git/commitdiff_plain/9eb6da139e4958a8f31c03e62073f1a0f6e20898 support count combined with trim, interval --- diff --git a/barcat b/barcat index 9a19d02..0f0cfb8 100755 --- a/barcat +++ b/barcat @@ -206,25 +206,29 @@ if (defined $opt{interval}) { eval { require Tie::Array::Sorted; tie @order, 'Tie::Array::Sorted', sub { $_[1] <=> $_[0] }; - } or warn $@, "Expect slowdown with large datasets!\n"; + } or warn $@, "Expect slowdown with large datasets!\n" + unless $opt{count}; } my $float = qr<[0-9]* [.]? [0-9]+ (?: e[+-]?[0-9]+ )?>; # positive numberish my $valmatch = qr< $opt{anchor} ( \h* -? $float |) >; while (defined ($_ = $opt{input} ? shift @{ $opt{input} } : readline)) { s/\r?\n\z//; + my $valnum; if ($opt{count}) { - my ($valnum) = m/$opt{anchor} (\S*)/; + ($valnum) = m/$opt{anchor} (\S*)/; $valnum //= ''; $uniq{$valnum}++ and next; - push @lines, "\n " . $_; push @values, $valnum; - next; + s/\A/\n /; + } + else { + s/\A\h*// unless $opt{unmodified}; + $valnum = s/$valmatch/\n/ && $1; + push @values, $valnum; + push @order, $valnum if length $valnum; } - s/\A\h*// unless $opt{unmodified}; - my $valnum = s/$valmatch/\n/ && $1; - push @values, $valnum; - push @order, $valnum if length $valnum; + if (defined $opt{trim} and defined $valnum) { my $trimpos = abs $opt{trim}; $trimpos -= length $valnum if $opt{unmodified}; @@ -237,6 +241,8 @@ while (defined ($_ = $opt{input} ? shift @{ $opt{input} } : readline)) { } } push @lines, $_; +} +continue { show_lines() if defined $opt{interval} and $opt{interval} < 0 and $. % $opt{interval} == 0; } @@ -257,7 +263,7 @@ state $nr = $opt{hidemin} ? $opt{hidemin}->($#lines) : 0; my $limit = $opt{hidemax} ? $opt{hidemax}->($#lines, $nr) : $#lines; if ($opt{count}) { - $_ = $uniq{$_} for @values; + $_ = $uniq{$_} for @values[$nr .. $limit]; @order = @values; } @@ -388,6 +394,7 @@ continue { $nr++; } say $opt{palette} ? color(0) : '' if $opt{spark}; +%uniq = () if $opt{interval} and $opt{count}; return $nr; } diff --git a/t/t1672-missing_uniq.out b/t/t1672-missing_uniq.out new file mode 100644 index 0000000..15f299f --- /dev/null +++ b/t/t1672-missing_uniq.out @@ -0,0 +1,4 @@ +barcat -cf1 -l0 input/duplicates.txt +18 -+----------=----->------------------ + 1 -+ + 1 -+ diff --git a/t/t1673-uniq_abbreviated.out b/t/t1673-uniq_abbreviated.out new file mode 100644 index 0000000..9a566eb --- /dev/null +++ b/t/t1673-uniq_abbreviated.out @@ -0,0 +1,5 @@ +barcat -cf+1 -l5 input/duplicolumns.txt +4 pr… ----<--------+--=------ +6 ra… ----<--------+--=---------->------ +1 pr… ----<- +1 pr… ----<- diff --git a/t/t1806-reset_uniq.out b/t/t1806-reset_uniq.out new file mode 100644 index 0000000..8b9467f --- /dev/null +++ b/t/t1806-reset_uniq.out @@ -0,0 +1,11 @@ +barcat -ct-7 input/duplicates.txt +3 thrice ---------<----+--=------>------ +2 twice ---------<----+--=--- +1 once ---------< +1 most ---------< + ----- +++++++++++++++++ +6 most ----<--+---=>-------------------- +1 ----<- +3 most ---<----+=>--- +2 indented ---<----+ +1 different ---<-