use std::io::{self, Write as _};
use std::process::{Command, Stdio};
const MAX_CLIPBOARD_BYTES: usize = 1024 * 1024;
pub trait Clipboard {
fn copy(&mut self, text: &str) -> io::Result<()>;
}
#[derive(Debug, Default, Clone)]
pub struct MemoryClipboard {
contents: Option<String>,
}
impl MemoryClipboard {
#[must_use]
pub fn contents(&self) -> Option<&str> {
self.contents.as_deref()
}
}
impl Clipboard for MemoryClipboard {
fn copy(&mut self, text: &str) -> io::Result<()> {
validate(text)?;
self.contents = Some(text.to_owned());
Ok(())
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct SystemClipboard;
impl Clipboard for SystemClipboard {
fn copy(&mut self, text: &str) -> io::Result<()> {
validate(text)?;
#[cfg(target_os = "macos")]
{
pipe_to("pbcopy", &[], text)
}
#[cfg(target_os = "windows")]
{
pipe_to("clip", &[], text)
}
#[cfg(all(unix, not(target_os = "macos")))]
{
pipe_to("wl-copy", &[], text)
.or_else(|_| pipe_to("xclip", &["-selection", "clipboard"], text))
}
#[cfg(not(any(unix, target_os = "windows")))]
{
let _ = text;
Err(io::Error::new(
io::ErrorKind::Unsupported,
"system clipboard is unavailable",
))
}
}
}
fn validate(text: &str) -> io::Result<()> {
if text.len() > MAX_CLIPBOARD_BYTES || text.contains('\0') {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"clipboard text is invalid",
));
}
Ok(())
}
fn pipe_to(program: &str, arguments: &[&str], text: &str) -> io::Result<()> {
let mut child = Command::new(program)
.args(arguments)
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()?;
child
.stdin
.take()
.ok_or_else(|| io::Error::other("clipboard stdin is unavailable"))?
.write_all(text.as_bytes())?;
let status = child.wait()?;
if status.success() {
Ok(())
} else {
Err(io::Error::other("clipboard command failed"))
}
}