Skip to content

Commit

Permalink
Add --no-filename flag.
Browse files Browse the repository at this point in the history
When this flag is set, a filename is never shown for a match.

Closes BurntSushi#20
  • Loading branch information
amsharma91 committed Sep 20, 2016
1 parent 158d060 commit 4a6712c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/rg.1
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ This is the default when more than one file is searched.
.RS
.RE
.TP
.B \-\-no\-filename
Never show the filename for a match.
This is the default when one file is searched.
.RS
.RE
.TP
.B \-\-heading
Show the file name above clusters of matches from each file.
This is the default mode at a tty.
Expand Down
4 changes: 4 additions & 0 deletions doc/rg.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ the raw speed of grep.
: Prefix each match with the file name that contains it. This is the
default when more than one file is searched.

--no-filename
: Never show the filename for a match. This is the default when
one file is searched.

--heading
: Show the file name above clusters of matches from each file.
This is the default mode at a tty.
Expand Down
8 changes: 8 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ Less common options:
Prefix each match with the file name that contains it. This is the
default when more than one file is searched.
--no-filename
Never show the filename for a match. This is the default when
one file is searched.
--heading
Show the file name above clusters of matches from each file.
This is the default mode at a tty.
Expand Down Expand Up @@ -203,6 +207,7 @@ pub struct RawArgs {
flag_no_ignore_parent: bool,
flag_no_line_number: bool,
flag_no_mmap: bool,
flag_no_filename: bool,
flag_pretty: bool,
flag_quiet: bool,
flag_regexp: Vec<String>,
Expand Down Expand Up @@ -321,10 +326,13 @@ impl RawArgs {
self.flag_color == "always"
};
let eol = b'\n';

let mut with_filename = self.flag_with_filename;
if !with_filename {
with_filename = paths.len() > 1 || paths[0].is_dir();
}
with_filename = with_filename && !self.flag_no_filename;

let mut btypes = TypesBuilder::new();
btypes.add_defaults();
try!(self.add_types(&mut btypes));
Expand Down
12 changes: 12 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,18 @@ clean!(regression_65, "xyz", ".", |wd: WorkDir, mut cmd: Command| {
wd.assert_err(&mut cmd);
});

// See: https://github.com/BurntSushi/ripgrep/issues/20
sherlock!(feature_20, "Sherlock", ".", |wd: WorkDir, mut cmd: Command| {
cmd.arg("--no-filename");

let lines: String = wd.stdout(&mut cmd);
let expected = "\
For the Doctor Watsons of this world, as opposed to the Sherlock
be, to a very large extent, the result of luck. Sherlock Holmes
";
assert_eq!(lines, expected);
});

#[test]
fn binary_nosearch() {
let wd = WorkDir::new("binary_nosearch");
Expand Down

0 comments on commit 4a6712c

Please sign in to comment.