mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
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:
parent
63f78f82fb
commit
c606e7f08c
1 changed files with 16 additions and 0 deletions
|
|
@ -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,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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue