Skip to content

Commit

Permalink
printer: tweak binary detection message format
Browse files Browse the repository at this point in the history
This roughly matches similar changes made in GNU grep recently.
  • Loading branch information
BurntSushi committed Nov 2, 2020
1 parent 810be0b commit 2819212
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ TBD
===
Unreleased changes. Release notes have not yet been written.

In this release, a small tweak has been made to the output format when a binary
file is detected. Previously, it looked like this:

```
Binary file FOO matches (found "\0" byte around offset XXX)
```

Now it looks like this:

```
FOO: binary file matches (found "\0" byte around offset XXX)
```

Bug fixes:

* [BUG #1277](https://github.com/BurntSushi/ripgrep/issues/1277):
Expand Down
11 changes: 5 additions & 6 deletions crates/printer/src/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,25 +1358,24 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> {

let bin = self.searcher.binary_detection();
if let Some(byte) = bin.quit_byte() {
self.write(b"WARNING: stopped searching binary file ")?;
if let Some(path) = self.path() {
self.write_spec(self.config().colors.path(), path.as_bytes())?;
self.write(b" ")?;
self.write(b": ")?;
}
let remainder = format!(
"after match (found {:?} byte around offset {})\n",
"WARNING: stopped searching binary file after match \
(found {:?} byte around offset {})\n",
[byte].as_bstr(),
offset,
);
self.write(remainder.as_bytes())?;
} else if let Some(byte) = bin.convert_byte() {
self.write(b"Binary file ")?;
if let Some(path) = self.path() {
self.write_spec(self.config().colors.path(), path.as_bytes())?;
self.write(b" ")?;
self.write(b": ")?;
}
let remainder = format!(
"matches (found {:?} byte around offset {})\n",
"binary file matches (found {:?} byte around offset {})\n",
[byte].as_bstr(),
offset,
);
Expand Down
16 changes: 8 additions & 8 deletions tests/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ rgtest!(after_match1_implicit, |dir: Dir, mut cmd: TestCommand| {

let expected = "\
hay:1:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle
WARNING: stopped searching binary file hay after match (found \"\\0\" byte around offset 9741)
hay: WARNING: stopped searching binary file after match (found \"\\0\" byte around offset 9741)
";
eqnice!(expected, cmd.stdout());
});
Expand All @@ -53,7 +53,7 @@ rgtest!(after_match1_explicit, |dir: Dir, mut cmd: TestCommand| {

let expected = "\
1:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle
Binary file matches (found \"\\0\" byte around offset 9741)
binary file matches (found \"\\0\" byte around offset 9741)
";
eqnice!(expected, cmd.stdout());
});
Expand All @@ -64,7 +64,7 @@ rgtest!(after_match1_stdin, |_: Dir, mut cmd: TestCommand| {

let expected = "\
1:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle
Binary file matches (found \"\\0\" byte around offset 9741)
binary file matches (found \"\\0\" byte around offset 9741)
";
eqnice!(expected, cmd.pipe(HAY));
});
Expand All @@ -85,7 +85,7 @@ rgtest!(after_match1_implicit_binary, |dir: Dir, mut cmd: TestCommand| {

let expected = "\
hay:1:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle
Binary file hay matches (found \"\\0\" byte around offset 9741)
hay: binary file matches (found \"\\0\" byte around offset 9741)
";
eqnice!(expected, cmd.stdout());
});
Expand Down Expand Up @@ -200,7 +200,7 @@ rgtest!(after_match2_implicit, |dir: Dir, mut cmd: TestCommand| {

let expected = "\
hay:1:The Project Gutenberg EBook of A Study In Scarlet, by Arthur Conan Doyle
WARNING: stopped searching binary file hay after match (found \"\\0\" byte around offset 9741)
hay: WARNING: stopped searching binary file after match (found \"\\0\" byte around offset 9741)
";
eqnice!(expected, cmd.stdout());
});
Expand Down Expand Up @@ -240,7 +240,7 @@ rgtest!(before_match1_explicit, |dir: Dir, mut cmd: TestCommand| {
cmd.args(&["--no-mmap", "-n", "Heaven", "hay"]);

let expected = "\
Binary file matches (found \"\\0\" byte around offset 9741)
binary file matches (found \"\\0\" byte around offset 9741)
";
eqnice!(expected, cmd.stdout());
});
Expand All @@ -253,7 +253,7 @@ rgtest!(before_match1_implicit_binary, |dir: Dir, mut cmd: TestCommand| {
cmd.args(&["--no-mmap", "-n", "--binary", "Heaven", "-g", "hay"]);

let expected = "\
Binary file hay matches (found \"\\0\" byte around offset 9741)
hay: binary file matches (found \"\\0\" byte around offset 9741)
";
eqnice!(expected, cmd.stdout());
});
Expand Down Expand Up @@ -288,7 +288,7 @@ rgtest!(before_match2_explicit, |dir: Dir, mut cmd: TestCommand| {
cmd.args(&["--no-mmap", "-n", "a medical student", "hay"]);

let expected = "\
Binary file matches (found \"\\0\" byte around offset 9741)
binary file matches (found \"\\0\" byte around offset 9741)
";
eqnice!(expected, cmd.stdout());
});
Expand Down
6 changes: 3 additions & 3 deletions tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ rgtest!(unrestricted3, |dir: Dir, mut cmd: TestCommand| {
cmd.arg("-uuu").arg("foo");

let expected = "\
Binary file hay matches (found \"\\0\" byte around offset 3)
hay: binary file matches (found \"\\0\" byte around offset 3)
";
eqnice!(expected, cmd.stdout());
});
Expand Down Expand Up @@ -1001,7 +1001,7 @@ rgtest!(binary_convert, |dir: Dir, mut cmd: TestCommand| {
cmd.arg("--no-mmap").arg("foo").arg("file");

let expected = "\
Binary file matches (found \"\\0\" byte around offset 3)
binary file matches (found \"\\0\" byte around offset 3)
";
eqnice!(expected, cmd.stdout());
});
Expand All @@ -1011,7 +1011,7 @@ rgtest!(binary_convert_mmap, |dir: Dir, mut cmd: TestCommand| {
cmd.arg("--mmap").arg("foo").arg("file");

let expected = "\
Binary file matches (found \"\\0\" byte around offset 3)
binary file matches (found \"\\0\" byte around offset 3)
";
eqnice!(expected, cmd.stdout());
});
Expand Down

0 comments on commit 2819212

Please sign in to comment.