Add follow option for copy-paste

This commit is contained in:
sxyazi 2023-12-30 02:52:31 +08:00
parent 5127c2db33
commit c15cebbcfa
No known key found for this signature in database
4 changed files with 19 additions and 42 deletions

View file

@ -3,11 +3,14 @@ use yazi_shared::event::Exec;
use crate::{manager::Manager, tasks::Tasks};
pub struct Opt {
force: bool,
force: bool,
follow: bool,
}
impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self { Self { force: e.named.contains_key("force") } }
fn from(e: &Exec) -> Self {
Self { force: e.named.contains_key("force"), follow: e.named.contains_key("follow") }
}
}
impl Manager {
@ -16,6 +19,10 @@ impl Manager {
let (cut, ref src) = self.yanked;
let opt = opt.into() as Opt;
if cut { tasks.file_cut(src, dest, opt.force) } else { tasks.file_copy(src, dest, opt.force) }
if cut {
tasks.file_cut(src, dest, opt.force)
} else {
tasks.file_copy(src, dest, opt.force, opt.follow)
}
}
}

View file

@ -89,13 +89,13 @@ impl Tasks {
false
}
pub fn file_copy(&self, src: &HashSet<Url>, dest: &Url, force: bool) -> bool {
pub fn file_copy(&self, src: &HashSet<Url>, dest: &Url, force: bool, follow: bool) -> bool {
for u in src {
let to = dest.join(u.file_name().unwrap());
if force && u == &to {
debug!("file_copy: same file, skipping {:?}", to);
} else {
self.scheduler.file_copy(u.clone(), to, force);
self.scheduler.file_copy(u.clone(), to, force, follow);
}
}
false

View file

@ -207,7 +207,7 @@ impl Scheduler {
);
}
pub fn file_copy(&self, from: Url, mut to: Url, force: bool) {
pub fn file_copy(&self, from: Url, mut to: Url, force: bool, follow: bool) {
let name = format!("Copy {:?} to {:?}", from, to);
let id = self.running.write().add(TaskKind::User, name);
@ -217,7 +217,7 @@ impl Scheduler {
if !force {
to = unique_path(to).await;
}
file.paste(FileOpPaste { id, from, to, cut: false, follow: true, retry: 0 }).await.ok();
file.paste(FileOpPaste { id, from, to, cut: false, follow, retry: 0 }).await.ok();
}
.boxed(),
LOW,

View file

@ -38,40 +38,15 @@ pub async fn calculate_size(path: &Path) -> u64 {
pub fn copy_with_progress(from: &Path, to: &Path) -> mpsc::Receiver<Result<u64, io::Error>> {
let (tx, rx) = mpsc::channel(1);
let (tick_tx, mut tick_rx) = oneshot::channel();
let is_symlink = from.is_symlink();
tokio::spawn({
let (from, to) = (from.to_path_buf(), to.to_path_buf());
async move {
if is_symlink {
#[cfg(unix)]
{
_ = match fs::symlink(from, to).await {
Ok(()) => tick_tx.send(Ok(1)),
Err(e) => tick_tx.send(Err(e)),
}
}
#[cfg(windows)]
{
if from.is_dir() {
_ = match fs::symlink_dir(from, to).await {
Ok(()) => tick_tx.send(Ok(1)),
Err(e) => tick_tx.send(Err(e)),
}
} else {
_ = match fs::symlink_file(from, to).await {
Ok(()) => tick_tx.send(Ok(1)),
Err(e) => tick_tx.send(Err(e)),
}
}
}
} else {
_ = match fs::copy(from, to).await {
Ok(len) => tick_tx.send(Ok(len)),
Err(e) => tick_tx.send(Err(e)),
};
}
_ = match fs::copy(from, to).await {
Ok(len) => tick_tx.send(Ok(len)),
Err(e) => tick_tx.send(Err(e)),
};
}
});
@ -104,12 +79,7 @@ pub fn copy_with_progress(from: &Path, to: &Path) -> mpsc::Receiver<Result<u64,
None => {}
}
let len = if is_symlink {
1
} else {
fs::symlink_metadata(&to).await.map(|m| m.len()).unwrap_or(0)
};
let len = fs::symlink_metadata(&to).await.map(|m| m.len()).unwrap_or(0);
if len > last {
tx.send(Ok(len - last)).await.ok();
last = len;