diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-04-11 03:16:06 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-04-11 03:16:28 +0300 |
| commit | 6598c88d35268eec8f0902799152b34680f44fdd (patch) | |
| tree | e75ac9251bd69b4ef1f4a895ac33a673dd108dbb /src/platform/macos.rs | |
| parent | aa6da3c8335e871c1dbed1da721c39ae9fcbe97f (diff) | |
fix: prefer native clipboard tools over osc52 fallback
Diffstat (limited to 'src/platform/macos.rs')
| -rw-r--r-- | src/platform/macos.rs | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/src/platform/macos.rs b/src/platform/macos.rs index a5653d2b..f5de67ce 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -1,8 +1,10 @@ use std::ffi::OsStr; +use std::io::Write; use std::os::unix::ffi::OsStrExt; use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; -use super::{ForegroundJob, ForegroundProcess, Signal}; +use super::{ClipboardCommand, ForegroundJob, ForegroundProcess, Signal}; /// Collect the foreground terminal job for a given child PID. pub fn foreground_job(child_pid: u32) -> Option<ForegroundJob> { @@ -182,6 +184,44 @@ fn process_comm_name(pid: u32) -> Option<String> { process_bsdinfo(pid).and_then(|info| comm_from_bsdinfo(&info)) } +pub fn write_clipboard(bytes: &[u8]) -> bool { + run_clipboard_command( + &ClipboardCommand { + program: "pbcopy", + args: &[], + }, + bytes, + ) +} + +fn run_clipboard_command(command: &ClipboardCommand, bytes: &[u8]) -> bool { + let mut child = match Command::new(command.program) + .args(command.args) + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + { + Ok(child) => child, + Err(_) => return false, + }; + + let Some(mut stdin) = child.stdin.take() else { + let _ = child.kill(); + let _ = child.wait(); + return false; + }; + + if stdin.write_all(bytes).is_err() { + let _ = child.kill(); + let _ = child.wait(); + return false; + } + drop(stdin); + + child.wait().map(|status| status.success()).unwrap_or(false) +} + fn process_bsdinfo(pid: u32) -> Option<libc::proc_bsdinfo> { let mut info: libc::proc_bsdinfo = unsafe { std::mem::zeroed() }; let size = std::mem::size_of::<libc::proc_bsdinfo>() as libc::c_int; |
