Skip to content

Commit 1a6e184

Browse files
author
Jayce Fayne
committed
refactor + use which on all platforms
1 parent d4326e2 commit 1a6e184

File tree

1 file changed

+34
-48
lines changed

1 file changed

+34
-48
lines changed

src/clipboard.rs

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
use anyhow::{anyhow, Result};
2-
#[cfg(target_family = "unix")]
3-
#[cfg(not(target_os = "macos"))]
4-
use std::ffi::OsStr;
52
use std::io::Write;
3+
use std::path::PathBuf;
64
use std::process::{Command, Stdio};
5+
use which::which;
76

8-
fn execute_copy_command(command: Command, text: &str) -> Result<()> {
9-
let mut command = command;
7+
fn exec_copy_with_args(
8+
command: &str,
9+
args: &[&str],
10+
text: &str,
11+
) -> Result<()> {
12+
let binary = which(command)
13+
.ok()
14+
.unwrap_or_else(|| PathBuf::from(command));
1015

11-
let mut process = command
16+
let mut process = Command::new(binary)
17+
.args(args)
1218
.stdin(Stdio::piped())
1319
.stdout(Stdio::null())
1420
.spawn()
@@ -28,55 +34,35 @@ fn execute_copy_command(command: Command, text: &str) -> Result<()> {
2834
Ok(())
2935
}
3036

31-
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
32-
fn gen_command(
33-
path: impl AsRef<OsStr>,
34-
xclip_syntax: bool,
35-
) -> Command {
36-
let mut c = Command::new(path);
37-
if xclip_syntax {
38-
c.arg("-selection");
39-
c.arg("clipboard");
40-
} else {
41-
c.arg("--clipboard");
42-
}
43-
c
37+
fn exec_copy(command: &str, text: &str) -> Result<()> {
38+
exec_copy_with_args(command, &[], text)
4439
}
4540

4641
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
47-
pub fn copy_string(string: &str) -> Result<()> {
48-
use std::env;
49-
use std::path::PathBuf;
50-
use which::which;
51-
let cmd = if env::var("WAYLAND_DISPLAY").is_ok() {
52-
Command::new(
53-
which("wl-copy")
54-
.ok()
55-
.unwrap_or_else(|| PathBuf::from("wl-copy")),
56-
)
57-
} else {
58-
let (path, xclip_syntax) = which("xclip").ok().map_or_else(
59-
|| {
60-
(
61-
which("xsel")
62-
.ok()
63-
.unwrap_or_else(|| PathBuf::from("xsel")),
64-
false,
65-
)
66-
},
67-
|path| (path, true),
68-
);
69-
gen_command(path, xclip_syntax)
70-
};
71-
execute_copy_command(cmd, string)
42+
pub fn copy_string(text: &str) -> Result<()> {
43+
if std::env::var("WAYLAND_DISPLAY").is_ok() {
44+
return exec_copy("wl-copy", text);
45+
}
46+
47+
if exec_copy_with_args(
48+
"xclip",
49+
&["-selection", "clipboard"],
50+
text,
51+
)
52+
.is_err()
53+
{
54+
return exec_copy_with_args("xsel", &["--clipboard"], text);
55+
}
56+
57+
Ok(())
7258
}
7359

7460
#[cfg(target_os = "macos")]
75-
pub fn copy_string(string: &str) -> Result<()> {
76-
execute_copy_command(Command::new("pbcopy"), string)
61+
pub fn copy_string(text: &str) -> Result<()> {
62+
exec_copy("pbcopy", text)
7763
}
7864

7965
#[cfg(windows)]
80-
pub fn copy_string(string: &str) -> Result<()> {
81-
execute_copy_command(Command::new("clip"), string)
66+
pub fn copy_string(text: &str) -> Result<()> {
67+
exec_copy("clip", text)
8268
}

0 commit comments

Comments
 (0)