From cb217e5e7f6828ba153ca33a2ff40582ed8d403d Mon Sep 17 00:00:00 2001 From: alan910127 <70696274+alan910127@users.noreply.github.com> Date: Sun, 3 Nov 2024 00:55:45 +0800 Subject: [PATCH] refactor: use backslash_to_slash function --- yazi-core/src/tab/commands/copy.rs | 45 ++++++------------------------ 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/yazi-core/src/tab/commands/copy.rs b/yazi-core/src/tab/commands/copy.rs index 0b3619be..fc722fe1 100644 --- a/yazi-core/src/tab/commands/copy.rs +++ b/yazi-core/src/tab/commands/copy.rs @@ -84,47 +84,20 @@ impl From<&Cmd> for PathSeparator { } } -#[cfg(not(target_os = "windows"))] +#[cfg(unix)] fn path_to_os_str(path: &Path, _separator: PathSeparator) -> Cow<'_, OsStr> { - return Cow::Borrowed(path.as_os_str()); + Cow::Borrowed(path.as_os_str()) } -#[cfg(target_os = "windows")] +#[cfg(windows)] fn path_to_os_str(path: &Path, separator: PathSeparator) -> Cow<'_, OsStr> { - if let PathSeparator::Auto = separator { - return Cow::Borrowed(path.as_os_str()); - }; - - use std::path::Component; - - let mut s = String::with_capacity(path.as_os_str().len()); - for component in path.components() { - match component { - Component::RootDir => {} - Component::CurDir => s.push('.'), - Component::ParentDir => s.push_str(".."), - Component::Normal(path) => s.push_str(&path.to_string_lossy()), - Component::Prefix(prefix) => { - // "C:\foo" => [Prefix("C:"), RootDir, Normal(foo)] - s.push_str(&prefix.as_os_str().to_string_lossy()); - // If we push a "/" below, we will met a RootDir and push a "/" - // again resulting in "C://". So we need to skip that. - continue; - } - }; - s.push('/'); + match separator { + PathSeparator::Auto => Cow::Borrowed(path.as_os_str()), + PathSeparator::Unix => match yazi_shared::fs::path::backslash_to_slash(path) { + Cow::Borrowed(path) => Cow::Borrowed(path.as_os_str()), + Cow::Owned(path) => Cow::Owned(OsString::from(path)), + }, } - - use std::os::windows::ffi::OsStrExt as _; - use std::path::MAIN_SEPARATOR; - - let path_ends_with_main_sep = - path.as_os_str().encode_wide().last() == Some(MAIN_SEPARATOR as u16); - if !path_ends_with_main_sep && s != "/" && s.ends_with('/') { - s.pop(); - } - - return Cow::Owned(OsString::from(s)); } #[cfg(test)]