-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use command for clipboard instead of copypasta (#36)
* use copy command instead of copypasta * remove a step for installing dependencies * rename copy_string to copy_to_clipboard
- Loading branch information
Showing
7 changed files
with
74 additions
and
92 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
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,68 +1,68 @@ | ||
use copypasta::ClipboardContext; | ||
use copypasta::ClipboardProvider; | ||
use anyhow::{anyhow, Result}; | ||
#[cfg(all(target_family = "unix", not(target_os = "macos")))] | ||
use std::ffi::OsStr; | ||
use std::io::Write; | ||
use std::process::{Command, Stdio}; | ||
|
||
pub struct Clipboard { | ||
clipboard: Box<dyn ClipboardProvider>, | ||
selection: Option<Box<dyn ClipboardProvider>>, | ||
} | ||
fn execute_copy_command(command: Command, text: &str) -> Result<()> { | ||
let mut command = command; | ||
|
||
impl Clipboard { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
let mut process = command | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::null()) | ||
.spawn() | ||
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?; | ||
|
||
// #[cfg(any(test, not(any(target_os = "macos", windows))))] | ||
// pub fn new_nop() -> Self { | ||
// Self { | ||
// clipboard: Box::new(NopClipboardContext::new().unwrap()), | ||
// selection: None, | ||
// } | ||
// } | ||
} | ||
process | ||
.stdin | ||
.as_mut() | ||
.ok_or_else(|| anyhow!("`{:?}`", command))? | ||
.write_all(text.as_bytes()) | ||
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?; | ||
|
||
impl Default for Clipboard { | ||
fn default() -> Self { | ||
return Self { | ||
clipboard: Box::new(ClipboardContext::new().unwrap()), | ||
selection: None, | ||
}; | ||
process | ||
.wait() | ||
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?; | ||
|
||
// #[cfg(target_os = "linux")] | ||
// return Self { | ||
// clipboard: Box::new(ClipboardContext::new().unwrap()), | ||
// selection: Some(Box::new( | ||
// X11ClipboardContext::<X11SelectionClipboard>::new().unwrap(), | ||
// )), | ||
// }; | ||
Ok(()) | ||
} | ||
|
||
// #[cfg(not(any(target_os = "macos", windows)))] | ||
// return Self::new_nop(); | ||
#[cfg(all(target_family = "unix", not(target_os = "macos")))] | ||
fn gen_command(path: impl AsRef<OsStr>, xclip_syntax: bool) -> Command { | ||
let mut c = Command::new(path); | ||
if xclip_syntax { | ||
c.arg("-selection"); | ||
c.arg("clipboard"); | ||
} else { | ||
c.arg("--clipboard"); | ||
} | ||
c | ||
} | ||
|
||
impl Clipboard { | ||
pub fn store(&mut self, text: impl Into<String>) { | ||
let clipboard = match &mut self.selection { | ||
Some(provider) => provider, | ||
None => &mut self.clipboard, | ||
}; | ||
#[cfg(all(target_family = "unix", not(target_os = "macos")))] | ||
pub fn copy_to_clipboard(string: &str) -> Result<()> { | ||
use std::path::PathBuf; | ||
use which::which; | ||
let (path, xclip_syntax) = which("xclip").ok().map_or_else( | ||
|| { | ||
( | ||
which("xsel").ok().unwrap_or_else(|| PathBuf::from("xsel")), | ||
false, | ||
) | ||
}, | ||
|path| (path, true), | ||
); | ||
|
||
clipboard.set_contents(text.into()).unwrap_or_else(|err| { | ||
panic!("Unable to store text in clipboard: {}", err); | ||
}); | ||
} | ||
let cmd = gen_command(path, xclip_syntax); | ||
execute_copy_command(cmd, string) | ||
} | ||
|
||
pub fn _load(&mut self) -> String { | ||
let clipboard = match &mut self.selection { | ||
Some(provider) => provider, | ||
None => &mut self.clipboard, | ||
}; | ||
#[cfg(target_os = "macos")] | ||
pub fn copy_to_clipboard(string: &str) -> Result<()> { | ||
execute_copy_command(Command::new("pbcopy"), string) | ||
} | ||
|
||
match clipboard.get_contents() { | ||
Err(err) => { | ||
panic!("Unable to load text from clipboard: {}", err); | ||
} | ||
Ok(text) => text, | ||
} | ||
} | ||
#[cfg(windows)] | ||
pub fn copy_to_clipboard(string: &str) -> Result<()> { | ||
execute_copy_command(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