diff options
author | Allen Wild <allenwild93@gmail.com> | 2021-02-14 18:13:56 -0500 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2021-02-15 20:08:59 +0100 |
commit | 1a3615df9cada1a47ceb9f76c0f733be864f0b80 (patch) | |
tree | 2ec1485fbddbf79cba6aded7ad0a4e2fb0667cf1 | |
parent | cf7dd43f80fb455fe7907c026fae5404f832868b (diff) |
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
-rw-r--r-- | src/filesystem.rs | 21 | ||||
-rw-r--r-- | 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<PathBuf> { } 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<PathBuf> { @@ -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<String> { + 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<ExitCode> { _ => 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))) |