summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMohammad AlSaleh <CE.Mohammad.AlSaleh@gmail.com>2019-09-26 14:50:40 +0300
committerAndrew Gallant <jamslam@gmail.com>2020-02-17 17:16:28 -0500
commite71eedf0eb802c27541292f06676aa782be9bd21 (patch)
treeea54ad6eceeee6e08aa85f23809132c9780d8ceb
parent88f46d12f1f3f7c2eac925ecb0b1c816ffddb943 (diff)
cli: add --no-context-separator flag
--context-separator='' still adds a new line separator, which could still potentially be useful. So we add a new `--no-context-separator` flag that completely disables context separators even when the -A/-B/-C context flags are used. Closes #1390
-rw-r--r--CHANGELOG.md5
-rw-r--r--complete/_rg1
-rw-r--r--src/app.rs23
-rw-r--r--src/args.rs16
-rw-r--r--tests/feature.rs67
5 files changed, 102 insertions, 10 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 083000de..25d0a209 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,11 @@ Performance improvements:
* [PERF #1381](https://github.com/BurntSushi/ripgrep/pull/1381):
Directory traversal is sped up with speculative ignore-file existence checks.
+Feature enhancements:
+
+* [FEATURE #1390](https://github.com/BurntSushi/ripgrep/pull/1390):
+ Add new `--no-context-separator` flag that always hides context separators.
+
Bug fixes:
* [BUG #1335](https://github.com/BurntSushi/ripgrep/issues/1335):
diff --git a/complete/_rg b/complete/_rg
index d7dcfd64..d77ef5b6 100644
--- a/complete/_rg
+++ b/complete/_rg
@@ -277,6 +277,7 @@ _rg() {
))'
'*--colors=[specify color and style settings]: :->colorspec'
'--context-separator=[specify string used to separate non-continuous context lines in output]:separator'
+ $no"--no-context-separator[don't print context separators]"
'--debug[show debug messages]'
'--dfa-size-limit=[specify upper size limit of generated DFA]:DFA size (bytes)'
"(1 stats)--files[show each file that would be searched (but don't search)]"
diff --git a/src/app.rs b/src/app.rs
index 0733c80b..cd4a927b 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -963,12 +963,27 @@ This overrides both the -B/--before-context and -A/--after-context flags.
fn flag_context_separator(args: &mut Vec<RGArg>) {
const SHORT: &str = "Set the context separator string.";
- const LONG: &str = long!("\
-The string used to separate non-contiguous context lines in the output. Escape
+ const LONG: &str = long!(
+ "\
+The string used to separate non-contiguous context lines in the output. This
+is only used when one of the context flags is used (-A, -B or -C). Escape
sequences like \\x7F or \\t may be used. The default value is --.
-");
+
+When the context separator is set to an empty string, then a line break
+is still inserted. To completely disable context separators, use the
+--no-context-separator flag.
+"
+ );
+
let arg = RGArg::flag("context-separator", "SEPARATOR")
- .help(SHORT).long_help(LONG);
+ .help(SHORT)
+ .long_help(LONG)
+ .overrides("no-context-separator");
+ args.push(arg);
+
+ let arg = RGArg::switch("no-context-separator")
+ .hidden()
+ .overrides("context-separator");
args.push(arg);
}
diff --git a/src/args.rs b/src/args.rs
index 63526889..075d7d29 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -784,7 +784,7 @@ impl ArgMatches {
.byte_offset(self.is_present("byte-offset"))
.trim_ascii(self.is_present("trim"))
.separator_search(None)
- .separator_context(Some(self.context_separator()))
+ .separator_context(self.context_separator())
.separator_field_match(b":".to_vec())
.separator_field_context(b"-".to_vec())
.separator_path(self.path_separator()?)
@@ -1020,10 +1020,14 @@ impl ArgMatches {
/// Returns the unescaped context separator in UTF-8 bytes.
///
/// If one was not provided, the default `--` is returned.
- fn context_separator(&self) -> Vec<u8> {
- match self.value_of_os("context-separator") {
- None => b"--".to_vec(),
- Some(sep) => cli::unescape_os(&sep),
+ /// If --no-context-separator is passed, None is returned.
+ fn context_separator(&self) -> Option<Vec<u8>> {
+ let nosep = self.is_present("no-context-separator");
+ let sep = self.value_of_os("context-separator");
+ match (nosep, sep) {
+ (true, _) => None,
+ (false, None) => Some(b"--".to_vec()),
+ (false, Some(sep)) => Some(cli::unescape_os(&sep)),
}
}
@@ -1092,7 +1096,7 @@ impl ArgMatches {
Ok(if self.heading() {
Some(b"".to_vec())
} else if ctx_before > 0 || ctx_after > 0 {
- Some(self.context_separator().clone())
+ self.context_separator()
} else {
None
})
diff --git a/tests/feature.rs b/tests/feature.rs
index 13e87535..4d918163 100644
--- a/tests/feature.rs
+++ b/tests/feature.rs
@@ -727,3 +727,70 @@ rgtest!(f1207_ignore_encoding, |dir: Dir, mut cmd: TestCommand| {
cmd.arg("--encoding").arg("none").arg("-a").arg("\\x00").arg("foo");
eqnice!("\u{FFFD}\u{FFFD}\x00b\n", cmd.stdout());
});
+
+rgtest!(no_context_sep, |dir: Dir, mut cmd: TestCommand| {
+ dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");
+ cmd.args(&[
+ "-A1",
+ "--no-context-separator",
+ "foo",
+ "test",
+ ]);
+ eqnice!("foo\nctx\nfoo\nctx\n", cmd.stdout());
+});
+
+rgtest!(no_context_sep_overrides, |dir: Dir, mut cmd: TestCommand| {
+ dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");
+ cmd.args(&[
+ "-A1",
+ "--context-separator", "AAA",
+ "--no-context-separator",
+ "foo",
+ "test",
+ ]);
+ eqnice!("foo\nctx\nfoo\nctx\n", cmd.stdout());
+});
+
+rgtest!(no_context_sep_overridden, |dir: Dir, mut cmd: TestCommand| {
+ dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");
+ cmd.args(&[
+ "-A1",
+ "--no-context-separator",
+ "--context-separator", "AAA",
+ "foo",
+ "test",
+ ]);
+ eqnice!("foo\nctx\nAAA\nfoo\nctx\n", cmd.stdout());
+});
+
+rgtest!(context_sep, |dir: Dir, mut cmd: TestCommand| {
+ dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");
+ cmd.args(&[
+ "-A1",
+ "--context-separator", "AAA",
+ "foo",
+ "test",
+ ]);
+ eqnice!("foo\nctx\nAAA\nfoo\nctx\n", cmd.stdout());
+});
+
+rgtest!(context_sep_default, |dir: Dir, mut cmd: TestCommand| {
+ dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");
+ cmd.args(&[
+ "-A1",
+ "foo",
+ "test",
+ ]);
+ eqnice!("foo\nctx\n--\nfoo\nctx\n", cmd.stdout());
+});
+
+rgtest!(context_sep_empty, |dir: Dir, mut cmd: TestCommand| {
+ dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");
+ cmd.args(&[
+ "-A1",
+ "--context-separator", "",
+ "foo",
+ "test",
+ ]);
+ eqnice!("foo\nctx\n\nfoo\nctx\n", cmd.stdout());
+});