mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
fix: build failed on windows
This commit is contained in:
parent
d4f8649f5d
commit
13f7d888fd
1 changed files with 41 additions and 22 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::ffi::{OsStr, OsString};
|
use std::ffi::{OsStr, OsString};
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
use std::path::Component;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use yazi_plugin::CLIPBOARD;
|
use yazi_plugin::CLIPBOARD;
|
||||||
|
|
@ -29,13 +28,31 @@ impl Tab {
|
||||||
let mut s = OsString::new();
|
let mut s = OsString::new();
|
||||||
let mut it = self.selected_or_hovered(true).peekable();
|
let mut it = self.selected_or_hovered(true).peekable();
|
||||||
while let Some(u) = it.next() {
|
while let Some(u) = it.next() {
|
||||||
s.push(match opt.type_.as_str() {
|
match opt.type_.as_str() {
|
||||||
"path" => path_to_os_str(u, opt.separator),
|
"path" => {
|
||||||
"dirname" => u.parent().map_or(OsStr::new(""), |p| path_to_os_str(p, opt.separator)),
|
match path_to_os_str(u, opt.separator) {
|
||||||
"filename" => u.name(),
|
Cow::Borrowed(p) => s.push(p),
|
||||||
"name_without_ext" => u.file_stem().unwrap_or(OsStr::new("")),
|
Cow::Owned(p) => s.push(&p),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
"dirname" => {
|
||||||
|
if let Some(parent) = u.parent() {
|
||||||
|
match path_to_os_str(parent, opt.separator) {
|
||||||
|
Cow::Borrowed(p) => s.push(p),
|
||||||
|
Cow::Owned(p) => s.push(&p),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"filename" => {
|
||||||
|
s.push(u.name());
|
||||||
|
}
|
||||||
|
"name_without_ext" => {
|
||||||
|
if let Some(stem) = u.file_stem() {
|
||||||
|
s.push(stem);
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => return,
|
_ => return,
|
||||||
});
|
}
|
||||||
if it.peek().is_some() {
|
if it.peek().is_some() {
|
||||||
s.push("\n");
|
s.push("\n");
|
||||||
}
|
}
|
||||||
|
|
@ -68,17 +85,19 @@ impl From<&Cmd> for PathSeparator {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
fn path_to_os_str(path: &Path, _separator: PathSeparator) -> &OsStr {
|
fn path_to_os_str(path: &Path, _separator: PathSeparator) -> Cow<'_, OsStr> {
|
||||||
return path.as_os_str();
|
return Cow::Borrowed(path.as_os_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
fn path_to_os_str(path: &Path, separator: PathSeparator) -> &OsStr {
|
fn path_to_os_str(path: &Path, separator: PathSeparator) -> Cow<'_, OsStr> {
|
||||||
if let PathSeparator::Auto = separator {
|
if let PathSeparator::Auto = separator {
|
||||||
return path.as_os_str();
|
return Cow::Borrowed(path.as_os_str());
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut s = OsString::new();
|
use std::path::Component;
|
||||||
|
|
||||||
|
let mut s = OsString::with_capacity(path.as_os_str().len());
|
||||||
for component in path.components() {
|
for component in path.components() {
|
||||||
match component {
|
match component {
|
||||||
Component::RootDir => {}
|
Component::RootDir => {}
|
||||||
|
|
@ -96,7 +115,7 @@ fn path_to_os_str(path: &Path, separator: PathSeparator) -> &OsStr {
|
||||||
s.push("/");
|
s.push("/");
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.as_os_str();
|
return Cow::Owned(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
@ -110,8 +129,8 @@ mod tests {
|
||||||
fn test_path_to_os_str_windows_auto() {
|
fn test_path_to_os_str_windows_auto() {
|
||||||
let path = PathBuf::from("C:\\Users\\JohnDoe\\Downloads\\image.png");
|
let path = PathBuf::from("C:\\Users\\JohnDoe\\Downloads\\image.png");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
path_to_os_str(&path, PathSeparator::Auto),
|
path_to_os_str(&path, PathSeparator::Auto).to_str(),
|
||||||
"C:\\Users\\JohnDoe\\Downloads\\image.png",
|
Some("C:\\Users\\JohnDoe\\Downloads\\image.png"),
|
||||||
"windows-auto",
|
"windows-auto",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -121,8 +140,8 @@ mod tests {
|
||||||
fn test_path_to_os_str_windows_unix() {
|
fn test_path_to_os_str_windows_unix() {
|
||||||
let path = PathBuf::from("C:\\Users\\JohnDoe\\Downloads\\image.png");
|
let path = PathBuf::from("C:\\Users\\JohnDoe\\Downloads\\image.png");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
path_to_os_str(&path, PathSeparator::Unix),
|
path_to_os_str(&path, PathSeparator::Unix).to_str(),
|
||||||
"C:/Users/JohnDow/Downloads/image.png",
|
Some("C:/Users/JohnDow/Downloads/image.png"),
|
||||||
"windows-unix",
|
"windows-unix",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -132,8 +151,8 @@ mod tests {
|
||||||
fn test_path_to_os_str_unix_auto() {
|
fn test_path_to_os_str_unix_auto() {
|
||||||
let path = PathBuf::from("/home/johndoe/Downloads/image.png");
|
let path = PathBuf::from("/home/johndoe/Downloads/image.png");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
path_to_os_str(&path, PathSeparator::Auto),
|
path_to_os_str(&path, PathSeparator::Auto).to_str(),
|
||||||
"/home/johndoe/Downloads/image.png",
|
Some("/home/johndoe/Downloads/image.png"),
|
||||||
"unix-auto"
|
"unix-auto"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -143,8 +162,8 @@ mod tests {
|
||||||
fn test_path_to_os_str_unix_unix() {
|
fn test_path_to_os_str_unix_unix() {
|
||||||
let path = PathBuf::from("/home/johndoe/Downloads/image.png");
|
let path = PathBuf::from("/home/johndoe/Downloads/image.png");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
path_to_os_str(&path, PathSeparator::Unix),
|
path_to_os_str(&path, PathSeparator::Unix).to_str(),
|
||||||
"/home/johndoe/Downloads/image.png",
|
Some("/home/johndoe/Downloads/image.png"),
|
||||||
"unix-unix"
|
"unix-unix"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue