Skip to content

Commit 1a3615d

Browse files
aswildsharkdp
authored andcommitted
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: #537
1 parent cf7dd43 commit 1a3615d

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/filesystem.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::borrow::Cow;
2-
use std::env::current_dir;
2+
use std::env;
33
use std::ffi::OsStr;
44
use std::fs;
55
use std::io;
@@ -15,7 +15,7 @@ pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {
1515
}
1616

1717
let path = path.strip_prefix(".").unwrap_or(path);
18-
current_dir().map(|path_buf| path_buf.join(path))
18+
env::current_dir().map(|path_buf| path_buf.join(path))
1919
}
2020

2121
pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {
@@ -108,6 +108,23 @@ pub fn strip_current_dir(path: &Path) -> &Path {
108108
path.strip_prefix(".").unwrap_or(path)
109109
}
110110

111+
/// Default value for the path_separator, mainly for MSYS/MSYS2, which set the MSYSTEM
112+
/// environment variable, and we set fd's path separator to '/' rather than Rust's default of '\'.
113+
///
114+
/// Returns Some to use a nonstandard path separator, or None to use rust's default on the target
115+
/// platform.
116+
pub fn default_path_separator() -> Option<String> {
117+
if cfg!(windows) {
118+
let msystem = env::var("MSYSTEM").ok()?;
119+
match msystem.as_str() {
120+
"MINGW64" | "MINGW32" | "MSYS" => Some("/".to_owned()),
121+
_ => None,
122+
}
123+
} else {
124+
None
125+
}
126+
}
127+
111128
#[cfg(test)]
112129
mod tests {
113130
use super::strip_current_dir;

src/main.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ fn run() -> Result<ExitCode> {
173173
_ => ansi_colors_support && env::var_os("NO_COLOR").is_none() && interactive_terminal,
174174
};
175175

176-
let path_separator = matches.value_of("path-separator").map(|str| str.to_owned());
176+
let path_separator = matches
177+
.value_of("path-separator")
178+
.map_or_else(filesystem::default_path_separator, |s| Some(s.to_owned()));
177179

178180
let ls_colors = if colored_output {
179181
Some(LsColors::from_env().unwrap_or_else(|| LsColors::from_string(DEFAULT_LS_COLORS)))

0 commit comments

Comments
 (0)