diff --git a/Ack.pm b/Ack.pm index d695d06..8c495a1 100644 --- a/Ack.pm +++ b/Ack.pm @@ -196,6 +196,7 @@ sub get_command_line_options { 'color|colour!' => \$opt{color}, 'color-match=s' => \$ENV{ACK_COLOR_MATCH}, 'color-filename=s' => \$ENV{ACK_COLOR_FILENAME}, + 'column!' => \$opt{column}, count => \$opt{count}, 'env!' => sub { }, # ignore this option, it is handled beforehand f => \$opt{f}, @@ -703,6 +704,7 @@ Search output: -H, --with-filename Print the filename for each match -h, --no-filename Suppress the prefixing filename on output -c, --count Show number of lines matching per file + --column Show the column number of the first match -A NUM, --after-context=NUM Print NUM lines of trailing context after matching @@ -943,6 +945,7 @@ sub print_blank_line { App::Ack::print( "\n" ) } sub print_separator { App::Ack::print( "--\n" ) } sub print_filename { App::Ack::print( $_[0], $_[1] ) } sub print_line_no { App::Ack::print( $_[0], $_[1] ) } +sub print_column_no { App::Ack::print( $_[0], $_[1] ) } sub print_count { my $filename = shift; my $nmatches = shift; @@ -1100,6 +1103,7 @@ sub print_match_or_context { my $color = $opt->{color}; my $heading = $opt->{heading}; my $show_filename = $opt->{show_filename}; + my $show_column = $opt->{with_column}; if ( $show_filename ) { if ( not defined $display_filename ) { @@ -1137,6 +1141,8 @@ sub print_match_or_context { } } else { + my $col = $-[0] + 1; + if ( $color && $is_match && $regex && s/$regex/Term::ANSIColor::colored( substr($_, $-[0], $+[0] - $-[0]), $ENV{ACK_COLOR_MATCH} )/eg ) { # At the end of the line reset the color and remove newline @@ -1146,6 +1152,9 @@ sub print_match_or_context { # remove any kind of newline at the end of the line s/[\r\n]*\z//; } + if ( $show_column ) { + App::Ack::print_column_no( $col, $sep ); + } App::Ack::print($_ . "\n"); } $any_output = 1; diff --git a/Changes b/Changes index ca8290e..3eba6ad 100644 --- a/Changes +++ b/Changes @@ -13,8 +13,12 @@ NEXT let you define ACK_COLOR_MATCH and ACK_COLOR_FILENAME from the command line. + Added new switch --column to display the column of the first + hit on the row. Thanks to Eric Van Dewoestine. + [FIXES] - More tweaks to get the detection of input and output pipes working. + More tweaks to get the detection of input and output pipes + working. Fixed an amazingly bad call to cmp_ok() in t/ack-passthru.t. diff --git a/MANIFEST b/MANIFEST index f2ac36b..ecc3b4a 100644 --- a/MANIFEST +++ b/MANIFEST @@ -24,6 +24,7 @@ t/ack-a.t t/ack-binary.t t/ack-c.t t/ack-color.t +t/ack-column.t t/ack-env.t t/ack-g.t t/ack-group.t diff --git a/ack b/ack index 4ade1a7..aae8c82 100755 --- a/ack +++ b/ack @@ -221,6 +221,11 @@ Sets the color to be used for filenames. Sets the color to be used for matches. +=item B<--column> + +Show the column number of the first match. This is helpful for editors +that can place your cursor at a given position. + =item B<--env>, B<--noenv> B<--noenv> disables all environment processing. No F<.ackrc> is read @@ -791,6 +796,7 @@ L How appropriate to have Inowledgements! Thanks to everyone who has contributed to ack in any way, including +Eric Van Dewoestine. Sitaram Chamarty, Adam James, Richard Carlsson, @@ -1197,6 +1203,7 @@ sub get_command_line_options { 'color|colour!' => \$opt{color}, 'color-match=s' => \$ENV{ACK_COLOR_MATCH}, 'color-filename=s' => \$ENV{ACK_COLOR_FILENAME}, + 'column!' => \$opt{column}, count => \$opt{count}, 'env!' => sub { }, # ignore this option, it is handled beforehand f => \$opt{f}, @@ -1621,6 +1628,7 @@ Search output: -H, --with-filename Print the filename for each match -h, --no-filename Suppress the prefixing filename on output -c, --count Show number of lines matching per file + --column Show the column number of the first match -A NUM, --after-context=NUM Print NUM lines of trailing context after matching @@ -1831,6 +1839,7 @@ sub print_blank_line { App::Ack::print( "\n" ) } sub print_separator { App::Ack::print( "--\n" ) } sub print_filename { App::Ack::print( $_[0], $_[1] ) } sub print_line_no { App::Ack::print( $_[0], $_[1] ) } +sub print_column_no { App::Ack::print( $_[0], $_[1] ) } sub print_count { my $filename = shift; my $nmatches = shift; @@ -1976,6 +1985,7 @@ sub print_match_or_context { my $color = $opt->{color}; my $heading = $opt->{heading}; my $show_filename = $opt->{show_filename}; + my $show_column = $opt->{with_column}; if ( $show_filename ) { if ( not defined $display_filename ) { @@ -2013,6 +2023,8 @@ sub print_match_or_context { } } else { + my $col = $-[0] + 1; + if ( $color && $is_match && $regex && s/$regex/Term::ANSIColor::colored( substr($_, $-[0], $+[0] - $-[0]), $ENV{ACK_COLOR_MATCH} )/eg ) { # At the end of the line reset the color and remove newline @@ -2022,6 +2034,9 @@ sub print_match_or_context { # remove any kind of newline at the end of the line s/[\r\n]*\z//; } + if ( $show_column ) { + App::Ack::print_column_no( $col, $sep ); + } App::Ack::print($_ . "\n"); } $any_output = 1; diff --git a/ack-base b/ack-base index 73639d9..d57dce6 100755 --- a/ack-base +++ b/ack-base @@ -213,6 +213,11 @@ Sets the color to be used for filenames. Sets the color to be used for matches. +=item B<--column> + +Show the column number of the first match. This is helpful for editors +that can place your cursor at a given position. + =item B<--env>, B<--noenv> B<--noenv> disables all environment processing. No F<.ackrc> is read @@ -783,6 +788,7 @@ L How appropriate to have Inowledgements! Thanks to everyone who has contributed to ack in any way, including +Eric Van Dewoestine. Sitaram Chamarty, Adam James, Richard Carlsson, diff --git a/ack-help.txt b/ack-help.txt index 6e12377..841648c 100644 --- a/ack-help.txt +++ b/ack-help.txt @@ -37,6 +37,7 @@ Search output: -H, --with-filename Print the filename for each match -h, --no-filename Suppress the prefixing filename on output -c, --count Show number of lines matching per file + --column Show the column number of the first match -A NUM, --after-context=NUM Print NUM lines of trailing context after matching diff --git a/t/ack-column.t b/t/ack-column.t new file mode 100644 index 0000000..7305fe5 --- /dev/null +++ b/t/ack-column.t @@ -0,0 +1,67 @@ +#!perl + +use warnings; +use strict; + +use Test::More tests => 4; + +use File::Next (); + +use lib 't'; +use Util; + +prep_environment(); + +my $weasel = File::Next::reslash( 't/text/science-of-myth.txt' ); + +WITH_COLUMNS: { + my @expected = split( /\n/, <<'HERE' ); +3:4:In the case of Christianity and Judaism there exists the belief +6:1:The Buddhists believe that the functional aspects override the myth +7:8:While other religions use the literal core to build foundations with +8:11:See, half the world sees the myth as fact, and it's seen as a lie by the other half +9:5:And the simple truth is that it's none of that 'cause +10:24:Somehow no matter what the world keeps turning +14:43:In fact, for better understanding we take the facts of science and apply them +15:35:And if both factors keep evolving then we continue getting information +16:17:But closing off the possibilities makes it hard to see the bigger picture +18:10:Consider the case of the woman whose faith helped her make it through +22:18:And if it works, then it gets the job done +23:24:Somehow no matter what the world keeps turning +26:9: -- "The Science Of Myth", Screeching Weasel +HERE + @expected = map { "${weasel}:$_" } @expected; + + my @files = ( $weasel ); + my @args = qw( the -w --with-filename --column ); + my @results = run_ack( @args, @files ); + + sets_match( \@results, \@expected, 'Checking column numbers' ); +} + + +WITHOUT_COLUMNS: { + my @expected = split( /\n/, <<'HERE' ); +3:In the case of Christianity and Judaism there exists the belief +6:The Buddhists believe that the functional aspects override the myth +7:While other religions use the literal core to build foundations with +8:See, half the world sees the myth as fact, and it's seen as a lie by the other half +9:And the simple truth is that it's none of that 'cause +10:Somehow no matter what the world keeps turning +14:In fact, for better understanding we take the facts of science and apply them +15:And if both factors keep evolving then we continue getting information +16:But closing off the possibilities makes it hard to see the bigger picture +18:Consider the case of the woman whose faith helped her make it through +22:And if it works, then it gets the job done +23:Somehow no matter what the world keeps turning +26: -- "The Science Of Myth", Screeching Weasel +HERE + @expected = map { "${weasel}:$_" } @expected; + + my @files = ( $weasel ); + my @args = qw( the -w --with-filename --no-column ); + my @results = run_ack( @args, @files ); + + sets_match( \@results, \@expected, 'Checking without column numbers' ); +} +