feat: add ya.clipboard() Lua API (#980)

Co-authored-by: sxyazi <sxyazi@gmail.com>
This commit is contained in:
Mika Vilpas 2024-05-01 15:43:01 +03:00 committed by sxyazi
parent bdb28f7691
commit 28972ff54d
No known key found for this signature in database
10 changed files with 30 additions and 13 deletions

5
Cargo.lock generated
View File

@ -2762,9 +2762,7 @@ name = "yazi-core"
version = "0.2.5" version = "0.2.5"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64 0.22.0",
"bitflags 2.5.0", "bitflags 2.5.0",
"clipboard-win",
"crossterm", "crossterm",
"futures", "futures",
"libc", "libc",
@ -2843,6 +2841,9 @@ version = "0.2.5"
dependencies = [ dependencies = [
"ansi-to-tui", "ansi-to-tui",
"anyhow", "anyhow",
"base64 0.22.0",
"clipboard-win",
"crossterm",
"futures", "futures",
"md-5", "md-5",
"mlua", "mlua",

View File

@ -20,7 +20,6 @@ yazi-shared = { path = "../yazi-shared", version = "0.2.5" }
# External dependencies # External dependencies
anyhow = "1.0.82" anyhow = "1.0.82"
base64 = "0.22.0"
bitflags = "2.5.0" bitflags = "2.5.0"
crossterm = "0.27.0" crossterm = "0.27.0"
futures = "0.3.30" futures = "0.3.30"
@ -40,6 +39,3 @@ tracing = { version = "0.1.40", features = [ "max_level_debug", "release_max_lev
[target."cfg(unix)".dependencies] [target."cfg(unix)".dependencies]
libc = "0.2.153" libc = "0.2.153"
[target."cfg(windows)".dependencies]
clipboard-win = "5.3.1"

View File

@ -1,6 +1,7 @@
use yazi_plugin::CLIPBOARD;
use yazi_shared::{event::Cmd, render}; use yazi_shared::{event::Cmd, render};
use crate::{input::{op::InputOp, Input}, CLIPBOARD}; use crate::input::{op::InputOp, Input};
pub struct Opt { pub struct Opt {
before: bool, before: bool,

View File

@ -3,10 +3,10 @@ use std::ops::Range;
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use yazi_config::{popup::Position, INPUT}; use yazi_config::{popup::Position, INPUT};
use yazi_plugin::CLIPBOARD;
use yazi_shared::{render, InputError}; use yazi_shared::{render, InputError};
use super::{mode::InputMode, op::InputOp, InputSnap, InputSnaps}; use super::{mode::InputMode, op::InputOp, InputSnap, InputSnaps};
use crate::CLIPBOARD;
#[derive(Default)] #[derive(Default)]
pub struct Input { pub struct Input {

View File

@ -6,7 +6,6 @@
clippy::unit_arg clippy::unit_arg
)] )]
mod clipboard;
pub mod completion; pub mod completion;
pub mod folder; pub mod folder;
pub mod help; pub mod help;
@ -19,12 +18,9 @@ pub mod tab;
pub mod tasks; pub mod tasks;
pub mod which; pub mod which;
pub use clipboard::*;
pub use step::*; pub use step::*;
pub fn init() { pub fn init() {
CLIPBOARD.with(Default::default);
manager::WATCHED.with(Default::default); manager::WATCHED.with(Default::default);
manager::LINKED.with(Default::default); manager::LINKED.with(Default::default);
} }

View File

@ -1,8 +1,9 @@
use std::ffi::{OsStr, OsString}; use std::ffi::{OsStr, OsString};
use yazi_plugin::CLIPBOARD;
use yazi_shared::event::Cmd; use yazi_shared::event::Cmd;
use crate::{tab::Tab, CLIPBOARD}; use crate::tab::Tab;
pub struct Opt { pub struct Opt {
type_: String, type_: String,

View File

@ -23,6 +23,8 @@ yazi-shared = { path = "../yazi-shared", version = "0.2.5" }
# External dependencies # External dependencies
ansi-to-tui = "3.1.0" ansi-to-tui = "3.1.0"
anyhow = "1.0.82" anyhow = "1.0.82"
base64 = "0.22.0"
crossterm = "0.27.0"
futures = "0.3.30" futures = "0.3.30"
md-5 = "0.10.6" md-5 = "0.10.6"
mlua = { version = "0.9.7", features = [ "lua54", "serialize", "macros", "async" ] } 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] [target."cfg(unix)".dependencies]
uzers = "0.11.3" uzers = "0.11.3"
[target."cfg(windows)".dependencies]
clipboard-win = "5.3.1"

View File

@ -2,6 +2,7 @@
pub mod bindings; pub mod bindings;
mod cast; mod cast;
mod clipboard;
mod config; mod config;
pub mod elements; pub mod elements;
pub mod external; pub mod external;
@ -17,12 +18,15 @@ pub mod url;
pub mod utils; pub mod utils;
pub use cast::*; pub use cast::*;
pub use clipboard::*;
pub use config::*; pub use config::*;
pub use lua::*; pub use lua::*;
pub use opt::*; pub use opt::*;
pub use runtime::*; pub use runtime::*;
pub fn init() { pub fn init() {
CLIPBOARD.with(Default::default);
crate::loader::init(); crate::loader::init();
crate::init_lua(); crate::init_lua();
} }

View File

@ -4,6 +4,7 @@ use mlua::{Lua, Table};
use unicode_width::UnicodeWidthChar; use unicode_width::UnicodeWidthChar;
use super::Utils; use super::Utils;
use crate::CLIPBOARD;
impl Utils { impl Utils {
pub(super) fn text(lua: &Lua, ya: &Table) -> mlua::Result<()> { 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<String>| 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(()) Ok(())
} }