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
This commit is contained in:
Mohammad AlSaleh 2019-09-26 14:50:40 +03:00 committed by Andrew Gallant
parent 88f46d12f1
commit e71eedf0eb
5 changed files with 102 additions and 10 deletions

View File

@ -7,6 +7,11 @@ Performance improvements:
* [PERF #1381](https://github.com/BurntSushi/ripgrep/pull/1381): * [PERF #1381](https://github.com/BurntSushi/ripgrep/pull/1381):
Directory traversal is sped up with speculative ignore-file existence checks. 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 fixes:
* [BUG #1335](https://github.com/BurntSushi/ripgrep/issues/1335): * [BUG #1335](https://github.com/BurntSushi/ripgrep/issues/1335):

View File

@ -277,6 +277,7 @@ _rg() {
))' ))'
'*--colors=[specify color and style settings]: :->colorspec' '*--colors=[specify color and style settings]: :->colorspec'
'--context-separator=[specify string used to separate non-continuous context lines in output]:separator' '--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]' '--debug[show debug messages]'
'--dfa-size-limit=[specify upper size limit of generated DFA]:DFA size (bytes)' '--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)]" "(1 stats)--files[show each file that would be searched (but don't search)]"

View File

@ -963,12 +963,27 @@ This overrides both the -B/--before-context and -A/--after-context flags.
fn flag_context_separator(args: &mut Vec<RGArg>) { fn flag_context_separator(args: &mut Vec<RGArg>) {
const SHORT: &str = "Set the context separator string."; const SHORT: &str = "Set the context separator string.";
const LONG: &str = long!("\ const LONG: &str = long!(
The string used to separate non-contiguous context lines in the output. Escape "\
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 --. 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") 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); args.push(arg);
} }

View File

@ -784,7 +784,7 @@ impl ArgMatches {
.byte_offset(self.is_present("byte-offset")) .byte_offset(self.is_present("byte-offset"))
.trim_ascii(self.is_present("trim")) .trim_ascii(self.is_present("trim"))
.separator_search(None) .separator_search(None)
.separator_context(Some(self.context_separator())) .separator_context(self.context_separator())
.separator_field_match(b":".to_vec()) .separator_field_match(b":".to_vec())
.separator_field_context(b"-".to_vec()) .separator_field_context(b"-".to_vec())
.separator_path(self.path_separator()?) .separator_path(self.path_separator()?)
@ -1020,10 +1020,14 @@ impl ArgMatches {
/// Returns the unescaped context separator in UTF-8 bytes. /// Returns the unescaped context separator in UTF-8 bytes.
/// ///
/// If one was not provided, the default `--` is returned. /// If one was not provided, the default `--` is returned.
fn context_separator(&self) -> Vec<u8> { /// If --no-context-separator is passed, None is returned.
match self.value_of_os("context-separator") { fn context_separator(&self) -> Option<Vec<u8>> {
None => b"--".to_vec(), let nosep = self.is_present("no-context-separator");
Some(sep) => cli::unescape_os(&sep), 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() { Ok(if self.heading() {
Some(b"".to_vec()) Some(b"".to_vec())
} else if ctx_before > 0 || ctx_after > 0 { } else if ctx_before > 0 || ctx_after > 0 {
Some(self.context_separator().clone()) self.context_separator()
} else { } else {
None None
}) })

View File

@ -727,3 +727,70 @@ rgtest!(f1207_ignore_encoding, |dir: Dir, mut cmd: TestCommand| {
cmd.arg("--encoding").arg("none").arg("-a").arg("\\x00").arg("foo"); cmd.arg("--encoding").arg("none").arg("-a").arg("\\x00").arg("foo");
eqnice!("\u{FFFD}\u{FFFD}\x00b\n", cmd.stdout()); 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());
});