use std::cell::RefCell;
thread_local! {
static CLIPBOARD: RefCell<Option<arboard::Clipboard>> = const { RefCell::new(None) };
}
fn with_clipboard<R, F: FnOnce(&mut arboard::Clipboard) -> R>(f: F) -> Option<R> {
CLIPBOARD.with(|cb| {
let mut cb = cb.borrow_mut();
if cb.is_none() {
*cb = arboard::Clipboard::new().ok();
}
cb.as_mut().map(f)
})
}
pub fn copy_text(text: &str) {
let _ = with_clipboard(|cb| cb.set_text(text.to_string()));
}
pub fn paste_text() -> Option<String> {
with_clipboard(|cb| cb.get_text().ok()).flatten()
}
pub fn cut_text(text: &str) {
copy_text(text);
}