From 1a3615df9cada1a47ceb9f76c0f733be864f0b80 Mon Sep 17 00:00:00 2001 From: Allen Wild Date: Sun, 14 Feb 2021 18:13:56 -0500 Subject: [PATCH] set default path separator to '/' in MSYS MSYS and MSYS2 environments (such as Git Bash) have a UNIX like filesystem which uses '/' as the path separator rather than '\', but Rust doesn't know about this by default. On Windows, check the MSYSTEM environment variable and set the default value of the --path-separator option to '/' for convenience. There is no similar detection of Cygwin because there seems to be no way for Rust (and any native Win32) programs to detect that they're being called from a Cygwin environment. Cygwin users can use a shell alias/function/script to wrap fd. Fixes: https://github.com/sharkdp/fd/issues/537 --- src/filesystem.rs | 21 +++++++++++++++++++-- src/main.rs | 4 +++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/filesystem.rs b/src/filesystem.rs index a14f74d..15ca56d 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -1,5 +1,5 @@ use std::borrow::Cow; -use std::env::current_dir; +use std::env; use std::ffi::OsStr; use std::fs; use std::io; @@ -15,7 +15,7 @@ pub fn path_absolute_form(path: &Path) -> io::Result { } let path = path.strip_prefix(".").unwrap_or(path); - current_dir().map(|path_buf| path_buf.join(path)) + env::current_dir().map(|path_buf| path_buf.join(path)) } pub fn absolute_path(path: &Path) -> io::Result { @@ -108,6 +108,23 @@ pub fn strip_current_dir(path: &Path) -> &Path { path.strip_prefix(".").unwrap_or(path) } +/// Default value for the path_separator, mainly for MSYS/MSYS2, which set the MSYSTEM +/// environment variable, and we set fd's path separator to '/' rather than Rust's default of '\'. +/// +/// Returns Some to use a nonstandard path separator, or None to use rust's default on the target +/// platform. +pub fn default_path_separator() -> Option { + if cfg!(windows) { + let msystem = env::var("MSYSTEM").ok()?; + match msystem.as_str() { + "MINGW64" | "MINGW32" | "MSYS" => Some("/".to_owned()), + _ => None, + } + } else { + None + } +} + #[cfg(test)] mod tests { use super::strip_current_dir; diff --git a/src/main.rs b/src/main.rs index 32b99c2..6f0eece 100644 --- a/src/main.rs +++ b/src/main.rs @@ -173,7 +173,9 @@ fn run() -> Result { _ => ansi_colors_support && env::var_os("NO_COLOR").is_none() && interactive_terminal, }; - let path_separator = matches.value_of("path-separator").map(|str| str.to_owned()); + let path_separator = matches + .value_of("path-separator") + .map_or_else(filesystem::default_path_separator, |s| Some(s.to_owned())); let ls_colors = if colored_output { Some(LsColors::from_env().unwrap_or_else(|| LsColors::from_string(DEFAULT_LS_COLORS)))