support count combined with trim, interval
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 3 Dec 2022 01:32:56 +0000 (02:32 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 12 Dec 2022 21:44:42 +0000 (22:44 +0100)
barcat
t/t1672-missing_uniq.out [new file with mode: 0644]
t/t1673-uniq_abbreviated.out [new file with mode: 0644]
t/t1806-reset_uniq.out [new file with mode: 0644]

diff --git a/barcat b/barcat
index 9a19d02a2c141529c3ddb5cbd875e9027649986c..0f0cfb8d100aefe0a12665c8ffbd493d605b0050 100755 (executable)
--- 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 (file)
index 0000000..15f299f
--- /dev/null
@@ -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 (file)
index 0000000..9a566eb
--- /dev/null
@@ -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 (file)
index 0000000..8b9467f
--- /dev/null
@@ -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 ---<-