-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Maximum and minimum file size #534
base: dev
Are you sure you want to change the base?
Changes from 3 commits
18ed99b
aa6bda4
9bf5f9a
c77b2ec
3652c49
ea9f26b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package App::Ack::Filter::Size; | ||
|
||
use strict; | ||
use warnings; | ||
use base 'App::Ack::Filter'; | ||
|
||
use Carp 'croak'; | ||
|
||
use App::Ack::Filter (); | ||
|
||
sub _parse_size { | ||
my $s = $_[0] || return 0; | ||
|
||
if ( $s =~ m/^\s*(\d+(?:\.\d+)?)(?:\s*([KMGT]?)B?)?\s*$/i ) { | ||
my $n = $1; | ||
if ($2) { | ||
my $u = lc $2; | ||
$n *= 1024 while $u =~ tr/tgmk/gmk/d; | ||
} | ||
return int $n; | ||
} | ||
else { | ||
Carp::croak('Invalid size'); | ||
} | ||
} | ||
|
||
sub new { | ||
my ( $class, $min, $max ) = @_; | ||
return bless { | ||
min => $min, | ||
max => $max, | ||
}, $class; | ||
} | ||
|
||
sub filter { | ||
my ( $self, $resource ) = @_; | ||
|
||
my $min = $self->{'min'} || 0; | ||
my $max = $self->{'max'}; | ||
|
||
my $file = $resource->name; | ||
|
||
return 1 if $file eq '-'; | ||
|
||
my $size = (-s $file) || 0; # paranoid? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is going to incur an extra There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, drat, looks like a commit went missing from the PR, which was to remove ine 43 (which is redundant) and use -s _ |
||
|
||
return 0 if $max and $size > $max; | ||
return $size >= $min; | ||
} | ||
|
||
sub inspect { | ||
my ( $self ) = @_; | ||
|
||
my $min = $self->{'min'} || 0; | ||
my $max = $self->{'max'} || '*'; | ||
|
||
return ref($self) . " - $min..$max"; | ||
} | ||
|
||
sub to_string { | ||
shift->inspect; | ||
} | ||
|
||
BEGIN { | ||
App::Ack::Filter->register_filter(size => __PACKAGE__); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if it makes sense to register this filter; registered filters are used for If we're not going to use this as a registered filter, I don't know if it makes sense to implement this feature using a filter class; users using the feature are going to incur extra overhead from a method call, which really adds up in large codebases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I was sticking to the form of the existing code without really understanding the workings, and beginning to come to the same conclusion. In practice, I don't particularly see size-based types being defined. I'm happy to just stick the logic straight into the ack script itself. |
||
} | ||
|
||
1; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!perl -T | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use Test::More tests => 12; | ||
use lib 't'; | ||
use Util; | ||
|
||
prep_environment(); | ||
|
||
my ( $stdout, $stderr ); | ||
my $help_types_output; | ||
|
||
# sanity check | ||
( $stdout, $stderr ) = run_ack_with_stderr('--perl', '-f', 't/swamp'); | ||
is( scalar(@{$stdout}), 11, 'Found initial 11 files' ); | ||
is_empty_array( $stderr, 'Nothing in stderr' ); | ||
|
||
( $stdout, $stderr ) = run_ack_with_stderr('--perl', '--max-file-size=0', '-f', 't/swamp'); | ||
is( scalar(@{$stdout}), 11, 'Found initial 11 files (max of 0 has no effect)' ); | ||
is_empty_array( $stderr, 'Nothing in stderr' ); | ||
|
||
( $stdout, $stderr ) = run_ack_with_stderr('--perl', '--max-file-size=100', '-f', 't/swamp'); | ||
is( scalar(@{$stdout}), 3, 'Found 3 files <= 100 bytes large' ); | ||
is_empty_array( $stderr, 'Nothing in stderr' ); | ||
|
||
( $stdout, $stderr ) = run_ack_with_stderr('--perl', '--max-file-size=101', '-f', 't/swamp'); | ||
is( scalar(@{$stdout}), 3, 'Found 8 files >= 101 bytes large' ); | ||
is_empty_array( $stderr, 'Nothing in stderr' ); | ||
|
||
( $stdout, $stderr ) = run_ack_with_stderr('--perl', '--min-file-size=101', '--max-file-size=150', '-f', 't/swamp'); | ||
is( scalar(@{$stdout}), 1, 'Found 1 file where 101 <= size <= 150' ); | ||
is_empty_array( $stderr, 'Nothing in stderr' ); | ||
|
||
( $stdout, $stderr ) = run_ack_with_stderr('--perl', '--max-file-size=100', '--min-file-size=101', '-f', 't/swamp'); | ||
is( scalar(@{$stdout}), 0, 'Found no files when max and min conflict' ); | ||
is_empty_array( $stderr, 'Nothing in stderr' ); | ||
|
||
# done testing |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!perl -T | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use Test::More tests => 1; | ||
|
||
use App::Ack::Filter::Size; | ||
|
||
pass( 'App::Ack::Filter::Size loaded with nothing else loaded first' ); | ||
|
||
done_testing(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty clever, but it might be too clever; maybe we should just have a hash of suffixes => number of bytes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And are we going to distinguish between MB and MiB, GB and GiB?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I keep being surprised at how decimal things are nowadays. Looks like things like
ls -l
defaults to 1K=1000 so I guess yes, we should.