mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
refactor: simplify the code
This commit is contained in:
parent
f5ed58c97a
commit
4a483c7119
1 changed files with 32 additions and 106 deletions
|
|
@ -1,6 +1,4 @@
|
||||||
use std::borrow::Cow;
|
use std::{borrow::Cow, ffi::{OsStr, OsString}, path::Path};
|
||||||
use std::ffi::{OsStr, OsString};
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use yazi_plugin::CLIPBOARD;
|
use yazi_plugin::CLIPBOARD;
|
||||||
use yazi_shared::event::Cmd;
|
use yazi_shared::event::Cmd;
|
||||||
|
|
@ -9,12 +7,15 @@ use crate::tab::Tab;
|
||||||
|
|
||||||
struct Opt {
|
struct Opt {
|
||||||
type_: String,
|
type_: String,
|
||||||
separator: PathSeparator,
|
separator: Separator,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Cmd> for Opt {
|
impl From<Cmd> for Opt {
|
||||||
fn from(mut c: Cmd) -> Self {
|
fn from(mut c: Cmd) -> Self {
|
||||||
Self { type_: c.take_first_str().unwrap_or_default(), separator: PathSeparator::from(&c) }
|
Self {
|
||||||
|
type_: c.take_first_str().unwrap_or_default(),
|
||||||
|
separator: c.str("separator").unwrap_or_default().into(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -28,31 +29,13 @@ 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() {
|
||||||
match opt.type_.as_str() {
|
s.push(match opt.type_.as_str() {
|
||||||
"path" => {
|
"path" => opt.separator.transform(u),
|
||||||
match path_to_os_str(u, opt.separator) {
|
"dirname" => opt.separator.transform(u.parent().unwrap_or(Path::new(""))),
|
||||||
Cow::Borrowed(p) => s.push(p),
|
"filename" => opt.separator.transform(u.name()),
|
||||||
Cow::Owned(p) => s.push(&p),
|
"name_without_ext" => opt.separator.transform(u.file_stem().unwrap_or_default()),
|
||||||
};
|
|
||||||
}
|
|
||||||
"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");
|
||||||
}
|
}
|
||||||
|
|
@ -67,88 +50,31 @@ impl Tab {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Clone, Copy)]
|
// --- Separator
|
||||||
enum PathSeparator {
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||||
Unix,
|
enum Separator {
|
||||||
#[default]
|
|
||||||
Auto,
|
Auto,
|
||||||
|
Unix,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&Cmd> for PathSeparator {
|
impl From<&str> for Separator {
|
||||||
fn from(c: &Cmd) -> Self {
|
fn from(value: &str) -> Self {
|
||||||
match c.str("separator") {
|
match value {
|
||||||
Some("unix") => PathSeparator::Unix,
|
"unix" => Self::Unix,
|
||||||
Some("auto") => PathSeparator::Auto,
|
_ => Self::Auto,
|
||||||
_ => Default::default(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
impl Separator {
|
||||||
fn path_to_os_str(path: &Path, _separator: PathSeparator) -> Cow<'_, OsStr> {
|
fn transform<T: AsRef<Path> + ?Sized>(self, p: &T) -> Cow<OsStr> {
|
||||||
Cow::Borrowed(path.as_os_str())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
fn path_to_os_str(path: &Path, separator: PathSeparator) -> Cow<'_, OsStr> {
|
if self == Self::Unix {
|
||||||
use yazi_shared::fs::backslash_to_slash;
|
return match yazi_shared::fs::backslash_to_slash(p.as_ref()) {
|
||||||
|
Cow::Owned(p) => Cow::Owned(p.into_os_string()),
|
||||||
match separator {
|
Cow::Borrowed(p) => Cow::Borrowed(p.as_os_str()),
|
||||||
PathSeparator::Auto => Cow::Borrowed(path.as_os_str()),
|
};
|
||||||
PathSeparator::Unix => match backslash_to_slash(path) {
|
}
|
||||||
Cow::Borrowed(path) => Cow::Borrowed(path.as_os_str()),
|
Cow::Borrowed(p.as_ref().as_os_str())
|
||||||
Cow::Owned(path) => Cow::Owned(OsString::from(path)),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
#[test]
|
|
||||||
fn test_path_to_os_str_windows_auto() {
|
|
||||||
let path = PathBuf::from("C:\\Users\\JohnDoe\\Downloads\\image.png");
|
|
||||||
assert_eq!(
|
|
||||||
path_to_os_str(&path, PathSeparator::Auto).to_str(),
|
|
||||||
Some("C:\\Users\\JohnDoe\\Downloads\\image.png"),
|
|
||||||
"windows-auto",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
#[test]
|
|
||||||
fn test_path_to_os_str_windows_unix() {
|
|
||||||
let path = PathBuf::from("C:\\Users\\JohnDoe\\Downloads\\image.png");
|
|
||||||
assert_eq!(
|
|
||||||
path_to_os_str(&path, PathSeparator::Unix).to_str(),
|
|
||||||
Some("C:/Users/JohnDoe/Downloads/image.png"),
|
|
||||||
"windows-unix",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
|
||||||
#[test]
|
|
||||||
fn test_path_to_os_str_unix_auto() {
|
|
||||||
let path = PathBuf::from("/home/johndoe/Downloads/image.png");
|
|
||||||
assert_eq!(
|
|
||||||
path_to_os_str(&path, PathSeparator::Auto).to_str(),
|
|
||||||
Some("/home/johndoe/Downloads/image.png"),
|
|
||||||
"unix-auto"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
|
||||||
#[test]
|
|
||||||
fn test_path_to_os_str_unix_unix() {
|
|
||||||
let path = PathBuf::from("/home/johndoe/Downloads/image.png");
|
|
||||||
assert_eq!(
|
|
||||||
path_to_os_str(&path, PathSeparator::Unix).to_str(),
|
|
||||||
Some("/home/johndoe/Downloads/image.png"),
|
|
||||||
"unix-unix"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue