2022-02-28 22:20:50 +00:00
|
|
|
#[cfg(all(
|
|
|
|
unix,
|
|
|
|
not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
|
|
|
|
))]
|
|
|
|
fn is_program_in_path(program: &str) -> bool {
|
|
|
|
use std::{env, fs};
|
|
|
|
|
|
|
|
if let Ok(path) = env::var("PATH") {
|
2022-02-28 22:29:16 +00:00
|
|
|
for p in path.split(':') {
|
2022-02-28 22:20:50 +00:00
|
|
|
let p_str = format!("{}/{}", p, program);
|
|
|
|
if fs::metadata(p_str).is_ok() {
|
2022-02-28 22:29:16 +00:00
|
|
|
return true;
|
2022-02-28 22:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2022-02-25 17:24:24 +00:00
|
|
|
#[cfg(all(
|
|
|
|
unix,
|
|
|
|
not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
|
|
|
|
))]
|
|
|
|
pub fn set_clipboard(content: String) {
|
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")
|
|
|
|
// 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")
|
|
|
|
// 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")
|
|
|
|
} else {
|
2022-02-28 22:29:16 +00:00
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
"WARN".yellow(),
|
|
|
|
"command for clipboard not found".magenta()
|
|
|
|
);
|
|
|
|
return;
|
2022-02-28 22:20:50 +00:00
|
|
|
}
|
2022-02-26 21:51:12 +00:00
|
|
|
|
2022-02-26 21:51:41 +00:00
|
|
|
child
|
|
|
|
.stdin
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.write_all(content.as_bytes())
|
|
|
|
.expect("execute command");
|
2022-02-26 21:51:12 +00:00
|
|
|
child.wait_with_output().unwrap();
|
2022-02-25 17:24:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(all(
|
|
|
|
unix,
|
|
|
|
not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
|
|
|
|
)))]
|
|
|
|
use arboard::Clipboard;
|
|
|
|
|
|
|
|
#[cfg(not(all(
|
|
|
|
unix,
|
|
|
|
not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
|
|
|
|
)))]
|
|
|
|
pub fn set_clipboard(content: String) {
|
|
|
|
let mut clipboard = Clipboard::new().unwrap();
|
|
|
|
clipboard.set_text(content).unwrap();
|
|
|
|
}
|