Skip to content

Commit

Permalink
feat: cut and paste (x -> p) sends new "move" events
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Apr 6, 2024
1 parent 357f87a commit edb2840
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 10 deletions.
9 changes: 8 additions & 1 deletion yazi-core/src/manager/commands/paste.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use yazi_dds::Pubsub;
use yazi_shared::event::Cmd;

use crate::{manager::Manager, tasks::Tasks};

pub struct Opt {
force: bool,
force: bool,
follow: bool,
}

Expand All @@ -21,6 +22,12 @@ impl Manager {
if self.yanked.cut {
tasks.file_cut(&src, dest, opt.force);

src.iter().for_each(|source_file| {
if let Some(name) = source_file.file_name().map(|s| dest.join(s)) {
Pubsub::pub_from_move(self.tabs.cursor, source_file, &name);
}
});

self.tabs.iter_mut().for_each(|t| _ = t.selected.remove_many(&src, false));
self.unyank(());
} else {
Expand Down
7 changes: 6 additions & 1 deletion yazi-dds/src/body/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use anyhow::Result;
use mlua::{ExternalResult, IntoLua, Lua, Value};
use serde::Serialize;

use super::{BodyBulk, BodyCd, BodyCustom, BodyHey, BodyHi, BodyHover, BodyRename, BodyYank};
use super::{
BodyBulk, BodyCd, BodyCustom, BodyHey, BodyHi, BodyHover, BodyMove, BodyRename, BodyYank,
};
use crate::Payload;

#[derive(Debug, Serialize)]
Expand All @@ -13,6 +15,7 @@ pub enum Body<'a> {
Cd(BodyCd<'a>),
Hover(BodyHover<'a>),
Rename(BodyRename<'a>),
Move(BodyMove<'a>),
Bulk(BodyBulk<'a>),
Yank(BodyYank<'a>),
Custom(BodyCustom),
Expand Down Expand Up @@ -52,6 +55,7 @@ impl<'a> Body<'a> {
Self::Cd(_) => "cd",
Self::Hover(_) => "hover",
Self::Rename(_) => "rename",
Body::Move(_) => "move",
Self::Bulk(_) => "bulk",
Self::Yank(_) => "yank",
Self::Custom(b) => b.kind.as_str(),
Expand Down Expand Up @@ -92,6 +96,7 @@ impl IntoLua<'_> for Body<'static> {
Body::Cd(b) => b.into_lua(lua),
Body::Hover(b) => b.into_lua(lua),
Body::Rename(b) => b.into_lua(lua),
Body::Move(b) => b.into_lua(lua),
Body::Bulk(b) => b.into_lua(lua),
Body::Yank(b) => b.into_lua(lua),
Body::Custom(b) => b.into_lua(lua),
Expand Down
2 changes: 2 additions & 0 deletions yazi-dds/src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod custom;
mod hey;
mod hi;
mod hover;
mod move_event;
mod rename;
mod yank;

Expand All @@ -17,5 +18,6 @@ pub use custom::*;
pub use hey::*;
pub use hi::*;
pub use hover::*;
pub use move_event::*;
pub use rename::*;
pub use yank::*;
46 changes: 46 additions & 0 deletions yazi-dds/src/body/move_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::borrow::Cow;

use mlua::{IntoLua, Lua, Value};
use serde::{Deserialize, Serialize};
use yazi_shared::fs::Url;

use super::Body;

#[derive(Debug, Serialize, Deserialize)]
pub struct BodyMove<'a> {
pub tab: usize,
pub from: Cow<'a, Url>,
pub to: Cow<'a, Url>,
}

impl<'a> BodyMove<'a> {
#[inline]
pub fn borrowed(tab: usize, from: &'a Url, to: &'a Url) -> Body<'a> {
Self { tab, from: Cow::Borrowed(from), to: Cow::Borrowed(to) }.into()
}
}

impl BodyMove<'static> {
#[inline]
pub fn dummy(tab: usize, from: &Url, to: &Url) -> Body<'static> {
Self { tab, from: Cow::Owned(from.clone()), to: Cow::Owned(to.clone()) }.into()
}
}

impl<'a> From<BodyMove<'a>> for Body<'a> {
fn from(value: BodyMove<'a>) -> Self {
Self::Move(value)
}
}

impl IntoLua<'_> for BodyMove<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
lua
.create_table_from([
("tab", self.tab.into_lua(lua)?),
("from", lua.create_any_userdata(self.from.into_owned())?.into_lua(lua)?),
("to", lua.create_any_userdata(self.to.into_owned())?.into_lua(lua)?),
])?
.into_lua(lua)
}
}
17 changes: 12 additions & 5 deletions yazi-dds/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ use crate::{body::Body, ID};
#[derive(Debug)]
pub struct Payload<'a> {
pub receiver: u64,
pub sender: u64,
pub body: Body<'a>,
pub sender: u64,
pub body: Body<'a>,
}

impl<'a> Payload<'a> {
pub(super) fn new(body: Body<'a>) -> Self { Self { receiver: 0, sender: *ID, body } }
pub(super) fn new(body: Body<'a>) -> Self {
Self { receiver: 0, sender: *ID, body }
}

pub(super) fn flush(&self) { writeln!(std::io::stdout(), "{self}").ok(); }
pub(super) fn flush(&self) {
writeln!(std::io::stdout(), "{self}").ok();
}

pub(super) fn try_flush(&self) {
let b = if self.receiver == 0 {
Expand Down Expand Up @@ -71,7 +75,9 @@ impl FromStr for Payload<'_> {
}

impl<'a> From<Body<'a>> for Payload<'a> {
fn from(value: Body<'a>) -> Self { Self::new(value) }
fn from(value: Body<'a>) -> Self {
Self::new(value)
}
}

impl Display for Payload<'_> {
Expand All @@ -82,6 +88,7 @@ impl Display for Payload<'_> {
Body::Cd(b) => serde_json::to_string(b),
Body::Hover(b) => serde_json::to_string(b),
Body::Rename(b) => serde_json::to_string(b),
Body::Move(b) => serde_json::to_string(b),
Body::Bulk(b) => serde_json::to_string(b),
Body::Yank(b) => serde_json::to_string(b),
Body::Custom(b) => serde_json::to_string(b),
Expand Down
25 changes: 22 additions & 3 deletions yazi-dds/src/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use parking_lot::RwLock;
use yazi_boot::BOOT;
use yazi_shared::{fs::Url, RoCell};

use crate::{body::{Body, BodyCd, BodyHi, BodyHover, BodyRename, BodyYank}, Client, ID, PEERS};
use crate::{
body::{Body, BodyCd, BodyHi, BodyHover, BodyMove, BodyRename, BodyYank},
Client, ID, PEERS,
};

pub static LOCAL: RoCell<RwLock<HashMap<String, HashMap<String, Function<'static>>>>> =
RoCell::new();
Expand Down Expand Up @@ -59,13 +62,17 @@ impl Pubsub {
sub!(REMOTE)(plugin, kind, f) && Self::pub_from_hi()
}

pub fn unsub(plugin: &str, kind: &str) -> bool { unsub!(LOCAL)(plugin, kind) }
pub fn unsub(plugin: &str, kind: &str) -> bool {
unsub!(LOCAL)(plugin, kind)
}

pub fn unsub_remote(plugin: &str, kind: &str) -> bool {
unsub!(REMOTE)(plugin, kind) && Self::pub_from_hi()
}

pub fn pub_(body: Body<'static>) { body.with_receiver(*ID).emit(); }
pub fn pub_(body: Body<'static>) {
body.with_receiver(*ID).emit();
}

pub fn pub_to(receiver: u64, body: Body<'static>) {
if receiver == *ID {
Expand Down Expand Up @@ -142,4 +149,16 @@ impl Pubsub {
BodyYank::borrowed(cut, urls).with_receiver(*ID).flush();
}
}

pub fn pub_from_move(tab: usize, from: &Url, to: &Url) {
if LOCAL.read().contains_key("move") {
Self::pub_(BodyMove::dummy(tab, from, to));
}
if PEERS.read().values().any(|p| p.able("move")) {
Client::push(BodyMove::borrowed(tab, from, to));
}
if BOOT.local_events.contains("move") {
BodyMove::borrowed(tab, from, to).with_receiver(*ID).flush();
}
}
}

0 comments on commit edb2840

Please sign in to comment.