Skip to content

Commit

Permalink
core: Add field-match-separator and field-context-separator
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Huang committed May 31, 2021
1 parent 94e4b8e commit 5684882
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
2 changes: 2 additions & 0 deletions complete/_rg
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ _rg() {
"--no-config[don't load configuration files]"
'(-0 --null)'{-0,--null}'[print NUL byte after file names]'
'--path-separator=[specify path separator to use when printing file names]:separator'
'--field-match-separator=[specify field match separator to use when printing file names]:separator'
'--field-context-separator=[specify field context separator to use when printing file names]:separator'
'(-q --quiet)'{-q,--quiet}'[suppress normal output]'
'--regex-size-limit=[specify upper size limit of compiled regex]:regex size (bytes)'
'*'{-u,--unrestricted}'[reduce level of "smart" searching]'
Expand Down
30 changes: 30 additions & 0 deletions crates/core/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ pub fn all_args_and_flags() -> Vec<RGArg> {
flag_one_file_system(&mut args);
flag_only_matching(&mut args);
flag_path_separator(&mut args);
flag_field_match_separator(&mut args);
flag_field_context_separator(&mut args);
flag_passthru(&mut args);
flag_pcre2(&mut args);
flag_pcre2_version(&mut args);
Expand Down Expand Up @@ -2337,6 +2339,34 @@ cygwin). A path separator is limited to a single byte.
args.push(arg);
}

fn flag_field_match_separator(args: &mut Vec<RGArg>) {
const SHORT: &str = "Set the field match separator.";
const LONG: &str = long!(
"\
Set the field match separator to use when printing file paths. This defaults to
:. A field match separator is limited to a single byte.
"
);
let arg = RGArg::flag("field-match-separator", "SEPARATOR")
.help(SHORT)
.long_help(LONG);
args.push(arg);
}

fn flag_field_context_separator(args: &mut Vec<RGArg>) {
const SHORT: &str = "Set the field context separator.";
const LONG: &str = long!(
"\
Set the field context separator to use when printing file paths. This defaults to
-. A field context separator is limited to a single byte.
"
);
let arg = RGArg::flag("field-context-separator", "SEPARATOR")
.help(SHORT)
.long_help(LONG);
args.push(arg);
}

fn flag_passthru(args: &mut Vec<RGArg>) {
const SHORT: &str = "Print both matching and non-matching lines.";
const LONG: &str = long!(
Expand Down
52 changes: 50 additions & 2 deletions crates/core/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,8 @@ impl ArgMatches {
.trim_ascii(self.is_present("trim"))
.separator_search(None)
.separator_context(self.context_separator())
.separator_field_match(b":".to_vec())
.separator_field_context(b"-".to_vec())
.separator_field_match(self.field_match_separator()?)
.separator_field_context(self.field_context_separator()?)
.separator_path(self.path_separator()?)
.path_terminator(self.path_terminator());
if separator_search {
Expand Down Expand Up @@ -1377,6 +1377,54 @@ impl ArgMatches {
}
}

/// Returns the unescaped field match separator as a single byte, if one
/// exists.
///
/// If the provided field match separator is more than a single byte, then
/// an error is returned.
fn field_match_separator(&self) -> Result<Vec<u8>> {
let sep = match self.value_of_os("field-match-separator") {
None => return Ok(b":".to_vec()),
Some(sep) => cli::unescape_os(&sep),
};
if sep.is_empty() {
Ok(vec![])
} else if sep.len() > 1 {
Err(From::from(format!(
"A field match separator must be exactly one byte, but \
the given separator is {} bytes: {}\n.",
sep.len(),
cli::escape(&sep),
)))
} else {
Ok(sep)
}
}

/// Returns the unescaped field context separator as a single byte, if one
/// exists.
///
/// If the provided field context separator is more than a single byte,
/// then an error is returned.
fn field_context_separator(&self) -> Result<Vec<u8>> {
let sep = match self.value_of_os("field-context-separator") {
None => return Ok(b"-".to_vec()),
Some(sep) => cli::unescape_os(&sep),
};
if sep.is_empty() {
Ok(vec![])
} else if sep.len() > 1 {
Err(From::from(format!(
"A field context separator must be exactly one byte, but \
the given separator is {} bytes: {}\n.",
sep.len(),
cli::escape(&sep),
)))
} else {
Ok(sep)
}
}

/// Get a sequence of all available patterns from the command line.
/// This includes reading the -e/--regexp and -f/--file flags.
///
Expand Down

0 comments on commit 5684882

Please sign in to comment.