2022-02-28 22:20:50 +00:00
|
|
|
#[cfg(all(
|
|
|
|
unix,
|
|
|
|
not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
|
|
|
|
))]
|
2022-04-03 19:01:58 +00:00
|
|
|
// use xclip (or a similar program that is installed) because the kernel deletes the clipboard after the process ends
|
2022-05-18 18:52:47 +00:00
|
|
|
pub fn set_clipboard(content: &str) {
|
2022-04-03 19:01:58 +00:00
|
|
|
fn is_program_in_path(program: &str) -> bool {
|
|
|
|
if let Ok(path) = std::env::var("PATH") {
|
|
|
|
for p in path.split(':') {
|
|
|
|
let p_str = format!("{}/{}", p, program);
|
|
|
|
if std::fs::metadata(p_str).is_ok() {
|
|
|
|
return true;
|
|
|
|
}
|
2022-02-28 22:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-03 19:01:58 +00:00
|
|
|
false
|
2022-02-28 22:20:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 21:51:41 +00:00
|
|
|
use std::{
|
|
|
|
io::Write,
|
|
|
|
process::{Command, Stdio},
|
|
|
|
};
|
2022-02-26 21:51:12 +00:00
|
|
|
|
2022-02-28 22:20:50 +00:00
|
|
|
use colored::Colorize;
|
|
|
|
|
|
|
|
let mut child;
|
|
|
|
|
|
|
|
// xsel
|
|
|
|
if is_program_in_path("xsel") {
|
|
|
|
child = Command::new("xsel")
|
|
|
|
.arg("--input")
|
|
|
|
.arg("--clipboard")
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("execute command xsel")
|
2022-04-03 19:01:58 +00:00
|
|
|
|
2022-02-28 22:20:50 +00:00
|
|
|
// xclip
|
|
|
|
} else if is_program_in_path("xclip") {
|
|
|
|
child = Command::new("xclip")
|
|
|
|
.arg("-in")
|
|
|
|
.arg("-selection")
|
|
|
|
.arg("clipboard")
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("execute command xclip")
|
2022-04-03 19:01:58 +00:00
|
|
|
|
2022-02-28 22:20:50 +00:00
|
|
|
// termux
|
|
|
|
} else if is_program_in_path("termux-clipboard-set") {
|
|
|
|
child = Command::new("termux-clipboard-set")
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("execute command termux-clipboard-set")
|
2022-04-03 19:01:58 +00:00
|
|
|
|
|
|
|
// the above programs responsible for the clipboard were not found
|
2022-02-28 22:20:50 +00:00
|
|
|
} else {
|
2022-02-28 22:29:16 +00:00
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
"WARN".yellow(),
|
|
|
|
"command for clipboard not found".magenta()
|
|
|
|
);
|
2022-04-03 19:01:58 +00:00
|
|
|
|
2022-04-03 19:11:11 +00:00
|
|
|
return;
|
2022-02-28 22:20:50 +00:00
|
|
|
}
|
2022-02-26 21:51:12 +00:00
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
// copy the content (send it to stdin command)
|
2022-02-26 21:51:41 +00:00
|
|
|
child
|
|
|
|
.stdin
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.write_all(content.as_bytes())
|
|
|
|
.expect("execute command");
|
2022-02-25 17:24:24 +00:00
|
|
|
|
2022-04-03 19:01:58 +00:00
|
|
|
child
|
|
|
|
.wait_with_output()
|
|
|
|
.expect("wait for clipboard command output");
|
|
|
|
}
|
2022-02-25 17:24:24 +00:00
|
|
|
|
|
|
|
#[cfg(not(all(
|
|
|
|
unix,
|
|
|
|
not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
|
|
|
|
)))]
|
2022-05-18 18:52:47 +00:00
|
|
|
pub fn set_clipboard(content: &str) {
|
2022-04-03 19:01:58 +00:00
|
|
|
let mut clipboard = arboard::Clipboard::new().unwrap();
|
2022-05-18 19:25:35 +00:00
|
|
|
clipboard.set_text(content.to_string()).unwrap();
|
2022-02-25 17:24:24 +00:00
|
|
|
}
|