Skip to content

Use helper binary for clipboard access (#262) #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ release-linux-musl: build-linux-musl-release
tar -C ./target/x86_64-unknown-linux-musl/release/ -czvf ./release/gitui-linux-musl.tar.gz ./gitui

build-linux-musl-debug:
cargo build --target=x86_64-unknown-linux-musl --no-default-features
cargo build --target=x86_64-unknown-linux-musl

build-linux-musl-release:
cargo build --release --target=x86_64-unknown-linux-musl --no-default-features
cargo build --release --target=x86_64-unknown-linux-musl

test-linux-musl:
cargo test --workspace --target=x86_64-unknown-linux-musl --no-default-features
cargo test --workspace --target=x86_64-unknown-linux-musl

test:
cargo test --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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can take full ownership of command here.

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