use anyhow::{Context, Result};
use arboard::Clipboard;
use std::io::Write;
use std::process::{Command, Stdio};
fn is_flatpak() -> bool {
std::path::Path::new("/.flatpak-info").exists()
}
fn copy_via_wl_copy(text: &str) -> Result<()> {
let mut child = Command::new("wl-copy")
.stdin(Stdio::piped())
.spawn()
.context("Failed to spawn wl-copy")?;
if let Some(mut stdin) = child.stdin.take() {
stdin
.write_all(text.as_bytes())
.context("Failed to write to wl-copy")?;
}
let status = child.wait().context("Failed to wait for wl-copy")?;
if !status.success() {
anyhow::bail!("wl-copy exited with non-zero status");
}
Ok(())
}
pub fn copy_to_clipboard(text: &str) -> Result<()> {
if is_flatpak() {
return copy_via_wl_copy(text);
}
let mut clipboard = Clipboard::new().context("Failed to access clipboard")?;
clipboard
.set_text(text)
.context("Failed to copy text to clipboard")?;
Ok(())
}