Skip to content

Commit 3716e9f

Browse files
denismaxim0vStephan Dilly
authored and
Stephan Dilly
committed
feat: xclip fallback for linux with xsel #352 (#390)
closes #352
1 parent cc22408 commit 3716e9f

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ anyhow = "1.0.33"
4141
unicode-width = "0.1"
4242
textwrap = "0.12"
4343

44+
[target.'cfg(target_os = "linux")'.dependencies]
45+
which = "4.0.2"
46+
4447
[target.'cfg(not(windows))'.dependencies]
4548
pprof = { version = "0.3", features = ["flamegraph"], optional = true }
4649

src/clipboard.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::Result;
2+
use std::ffi::OsStr;
23
use std::io::Write;
34
use std::process::{Command, Stdio};
45

@@ -27,10 +28,37 @@ fn execute_copy_command(command: Command, text: &str) -> Result<()> {
2728
Ok(())
2829
}
2930

31+
fn gen_command(
32+
path: impl AsRef<OsStr>,
33+
xclip_syntax: bool,
34+
) -> Command {
35+
let mut c = Command::new(path);
36+
if xclip_syntax {
37+
c.arg("-selection");
38+
c.arg("clipboard");
39+
} else {
40+
c.arg("--clipboard");
41+
}
42+
c
43+
}
44+
3045
#[cfg(target_os = "linux")]
3146
pub fn copy_string(string: &str) -> Result<()> {
32-
let mut cmd = Command::new("xclip");
33-
cmd.arg("-selection").arg("clipboard");
47+
use std::path::PathBuf;
48+
use which::which;
49+
let (path, xclip_syntax) = which("xclip").ok().map_or_else(
50+
|| {
51+
(
52+
which("xsel")
53+
.ok()
54+
.unwrap_or_else(|| PathBuf::from("xsel")),
55+
false,
56+
)
57+
},
58+
|path| (path, true),
59+
);
60+
61+
let cmd = gen_command(path, xclip_syntax);
3462
execute_copy_command(cmd, string)
3563
}
3664

0 commit comments

Comments
 (0)