sample options (original line(s), unique threshold)
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 3 Feb 2011 23:09:39 +0000 (00:09 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Fri, 4 Feb 2011 00:47:28 +0000 (01:47 +0100)
git-grep-footer

index bc71345250a9190b0e9297b4b1e0572dfc6df5a5..906ae929f56f1cb439af2dcb0cc9c7dbbace0e4e 100755 (executable)
@@ -10,8 +10,9 @@ use Getopt::Long;
 GetOptions(\my %opt,
        'debug!',
        'simplify|s:s',
-       'unique|u!',
        'ignore-case|i!',
+       'min|min-count|unique|u:i',
+       'max|max-count|show|n:i',
 ) or die;
 
 local $| = 1;
@@ -53,6 +54,8 @@ while (readline) {
                                next LINE;
                        };
 
+                       push @header, $_ if defined $opt{max};
+
                        given ($opt{simplify} // 'no') {
                                when ('strict') {
                                        $header[1] =~ s{
@@ -64,7 +67,7 @@ while (readline) {
                                }
                                when (['text', '']) {
                                        when ($header[0] =~ /[ _-] (?: by | to ) $/imsx) {
-                                               pop @header;
+                                               $header[1] = undef;
                                        }
                                        for ($header[1]) {
                                                s{\b (https?)://\S+ }{[$1]}gmsx;  # url
@@ -74,7 +77,7 @@ while (readline) {
                                        }
                                }
                                when (['all', 'any']) {
-                                       pop @header;
+                                       $header[1] = undef;
                                }
                                when ('no') {
                                }
@@ -84,9 +87,11 @@ while (readline) {
                        }
 
                        if ($opt{'ignore-case'}) {
-                               $_ = lc for @header;
+                               $_ = lc for $header[0], $header[1] // ();
                        }
 
+                       pop @header if not defined $header[-1];
+
                        push @headers, \@header;
                }
 
@@ -97,11 +102,13 @@ while (readline) {
                }
 
                for (@headers) {
-                       if ($opt{unique}) {
+                       if (defined $opt{min} or $opt{max}) {
                                state $seen;
-                               next if $seen->{ $_->[0] }->{ $_->[1] // '' }++;
+                               my $count = $seen->{ $_->[0] }->{ $_->[1] // '' }++;
+                               next if $count >= ($opt{min} // 0) + ($opt{max} || 1);
+                               next if $count < ($opt{min} // 0);
                        }
-                       say join ': ', @$_;
+                       say $_->[2] // join(': ', @$_);
                }
 
                last BLOCK;