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
This commit is contained in:
Allen Wild 2021-02-14 18:13:56 -05:00 committed by David Peter
parent cf7dd43f80
commit 1a3615df9c
2 changed files with 22 additions and 3 deletions

View file

@ -1,5 +1,5 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::env::current_dir; use std::env;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs; use std::fs;
use std::io; 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); 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> { 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) 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)] #[cfg(test)]
mod tests { mod tests {
use super::strip_current_dir; use super::strip_current_dir;

View file

@ -173,7 +173,9 @@ fn run() -> Result<ExitCode> {
_ => ansi_colors_support && env::var_os("NO_COLOR").is_none() && interactive_terminal, _ => 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 { let ls_colors = if colored_output {
Some(LsColors::from_env().unwrap_or_else(|| LsColors::from_string(DEFAULT_LS_COLORS))) Some(LsColors::from_env().unwrap_or_else(|| LsColors::from_string(DEFAULT_LS_COLORS)))