Skip to content

Commit

Permalink
And/or highlighting seems ok
Browse files Browse the repository at this point in the history
  • Loading branch information
petdance committed Dec 15, 2024
1 parent 16645ef commit 029fc9b
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions andor.pl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@



use Term::ANSIColor 1.10 ();


MAIN: {
my @words = @ARGV;

my @lines = <DATA>;
chomp @lines;

my @words = qw( a[pb] e$ x );

AND: {
say "AND";
my @and_parts = map { "(?=.*$_)" } @words;
Expand All @@ -26,6 +29,7 @@
}

OR: {
say '';
say "OR";
my $match_or_re = join( '|', @words );
$match_or_re = qr/$match_or_re/;
Expand All @@ -51,12 +55,24 @@ sub _iter {

for ( @lines ) {
my $line = "$_";

if ( $line =~ /$match_re/ ) {
$line =~ s/$hl_re/[$1]/g;
say "YES: $line";
}
else {
say "NO: $line";
while ( $line =~ /$hl_re/g ) {
my $match_start = $-[0] // next;
my $match_end = $+[0];
my $match_length = $match_end - $match_start;
last if $match_length <= 0;

my $substring = substr( $line, $match_start, $match_length );
my $substitution = Term::ANSIColor::colored( $substring, 'black on_yellow' );

# Fourth argument replaces the string specified by the first three.
substr( $line, $match_start, $match_length, $substitution );

# Move the offset of where /g left off forward the number of spaces of highlighting.
pos($line) = $match_end + (length( $substitution ) - length( $substring ));
}
say $line;
}
}

Expand Down

0 comments on commit 029fc9b

Please sign in to comment.