Skip to content

Commit

Permalink
refactor + use which on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
JayceFayne committed Jun 15, 2022
1 parent d4326e2 commit cb7c676
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 50 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ tui = { version = "0.16", default-features = false, features = ['crossterm', 'se
unicode-segmentation = "1.9"
unicode-truncate = "0.2"
unicode-width = "0.1"

[target.'cfg(all(target_family="unix",not(target_os="macos")))'.dependencies]
which = "4.2"

# pprof is not available on windows
Expand Down
82 changes: 34 additions & 48 deletions src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -28,55 +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::env;
use std::path::PathBuf;
use which::which;
let cmd = if env::var("WAYLAND_DISPLAY").is_ok() {
Command::new(
which("wl-copy")
.ok()
.unwrap_or_else(|| PathBuf::from("wl-copy")),
)
} else {
let (path, xclip_syntax) = which("xclip").ok().map_or_else(
|| {
(
which("xsel")
.ok()
.unwrap_or_else(|| PathBuf::from("xsel")),
false,
)
},
|path| (path, true),
);
gen_command(path, xclip_syntax)
};
execute_copy_command(cmd, string)
pub fn copy_string(text: &str) -> Result<()> {
if std::env::var("WAYLAND_DISPLAY").is_ok() {
return exec_copy("wl-copy", text);
}

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)
}

0 comments on commit cb7c676

Please sign in to comment.