Skip to content

Commit

Permalink
feat: new copy --separator option to allow specifying the path sepa…
Browse files Browse the repository at this point in the history
…rator (#1877)

Co-authored-by: sxyazi <sxyazi@gmail.com>
  • Loading branch information
alan910127 and sxyazi authored Nov 3, 2024
1 parent 5531874 commit 565f9cc
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions yazi-core/src/tab/commands/copy.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use std::ffi::{OsStr, OsString};
use std::{borrow::Cow, ffi::{OsStr, OsString}, path::Path};

use yazi_plugin::CLIPBOARD;
use yazi_shared::event::Cmd;

use crate::tab::Tab;

struct Opt {
type_: String,
type_: String,
separator: Separator,
}

impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self { Self { type_: c.take_first_str().unwrap_or_default() } }
fn from(mut c: Cmd) -> Self {
Self {
type_: c.take_first_str().unwrap_or_default(),
separator: c.str("separator").unwrap_or_default().into(),
}
}
}

impl Tab {
Expand All @@ -24,10 +30,10 @@ impl Tab {
let mut it = self.selected_or_hovered(true).peekable();
while let Some(u) = it.next() {
s.push(match opt.type_.as_str() {
"path" => u.as_os_str(),
"dirname" => u.parent().map_or(OsStr::new(""), |p| p.as_os_str()),
"filename" => u.name(),
"name_without_ext" => u.file_stem().unwrap_or(OsStr::new("")),
"path" => opt.separator.transform(u),
"dirname" => opt.separator.transform(u.parent().unwrap_or(Path::new(""))),
"filename" => opt.separator.transform(u.name()),
"name_without_ext" => opt.separator.transform(u.file_stem().unwrap_or_default()),
_ => return,
});
if it.peek().is_some() {
Expand All @@ -43,3 +49,32 @@ impl Tab {
futures::executor::block_on(CLIPBOARD.set(s));
}
}

// --- Separator
#[derive(Clone, Copy, PartialEq, Eq)]
enum Separator {
Auto,
Unix,
}

impl From<&str> for Separator {
fn from(value: &str) -> Self {
match value {
"unix" => Self::Unix,
_ => Self::Auto,
}
}
}

impl Separator {
fn transform<T: AsRef<Path> + ?Sized>(self, p: &T) -> Cow<OsStr> {
#[cfg(windows)]
if self == Self::Unix {
return match yazi_shared::fs::backslash_to_slash(p.as_ref()) {
Cow::Owned(p) => Cow::Owned(p.into_os_string()),
Cow::Borrowed(p) => Cow::Borrowed(p.as_os_str()),
};
}
Cow::Borrowed(p.as_ref().as_os_str())
}
}

0 comments on commit 565f9cc

Please sign in to comment.