From 28972ff54db289fc0b8e561db36db26d8d7db633 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Wed, 1 May 2024 15:43:01 +0300 Subject: [PATCH] feat: add `ya.clipboard()` Lua API (#980) Co-authored-by: sxyazi --- Cargo.lock | 5 +++-- yazi-core/Cargo.toml | 4 ---- yazi-core/src/input/commands/paste.rs | 3 ++- yazi-core/src/input/input.rs | 2 +- yazi-core/src/lib.rs | 4 ---- yazi-core/src/tab/commands/copy.rs | 3 ++- yazi-plugin/Cargo.toml | 5 +++++ {yazi-core => yazi-plugin}/src/clipboard.rs | 0 yazi-plugin/src/lib.rs | 4 ++++ yazi-plugin/src/utils/text.rs | 13 +++++++++++++ 10 files changed, 30 insertions(+), 13 deletions(-) rename {yazi-core => yazi-plugin}/src/clipboard.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index f06f724e..fe2d1f46 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2762,9 +2762,7 @@ name = "yazi-core" version = "0.2.5" dependencies = [ "anyhow", - "base64 0.22.0", "bitflags 2.5.0", - "clipboard-win", "crossterm", "futures", "libc", @@ -2843,6 +2841,9 @@ version = "0.2.5" dependencies = [ "ansi-to-tui", "anyhow", + "base64 0.22.0", + "clipboard-win", + "crossterm", "futures", "md-5", "mlua", diff --git a/yazi-core/Cargo.toml b/yazi-core/Cargo.toml index d7dc8b00..edddfec7 100644 --- a/yazi-core/Cargo.toml +++ b/yazi-core/Cargo.toml @@ -20,7 +20,6 @@ yazi-shared = { path = "../yazi-shared", version = "0.2.5" } # External dependencies anyhow = "1.0.82" -base64 = "0.22.0" bitflags = "2.5.0" crossterm = "0.27.0" futures = "0.3.30" @@ -40,6 +39,3 @@ tracing = { version = "0.1.40", features = [ "max_level_debug", "release_max_lev [target."cfg(unix)".dependencies] libc = "0.2.153" - -[target."cfg(windows)".dependencies] -clipboard-win = "5.3.1" diff --git a/yazi-core/src/input/commands/paste.rs b/yazi-core/src/input/commands/paste.rs index 4cd88758..2c313ea4 100644 --- a/yazi-core/src/input/commands/paste.rs +++ b/yazi-core/src/input/commands/paste.rs @@ -1,6 +1,7 @@ +use yazi_plugin::CLIPBOARD; use yazi_shared::{event::Cmd, render}; -use crate::{input::{op::InputOp, Input}, CLIPBOARD}; +use crate::input::{op::InputOp, Input}; pub struct Opt { before: bool, diff --git a/yazi-core/src/input/input.rs b/yazi-core/src/input/input.rs index 99e9d27d..243f24dc 100644 --- a/yazi-core/src/input/input.rs +++ b/yazi-core/src/input/input.rs @@ -3,10 +3,10 @@ use std::ops::Range; use tokio::sync::mpsc::UnboundedSender; use unicode_width::UnicodeWidthStr; use yazi_config::{popup::Position, INPUT}; +use yazi_plugin::CLIPBOARD; use yazi_shared::{render, InputError}; use super::{mode::InputMode, op::InputOp, InputSnap, InputSnaps}; -use crate::CLIPBOARD; #[derive(Default)] pub struct Input { diff --git a/yazi-core/src/lib.rs b/yazi-core/src/lib.rs index 43ab4f08..9850ca81 100644 --- a/yazi-core/src/lib.rs +++ b/yazi-core/src/lib.rs @@ -6,7 +6,6 @@ clippy::unit_arg )] -mod clipboard; pub mod completion; pub mod folder; pub mod help; @@ -19,12 +18,9 @@ pub mod tab; pub mod tasks; pub mod which; -pub use clipboard::*; pub use step::*; pub fn init() { - CLIPBOARD.with(Default::default); - manager::WATCHED.with(Default::default); manager::LINKED.with(Default::default); } diff --git a/yazi-core/src/tab/commands/copy.rs b/yazi-core/src/tab/commands/copy.rs index 22344720..6c8e0bb4 100644 --- a/yazi-core/src/tab/commands/copy.rs +++ b/yazi-core/src/tab/commands/copy.rs @@ -1,8 +1,9 @@ use std::ffi::{OsStr, OsString}; +use yazi_plugin::CLIPBOARD; use yazi_shared::event::Cmd; -use crate::{tab::Tab, CLIPBOARD}; +use crate::tab::Tab; pub struct Opt { type_: String, diff --git a/yazi-plugin/Cargo.toml b/yazi-plugin/Cargo.toml index 55033878..4c89ac79 100644 --- a/yazi-plugin/Cargo.toml +++ b/yazi-plugin/Cargo.toml @@ -23,6 +23,8 @@ yazi-shared = { path = "../yazi-shared", version = "0.2.5" } # External dependencies ansi-to-tui = "3.1.0" anyhow = "1.0.82" +base64 = "0.22.0" +crossterm = "0.27.0" futures = "0.3.30" md-5 = "0.10.6" mlua = { version = "0.9.7", features = [ "lua54", "serialize", "macros", "async" ] } @@ -43,3 +45,6 @@ tracing = { version = "0.1.40", features = [ "max_level_debug", "release_max_lev [target."cfg(unix)".dependencies] uzers = "0.11.3" + +[target."cfg(windows)".dependencies] +clipboard-win = "5.3.1" diff --git a/yazi-core/src/clipboard.rs b/yazi-plugin/src/clipboard.rs similarity index 100% rename from yazi-core/src/clipboard.rs rename to yazi-plugin/src/clipboard.rs diff --git a/yazi-plugin/src/lib.rs b/yazi-plugin/src/lib.rs index 069f6649..cc74d28c 100644 --- a/yazi-plugin/src/lib.rs +++ b/yazi-plugin/src/lib.rs @@ -2,6 +2,7 @@ pub mod bindings; mod cast; +mod clipboard; mod config; pub mod elements; pub mod external; @@ -17,12 +18,15 @@ pub mod url; pub mod utils; pub use cast::*; +pub use clipboard::*; pub use config::*; pub use lua::*; pub use opt::*; pub use runtime::*; pub fn init() { + CLIPBOARD.with(Default::default); + crate::loader::init(); crate::init_lua(); } diff --git a/yazi-plugin/src/utils/text.rs b/yazi-plugin/src/utils/text.rs index 92044180..a1045261 100644 --- a/yazi-plugin/src/utils/text.rs +++ b/yazi-plugin/src/utils/text.rs @@ -4,6 +4,7 @@ use mlua::{Lua, Table}; use unicode_width::UnicodeWidthChar; use super::Utils; +use crate::CLIPBOARD; impl Utils { pub(super) fn text(lua: &Lua, ya: &Table) -> mlua::Result<()> { @@ -31,6 +32,18 @@ impl Utils { })?, )?; + ya.raw_set( + "clipboard", + lua.create_async_function(|lua, text: Option| async move { + if let Some(text) = text { + CLIPBOARD.set(text).await; + Ok(None) + } else { + Some(lua.create_string(CLIPBOARD.get().await.as_encoded_bytes())).transpose() + } + })?, + )?; + Ok(()) }