From 24e108e1ad85371759963918b2fe48e3be74f81f Mon Sep 17 00:00:00 2001 From: Nathan Moreau Date: Thu, 11 Apr 2019 00:30:56 +0200 Subject: [PATCH] Add path-separator option. Example usage: `fd.exe --path-separator /` on windows. --- src/app.rs | 10 ++++++++++ src/internal/opts.rs | 3 +++ src/main.rs | 15 +++++++++++++++ src/output.rs | 16 +++++++++++++++- 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/app.rs b/src/app.rs index 950ec1e..38b8154 100644 --- a/src/app.rs +++ b/src/app.rs @@ -207,6 +207,12 @@ pub fn build_app() -> App<'static, 'static> { .hidden_short_help(true), ) .arg(arg("pattern")) + .arg( + arg("path-separator") + .takes_value(true) + .long("path-separator") + .number_of_values(1), + ) .arg(arg("path").multiple(true)) .arg( arg("search-path") @@ -249,6 +255,10 @@ fn usage() -> HashMap<&'static str, Help> { doc!(h, "absolute-path" , "Show absolute instead of relative paths" , "Shows the full path starting from the root as opposed to relative paths."); + doc!(h, "path-separator" + , "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" , "Follow symbolic links" , "By default, fd does not descend into symlinked directories. Using this flag, symbolic \ diff --git a/src/internal/opts.rs b/src/internal/opts.rs index 4fd3dac..6c509d3 100644 --- a/src/internal/opts.rs +++ b/src/internal/opts.rs @@ -75,4 +75,7 @@ pub struct FdOptions { /// Whether or not to display filesystem errors pub show_filesystem_errors: bool, + + /// The separator used to print file paths. + pub path_separator: Option, } diff --git a/src/main.rs b/src/main.rs index eb6b995..306181c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,6 +114,20 @@ fn main() { _ => atty::is(Stream::Stdout), }; + let path_separator: Option = match matches.value_of("path-separator") { + Some(sep_str) => { + 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)] let colored_output = colored_output && ansi_term::enable_ansi_support().is_ok(); @@ -244,6 +258,7 @@ fn main() { size_constraints: size_limits, time_constraints, show_filesystem_errors: matches.is_present("show-errors"), + path_separator, }; match RegexBuilder::new(&pattern_regex) diff --git a/src/output.rs b/src/output.rs index a5d0c1e..414a296 100644 --- a/src/output.rs +++ b/src/output.rs @@ -67,7 +67,21 @@ fn print_entry_colorized( .map(Style::to_ansi_term_style) .unwrap_or(default_style); - write!(stdout, "{}", style.paint(component.to_string_lossy()))?; + let path_string = component.to_string_lossy(); + + match config.path_separator { + None => write!(stdout, "{}", style.paint(path_string))?, + Some(sep) => { + let mut path_bytes = path_string.as_bytes().to_vec(); + 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))? + } + } if wants_to_quit.load(Ordering::Relaxed) { writeln!(stdout)?;