Skip to content

Commit

Permalink
First almost-working version with changes from the boolean-first-draf…
Browse files Browse the repository at this point in the history
…t branch
  • Loading branch information
petdance committed Sep 26, 2024
1 parent 7069053 commit 2cf4cc8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
71 changes: 68 additions & 3 deletions ack
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use App::Ack::Filter::Collection ();

# Global command-line options
our $opt_1;
our $opt_and;
our $opt_A;
our $opt_B;
our $opt_break;
Expand All @@ -41,6 +42,7 @@ our $opt_heading;
our $opt_L;
our $opt_l;
our $opt_m;
our $opt_or;
our $opt_output;
our $opt_passthru;
our $opt_p;
Expand Down Expand Up @@ -218,6 +220,8 @@ MAIN: {
$stats{search_re} = $search_re;
$stats{scan_re} = $scan_re;
$stats{search_not_re} = $search_not_re;

_build_and_or( \$opt_and, \$opt_or, $opt );
}
else {
if ( $opt_f ) {
Expand All @@ -230,6 +234,7 @@ MAIN: {
$stats{search_re} = $search_re;
$stats{scan_re} = $scan_re;
$stats{search_not_re} = $search_not_re;
_build_and_or( \$opt_and, \$opt_or, $opt );
}
# XXX What is this checking for?
if ( $search_re && $search_re =~ /\n/ ) {
Expand Down Expand Up @@ -281,7 +286,15 @@ MAIN: {

if ( $opt_debug ) {
require List::Util;
my @stats = qw( search_re scan_re search_not_re prescans linescans filematches linematches );
my @stats = qw(
search_re
scan_re
search_not_re
prescans
linescans
filematches
linematches
);
my $width = List::Util::max( map { length } @stats );

for my $stat ( @stats ) {
Expand All @@ -294,8 +307,10 @@ MAIN: {
App::Ack::exit_from_ack( $nmatches );
}


# End of MAIN


sub file_loop_fg {
my $files = shift;

Expand Down Expand Up @@ -739,6 +754,7 @@ sub print_matches_in_file {
else {
if ( $does_match ) {
# @- = @LAST_MATCH_START
# @+ = @LAST_MATCH_END
$match_colno = $-[0] + 1;
}
}
Expand Down Expand Up @@ -869,6 +885,7 @@ sub print_matches_in_file {
my $last_match_lineno;
my $in_range = range_setup();

my $highlight_re = _build_highlight_re( $search_re, $opt_and, $opt_or );
while ( <$fh> ) {
chomp;

Expand All @@ -883,6 +900,19 @@ sub print_matches_in_file {
}
if ( $is_match ) {
$match_colno = $-[0] + 1;
if ( $opt_and ) {
for my $re ( @{$opt_and} ) {
$is_match &&= /$re/;
}
}
if ( $opt_or ) {
for my $re ( @{$opt_or} ) {
$is_match ||= /$re/;
}
}
$match_colno = undef unless $is_match;
}
if ( $is_match ) {
if ( !$has_printed_from_this_file ) {
$stats{filematches}++;
if ( $opt_break && $has_printed_from_any_file ) {
Expand Down Expand Up @@ -968,9 +998,10 @@ sub print_line_with_options {
else {
my $underline = '';

my $highlight_re = _build_highlight_re( $search_re, $opt_and, $opt_or );
# We have to do underlining before any highlighting because highlighting modifies string length.
if ( $opt_underline && !$skip_coloring ) {
while ( $line =~ /$search_re/og ) {
while ( $line =~ /$highlight_re/og ) {
my $match_start = $-[0] // next;
my $match_end = $+[0];
my $match_length = $match_end - $match_start;
Expand All @@ -985,7 +1016,7 @@ sub print_line_with_options {
if ( $opt_color && !$skip_coloring ) {
my $highlighted = 0; # If highlighted, need to escape afterwards.

while ( $line =~ /$search_re/og ) {
while ( $line =~ /$highlight_re/og ) {
my $match_start = $-[0] // next;
my $match_end = $+[0];
my $match_length = $match_end - $match_start;
Expand Down Expand Up @@ -1169,6 +1200,40 @@ sub _build_search_not_re {
}


sub _build_and_or {
my $opt_and = shift;
my $opt_or = shift;
my $opt = shift;

for my $and_regex ( @{$opt->{and}} ) {
my ($built, undef) = build_regex( $and_regex, $opt );
push( @{$$opt_and}, $built );
}
for my $or_regex ( @{$opt->{or}} ) {
my ($built, undef) = build_regex( $or_regex, $opt );
push( @{$$opt_or}, $built );
}
return;

}


sub _build_highlight_re {
my $highlight_re;
if ( $opt_and ) {
$highlight_re = join( '|', $search_re, @{$opt_and} );
}
elsif ( $opt_or ) {
$highlight_re = join( '|', $search_re, @{$opt_or} );
}
else {
$highlight_re = $search_re;
}

return $highlight_re;
}


=pod
=encoding UTF-8
Expand Down
1 change: 1 addition & 0 deletions dev/crank-mutex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sub invalid_combinations {
my @case = qw( -i -I --smart-case );

return (
[qw(--and)] => [qw( --or )],
[qw(-l)] => [@context, @file_lists, @pretty, @filename, @output, qw(--passthru --column --show-types)],
[qw(-L)] => [@context, @file_lists, @pretty, @filename, @output, qw(--passthru --column --show-types -c -v)],
[@output] => [@context, @file_lists, @output, qw(-c --column --show-types)],
Expand Down
10 changes: 10 additions & 0 deletions lib/App/Ack/ConfigLoader.pm
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,12 @@ sub get_arg_spec {
}

$opt->{not} = [];
$opt->{and} = [];
$opt->{or} = [];

return {
1 => sub { $opt->{1} = $opt->{m} = 1 },
'and=s' => $opt->{and},
'A|after-context:-1' => sub { shift; $opt->{A} = _context_value(shift) },
'B|before-context:-1' => sub { shift; $opt->{B} = _context_value(shift) },
'C|context:-1' => sub { shift; $opt->{B} = $opt->{A} = _context_value(shift) },
Expand Down Expand Up @@ -324,6 +327,7 @@ sub get_arg_spec {
'match=s' => \$opt->{regex},
'n|no-recurse' => \$opt->{n},
o => sub { $opt->{output} = '$&' },
'or=s' => $opt->{or},
'output=s' => \$opt->{output},
'pager:s' => sub {
my ( undef, $value ) = @_;
Expand Down Expand Up @@ -928,6 +932,9 @@ sub mutex_options {
v => 1,
'with-filename' => 1,
},
and => {
or => 1,
},
break => {
L => 1,
c => 1,
Expand Down Expand Up @@ -1103,6 +1110,9 @@ sub mutex_options {
'show-types' => 1,
v => 1,
},
or => {
and => 1,
},
output => {
A => 1,
B => 1,
Expand Down

0 comments on commit 2cf4cc8

Please sign in to comment.