1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use cli_clipboard::{ClipboardContext, ClipboardProvider};

use crate::create_body;

#[doc(hidden)]
pub fn init(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
    create_body!(lua,
        "set"=> lua.create_function(set)?,
        "get"=> lua.create_function(get)?,
        "clear" => lua.create_function(clear)?
    )
}

pub fn set(_: &mlua::Lua, value: String) -> mlua::Result<()> {
    #[cfg(not(target_os = "linux"))]
    {
        let mut ctx = ClipboardContext::new().map_err(|x| mlua::Error::RuntimeError(x.to_string()))?;
        ctx.set_contents(value)
            .map_err(|x| mlua::Error::RuntimeError(x.to_string()))?;
    }
    #[cfg(target_os = "linux")]
    {
        use clipboard_ext::{prelude::*, x11_fork::ClipboardContext};
        fn set_clipboard(value: String) -> Result<(), Box<dyn std::error::Error>> {
            let mut ctx = ClipboardContext::new()?;
            ctx.set_contents(value)?;
            Ok(())
        }
        if set_clipboard(value.clone()).is_err() {
            let mut ctx = ClipboardContext::new().map_err(|x| mlua::Error::RuntimeError(x.to_string()))?;
            ctx.set_contents(value)
                .map_err(|x| mlua::Error::RuntimeError(x.to_string()))?;
        }
    }
    Ok(())
}

pub fn get(_: &mlua::Lua, _: ()) -> mlua::Result<String> {
    let mut ctx = ClipboardContext::new().unwrap();
    ctx.get_contents().map_err(|x| mlua::Error::RuntimeError(x.to_string()))
}

pub fn clear(_: &mlua::Lua, _: ()) -> mlua::Result<()> {
    let mut ctx = ClipboardContext::new().map_err(|x| mlua::Error::RuntimeError(x.to_string()))?;
    ctx.clear().map_err(|x| mlua::Error::RuntimeError(x.to_string()))
}