Skip to content

Commit

Permalink
Add ya.clipboard() lua api (#980)
Browse files Browse the repository at this point in the history
Co-authored-by: sxyazi <[email protected]>
  • Loading branch information
mikavilpas and sxyazi authored May 1, 2024
1 parent bdb28f7 commit 51c7f03
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 13 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions yazi-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
3 changes: 2 additions & 1 deletion yazi-core/src/input/commands/paste.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/input/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions yazi-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
clippy::unit_arg
)]

mod clipboard;
pub mod completion;
pub mod folder;
pub mod help;
Expand All @@ -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);
}
3 changes: 2 additions & 1 deletion yazi-core/src/tab/commands/copy.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
5 changes: 5 additions & 0 deletions yazi-plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
Expand All @@ -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"
File renamed without changes.
4 changes: 4 additions & 0 deletions yazi-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub mod bindings;
mod cast;
mod clipboard;
mod config;
pub mod elements;
pub mod external;
Expand All @@ -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();
}
13 changes: 13 additions & 0 deletions yazi-plugin/src/utils/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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(())
}

Expand Down

0 comments on commit 51c7f03

Please sign in to comment.