Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --no-lineno Option to Omit Line Numbers #351

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
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
30 changes: 18 additions & 12 deletions ack
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ our $opt_heading;
our $opt_L;
our $opt_l;
our $opt_m;
our $opt_no_lineno;
our $opt_output;
our $opt_passthru;
our $opt_p;
Expand Down Expand Up @@ -144,6 +145,7 @@ MAIN: {
$opt_L = $opt->{L};
$opt_l = $opt->{l};
$opt_m = $opt->{m};
$opt_no_lineno = $opt->{no_lineno};
$opt_output = $opt->{output};
$opt_p = $opt->{p};
$opt_passthru = $opt->{passthru};
Expand Down Expand Up @@ -905,20 +907,19 @@ sub print_line_with_options {
my @line_parts;

if ( $opt_show_filename && defined($filename) ) {
my $colno;
$colno = get_match_colno() if $opt_column;
# Because you can't "local"-ize lexical variables...
my ( $_filename, $_lineno ) = ( $filename, $lineno );

my $_colno;
$_colno = get_match_colno() if $opt_column;
if ( $opt_color ) {
$filename = Term::ANSIColor::colored( $filename, $ENV{ACK_COLOR_FILENAME} );
$lineno = Term::ANSIColor::colored( $lineno, $ENV{ACK_COLOR_LINENO} );
$colno = Term::ANSIColor::colored( $colno, $ENV{ACK_COLOR_COLNO} ) if $opt_column;
}
if ( $opt_heading ) {
push @line_parts, $lineno;
}
else {
push @line_parts, $filename, $lineno;
$_filename = Term::ANSIColor::colored( $_filename, $ENV{ACK_COLOR_FILENAME} );
$_lineno = Term::ANSIColor::colored( $_lineno, $ENV{ACK_COLOR_LINENO} );
$_colno = Term::ANSIColor::colored( $_colno, $ENV{ACK_COLOR_COLNO} ) if $opt_column;
}
push @line_parts, $colno if $opt_column;
push @line_parts, $_filename unless $opt_heading; # We've already printed it...
push @line_parts, $_lineno unless $opt_no_lineno;
push @line_parts, $_colno if $opt_column;
}

if ( $opt_output ) {
Expand Down Expand Up @@ -1457,6 +1458,11 @@ searched.
Print a filename heading above each file's results. This is the default
when used interactively.

=item B<--no-lineno>

Suppress the prefixing of line numbers on output. Can't be combined with
B<--column>.

=item B<--help>

Print a short help statement.
Expand Down
2 changes: 1 addition & 1 deletion dev/crank-mutex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sub invalid_combinations {
[qw(-h)] => [@file_lists],
[qw(-H)] => [@file_lists],
[qw(-c)] => [@context, @pretty, @fg, qw(--column)],
[qw(--column)] => [@file_lists],
[qw(--column)] => [@file_lists, qw(--no-lineno)],
[@context] => [@file_lists],
[qw(-f)] => [qw(-v)],
[@fg] => [@fg, @pretty, qw(-x -c -u --files-from)],
Expand Down
1 change: 1 addition & 0 deletions dev/generate-completion-scripts.pl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'--match' => 'specify PATTERN explicitly',
'--max-count' => 'stop reading after NUM matches',
'--no-filename' => q{don't print filenames on output},
'--no-lineno' => q{don't print line numbers on output},
'--noenv' => q{don't consider ackrc files/environment variables for configuration},
'--output' => 'output the evaluation of EXPR for each line',
'--pager' => q{direct ack's output through PAGER},
Expand Down
1 change: 1 addition & 0 deletions lib/App/Ack.pm
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ Search output:
-H, --with-filename Print the filename for each match (default:
on unless explicitly searching a single file)
-h, --no-filename Suppress the prefixing filename on output
--no-lineno Suppress the prefixing line number on output
--[no]column Show the column number of the first match

-A NUM, --after-context=NUM Print NUM lines of trailing context after
Expand Down
5 changes: 5 additions & 0 deletions lib/App/Ack/ConfigLoader.pm
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ sub get_arg_spec {
'heading!' => \$opt->{heading},
'h|no-filename' => \$opt->{h},
'H|with-filename' => \$opt->{H},
'no-lineno' => \$opt->{no_lineno},
'i|ignore-case' => sub { $opt->{i} = 1; $opt->{S} = 0; },
'I|no-ignore-case' => sub { $opt->{i} = 0; $opt->{S} = 0; },
'ignore-directory|ignore-dir=s' => _generate_ignore_dir('--ignore-dir', $opt),
Expand Down Expand Up @@ -949,6 +950,7 @@ sub mutex_options {
f => 1,
g => 1,
l => 1,
'no-lineno' => 1,
o => 1,
output => 1,
passthru => 1,
Expand Down Expand Up @@ -1067,6 +1069,9 @@ sub mutex_options {
L => 1,
l => 1,
},
'no-lineno' => {
column => 1,
},
o => {
A => 1,
B => 1,
Expand Down
1 change: 1 addition & 0 deletions t/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ sub get_expected_options {
'--match',
'--max-count',
'--no-filename',
'--no-lineno',
'--no-recurse',
'--nobreak',
'--nocolor',
Expand Down
101 changes: 101 additions & 0 deletions t/ack-no-lineno.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!perl

use strict;
use warnings;

use Test::More tests => 8;

use lib 't';
use Util;

prep_environment();

INTERACTIVE_MULTIPLE_FILES: {
my @args = qw( --no-lineno --no-color strict );
my @files = qw( t/swamp/options.pl t/swamp/pipe-stress-freaks.F );

my $target_file = reslash( 't/swamp/options.pl' );
my @expected = line_split( <<"HERE" );
$target_file
use strict;
HERE

my @results = run_ack_interactive( @args, @files );

lists_match( \@results, \@expected, 'Looking for strict interactively in multiple files' );
}

INTERACTIVE_MULTIPLE_FILES_NO_HEADING: {
my @args = qw( --no-lineno --no-heading --no-color strict );
my @files = qw( t/swamp/options.pl t/swamp/pipe-stress-freaks.F );

my $target_file = reslash( 't/swamp/options.pl' );
my @expected = line_split( <<"HERE" );
$target_file:use strict;
HERE

my @results = run_ack_interactive( @args, @files );

lists_match( \@results, \@expected, 'Looking for strict interactively in multiple files with --no-heading' );
}

INTERACTIVE_ONE_FILE: {
my @args = ( qw( --no-lineno --with-filename --no-color strict ) );
my @files = qw( t/swamp/options.pl );

my $target_file = reslash( 't/swamp/options.pl' );
my @expected = line_split( <<"HERE" );
$target_file
use strict;
HERE

my @results = run_ack_interactive( @args, @files );

lists_match( \@results, \@expected, 'Looking for strict interactively in one file with filename' );
}

INTERACTIVE_ONE_FILE_NO_HEADING: {
my @args = ( qw( --no-lineno --with-filename --no-heading --no-color strict ) );
my @files = qw( t/swamp/options.pl );

my $target_file = reslash( 't/swamp/options.pl' );
my @expected = line_split( <<"HERE" );
$target_file:use strict;
HERE

my @results = run_ack_interactive( @args, @files );

lists_match( \@results, \@expected, 'Looking for strict interactively in one file with filename and --no-heading' );
}

NON_INTERACTIVE_MULTIPLE_FILES: {
my @args = qw( --no-lineno strict );
my @files = qw( t/swamp/options.pl t/swamp/pipe-stress-freaks.F );

my $target_file = reslash( 't/swamp/options.pl' );
my @expected = line_split( <<"HERE" );
$target_file:use strict;
HERE

my @results = run_ack( @args, @files );

lists_match( \@results, \@expected, 'Looking for strict in multiple files' );
}

NON_INTERACTIVE_ONE_FILE: {
my @args = ( qw( --no-lineno --with-filename strict ) );
my @files = qw( t/swamp/options.pl );

my $target_file = reslash( 't/swamp/options.pl' );
my @expected = line_split( <<"HERE" );
$target_file:use strict;
HERE

my @results = run_ack( @args, @files );

lists_match( \@results, \@expected, 'Looking for strict in one file with filename' );
}

done_testing();

exit 0;
Loading