Skip to content

Commit 04bd45e

Browse files
cruesslerStephan Dilly
authored andcommitted
Use helper binary for clipboard access (#262)
Use `clip` on Windows, `pbcopy` on MacOX, `xclip` on Linux. - Remove dependency on `clipboard` - Remove feature `clipboard`
1 parent ea8b32d commit 04bd45e

File tree

6 files changed

+47
-129
lines changed

6 files changed

+47
-129
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ jobs:
2929
profile: minimal
3030
components: clippy
3131

32-
- name: Install dependencies for clipboard access
33-
if: matrix.os == 'ubuntu-latest'
34-
run: |
35-
sudo apt-get -qq install libxcb-shape0-dev libxcb-xfixes0-dev
36-
3732
- name: Build Debug
3833
run: |
3934
rustc --version

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 & 2 deletions
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 }
@@ -49,7 +48,7 @@ pprof = { version = "0.3", features = ["flamegraph"], optional = true }
4948
maintenance = { status = "actively-developed" }
5049

5150
[features]
52-
default=["clipboard"]
51+
default=[]
5352
timing=["scopetime/enabled"]
5453

5554
[workspace]

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ release-linux-musl: build-linux-musl-release
2626
tar -C ./target/x86_64-unknown-linux-musl/release/ -czvf ./release/gitui-linux-musl.tar.gz ./gitui
2727

2828
build-linux-musl-debug:
29-
cargo build --target=x86_64-unknown-linux-musl --no-default-features
29+
cargo build --target=x86_64-unknown-linux-musl
3030

3131
build-linux-musl-release:
32-
cargo build --release --target=x86_64-unknown-linux-musl --no-default-features
32+
cargo build --release --target=x86_64-unknown-linux-musl
3333

3434
test-linux-musl:
35-
cargo test --workspace --target=x86_64-unknown-linux-musl --no-default-features
35+
cargo test --workspace --target=x86_64-unknown-linux-musl
3636

3737
test:
3838
cargo test --workspace

src/clipboard.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
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

20-
#[cfg(not(feature = "clipboard"))]
21-
pub fn copy_string(_string: String) -> Result<()> {
22-
Ok(())
31+
#[cfg(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+
)
2337
}
2438

25-
#[cfg(feature = "clipboard")]
26-
pub const fn is_supported() -> bool {
27-
true
39+
#[cfg(target_os = "macos")]
40+
pub fn copy_string(string: &str) -> Result<()> {
41+
execute_copy_command(&mut Command::new("pbcopy"), string)
2842
}
2943

30-
#[cfg(not(feature = "clipboard"))]
31-
pub fn is_supported() -> bool {
32-
false
44+
#[cfg(windows)]
45+
pub fn copy_string(string: &str) -> Result<()> {
46+
execute_copy_command(&mut Command::new("clip"), string)
3347
}

src/components/diff.rs

Lines changed: 7 additions & 11 deletions
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
}
@@ -611,13 +611,11 @@ impl Component for DiffComponent {
611611
self.focused,
612612
));
613613

614-
if crate::clipboard::is_supported() {
615-
out.push(CommandInfo::new(
616-
strings::commands::copy(&self.key_config),
617-
true,
618-
self.focused,
619-
));
620-
}
614+
out.push(CommandInfo::new(
615+
strings::commands::copy(&self.key_config),
616+
true,
617+
self.focused,
618+
));
621619

622620
out.push(
623621
CommandInfo::new(
@@ -697,9 +695,7 @@ impl Component for DiffComponent {
697695
}
698696
}
699697
Ok(true)
700-
} else if e == self.key_config.copy
701-
&& crate::clipboard::is_supported()
702-
{
698+
} else if e == self.key_config.copy {
703699
self.copy_selection()?;
704700
Ok(true)
705701
} else {

0 commit comments

Comments
 (0)