use std::{process::Stdio, io::Write};
use serde::{Deserialize, Serialize};
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
pub struct ClipBoard {
pub content: Vec<String>,
}
impl ClipBoard {
pub fn new() -> ClipBoard {
ClipBoard {
content: Vec::new(),
}
}
pub fn add(&mut self, content: String) {
self.content.push(content);
}
pub fn remove(&mut self, item: &str) {
let index = self.content.iter().position(|x| x == item);
if let Some(i) = index {
self.content.remove(i);
}
}
}
pub enum CopyCommand {
XClip,
Wayland,
PBCopy,
}
pub fn copy_to_system_clipboard(content: &str, command: CopyCommand) {
match command {
CopyCommand::XClip => {
let mut command = std::process::Command::new("xclip")
.arg("-sel")
.arg("c")
.stdin(Stdio::piped())
.spawn()
.expect("failed to execute process");
let mut child_stdin = command.stdin.take().unwrap();
child_stdin.write_all(content.as_bytes()).expect("failed to write to stdin of xclip");
},
_ => {
unimplemented!("Only xclip is supported at the moment!")
}
}
}