This commit is contained in:
sxyazi 2024-04-25 19:06:45 +08:00
parent 6066f1e382
commit 87462a70d1
No known key found for this signature in database
3 changed files with 11 additions and 19 deletions

View file

@ -21,7 +21,6 @@ impl Manager {
let root = max_common_root(&old);
let old: Vec<_> = old.into_iter().map(|p| p.strip_prefix(&root).unwrap().to_owned()).collect();
let tab = self.tabs.cursor;
tokio::spawn(async move {
let tmp = PREVIEW.tmpfile("bulk");
@ -43,7 +42,7 @@ impl Manager {
AppProxy::stop().await;
let new: Vec<_> = fs::read_to_string(&tmp).await?.lines().map(PathBuf::from).collect();
Self::bulk_rename_do(cwd, root, old, new, tab).await
Self::bulk_rename_do(cwd, root, old, new).await
});
}
@ -52,7 +51,6 @@ impl Manager {
root: PathBuf,
old: Vec<PathBuf>,
new: Vec<PathBuf>,
tab: usize,
) -> Result<()> {
Term::clear(&mut stderr())?;
if old.len() != new.len() {
@ -98,7 +96,7 @@ impl Manager {
}
if !succeeded.is_empty() {
Pubsub::pub_from_bulk(tab, succeeded.iter().map(|(u, f)| (u, &f.url)).collect());
Pubsub::pub_from_bulk(succeeded.iter().map(|(u, f)| (u, &f.url)).collect());
FilesOp::Upserting(cwd, succeeded).emit();
}
drop(permit);

View file

@ -8,28 +8,27 @@ use super::Body;
#[derive(Debug, Serialize, Deserialize)]
pub struct BodyBulk<'a> {
pub tab: usize,
pub changes: HashMap<Cow<'a, Url>, Cow<'a, Url>>,
}
impl<'a> BodyBulk<'a> {
#[inline]
pub fn borrowed(tab: usize, changes: &HashMap<&'a Url, &'a Url>) -> Body<'a> {
pub fn borrowed(changes: &HashMap<&'a Url, &'a Url>) -> Body<'a> {
let iter = changes.iter().map(|(&from, &to)| (Cow::Borrowed(from), Cow::Borrowed(to)));
Self { tab, changes: iter.collect() }.into()
Self { changes: iter.collect() }.into()
}
}
impl BodyBulk<'static> {
#[inline]
pub fn owned(tab: usize, changes: &HashMap<&Url, &Url>) -> Body<'static> {
pub fn owned(changes: &HashMap<&Url, &Url>) -> Body<'static> {
let changes = changes
.iter()
.map(|(&from, &to)| (Cow::Owned(from.clone()), Cow::Owned(to.clone())))
.collect();
Self { tab, changes }.into()
Self { changes }.into()
}
}
@ -39,21 +38,16 @@ impl<'a> From<BodyBulk<'a>> for Body<'a> {
impl IntoLua<'_> for BodyBulk<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
BodyBulkIter { tab: self.tab, inner: self.changes.into_iter() }.into_lua(lua)
BodyBulkIter { inner: self.changes.into_iter() }.into_lua(lua)
}
}
// --- Iterator
pub struct BodyBulkIter {
pub tab: usize,
pub inner: hash_map::IntoIter<Cow<'static, Url>, Cow<'static, Url>>,
}
impl UserData for BodyBulkIter {
fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("tab", |_, me| Ok(me.tab));
}
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_meta_method(MetaMethod::Len, |_, me, ()| Ok(me.inner.len()));

View file

@ -130,15 +130,15 @@ impl Pubsub {
}
}
pub fn pub_from_bulk(tab: usize, changes: HashMap<&Url, &Url>) {
pub fn pub_from_bulk(changes: HashMap<&Url, &Url>) {
if LOCAL.read().contains_key("bulk") {
Self::pub_(BodyBulk::owned(tab, &changes));
Self::pub_(BodyBulk::owned(&changes));
}
if PEERS.read().values().any(|p| p.able("bulk")) {
Client::push(BodyBulk::borrowed(tab, &changes));
Client::push(BodyBulk::borrowed(&changes));
}
if BOOT.local_events.contains("bulk") {
BodyBulk::borrowed(tab, &changes).with_receiver(*ID).flush();
BodyBulk::borrowed(&changes).with_receiver(*ID).flush();
}
}