Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion ack
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,16 @@ sub pmif_normal {
return $nmatches;
}

sub color_match {
my ( $text, $color ) = @_;

if ( $color =~ /\A([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\z/ ) {
return "\e[38;5;$1m${text}\e[0m";
}

return Term::ANSIColor::colored( $text, $color );
}


sub print_line_with_options {
my ( $filename, $line, $lineno, $separator, $skip_coloring ) = @_;
Expand Down Expand Up @@ -983,7 +993,9 @@ sub print_line_with_options {
last if $match_length <= 0;

my $substring = substr( $line, $match_start, $match_length );
my $substitution = Term::ANSIColor::colored( $substring, $ENV{ACK_COLOR_MATCH} );
# my $substitution = Term::ANSIColor::colored( $substring, $ENV{ACK_COLOR_MATCH} );
my $substitution = color_match( $substring, $ENV{ACK_COLOR_MATCH} );


# Fourth argument replaces the string specified by the first three.
substr( $line, $match_start, $match_length, $substitution );
Expand Down
22 changes: 21 additions & 1 deletion t/ack-color.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Test::More;
use lib 't';
use Util;

plan tests => 15;
plan tests => 16;

prep_environment();

Expand Down Expand Up @@ -134,6 +134,26 @@ subtest 'Passing args for colors' => sub {
};


subtest 'ANSI 256 colors' => sub {
plan tests => 2;

my $file = reslash( 't/text/ozymandias.txt' );
my @args = qw( mighty -i -w --color --color-match=208 );
my @results = run_ack( @args, $file );

is_deeply( \@results, [
"Look on my works, ye \e[38;5;208mMighty\e[0m, and despair!'$line_end",
], '256-color match' );

@args = qw( mighty -i -w --color --color-match=255 );
@results = run_ack( @args, $file );

is_deeply( \@results, [
"Look on my works, ye \e[38;5;255mMighty\e[0m, and despair!'$line_end",
], '256-color match at upper bound' );
};


subtest 'Filename colors with count' => sub {
if ( !has_io_pty() ) {
plan skip_all => 'IO::Pty is required for this test';
Expand Down