From c606e7f08cc3768ca853ee2b265eb069fd6007d5 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Tue, 30 Apr 2024 18:03:48 +0300 Subject: [PATCH] feat(plugin): add ya.clipboard() lua API It allows accessing the clipboard content as well as setting it. ```lua -- get contents of the clipboard local content = ya.clipboard() -- set contents of the clipboard ya.clipboard("new clipboard content") ``` --- yazi-plugin/src/utils/text.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/yazi-plugin/src/utils/text.rs b/yazi-plugin/src/utils/text.rs index 92044180..0c1c4cdf 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,21 @@ impl Utils { })?, )?; + ya.raw_set( + "clipboard", + lua.create_async_function(|lua, text: mlua::String| async move { + let text = text.to_string_lossy().into_owned(); + + if text.is_empty() { + let clipboard_data = CLIPBOARD.get().await; + Some(lua.create_string(clipboard_data.as_encoded_bytes())).transpose() + } else { + CLIPBOARD.set(text).await; + Ok(None) + } + })?, + )?; + Ok(()) }