Handle any string replacement.

This commit is contained in:
Nathan Moreau 2019-04-12 01:33:51 +02:00 committed by David Peter
parent 3857fa8f62
commit 29bf9d731d
4 changed files with 6 additions and 24 deletions

View file

@ -258,8 +258,7 @@ fn usage() -> HashMap<&'static str, Help> {
, "Shows the full path starting from the root as opposed to relative paths."); , "Shows the full path starting from the root as opposed to relative paths.");
doc!(h, "path-separator" doc!(h, "path-separator"
, "Set the path separator to use when printing file paths." , "Set the path separator to use when printing file paths."
, "Set the path separator to use when printing file paths. \ , "Set the path separator to use when printing file paths.");
A path separator is limited to a single byte.");
doc!(h, "follow" doc!(h, "follow"
, "Follow symbolic links" , "Follow symbolic links"
, "By default, fd does not descend into symlinked directories. Using this flag, symbolic \ , "By default, fd does not descend into symlinked directories. Using this flag, symbolic \

View file

@ -77,5 +77,5 @@ pub struct FdOptions {
pub show_filesystem_errors: bool, pub show_filesystem_errors: bool,
/// The separator used to print file paths. /// The separator used to print file paths.
pub path_separator: Option<u8>, pub path_separator: Option<String>,
} }

View file

@ -114,19 +114,8 @@ fn main() {
_ => atty::is(Stream::Stdout), _ => atty::is(Stream::Stdout),
}; };
let path_separator: Option<u8> = match matches.value_of("path-separator") { let path_separator: Option<String> =
Some(sep_str) => { matches.value_of("path-separator").map(|str| str.to_owned());
let sep = sep_str.as_bytes();
if sep.len() != 1 {
print_error_and_exit!(
"'{}' is not a valid path separator. See 'fd --help'.",
sep_str
);
}
Some(sep[0])
}
None => None,
};
#[cfg(windows)] #[cfg(windows)]
let colored_output = colored_output && ansi_term::enable_ansi_support().is_ok(); let colored_output = colored_output && ansi_term::enable_ansi_support().is_ok();

View file

@ -69,16 +69,10 @@ fn print_entry_colorized(
let path_string = component.to_string_lossy(); let path_string = component.to_string_lossy();
match config.path_separator { match &config.path_separator {
None => write!(stdout, "{}", style.paint(path_string))?, None => write!(stdout, "{}", style.paint(path_string))?,
Some(sep) => { Some(sep) => {
let mut path_bytes = path_string.as_bytes().to_vec(); let path_string = path_string.replace(std::path::MAIN_SEPARATOR, &sep);
for b in &mut path_bytes {
if *b == b'/' || (cfg!(windows) && *b == b'\\') {
*b = sep;
}
}
let path_string = String::from_utf8_lossy(&path_bytes);
write!(stdout, "{}", style.paint(path_string))? write!(stdout, "{}", style.paint(path_string))?
} }
} }