summaryrefslogtreecommitdiffstats
path: root/src/clipboard.rs
diff options
context:
space:
mode:
authorJayceFayne <13365789+JayceFayne@users.noreply.github.com>2022-08-17 19:46:56 +0200
committerGitHub <noreply@github.com>2022-08-17 19:46:56 +0200
commitd4949a676b47e0240082a559360e6bb72e99dc73 (patch)
tree47ab2ad83553b42791e7f4ad2cdd204550e14c43 /src/clipboard.rs
parent074bb7cdb50ad679006ab8ac75a2e110417e725d (diff)
support copy to clipboard on wayland (#1233)
Diffstat (limited to 'src/clipboard.rs')
-rw-r--r--src/clipboard.rs72
1 files changed, 33 insertions, 39 deletions
diff --git a/src/clipboard.rs b/src/clipboard.rs
index a7ab23fa..fcb6af12 100644
--- a/src/clipboard.rs
+++ b/src/clipboard.rs
@@ -1,14 +1,20 @@
use anyhow::{anyhow, Result};
-#[cfg(target_family = "unix")]
-#[cfg(not(target_os = "macos"))]
-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<()> {
- let mut command = command;
+fn exec_copy_with_args(
+ command: &str,
+ args: &[&str],
+ text: &str,
+) -> Result<()> {
+ let binary = which(command)
+ .ok()
+ .unwrap_or_else(|| PathBuf::from(command));
- let mut process = command
+ let mut process = Command::new(binary)
+ .args(args)
.stdin(Stdio::piped())
.stdout(Stdio::null())
.spawn()
@@ -28,47 +34,35 @@ fn execute_copy_command(command: Command, text: &str) -> Result<()> {
Ok(())
}
-#[cfg(all(target_family = "unix", not(target_os = "macos")))]
-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
+fn exec_copy(command: &str, text: &str) -> Result<()> {
+ exec_copy_with_args(command, &[], text)
}
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
-pub fn copy_string(string: &str) -> Result<()> {
- 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),
- );
+pub fn copy_string(text: &str) -> Result<()> {
+ if std::env::var("WAYLAND_DISPLAY").is_ok() {
+ return exec_copy("wl-copy", text);
+ }
- let cmd = gen_command(path, xclip_syntax);
- execute_copy_command(cmd, string)
+ if exec_copy_with_args(
+ "xclip",
+ &["-selection", "clipboard"],
+ text,
+ )
+ .is_err()
+ {
+ return exec_copy_with_args("xsel", &["--clipboard"], text);
+ }
+
+ Ok(())
}
#[cfg(target_os = "macos")]
-pub fn copy_string(string: &str) -> Result<()> {
- execute_copy_command(Command::new("pbcopy"), string)
+pub fn copy_string(text: &str) -> Result<()> {
+ exec_copy("pbcopy", text)
}
#[cfg(windows)]
-pub fn copy_string(string: &str) -> Result<()> {
- execute_copy_command(Command::new("clip"), string)
+pub fn copy_string(text: &str) -> Result<()> {
+ exec_copy("clip", text)
}