Repository: sxyazi/yazi
File: yazi-core/src/tab/commands/copy.rs (Rust)

Code before:
		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('/');
	}

	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)]
mod tests {
	use std::path::PathBuf;

	use super::*;

	#[cfg(target_os = "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(target_os = "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(),

Review comment (suggestion):
`OsString::from`: Converts a `String` into an `OsString`. This conversion **does not** allocate or copy memory,

Code after:
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,
	separator: Separator,
}

impl From<Cmd> for Opt {
	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 {
	#[yazi_codegen::command]
	pub fn copy(&mut self, opt: Opt) {
		if !self.try_escape_visual() {
			return;
		}

		let mut s = OsString::new();
		let mut it = self.selected_or_hovered(true).peekable();
		while let Some(u) = it.next() {
			s.push(match opt.type_.as_str() {
				"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() {
				s.push("\n");
			}
		}

		// Copy the CWD path regardless even if the directory is empty
		if s.is_empty() && opt.type_ == "dirname" {
			s.push(self.cwd());
		}

		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())
	}
}