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

@@ -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);
}

View File

@@ -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
})