Add --path-separator flag.

This flag permits setting the path separator used for all file paths
printed by ripgrep in normal operation.

Fixes #275
This commit is contained in:
Andrew Gallant
2017-01-10 18:16:15 -05:00
parent 2143bcf9cb
commit 8751e55706
6 changed files with 89 additions and 5 deletions

View File

@@ -62,6 +62,7 @@ pub struct Args {
no_ignore_vcs: bool,
no_messages: bool,
null: bool,
path_separator: Option<u8>,
quiet: bool,
quiet_matched: QuietMatched,
replace: Option<Vec<u8>>,
@@ -151,6 +152,7 @@ impl Args {
.heading(self.heading)
.line_per_match(self.line_per_match)
.null(self.null)
.path_separator(self.path_separator)
.with_filename(self.with_filename);
if let Some(ref rep) = self.replace {
p = p.replace(rep.clone());
@@ -347,6 +349,7 @@ impl<'a> ArgMatches<'a> {
no_ignore_vcs: self.no_ignore_vcs(),
no_messages: self.is_present("no-messages"),
null: self.is_present("null"),
path_separator: try!(self.path_separator()),
quiet: quiet,
quiet_matched: QuietMatched::new(quiet),
replace: self.replace(),
@@ -616,6 +619,25 @@ impl<'a> ArgMatches<'a> {
}
}
/// Returns the unescaped path separator in UTF-8 bytes.
fn path_separator(&self) -> Result<Option<u8>> {
match self.value_of_lossy("path-separator") {
None => Ok(None),
Some(sep) => {
let sep = unescape(&sep);
if sep.is_empty() {
Ok(None)
} else if sep.len() > 1 {
Err(From::from(format!(
"A path separator must be exactly one byte, but \
the given separator is {} bytes.", sep.len())))
} else {
Ok(Some(sep[0]))
}
}
}
}
/// Returns the before and after contexts from the command line.
///
/// If a context setting was absent, then `0` is returned.