This commit is contained in:
evpeople 2025-01-29 20:24:45 +08:00
parent f5da79f302
commit a2ea22a5f2
No known key found for this signature in database
GPG key ID: CF5768B6A73046FF
2 changed files with 51 additions and 53 deletions

View file

@ -1,16 +1,15 @@
use std::{borrow::Cow, collections::{HashMap, HashSet}, ffi::{OsStr, OsString}, io::{stderr, BufWriter, Write}, path::PathBuf}; use std::{borrow::Cow, collections::{HashMap, HashSet}, ffi::{OsStr, OsString}, io::{BufWriter, Write, stderr}, path::PathBuf};
use anyhow::{Result, anyhow}; use anyhow::{Result, anyhow};
use scopeguard::defer; use scopeguard::defer;
use tokio::{io::{AsyncReadExt, AsyncWriteExt, stdin}}; use tokio::{fs, io::{AsyncReadExt, AsyncWriteExt, stdin}};
use tokio::fs;
use yazi_config::{OPEN, PREVIEW}; use yazi_config::{OPEN, PREVIEW};
use yazi_dds::Pubsub; use yazi_dds::Pubsub;
use yazi_fs::{max_common_root, maybe_exists, ok_or_not_found, paths_to_same_file, realname, File, FilesOp}; use yazi_fs::{File, FilesOp, max_common_root, maybe_exists, ok_or_not_found, paths_to_same_file, realname};
use yazi_proxy::{AppProxy, HIDER, TasksProxy, WATCHER}; use yazi_proxy::{AppProxy, HIDER, TasksProxy, WATCHER};
use yazi_shared::{terminal_clear, url::{Url, UrnBuf}}; use yazi_shared::{terminal_clear, url::{Url, UrnBuf}};
use crate::manager:: Manager; use crate::manager::Manager;
impl Manager { impl Manager {
pub(super) fn bulk_create(&self) { pub(super) fn bulk_create(&self) {
@ -32,12 +31,12 @@ impl Manager {
defer!(AppProxy::resume()); defer!(AppProxy::resume());
AppProxy::stop().await; AppProxy::stop().await;
let new: Vec<_> = fs::read_to_string(&tmp).await?.lines().map(Url::from).collect(); let new: Vec<_> = fs::read_to_string(&tmp).await?.lines().map(Url::from).collect();
Self::bulk_create_do( new).await Self::bulk_create_do(new).await
}); });
} }
async fn bulk_create_do(new:Vec<Url>) -> Result<()> { async fn bulk_create_do(new: Vec<Url>) -> Result<()> {
terminal_clear(&mut stderr())?; terminal_clear(&mut stderr())?;
if new.is_empty() { if new.is_empty() {
return Ok(()); return Ok(());
@ -48,7 +47,7 @@ impl Manager {
for n in &new { for n in &new {
if n.to_str().unwrap().ends_with('/') || n.to_str().unwrap().ends_with('\\') { if n.to_str().unwrap().ends_with('/') || n.to_str().unwrap().ends_with('\\') {
writeln!(stderr, "create new dir-> {}", n.display())?; writeln!(stderr, "create new dir-> {}", n.display())?;
}else{ } else {
writeln!(stderr, "create new file-> {}", n.display())?; writeln!(stderr, "create new file-> {}", n.display())?;
} }
} }
@ -64,43 +63,41 @@ impl Manager {
let permit = WATCHER.acquire().await.unwrap(); let permit = WATCHER.acquire().await.unwrap();
let (mut failed, mut succeeded) = (Vec::new(), HashMap::with_capacity(new.len())); let (mut failed, mut succeeded) = (Vec::new(), HashMap::with_capacity(new.len()));
for n in new { for n in new {
let Some(parent) = n.parent_url() else { return Ok(()) }; let Some(parent) = n.parent_url() else { return Ok(()) };
let dir = n.to_str().unwrap().ends_with('/') || n.to_str().unwrap().ends_with('\\'); let dir = n.to_str().unwrap().ends_with('/') || n.to_str().unwrap().ends_with('\\');
if dir { if dir {
if let Err(e) = fs::create_dir_all(&n).await { if let Err(e) = fs::create_dir_all(&n).await {
failed.push((PathBuf::new(), n.into(), e.into())); failed.push((PathBuf::new(), n.into(), e.into()));
} else if let Ok(f) = File::from(n.clone()).await { } else if let Ok(f) = File::from(n.clone()).await {
succeeded.insert(n.clone(), f); succeeded.insert(n.clone(), f);
} else { } else {
failed.push((PathBuf::new(), n, anyhow!("Failed to retrieve file info"))); failed.push((PathBuf::new(), n, anyhow!("Failed to retrieve file info")));
} }
} else { } else {
fs::create_dir_all(&parent).await.ok(); fs::create_dir_all(&parent).await.ok();
if let Some(real) = realname(&n).await { if let Some(real) = realname(&n).await {
if let Err(e) = ok_or_not_found(fs::remove_file(&n).await) { if let Err(e) = ok_or_not_found(fs::remove_file(&n).await) {
failed.push((PathBuf::new(), n.into(), e.into())); failed.push((PathBuf::new(), n.into(), e.into()));
continue; continue;
} }
FilesOp::Deleting(parent.clone(), HashSet::from_iter([UrnBuf::from(real)])).emit(); FilesOp::Deleting(parent.clone(), HashSet::from_iter([UrnBuf::from(real)])).emit();
} }
if let Err(e) = fs::File::create(&n).await { if let Err(e) = fs::File::create(&n).await {
failed.push((PathBuf::new(), n.into(), e.into())); failed.push((PathBuf::new(), n.into(), e.into()));
} else if let Ok(f) = File::from(n.clone()).await { } else if let Ok(f) = File::from(n.clone()).await {
succeeded.insert(n.clone(), f); succeeded.insert(n.clone(), f);
} else { } else {
failed.push((PathBuf::new(), n.into(), anyhow!("Failed to retrieve file info"))); failed.push((PathBuf::new(), n.into(), anyhow!("Failed to retrieve file info")));
} }
} }
} }
if !succeeded.is_empty() { if !succeeded.is_empty() {
Pubsub::pub_from_bulk(succeeded.iter().map(|(o, n)| (o, &n.url)).collect()); Pubsub::pub_from_bulk(succeeded.iter().map(|(o, n)| (o, &n.url)).collect());
FilesOp::create(succeeded.into_values().collect()); FilesOp::create(succeeded.into_values().collect());
} }
drop(permit); drop(permit);

View file

@ -74,17 +74,18 @@ impl FilesOp {
} }
} }
} }
pub fn create(files: Vec<File>) { pub fn create(files: Vec<File>) {
let mut parents: HashMap<_, Vec<_>> = Default::default(); let mut parents: HashMap<_, Vec<_>> = Default::default();
for file in files { for file in files {
let Some(parent) = file.url.parent_url() else { continue }; let Some(parent) = file.url.parent_url() else { continue };
parents.entry(parent).or_default().push(file); parents.entry(parent).or_default().push(file);
} }
for (parent, files) in parents { for (parent, files) in parents {
Self::Creating(parent, files).emit(); Self::Creating(parent, files).emit();
} }
} }
pub fn mutate(ops: Vec<Self>) { pub fn mutate(ops: Vec<Self>) {
let mut parents: HashMap<_, (HashMap<_, _>, HashSet<_>)> = Default::default(); let mut parents: HashMap<_, (HashMap<_, _>, HashSet<_>)> = Default::default();