Skip to content

Commit

Permalink
Add --passthru option (fixes BurntSushi#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
okdana committed Jan 11, 2018
1 parent 34c0b1b commit f54d9ef
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ pub fn app() -> App<'static, 'static> {
.arg(flag("no-ignore-vcs"))
.arg(flag("null").short("0"))
.arg(flag("only-matching").short("o"))
.arg(flag("passthru").alias("passthrough")
.conflicts_with_all(&["only-matching", "replace"]))
.arg(flag("path-separator").value_name("SEPARATOR").takes_value(true))
.arg(flag("pretty").short("p"))
.arg(flag("replace").short("r")
Expand Down Expand Up @@ -499,6 +501,8 @@ lazy_static! {
"Print only matched parts of a line.",
"Print only the matched (non-empty) parts of a matching line, \
with each such part on a separate output line.");
doc!(h, "passthru",
"Show both matching and non-matching lines.");
doc!(h, "path-separator",
"Path separator to use when printing file paths.",
"The path separator to use when printing file paths. This \
Expand Down
10 changes: 8 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ impl<'a> ArgMatches<'a> {
/// Note that if -F/--fixed-strings is set, then all patterns will be
/// escaped. Similarly, if -w/--word-regexp is set, then all patterns
/// are surrounded by `\b`, and if -x/--line-regexp is set, then all
/// patterns are surrounded by `^...$`.
/// patterns are surrounded by `^...$`. Finally, if --passthru is set,
/// the pattern `^` is added to the end (to ensure that it works as
/// expected with multiple -e/-f patterns).
///
/// If any pattern is invalid UTF-8, then an error is returned.
fn patterns(&self) -> Result<Vec<String>> {
Expand Down Expand Up @@ -470,7 +472,11 @@ impl<'a> ArgMatches<'a> {
}
}
}
if pats.is_empty() {
// It's important that this be at the end; otherwise it would always
// match first, and we wouldn't get colours in the output
if self.is_present("passthru") && !self.is_present("count") {
pats.push("^".to_string())
} else if pats.is_empty() {
pats.push(self.empty_pattern())
}
Ok(pats)
Expand Down
55 changes: 55 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,61 @@ clean!(suggest_fixed_strings_for_invalid_regex, "foo(", ".",
assert_eq!(err.contains("--fixed-strings"), true);
});

#[test]
fn feature_740_passthru() {
let wd = WorkDir::new("feature_740");
wd.create("file", "\nfoo\nbar\nfoobar\n\nbaz\n");
wd.create("patterns", "foo\n\nbar\n");

// We can't assume that the way colour specs are translated to ANSI
// sequences will remain stable, and --replace doesn't currently work with
// pass-through, so for now we don't actually test the match sub-strings
let common_args = &["-n", "--passthru"];
let expected = "\
1:
2:foo
3:bar
4:foobar
5:
6:baz
";

// With single pattern
let mut cmd = wd.command();
cmd.args(common_args).arg("foo").arg("file");
let lines: String = wd.stdout(&mut cmd);
assert_eq!(lines, expected);

// With multiple -e patterns
let mut cmd = wd.command();
cmd.args(common_args)
.arg("-e").arg("foo").arg("-e").arg("bar").arg("file");
let lines: String = wd.stdout(&mut cmd);
assert_eq!(lines, expected);

// With multiple -f patterns
let mut cmd = wd.command();
cmd.args(common_args).arg("-f").arg("patterns").arg("file");
let lines: String = wd.stdout(&mut cmd);
assert_eq!(lines, expected);

// -c should override
let mut cmd = wd.command();
cmd.args(common_args).arg("-c").arg("foo").arg("file");
let lines: String = wd.stdout(&mut cmd);
assert_eq!(lines, "2\n");

// -o should conflict
let mut cmd = wd.command();
cmd.args(common_args).arg("-o").arg("foo").arg("file");
wd.assert_err(&mut cmd);

// -r should conflict
let mut cmd = wd.command();
cmd.args(common_args).arg("-r").arg("$0").arg("foo").arg("file");
wd.assert_err(&mut cmd);
}

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

0 comments on commit f54d9ef

Please sign in to comment.