pub fn read_string() -> Option<String> {
#[cfg(test)]
{
if !test_clip::real_clipboard_opt_in() {
return test_clip::mock_read();
}
}
#[cfg(target_os = "macos")]
{
if let Some(s) = macos::read_string_native() {
return Some(s);
}
read_pbpaste()
}
#[cfg(target_os = "linux")]
{
linux::read_string()
}
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
{
None
}
}
#[cfg(target_os = "macos")]
fn read_pbpaste() -> Option<String> {
let out = std::process::Command::new("pbpaste").output().ok()?;
if !out.status.success() {
return None;
}
Some(String::from_utf8_lossy(&out.stdout).to_string())
}
pub fn write_string(text: &str) -> bool {
if text.as_bytes().contains(&0) {
return false;
}
#[cfg(test)]
{
if !test_clip::real_clipboard_opt_in() {
return test_clip::mock_write(text);
}
}
#[cfg(target_os = "macos")]
{
if macos::write_string_native(text) {
return true;
}
write_pbcopy(text)
}
#[cfg(target_os = "linux")]
{
linux::write_string(text)
}
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
{
false
}
}
#[cfg(target_os = "linux")]
mod linux {
use std::io::Write;
use std::process::{Command, Stdio};
pub fn write_string(text: &str) -> bool {
write_via(&["wl-copy"], text)
|| write_via(&["xclip", "-selection", "clipboard"], text)
|| write_via(&["xsel", "--clipboard", "--input"], text)
}
fn write_via(argv: &[&str], text: &str) -> bool {
let Ok(mut child) = Command::new(argv[0])
.args(&argv[1..])
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
else {
return false;
};
let Some(mut stdin) = child.stdin.take() else {
let _ = child.wait();
return false;
};
if stdin.write_all(text.as_bytes()).is_err() {
let _ = child.wait();
return false;
}
drop(stdin);
child.wait().map(|s| s.success()).unwrap_or(false)
}
pub fn read_string() -> Option<String> {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
read_via(&["wl-paste", "--no-newline", "--type", "text"], deadline)
.or_else(|| read_via(&["xclip", "-selection", "clipboard", "-o"], deadline))
.or_else(|| read_via(&["xsel", "--clipboard", "--output"], deadline))
}
fn read_via(argv: &[&str], deadline: std::time::Instant) -> Option<String> {
use std::io::Read;
if std::time::Instant::now() >= deadline {
return None;
}
let mut child = Command::new(argv[0])
.args(&argv[1..])
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.ok()?;
let mut stdout = child.stdout.take()?;
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let mut buf = Vec::new();
let _ = stdout.read_to_end(&mut buf);
let _ = tx.send(buf);
});
let give_up = |child: &mut std::process::Child| {
let _ = child.kill();
let _ = child.wait();
};
let left = deadline.saturating_duration_since(std::time::Instant::now());
let Ok(bytes) = rx.recv_timeout(left) else {
give_up(&mut child);
return None;
};
let status = loop {
match child.try_wait() {
Ok(Some(status)) => break status,
Ok(None) if std::time::Instant::now() < deadline => {
std::thread::sleep(std::time::Duration::from_millis(5));
}
_ => {
give_up(&mut child);
return None;
}
}
};
if !status.success() {
return None;
}
Some(String::from_utf8_lossy(&bytes).to_string())
}
#[cfg(test)]
mod tests {
#[test]
fn a_clipboard_reader_that_never_answers_is_given_up_on() {
let deadline = || std::time::Instant::now() + std::time::Duration::from_secs(1);
let started = std::time::Instant::now();
assert_eq!(super::read_via(&["sh", "-c", "sleep 30"], deadline()), None);
assert_eq!(
super::read_via(&["sh", "-c", "exec 1>&-; sleep 30"], deadline()),
None
);
assert!(started.elapsed() < std::time::Duration::from_secs(10));
let past = std::time::Instant::now();
assert_eq!(super::read_via(&["sh", "-c", "printf foo"], past), None);
assert_eq!(
super::read_via(&["sh", "-c", "printf foo"], deadline()).as_deref(),
Some("foo")
);
}
}
}
#[cfg(target_os = "macos")]
fn write_pbcopy(text: &str) -> bool {
use std::io::Write;
use std::process::{Command, Stdio};
let Ok(mut child) = Command::new("pbcopy")
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
else {
return false;
};
let Some(mut stdin) = child.stdin.take() else {
let _ = child.wait();
return false;
};
if stdin.write_all(text.as_bytes()).is_err() {
let _ = child.wait();
return false;
}
drop(stdin);
child.wait().map(|st| st.success()).unwrap_or(false)
}
#[cfg(target_os = "macos")]
pub(crate) mod macos {
use std::ffi::CStr;
use std::os::raw::{c_char, c_void};
type ObjcId = *mut c_void;
type Sel = *mut c_void;
#[link(name = "objc")]
unsafe extern "C" {
fn objc_getClass(name: *const c_char) -> ObjcId;
fn sel_registerName(name: *const c_char) -> Sel;
fn objc_autoreleasePoolPush() -> *mut c_void;
fn objc_autoreleasePoolPop(pool: *mut c_void);
fn objc_msgSend();
}
#[link(name = "AppKit", kind = "framework")]
unsafe extern "C" {}
pub fn read_string_native() -> Option<String> {
unsafe {
let pool = objc_autoreleasePoolPush();
let result = read_string_native_inner();
objc_autoreleasePoolPop(pool);
result
}
}
unsafe fn read_string_native_inner() -> Option<String> {
unsafe {
let pasteboard_class = class(b"NSPasteboard\0")?;
let pasteboard = msg_send_id(pasteboard_class, sel(b"generalPasteboard\0")?);
if pasteboard.is_null() {
return None;
}
for type_name in [
b"public.utf8-plain-text\0".as_slice(),
b"public.plain-text\0".as_slice(),
b"public.text\0".as_slice(),
b"NSStringPboardType\0".as_slice(),
] {
if let Some(value) = pasteboard_string_for_type(pasteboard, type_name) {
return Some(value);
}
}
None
}
}
unsafe fn pasteboard_string_for_type(
pasteboard: ObjcId,
type_name: &'static [u8],
) -> Option<String> {
unsafe {
let string_class = class(b"NSString\0")?;
let pasteboard_type = msg_send_id_ptr(
string_class,
sel(b"stringWithUTF8String:\0")?,
type_name.as_ptr().cast(),
);
if pasteboard_type.is_null() {
return None;
}
let value = msg_send_id_id(pasteboard, sel(b"stringForType:\0")?, pasteboard_type);
if value.is_null() {
return None;
}
let utf8 = msg_send_ptr(value, sel(b"UTF8String\0")?);
if utf8.is_null() {
return None;
}
Some(CStr::from_ptr(utf8).to_string_lossy().into_owned())
}
}
unsafe fn class(name: &'static [u8]) -> Option<ObjcId> {
let c = unsafe { objc_getClass(name.as_ptr().cast()) };
(!c.is_null()).then_some(c)
}
unsafe fn sel(name: &'static [u8]) -> Option<Sel> {
let s = unsafe { sel_registerName(name.as_ptr().cast()) };
(!s.is_null()).then_some(s)
}
unsafe fn msg_send_id(receiver: ObjcId, selector: Sel) -> ObjcId {
unsafe {
let f: unsafe extern "C" fn(ObjcId, Sel) -> ObjcId =
std::mem::transmute(objc_msgSend as *const ());
f(receiver, selector)
}
}
unsafe fn msg_send_id_ptr(receiver: ObjcId, selector: Sel, arg: *const c_char) -> ObjcId {
unsafe {
let f: unsafe extern "C" fn(ObjcId, Sel, *const c_char) -> ObjcId =
std::mem::transmute(objc_msgSend as *const ());
f(receiver, selector, arg)
}
}
unsafe fn msg_send_id_id(receiver: ObjcId, selector: Sel, arg: ObjcId) -> ObjcId {
unsafe {
let f: unsafe extern "C" fn(ObjcId, Sel, ObjcId) -> ObjcId =
std::mem::transmute(objc_msgSend as *const ());
f(receiver, selector, arg)
}
}
unsafe fn msg_send_ptr(receiver: ObjcId, selector: Sel) -> *const c_char {
unsafe {
let f: unsafe extern "C" fn(ObjcId, Sel) -> *const c_char =
std::mem::transmute(objc_msgSend as *const ());
f(receiver, selector)
}
}
pub fn write_string_native(text: &str) -> bool {
unsafe {
let pool = objc_autoreleasePoolPush();
let ok = write_string_inner(text);
objc_autoreleasePoolPop(pool);
ok
}
}
#[cfg(test)]
pub fn clear_native() {
unsafe {
let pool = objc_autoreleasePoolPush();
clear_inner();
objc_autoreleasePoolPop(pool);
}
}
#[cfg(test)]
unsafe fn clear_inner() {
unsafe {
let Some(pb_class) = class(b"NSPasteboard\0") else {
return;
};
let Some(general_sel) = sel(b"generalPasteboard\0") else {
return;
};
let pasteboard = msg_send_id(pb_class, general_sel);
if pasteboard.is_null() {
return;
}
let Some(clear_sel) = sel(b"clearContents\0") else {
return;
};
let _ = msg_send_isize(pasteboard, clear_sel);
}
}
unsafe fn write_string_inner(text: &str) -> bool {
unsafe {
let Some(pb_class) = class(b"NSPasteboard\0") else {
return false;
};
let Some(general_sel) = sel(b"generalPasteboard\0") else {
return false;
};
let pasteboard = msg_send_id(pb_class, general_sel);
if pasteboard.is_null() {
return false;
}
let Some(clear_sel) = sel(b"clearContents\0") else {
return false;
};
let _ = msg_send_isize(pasteboard, clear_sel);
let Some(nsstring_class) = class(b"NSString\0") else {
return false;
};
let Some(utf8_sel) = sel(b"stringWithUTF8String:\0") else {
return false;
};
let Ok(payload) = std::ffi::CString::new(text) else {
return false;
};
let value = msg_send_id_ptr(nsstring_class, utf8_sel, payload.as_ptr());
if value.is_null() {
return false;
}
let Ok(type_cstr) = std::ffi::CString::new("public.utf8-plain-text") else {
return false;
};
let type_nsstr = msg_send_id_ptr(nsstring_class, utf8_sel, type_cstr.as_ptr());
if type_nsstr.is_null() {
return false;
}
let Some(set_string_sel) = sel(b"setString:forType:\0") else {
return false;
};
msg_send_bool_id_id(pasteboard, set_string_sel, value, type_nsstr)
}
}
unsafe fn msg_send_isize(receiver: ObjcId, selector: Sel) -> isize {
unsafe {
let f: unsafe extern "C" fn(ObjcId, Sel) -> isize =
std::mem::transmute(objc_msgSend as *const ());
f(receiver, selector)
}
}
unsafe fn msg_send_bool_id_id(
receiver: ObjcId,
selector: Sel,
arg1: ObjcId,
arg2: ObjcId,
) -> bool {
unsafe {
let f: unsafe extern "C" fn(ObjcId, Sel, ObjcId, ObjcId) -> u8 =
std::mem::transmute(objc_msgSend as *const ());
f(receiver, selector, arg1, arg2) != 0
}
}
}
#[cfg(test)]
pub(crate) mod test_clip {
use std::cell::{Cell, RefCell};
use std::sync::{Mutex, MutexGuard};
pub(crate) static CLIP_LOCK: Mutex<()> = Mutex::new(());
thread_local! {
static REAL_OPT_IN: Cell<bool> = const { Cell::new(false) };
static MOCK: RefCell<Option<String>> = const { RefCell::new(None) };
}
pub(crate) fn real_clipboard_opt_in() -> bool {
REAL_OPT_IN.with(|c| c.get())
}
pub(crate) fn mock_write(text: &str) -> bool {
MOCK.with(|b| *b.borrow_mut() = Some(text.to_string()));
true
}
pub(crate) fn mock_read() -> Option<String> {
MOCK.with(|b| b.borrow().clone())
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn native_read() -> Option<String> {
#[cfg(target_os = "macos")]
{
super::macos::read_string_native()
}
#[cfg(target_os = "linux")]
{
super::linux::read_string()
}
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn native_restore(prev: Option<String>) {
match prev {
Some(text) => {
#[cfg(target_os = "macos")]
{
let _ = super::macos::write_string_native(&text);
}
#[cfg(target_os = "linux")]
{
let _ = super::linux::write_string(&text);
}
}
None => {
#[cfg(target_os = "macos")]
{
super::macos::clear_native();
}
#[cfg(target_os = "linux")]
{
let _ = super::linux::write_string("");
}
}
}
}
pub(crate) struct ClipboardTestGuard {
_mutex: MutexGuard<'static, ()>,
_file_lock: std::fs::File,
#[cfg(any(target_os = "macos", target_os = "linux"))]
snapshot: Option<String>,
}
impl ClipboardTestGuard {
pub(crate) fn acquire() -> Self {
let mutex = CLIP_LOCK
.lock()
.unwrap_or_else(|poison| poison.into_inner());
let lock_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("target");
std::fs::create_dir_all(&lock_dir).expect("clipboard test lock dir must exist");
let file_lock = std::fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(lock_dir.join("croft-clipboard-test.lock"))
.expect("clipboard test lock file must open");
loop {
match file_lock.lock() {
Ok(()) => break,
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {}
Err(e) => panic!("clipboard test file lock failed: {e}"),
}
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
let snapshot = native_read();
REAL_OPT_IN.with(|c| c.set(true));
Self {
_mutex: mutex,
_file_lock: file_lock,
#[cfg(any(target_os = "macos", target_os = "linux"))]
snapshot,
}
}
}
impl Drop for ClipboardTestGuard {
fn drop(&mut self) {
#[cfg(any(target_os = "macos", target_os = "linux"))]
native_restore(self.snapshot.take());
REAL_OPT_IN.with(|c| c.set(false));
}
}
}
#[cfg(test)]
pub(crate) fn lock_clipboard_for_test() -> test_clip::ClipboardTestGuard {
test_clip::ClipboardTestGuard::acquire()
}
#[cfg(all(test, target_os = "macos"))]
mod tests {
use super::*;
fn lock_clipboard() -> test_clip::ClipboardTestGuard {
lock_clipboard_for_test()
}
fn unique_sentinel(label: &str) -> String {
format!(
"croft-clip-{label}-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0)
)
}
#[test]
fn write_string_lands_text_on_nspasteboard_for_pbpaste_to_read() {
let _g = lock_clipboard();
let sentinel = unique_sentinel("plain");
assert!(
write_string(&sentinel),
"write_string must succeed on macOS via NSPasteboard"
);
let got = read_string().expect("read_string must surface clipboard text");
assert_eq!(
got, sentinel,
"what we just wrote with NSPasteboard.setString:forType: must be exactly what NSPasteboard.stringForType: returns next - if this ever drifts, Cmd+C in the croft terminal silently fails for the user"
);
}
#[test]
fn write_string_overwrites_previous_clipboard_contents() {
let _g = lock_clipboard();
let first = unique_sentinel("first");
let second = unique_sentinel("second");
assert!(write_string(&first));
assert!(write_string(&second));
assert_eq!(
read_string().as_deref(),
Some(second.as_str()),
"the second copy must overwrite the first - clearContents+setString:forType: must not append"
);
}
#[test]
fn write_string_round_trips_unicode_payloads() {
let _g = lock_clipboard();
let sentinel = format!("héllo · 世界 · 🦀 · {}", unique_sentinel("unicode"));
assert!(write_string(&sentinel));
assert_eq!(
read_string().as_deref(),
Some(sentinel.as_str()),
"multi-byte UTF-8 must round-trip - the embedded shell happily produces emoji and CJK and the clipboard must not mangle them"
);
}
#[test]
fn write_string_round_trips_multi_line_payloads() {
let _g = lock_clipboard();
let body = "first\nsecond\nthird";
let sentinel = format!("{body}-{}", unique_sentinel("multiline"));
assert!(write_string(&sentinel));
let got = read_string().expect("clipboard read");
assert_eq!(
got, sentinel,
"newline-separated terminal selections must round-trip verbatim"
);
}
#[test]
fn write_string_rejects_payloads_containing_a_nul_byte_without_panicking() {
let _g = lock_clipboard();
let baseline = unique_sentinel("baseline");
assert!(write_string(&baseline));
let with_nul = "before\0after";
assert!(
!write_string(with_nul),
"embedded nul byte must surface as a clean false from write_string instead of panicking inside the FFI bridge"
);
assert_eq!(
read_string().as_deref(),
Some(baseline.as_str()),
"a failed write must not clobber the previous clipboard contents"
);
}
#[test]
fn macos_native_write_string_succeeds_directly() {
let _g = lock_clipboard();
let sentinel = unique_sentinel("native-direct");
assert!(
macos::write_string_native(&sentinel),
"the NSPasteboard FFI bridge itself must succeed on macOS - if this fails, pbcopy will be the production fallback and we'll know the bridge regressed"
);
assert_eq!(read_string().as_deref(), Some(sentinel.as_str()));
}
}
#[cfg(all(test, target_os = "linux"))]
mod linux_backend_tests {
use super::*;
#[test]
fn write_then_read_round_trips_when_a_clipboard_backend_is_available() {
let _g = lock_clipboard_for_test();
let sentinel = format!(
"croft-linux-clip-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0)
);
if !write_string(&sentinel) {
eprintln!(
"linux_backend_tests: no clipboard backend (wl-copy/xclip/xsel) available — skipping"
);
return;
}
assert_eq!(
read_string().as_deref(),
Some(sentinel.as_str()),
"a Linux clipboard backend must return exactly what write_string just put on it"
);
}
}
#[cfg(all(test, not(any(target_os = "macos", target_os = "linux"))))]
mod other_os_backend_tests {
use super::*;
#[test]
fn clipboard_is_a_graceful_no_op_without_a_native_backend() {
let _g = lock_clipboard_for_test();
assert!(
!write_string("croft-no-backend-probe"),
"without a native backend, write_string must report false so the caller falls back to OSC 52"
);
assert_eq!(
read_string(),
None,
"without a native backend, read_string must yield None rather than panic"
);
}
}