Skip to content

Commit

Permalink
printer: --only-matching works with --replace
Browse files Browse the repository at this point in the history
When -o/--only-matching is used with -r/--replace, the replacement works
as expected. This is not a breaking change because the flags were
previously set to conflict.
  • Loading branch information
Evgeny Kulikov authored and BurntSushi committed Oct 21, 2017
1 parent 363a4fa commit f887bc1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub fn app() -> App<'static, 'static> {
.arg(flag("no-ignore-parent"))
.arg(flag("no-ignore-vcs"))
.arg(flag("null").short("0"))
.arg(flag("only-matching").short("o").conflicts_with("replace"))
.arg(flag("only-matching").short("o"))
.arg(flag("path-separator").value_name("SEPARATOR").takes_value(true))
.arg(flag("pretty").short("p"))
.arg(flag("replace").short("r")
Expand Down
7 changes: 6 additions & 1 deletion src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,12 @@ impl<W: WriteColor> Printer<W> {
let line = {
let replacer = CountingReplacer::new(
self.replace.as_ref().unwrap(), &mut count, &mut offsets);
re.replace_all(&buf[start..end], replacer)
if self.only_matching {
re.replace_all(
&buf[start + match_start..start + match_end], replacer)
} else {
re.replace_all(&buf[start..end], replacer)
}
};
if self.max_columns.map_or(false, |m| line.len() > m) {
let msg = format!(
Expand Down
14 changes: 14 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,20 @@ but Watson, Doctor has to have it taken out for him and dusted,
assert_eq!(lines, expected);
});

sherlock!(replace_with_only_matching, "of (\\w+)",
|wd: WorkDir, mut cmd: Command| {
cmd.arg("-o").arg("-r").arg("$1");
let lines: String = wd.stdout(&mut cmd);
let expected = "\
this
detective
luck
straw
cigar
";
assert_eq!(lines, expected);
});

sherlock!(file_types, "Sherlock", ".", |wd: WorkDir, mut cmd: Command| {
wd.create("file.py", "Sherlock");
wd.create("file.rs", "Sherlock");
Expand Down

0 comments on commit f887bc1

Please sign in to comment.