mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-05-19 01:30:21 -07:00
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:
parent
88f46d12f1
commit
e71eedf0eb
@ -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):
|
||||
|
@ -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)]"
|
||||
|
23
src/app.rs
23
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);
|
||||
}
|
||||
|
||||
|
16
src/args.rs
16
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
|
||||
})
|
||||
|
@ -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());
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user