diff options
author | Denis Maximov <denismaximov98@gmail.com> | 2020-11-01 02:22:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-01 01:22:03 +0100 |
commit | cad40d17eea23089d8efa0d4c939ad1f2fb5050f (patch) | |
tree | 2c7ee7b4a6e0e1c50888a35c3ba3848dde2af8d4 /src/clipboard.rs | |
parent | 99c3277e940c0a36804541ffa6c48ca6e0067f42 (diff) |
feat: xclip fallback for linux with xsel #352 (#390)
closes #352
Diffstat (limited to 'src/clipboard.rs')
-rw-r--r-- | src/clipboard.rs | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/clipboard.rs b/src/clipboard.rs index 6730ceb1..5f15e11f 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use std::ffi::OsStr; use std::io::Write; use std::process::{Command, Stdio}; @@ -27,10 +28,37 @@ fn execute_copy_command(command: Command, text: &str) -> Result<()> { Ok(()) } +fn gen_command( + path: impl AsRef<OsStr>, + xclip_syntax: bool, +) -> Command { + let mut c = Command::new(path); + if xclip_syntax { + c.arg("-selection"); + c.arg("clipboard"); + } else { + c.arg("--clipboard"); + } + c +} + #[cfg(target_os = "linux")] pub fn copy_string(string: &str) -> Result<()> { - let mut cmd = Command::new("xclip"); - cmd.arg("-selection").arg("clipboard"); + use std::path::PathBuf; + use which::which; + let (path, xclip_syntax) = which("xclip").ok().map_or_else( + || { + ( + which("xsel") + .ok() + .unwrap_or_else(|| PathBuf::from("xsel")), + false, + ) + }, + |path| (path, true), + ); + + let cmd = gen_command(path, xclip_syntax); execute_copy_command(cmd, string) } |