diff --git a/ack b/ack index 8d6ad61a..c290793e 100755 --- a/ack +++ b/ack @@ -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 ) = @_; @@ -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 ); diff --git a/t/ack-color.t b/t/ack-color.t index bdf4a4c3..cc75356a 100644 --- a/t/ack-color.t +++ b/t/ack-color.t @@ -8,7 +8,7 @@ use Test::More; use lib 't'; use Util; -plan tests => 15; +plan tests => 16; prep_environment(); @@ -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';