Skip to content

Commit

Permalink
Use helper binary for clipboard access (extrawurst#262)
Browse files Browse the repository at this point in the history
Use `clip` on Windows, `pbcopy` on MacOX, `xclip` on Linux.

Remove dependency on `clipboard`.
  • Loading branch information
cruessler committed Oct 10, 2020
1 parent ea8b32d commit e18c46b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 102 deletions.
90 changes: 2 additions & 88 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -40,7 +40,6 @@ serde = "1.0"
anyhow = "1.0.33"
unicode-width = "0.1"
textwrap = "0.12"
clipboard = { version = "0.5", optional = true }

[target.'cfg(not(windows))'.dependencies]
pprof = { version = "0.3", features = ["flamegraph"], optional = true }
Expand All @@ -50,6 +49,7 @@ maintenance = { status = "actively-developed" }

[features]
default=["clipboard"]
clipboard=[]
timing=["scopetime/enabled"]

[workspace]
Expand Down
53 changes: 41 additions & 12 deletions src/clipboard.rs
@@ -1,24 +1,53 @@
use anyhow::Result;
#[cfg(feature = "clipboard")]
use clipboard::{ClipboardContext, ClipboardProvider};
use std::io::Write;
use std::process::{Command, Stdio};

#[cfg(feature = "clipboard")]
pub fn copy_string(string: String) -> Result<()> {
fn execute_copy_command(
command: &mut Command,
string: &str,
) -> Result<()> {
use anyhow::anyhow;

let mut ctx: ClipboardContext = ClipboardProvider::new()
.map_err(|e| {
anyhow!("failed to get access to clipboard: {}", e)
})?;
ctx.set_contents(string).map_err(|e| {
anyhow!("failed to set clipboard contents: {}", e)
})?;
let mut process = command
.stdin(Stdio::piped())
.stdout(Stdio::null())
.spawn()
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?;

process
.stdin
.as_mut()
.ok_or_else(|| anyhow!("`{:?}`", command))?
.write_all(string.as_bytes())
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?;

process
.wait()
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?;

Ok(())
}

#[cfg(all(feature = "clipboard", target_os = "linux"))]
pub fn copy_string(string: &str) -> Result<()> {
execute_copy_command(
Command::new("xclip").arg("-selection").arg("clipboard"),
string,
)
}

#[cfg(all(feature = "clipboard", target_os = "macos"))]
pub fn copy_string(string: &str) -> Result<()> {
execute_copy_command(&mut Command::new("pbcopy"), string)
}

#[cfg(all(feature = "clipboard", windows))]
pub fn copy_string(string: &str) -> Result<()> {
execute_copy_command(&mut Command::new("clip"), string)
}

#[cfg(not(feature = "clipboard"))]
pub fn copy_string(_string: String) -> Result<()> {
pub fn copy_string(_string: &str) -> Result<()> {
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/diff.rs
Expand Up @@ -269,7 +269,7 @@ impl DiffComponent {
self,
"copy to clipboard error:",
crate::clipboard::copy_string(
lines_to_copy.join("\n")
&lines_to_copy.join("\n")
)
);
}
Expand Down

0 comments on commit e18c46b

Please sign in to comment.