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")
```
This commit is contained in:
Mika Vilpas 2024-04-30 18:03:48 +03:00
parent 63f78f82fb
commit c606e7f08c

View file

@ -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(())
}