Use helper binary for clipboard access (#262)

Use `clip` on Windows, `pbcopy` on MacOX, `xclip` on Linux.

- Remove dependency on `clipboard`
- Remove feature `clipboard`
This commit is contained in:
Christoph Rüßler 2020-10-10 17:52:11 +02:00 committed by Stephan Dilly
parent ea8b32d623
commit 04bd45e641
6 changed files with 47 additions and 129 deletions

View file

@ -29,11 +29,6 @@ jobs:
profile: minimal profile: minimal
components: clippy 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 - name: Build Debug
run: | run: |
rustc --version rustc --version

90
Cargo.lock generated
View file

@ -120,12 +120,6 @@ dependencies = [
"constant_time_eq", "constant_time_eq",
] ]
[[package]]
name = "block"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a"
[[package]] [[package]]
name = "bytemuck" name = "bytemuck"
version = "1.2.0" version = "1.2.0"
@ -183,28 +177,6 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "clipboard"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25a904646c0340239dcf7c51677b33928bf24fdf424b79a57909c0109075b2e7"
dependencies = [
"clipboard-win",
"objc",
"objc-foundation",
"objc_id",
"x11-clipboard",
]
[[package]]
name = "clipboard-win"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a093d6fed558e5fe24c3dfc85a68bb68f1c824f440d3ba5aca189e2998786b"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "cloudabi" name = "cloudabi"
version = "0.0.3" version = "0.0.3"
@ -448,7 +420,6 @@ dependencies = [
"bytesize", "bytesize",
"chrono", "chrono",
"clap", "clap",
"clipboard",
"crossbeam-channel", "crossbeam-channel",
"crossterm 0.18.0", "crossterm 0.18.0",
"dirs", "dirs",
@ -560,9 +531,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.72" version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9f8082297d534141b30c8d39e9b1773713ab50fdbe4ff30f750d063b3bfd701" checksum = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743"
[[package]] [[package]]
name = "libgit2-sys" name = "libgit2-sys"
@ -631,15 +602,6 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "malloc_buf"
version = "0.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "matches" name = "matches"
version = "0.1.8" version = "0.1.8"
@ -786,35 +748,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "objc"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1"
dependencies = [
"malloc_buf",
]
[[package]]
name = "objc-foundation"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9"
dependencies = [
"block",
"objc",
"objc_id",
]
[[package]]
name = "objc_id"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b"
dependencies = [
"objc",
]
[[package]] [[package]]
name = "object" name = "object"
version = "0.20.0" version = "0.20.0"
@ -1403,22 +1336,3 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "x11-clipboard"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89bd49c06c9eb5d98e6ba6536cf64ac9f7ee3a009b2f53996d405b3944f6bcea"
dependencies = [
"xcb",
]
[[package]]
name = "xcb"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e917a3f24142e9ff8be2414e36c649d47d6cc2ba81f16201cdef96e533e02de"
dependencies = [
"libc",
"log",
]

View file

@ -40,7 +40,6 @@ serde = "1.0"
anyhow = "1.0.33" anyhow = "1.0.33"
unicode-width = "0.1" unicode-width = "0.1"
textwrap = "0.12" textwrap = "0.12"
clipboard = { version = "0.5", optional = true }
[target.'cfg(not(windows))'.dependencies] [target.'cfg(not(windows))'.dependencies]
pprof = { version = "0.3", features = ["flamegraph"], optional = true } pprof = { version = "0.3", features = ["flamegraph"], optional = true }
@ -49,7 +48,7 @@ pprof = { version = "0.3", features = ["flamegraph"], optional = true }
maintenance = { status = "actively-developed" } maintenance = { status = "actively-developed" }
[features] [features]
default=["clipboard"] default=[]
timing=["scopetime/enabled"] timing=["scopetime/enabled"]
[workspace] [workspace]

View file

@ -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 tar -C ./target/x86_64-unknown-linux-musl/release/ -czvf ./release/gitui-linux-musl.tar.gz ./gitui
build-linux-musl-debug: 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: 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: 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: test:
cargo test --workspace cargo test --workspace

View file

@ -1,33 +1,47 @@
use anyhow::Result; use anyhow::Result;
#[cfg(feature = "clipboard")] use std::io::Write;
use clipboard::{ClipboardContext, ClipboardProvider}; use std::process::{Command, Stdio};
#[cfg(feature = "clipboard")] fn execute_copy_command(
pub fn copy_string(string: String) -> Result<()> { command: &mut Command,
string: &str,
) -> Result<()> {
use anyhow::anyhow; use anyhow::anyhow;
let mut ctx: ClipboardContext = ClipboardProvider::new() let mut process = command
.map_err(|e| { .stdin(Stdio::piped())
anyhow!("failed to get access to clipboard: {}", e) .stdout(Stdio::null())
})?; .spawn()
ctx.set_contents(string).map_err(|e| { .map_err(|e| anyhow!("`{:?}`: {}", command, e))?;
anyhow!("failed to set clipboard contents: {}", 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(()) Ok(())
} }
#[cfg(not(feature = "clipboard"))] #[cfg(target_os = "linux")]
pub fn copy_string(_string: String) -> Result<()> { pub fn copy_string(string: &str) -> Result<()> {
Ok(()) execute_copy_command(
Command::new("xclip").arg("-selection").arg("clipboard"),
string,
)
} }
#[cfg(feature = "clipboard")] #[cfg(target_os = "macos")]
pub const fn is_supported() -> bool { pub fn copy_string(string: &str) -> Result<()> {
true execute_copy_command(&mut Command::new("pbcopy"), string)
} }
#[cfg(not(feature = "clipboard"))] #[cfg(windows)]
pub fn is_supported() -> bool { pub fn copy_string(string: &str) -> Result<()> {
false execute_copy_command(&mut Command::new("clip"), string)
} }

View file

@ -269,7 +269,7 @@ impl DiffComponent {
self, self,
"copy to clipboard error:", "copy to clipboard error:",
crate::clipboard::copy_string( crate::clipboard::copy_string(
lines_to_copy.join("\n") &lines_to_copy.join("\n")
) )
); );
} }
@ -611,13 +611,11 @@ impl Component for DiffComponent {
self.focused, self.focused,
)); ));
if crate::clipboard::is_supported() { out.push(CommandInfo::new(
out.push(CommandInfo::new( strings::commands::copy(&self.key_config),
strings::commands::copy(&self.key_config), true,
true, self.focused,
self.focused, ));
));
}
out.push( out.push(
CommandInfo::new( CommandInfo::new(
@ -697,9 +695,7 @@ impl Component for DiffComponent {
} }
} }
Ok(true) Ok(true)
} else if e == self.key_config.copy } else if e == self.key_config.copy {
&& crate::clipboard::is_supported()
{
self.copy_selection()?; self.copy_selection()?;
Ok(true) Ok(true)
} else { } else {