Skip to content

Commit

Permalink
move search() method to App::Ack
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Aug 19, 2007
1 parent 0e2288d commit 0014cba
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 178 deletions.
93 changes: 93 additions & 0 deletions Ack.pm
Expand Up @@ -548,5 +548,98 @@ sub _search_v {
return $nmatches;
} # _search_v()

=head2 search
Main search method
=cut

sub search {
my $filename = shift;
my $regex = shift;
my %opt = @_;

my $is_binary;

my $fh;
if ( $filename eq '-' ) {
$fh = *STDIN;
$is_binary = 0;
}
else {
if ( !open( $fh, '<', $filename ) ) {
App::Ack::warn( "$filename: $!" );
return;
}
$is_binary = -B $filename;
}

# Negated counting is a pain, so I'm putting it in its own
# optimizable subroutine.
if ( $opt{v} ) {
return App::Ack::_search_v( $fh, $is_binary, $filename, $regex, %opt );
}

my $nmatches = 0;
local $_ = undef;
while (<$fh>) {
next unless $opt{passthru} || /$regex/o;
++$nmatches;
next if $opt{count}; # Counting means no lines

# No point in searching more if we only want a list,
# and don't want a count.
last if $opt{l};

if ( $is_binary ) {
print "Binary file $filename matches\n";
last;
}

my $out;
if ( $opt{o} ) {
$out = $opt{o}->() . "\n";
}
else {
$out = $_;
$out =~ s/($regex)/Term::ANSIColor::colored($1,$ENV{ACK_COLOR_MATCH})/eg if $opt{color};
}

if ( $opt{show_filename} ) {
my $display_filename =
$opt{color}
? Term::ANSIColor::colored( $filename, $ENV{ACK_COLOR_FILENAME} )
: $filename;
if ( $opt{group} ) {
print "$display_filename\n" if $nmatches == 1;
print "$.:";
}
else {
print "${display_filename}:$.:";
}
}
print $out;

last if $opt{m} && ( $nmatches >= $opt{m} );
} # while
close $fh or App::Ack::warn( "$filename: $!" );

if ( $opt{count} ) {
if ( $nmatches || !$opt{l} ) {
print "${filename}:" if $opt{show_filename};
print "${nmatches}\n";
}
}
elsif ( $opt{l} ) {
print "$filename\n" if $nmatches;
}
else {
print "\n" if $nmatches && $opt{show_filename} && $opt{group};
}

return $nmatches;
} # search()



1; # End of App::Ack
91 changes: 2 additions & 89 deletions ack
Expand Up @@ -170,7 +170,7 @@ sub main {
$opt{$_} and App::Ack::die( "Can't use -$_ when acting as a filter." );
}
$opt{show_filename} = 0;
search( '-', $regex, %opt );
App::Ack::search( '-', $regex, %opt );
exit 0;
}
else {
Expand Down Expand Up @@ -213,100 +213,13 @@ sub main {
else {
my $nmatches = 0;
while ( defined ( my $file = $iter->() ) ) {
$nmatches += search( $file, $regex, %opt );
$nmatches += App::Ack::search( $file, $regex, %opt );
last if $nmatches && $opt{1};
}
}
exit 0;
}

sub search {
my $filename = shift;
my $regex = shift;
my %opt = @_;

my $is_binary;

my $fh;
if ( $filename eq '-' ) {
$fh = *STDIN;
$is_binary = 0;
}
else {
if ( !open( $fh, '<', $filename ) ) {
App::Ack::warn( "$filename: $!" );
return;
}
$is_binary = -B $filename;
}

# Negated counting is a pain, so I'm putting it in its own
# optimizable subroutine.
if ( $opt{v} ) {
return App::Ack::_search_v( $fh, $is_binary, $filename, $regex, %opt );
}

my $nmatches = 0;
local $_ = undef;
while (<$fh>) {
next unless $opt{passthru} || /$regex/o;
++$nmatches;
next if $opt{count}; # Counting means no lines

# No point in searching more if we only want a list,
# and don't want a count.
last if $opt{l};

if ( $is_binary ) {
print "Binary file $filename matches\n";
last;
}

my $out;
if ( $opt{o} ) {
$out = $opt{o}->() . "\n";
}
else {
$out = $_;
$out =~ s/($regex)/Term::ANSIColor::colored($1,$ENV{ACK_COLOR_MATCH})/eg if $opt{color};
}

if ( $opt{show_filename} ) {
my $display_filename =
$opt{color}
? Term::ANSIColor::colored( $filename, $ENV{ACK_COLOR_FILENAME} )
: $filename;
if ( $opt{group} ) {
print "$display_filename\n" if $nmatches == 1;
print "$.:";
}
else {
print "${display_filename}:$.:";
}
}
print $out;

last if $opt{m} && ( $nmatches >= $opt{m} );
} # while
close $fh or App::Ack::warn( "$filename: $!" );

if ( $opt{count} ) {
if ( $nmatches || !$opt{l} ) {
print "${filename}:" if $opt{show_filename};
print "${nmatches}\n";
}
}
elsif ( $opt{l} ) {
print "$filename\n" if $nmatches;
}
else {
print "\n" if $nmatches && $opt{show_filename} && $opt{group};
}

return $nmatches;
} # search()


=encoding utf8
=head1 NAME
Expand Down

0 comments on commit 0014cba

Please sign in to comment.