forked from gitui-org/gitui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use helper binary for clipboard access (gitui-org#262)
Use `clip` on Windows, `pbcopy` on MacOX, `xclip` on Linux. - Remove dependency on `clipboard` - Remove feature `clipboard`
- Loading branch information
Showing
5 changed files
with
44 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters