From a90931192b41dff133b3a8747f4b4fa83545653a Mon Sep 17 00:00:00 2001 From: denis Date: Fri, 30 Oct 2020 16:52:56 +0200 Subject: [PATCH 1/4] feat: xclip fallback for linux with xsel #352 --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/clipboard.rs | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aecf0d4758..43a00c8308 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -461,6 +461,7 @@ dependencies = [ "textwrap 0.12.1", "tui", "unicode-width", + "which", ] [[package]] @@ -1357,6 +1358,16 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +[[package]] +name = "which" +version = "4.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87c14ef7e1b8b8ecfc75d5eca37949410046e66f15d185c01d70824f1f8111ef" +dependencies = [ + "libc", + "thiserror", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index d346ca4010..2600cdb03a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ serde = "1.0" anyhow = "1.0.33" unicode-width = "0.1" textwrap = "0.12" +which = "4.0.2" [target.'cfg(not(windows))'.dependencies] pprof = { version = "0.3", features = ["flamegraph"], optional = true } diff --git a/src/clipboard.rs b/src/clipboard.rs index 6730ceb1ba..a3af1b973f 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -1,6 +1,9 @@ use anyhow::Result; +use std::ffi::OsStr; use std::io::Write; +use std::path::PathBuf; use std::process::{Command, Stdio}; +use which::which; fn execute_copy_command(command: Command, text: &str) -> Result<()> { use anyhow::anyhow; @@ -27,10 +30,35 @@ fn execute_copy_command(command: Command, text: &str) -> Result<()> { Ok(()) } +fn gen_command( + path: impl AsRef, + 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"); + let (path, xclip_syntax) = which("xclip") + .ok() + .map(|path| (path, true)) + .unwrap_or_else(|| { + ( + which("xsel") + .ok() + .unwrap_or_else(|| PathBuf::from("xsel")), + false, + ) + }); + + let cmd = gen_command(path, xclip_syntax); execute_copy_command(cmd, string) } From 45c2ba30ac3428492fce645bd25eab95db779444 Mon Sep 17 00:00:00 2001 From: denis Date: Fri, 30 Oct 2020 17:16:40 +0200 Subject: [PATCH 2/4] fix: move imports --- src/clipboard.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clipboard.rs b/src/clipboard.rs index a3af1b973f..44350ab4c0 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -1,9 +1,7 @@ use anyhow::Result; use std::ffi::OsStr; use std::io::Write; -use std::path::PathBuf; use std::process::{Command, Stdio}; -use which::which; fn execute_copy_command(command: Command, text: &str) -> Result<()> { use anyhow::anyhow; @@ -46,6 +44,8 @@ fn gen_command( #[cfg(target_os = "linux")] pub fn copy_string(string: &str) -> Result<()> { + use std::path::PathBuf; + use which::which; let (path, xclip_syntax) = which("xclip") .ok() .map(|path| (path, true)) From 1a90cf98a8c906ae29ae1955894282e10fa9d413 Mon Sep 17 00:00:00 2001 From: denis Date: Fri, 30 Oct 2020 17:42:34 +0200 Subject: [PATCH 3/4] fix: replacing map and unwrap_or_else with a single map_or_else --- src/clipboard.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/clipboard.rs b/src/clipboard.rs index 44350ab4c0..5f15e11f76 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -46,17 +46,17 @@ fn gen_command( pub fn copy_string(string: &str) -> Result<()> { use std::path::PathBuf; use which::which; - let (path, xclip_syntax) = which("xclip") - .ok() - .map(|path| (path, true)) - .unwrap_or_else(|| { + 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) From a41b3499bd0855d553b5c41c98ca1b1b38f86bea Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sun, 1 Nov 2020 01:03:10 +0100 Subject: [PATCH 4/4] Update Cargo.toml Co-authored-by: Florian Wilkens --- Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 2600cdb03a..5cc2a80101 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,8 @@ serde = "1.0" anyhow = "1.0.33" unicode-width = "0.1" textwrap = "0.12" + +[target.'cfg(target_os = "linux")'.dependencies] which = "4.0.2" [target.'cfg(not(windows))'.dependencies]