Skip to content

Commit 5684882

Browse files
author
Anthony Huang
committed
core: Add field-match-separator and field-context-separator
1 parent 94e4b8e commit 5684882

3 files changed

Lines changed: 82 additions & 2 deletions

File tree

complete/_rg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ _rg() {
314314
"--no-config[don't load configuration files]"
315315
'(-0 --null)'{-0,--null}'[print NUL byte after file names]'
316316
'--path-separator=[specify path separator to use when printing file names]:separator'
317+
'--field-match-separator=[specify field match separator to use when printing file names]:separator'
318+
'--field-context-separator=[specify field context separator to use when printing file names]:separator'
317319
'(-q --quiet)'{-q,--quiet}'[suppress normal output]'
318320
'--regex-size-limit=[specify upper size limit of compiled regex]:regex size (bytes)'
319321
'*'{-u,--unrestricted}'[reduce level of "smart" searching]'

crates/core/app.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ pub fn all_args_and_flags() -> Vec<RGArg> {
614614
flag_one_file_system(&mut args);
615615
flag_only_matching(&mut args);
616616
flag_path_separator(&mut args);
617+
flag_field_match_separator(&mut args);
618+
flag_field_context_separator(&mut args);
617619
flag_passthru(&mut args);
618620
flag_pcre2(&mut args);
619621
flag_pcre2_version(&mut args);
@@ -2337,6 +2339,34 @@ cygwin). A path separator is limited to a single byte.
23372339
args.push(arg);
23382340
}
23392341

2342+
fn flag_field_match_separator(args: &mut Vec<RGArg>) {
2343+
const SHORT: &str = "Set the field match separator.";
2344+
const LONG: &str = long!(
2345+
"\
2346+
Set the field match separator to use when printing file paths. This defaults to
2347+
:. A field match separator is limited to a single byte.
2348+
"
2349+
);
2350+
let arg = RGArg::flag("field-match-separator", "SEPARATOR")
2351+
.help(SHORT)
2352+
.long_help(LONG);
2353+
args.push(arg);
2354+
}
2355+
2356+
fn flag_field_context_separator(args: &mut Vec<RGArg>) {
2357+
const SHORT: &str = "Set the field context separator.";
2358+
const LONG: &str = long!(
2359+
"\
2360+
Set the field context separator to use when printing file paths. This defaults to
2361+
-. A field context separator is limited to a single byte.
2362+
"
2363+
);
2364+
let arg = RGArg::flag("field-context-separator", "SEPARATOR")
2365+
.help(SHORT)
2366+
.long_help(LONG);
2367+
args.push(arg);
2368+
}
2369+
23402370
fn flag_passthru(args: &mut Vec<RGArg>) {
23412371
const SHORT: &str = "Print both matching and non-matching lines.";
23422372
const LONG: &str = long!(

crates/core/args.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,8 @@ impl ArgMatches {
786786
.trim_ascii(self.is_present("trim"))
787787
.separator_search(None)
788788
.separator_context(self.context_separator())
789-
.separator_field_match(b":".to_vec())
790-
.separator_field_context(b"-".to_vec())
789+
.separator_field_match(self.field_match_separator()?)
790+
.separator_field_context(self.field_context_separator()?)
791791
.separator_path(self.path_separator()?)
792792
.path_terminator(self.path_terminator());
793793
if separator_search {
@@ -1377,6 +1377,54 @@ impl ArgMatches {
13771377
}
13781378
}
13791379

1380+
/// Returns the unescaped field match separator as a single byte, if one
1381+
/// exists.
1382+
///
1383+
/// If the provided field match separator is more than a single byte, then
1384+
/// an error is returned.
1385+
fn field_match_separator(&self) -> Result<Vec<u8>> {
1386+
let sep = match self.value_of_os("field-match-separator") {
1387+
None => return Ok(b":".to_vec()),
1388+
Some(sep) => cli::unescape_os(&sep),
1389+
};
1390+
if sep.is_empty() {
1391+
Ok(vec![])
1392+
} else if sep.len() > 1 {
1393+
Err(From::from(format!(
1394+
"A field match separator must be exactly one byte, but \
1395+
the given separator is {} bytes: {}\n.",
1396+
sep.len(),
1397+
cli::escape(&sep),
1398+
)))
1399+
} else {
1400+
Ok(sep)
1401+
}
1402+
}
1403+
1404+
/// Returns the unescaped field context separator as a single byte, if one
1405+
/// exists.
1406+
///
1407+
/// If the provided field context separator is more than a single byte,
1408+
/// then an error is returned.
1409+
fn field_context_separator(&self) -> Result<Vec<u8>> {
1410+
let sep = match self.value_of_os("field-context-separator") {
1411+
None => return Ok(b"-".to_vec()),
1412+
Some(sep) => cli::unescape_os(&sep),
1413+
};
1414+
if sep.is_empty() {
1415+
Ok(vec![])
1416+
} else if sep.len() > 1 {
1417+
Err(From::from(format!(
1418+
"A field context separator must be exactly one byte, but \
1419+
the given separator is {} bytes: {}\n.",
1420+
sep.len(),
1421+
cli::escape(&sep),
1422+
)))
1423+
} else {
1424+
Ok(sep)
1425+
}
1426+
}
1427+
13801428
/// Get a sequence of all available patterns from the command line.
13811429
/// This includes reading the -e/--regexp and -f/--file flags.
13821430
///

0 commit comments

Comments
 (0)