use std::io::Write;
use std::process::{Command, Stdio};
pub fn copy_to_clipboard(text: &str) -> std::io::Result<bool> {
const CANDIDATES: &[(&str, &[&str])] = &[
("wl-copy", &[]),
("xclip", &["-selection", "clipboard"]),
("xsel", &["--clipboard", "--input"]),
];
for (program, args) in CANDIDATES {
match try_copy(program, args, text) {
Ok(true) => return Ok(true),
_ => continue,
}
}
Ok(false)
}
fn try_copy(program: &str, args: &[&str], text: &str) -> std::io::Result<bool> {
let mut child = Command::new(program)
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()?;
if let Some(mut stdin) = child.stdin.take() {
stdin.write_all(text.as_bytes())?;
}
let status = child.wait()?;
Ok(status.success())
}