Skip to content

Commit

Permalink
Use helper binary for clipboard access (gitui-org#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`
- Remove feature `clipboard`
  • Loading branch information
cruessler committed Oct 10, 2020
1 parent ea8b32d commit ee6a377
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 126 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ jobs:
profile: minimal
components: clippy

- name: Install dependencies for clipboard access
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get -qq install libxcb-shape0-dev libxcb-xfixes0-dev
- name: Build Debug
run: |
rustc --version
Expand Down
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.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
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 @@ -49,7 +48,7 @@ pprof = { version = "0.3", features = ["flamegraph"], optional = true }
maintenance = { status = "actively-developed" }

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

[workspace]
Expand Down
54 changes: 34 additions & 20 deletions src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
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(not(feature = "clipboard"))]
pub fn copy_string(_string: String) -> Result<()> {
Ok(())
#[cfg(target_os = "linux")]
pub fn copy_string(string: &str) -> Result<()> {
execute_copy_command(
Command::new("xclip").arg("-selection").arg("clipboard"),
string,
)
}

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

#[cfg(not(feature = "clipboard"))]
pub fn is_supported() -> bool {
false
#[cfg(windows)]
pub fn copy_string(string: &str) -> Result<()> {
execute_copy_command(&mut Command::new("clip"), string)
}
18 changes: 7 additions & 11 deletions src/components/diff.rs
Original file line number Diff line number Diff line change
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 Expand Up @@ -611,13 +611,11 @@ impl Component for DiffComponent {
self.focused,
));

if crate::clipboard::is_supported() {
out.push(CommandInfo::new(
strings::commands::copy(&self.key_config),
true,
self.focused,
));
}
out.push(CommandInfo::new(
strings::commands::copy(&self.key_config),
true,
self.focused,
));

out.push(
CommandInfo::new(
Expand Down Expand Up @@ -697,9 +695,7 @@ impl Component for DiffComponent {
}
}
Ok(true)
} else if e == self.key_config.copy
&& crate::clipboard::is_supported()
{
} else if e == self.key_config.copy {
self.copy_selection()?;
Ok(true)
} else {
Expand Down

0 comments on commit ee6a377

Please sign in to comment.