Skip to content

Commit e18c46b

Browse files
committed
Use helper binary for clipboard access (#262)
Use `clip` on Windows, `pbcopy` on MacOX, `xclip` on Linux. Remove dependency on `clipboard`.
1 parent ea8b32d commit e18c46b

File tree

4 files changed

+45
-102
lines changed

4 files changed

+45
-102
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ serde = "1.0"
4040
anyhow = "1.0.33"
4141
unicode-width = "0.1"
4242
textwrap = "0.12"
43-
clipboard = { version = "0.5", optional = true }
4443

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

5150
[features]
5251
default=["clipboard"]
52+
clipboard=[]
5353
timing=["scopetime/enabled"]
5454

5555
[workspace]

src/clipboard.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,53 @@
11
use anyhow::Result;
2-
#[cfg(feature = "clipboard")]
3-
use clipboard::{ClipboardContext, ClipboardProvider};
2+
use std::io::Write;
3+
use std::process::{Command, Stdio};
44

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

9-
let mut ctx: ClipboardContext = ClipboardProvider::new()
10-
.map_err(|e| {
11-
anyhow!("failed to get access to clipboard: {}", e)
12-
})?;
13-
ctx.set_contents(string).map_err(|e| {
14-
anyhow!("failed to set clipboard contents: {}", e)
15-
})?;
11+
let mut process = command
12+
.stdin(Stdio::piped())
13+
.stdout(Stdio::null())
14+
.spawn()
15+
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?;
16+
17+
process
18+
.stdin
19+
.as_mut()
20+
.ok_or_else(|| anyhow!("`{:?}`", command))?
21+
.write_all(string.as_bytes())
22+
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?;
23+
24+
process
25+
.wait()
26+
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?;
1627

1728
Ok(())
1829
}
1930

31+
#[cfg(all(feature = "clipboard", target_os = "linux"))]
32+
pub fn copy_string(string: &str) -> Result<()> {
33+
execute_copy_command(
34+
Command::new("xclip").arg("-selection").arg("clipboard"),
35+
string,
36+
)
37+
}
38+
39+
#[cfg(all(feature = "clipboard", target_os = "macos"))]
40+
pub fn copy_string(string: &str) -> Result<()> {
41+
execute_copy_command(&mut Command::new("pbcopy"), string)
42+
}
43+
44+
#[cfg(all(feature = "clipboard", windows))]
45+
pub fn copy_string(string: &str) -> Result<()> {
46+
execute_copy_command(&mut Command::new("clip"), string)
47+
}
48+
2049
#[cfg(not(feature = "clipboard"))]
21-
pub fn copy_string(_string: String) -> Result<()> {
50+
pub fn copy_string(_string: &str) -> Result<()> {
2251
Ok(())
2352
}
2453

src/components/diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl DiffComponent {
269269
self,
270270
"copy to clipboard error:",
271271
crate::clipboard::copy_string(
272-
lines_to_copy.join("\n")
272+
&lines_to_copy.join("\n")
273273
)
274274
);
275275
}

0 commit comments

Comments
 (0)