mirror of https://github.com/TaKO8Ki/gobang
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_clipboardpull/37/head
parent
5da6b1b796
commit
d493f46875
@ -1,68 +1,68 @@
|
|||||||
use copypasta::ClipboardContext;
|
use anyhow::{anyhow, Result};
|
||||||
use copypasta::ClipboardProvider;
|
#[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 {
|
fn execute_copy_command(command: Command, text: &str) -> Result<()> {
|
||||||
clipboard: Box<dyn ClipboardProvider>,
|
let mut command = command;
|
||||||
selection: Option<Box<dyn ClipboardProvider>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Clipboard {
|
let mut process = command
|
||||||
pub fn new() -> Self {
|
.stdin(Stdio::piped())
|
||||||
Self::default()
|
.stdout(Stdio::null())
|
||||||
}
|
.spawn()
|
||||||
|
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?;
|
||||||
|
|
||||||
// #[cfg(any(test, not(any(target_os = "macos", windows))))]
|
process
|
||||||
// pub fn new_nop() -> Self {
|
.stdin
|
||||||
// Self {
|
.as_mut()
|
||||||
// clipboard: Box::new(NopClipboardContext::new().unwrap()),
|
.ok_or_else(|| anyhow!("`{:?}`", command))?
|
||||||
// selection: None,
|
.write_all(text.as_bytes())
|
||||||
// }
|
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?;
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Clipboard {
|
process
|
||||||
fn default() -> Self {
|
.wait()
|
||||||
return Self {
|
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?;
|
||||||
clipboard: Box::new(ClipboardContext::new().unwrap()),
|
|
||||||
selection: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
// #[cfg(target_os = "linux")]
|
Ok(())
|
||||||
// return Self {
|
}
|
||||||
// clipboard: Box::new(ClipboardContext::new().unwrap()),
|
|
||||||
// selection: Some(Box::new(
|
|
||||||
// X11ClipboardContext::<X11SelectionClipboard>::new().unwrap(),
|
|
||||||
// )),
|
|
||||||
// };
|
|
||||||
|
|
||||||
// #[cfg(not(any(target_os = "macos", windows)))]
|
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
|
||||||
// return Self::new_nop();
|
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 {
|
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
|
||||||
pub fn store(&mut self, text: impl Into<String>) {
|
pub fn copy_to_clipboard(string: &str) -> Result<()> {
|
||||||
let clipboard = match &mut self.selection {
|
use std::path::PathBuf;
|
||||||
Some(provider) => provider,
|
use which::which;
|
||||||
None => &mut self.clipboard,
|
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| {
|
let cmd = gen_command(path, xclip_syntax);
|
||||||
panic!("Unable to store text in clipboard: {}", err);
|
execute_copy_command(cmd, string)
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn _load(&mut self) -> String {
|
#[cfg(target_os = "macos")]
|
||||||
let clipboard = match &mut self.selection {
|
pub fn copy_to_clipboard(string: &str) -> Result<()> {
|
||||||
Some(provider) => provider,
|
execute_copy_command(Command::new("pbcopy"), string)
|
||||||
None => &mut self.clipboard,
|
}
|
||||||
};
|
|
||||||
|
|
||||||
match clipboard.get_contents() {
|
#[cfg(windows)]
|
||||||
Err(err) => {
|
pub fn copy_to_clipboard(string: &str) -> Result<()> {
|
||||||
panic!("Unable to load text from clipboard: {}", err);
|
execute_copy_command(Command::new("clip"), string)
|
||||||
}
|
|
||||||
Ok(text) => text,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue