use std::io::Write;
pub fn copy(text: &str) {
if let Ok(path) = std::env::var("ROOTLE_CLIPBOARD") {
let _ = std::fs::write(path, text);
return;
}
osc52(text);
local_tool(text);
}
fn osc52(text: &str) {
use base64::Engine;
let encoded = base64::engine::general_purpose::STANDARD.encode(text);
let mut out = std::io::stdout();
let _ = write!(out, "\x1b]52;c;{encoded}\x07");
let _ = out.flush();
}
fn local_tool(text: &str) {
use std::process::{Command, Stdio};
for (program, flag) in [
("wl-copy", None),
("xclip", Some("-selection")),
("xsel", Some("--clipboard")),
("pbcopy", None),
] {
let mut cmd = Command::new(program);
if let Some(flag) = flag {
cmd.arg(flag);
if program == "xclip" {
cmd.arg("clipboard");
}
}
if cmd
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.and_then(|mut child| {
let _ = child
.stdin
.as_mut()
.expect("piped")
.write_all(text.as_bytes());
child.wait()
})
.is_ok()
{
return; }
}
}