@@ -16,12 +16,12 @@ pub trait ClipboardProvider: std::fmt::Debug {
17macro_rules! command_provider {
17macro_rules! command_provider {
18 (paste => $get_prg:literal $( , $get_arg:literal )* ; copy => $set_prg:literal $( , $set_arg:literal )* ; ) => {{
18 (paste => $get_prg:literal $( , $get_arg:literal )* ; copy => $set_prg:literal $( , $set_arg:literal )* ; ) => {{
19− Box::new(provider::CommandProvider {
19+ Box::new(provider::command::Provider {
20− get_cmd: provider::CommandConfig {
20+ get_cmd: provider::command::Config {
21 prg: $get_prg,
21 prg: $get_prg,
22 args: &[ $( $get_arg ),* ],
22 args: &[ $( $get_arg ),* ],
24− set_cmd: provider::CommandConfig {
24+ set_cmd: provider::command::Config {
25 prg: $set_prg,
25 prg: $set_prg,
26 args: &[ $( $set_arg ),* ],
26 args: &[ $( $set_arg ),* ],
@@ -35,20 +35,20 @@ macro_rules! command_provider {
35 primary_paste => $pr_get_prg:literal $( , $pr_get_arg:literal )* ;
35 primary_paste => $pr_get_prg:literal $( , $pr_get_arg:literal )* ;
36 primary_copy => $pr_set_prg:literal $( , $pr_set_arg:literal )* ;
36 primary_copy => $pr_set_prg:literal $( , $pr_set_arg:literal )* ;
38− Box::new(provider::CommandProvider {
38+ Box::new(provider::command::Provider {
39− get_cmd: provider::CommandConfig {
39+ get_cmd: provider::command::Config {
40 prg: $get_prg,
40 prg: $get_prg,
41 args: &[ $( $get_arg ),* ],
41 args: &[ $( $get_arg ),* ],
43− set_cmd: provider::CommandConfig {
43+ set_cmd: provider::command::Config {
44 prg: $set_prg,
44 prg: $set_prg,
45 args: &[ $( $set_arg ),* ],
45 args: &[ $( $set_arg ),* ],
47− get_primary_cmd: Some(provider::CommandConfig {
47+ get_primary_cmd: Some(provider::command::Config {
48 prg: $pr_get_prg,
48 prg: $pr_get_prg,
49 args: &[ $( $pr_get_arg ),* ],
49 args: &[ $( $pr_get_arg ),* ],
51− set_primary_cmd: Some(provider::CommandConfig {
51+ set_primary_cmd: Some(provider::command::Config {
52 prg: $pr_set_prg,
52 prg: $pr_set_prg,
53 args: &[ $( $pr_set_arg ),* ],
53 args: &[ $( $pr_set_arg ),* ],
@@ -63,6 +63,8 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
64#[cfg(target_os = "macos")]
64#[cfg(target_os = "macos")]
65pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
65pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
66 if exists("pbcopy") && exists("pbpaste") {
68 if exists("pbcopy") && exists("pbpaste") {
67 command_provider! {
69 command_provider! {
68 paste => "pbpaste";
70 paste => "pbpaste";
@@ -75,6 +77,7 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
76#[cfg(not(any(windows, target_os = "macos")))]
78#[cfg(not(any(windows, target_os = "macos")))]
77pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
79pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
80+ use provider::command::{env_var_is_set, exists, is_exit_success};
78 // TODO: support for user-defined provider, probably when we have plugin support by setting a
81 // TODO: support for user-defined provider, probably when we have plugin support by setting a
79 // variable?
82 // variable?
@@ -116,26 +119,9 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
119−fn exists(executable_name: &str) -> bool {
120− which::which(executable_name).is_ok()
123−fn env_var_is_set(env_var_name: &str) -> bool {
124− std::env::var_os(env_var_name).is_some()
127−fn is_exit_success(program: &str, args: &[&str]) -> bool {
128− std::process::Command::new(program)
132− .and_then(|out| out.status.success().then(|| ())) // TODO: use then_some when stabilized
136mod provider {
122mod provider {
137 use super::{ClipboardProvider, ClipboardType};
123 use super::{ClipboardProvider, ClipboardType};
138− use anyhow::{bail, Context as _, Result};
124+ use anyhow::Result;
139 use std::borrow::Cow;
125 use std::borrow::Cow;
141 #[cfg(not(target_os = "windows"))]
127 #[cfg(not(target_os = "windows"))]
@@ -210,94 +196,116 @@ mod provider {
213− #[derive(Debug)]
199+ pub mod command {
214− pub struct CommandConfig {
200+ use super::*;
215− pub prg: &'static str,
201+ use anyhow::{bail, Context as _, Result};
216− pub args: &'static [&'static str],
219− impl CommandConfig {
203+ pub fn exists(executable_name: &str) -> bool {
220− fn execute(&self, input: Option<&str>, pipe_output: bool) -> Result<Option<String>> {
204+ which::which(executable_name).is_ok()
222− use std::process::{Command, Stdio};
224− let stdin = input.map(|_| Stdio::piped()).unwrap_or_else(Stdio::null);
225− let stdout = pipe_output.then(Stdio::piped).unwrap_or_else(Stdio::null);
227− let mut child = Command::new(self.prg)
231− .stderr(Stdio::null())
234− if let Some(input) = input {
235− let mut stdin = child.stdin.take().context("stdin is missing")?;
237− .write_all(input.as_bytes())
238− .context("couldn't write in stdin")?;
241− // TODO: add timer?
207+ pub fn env_var_is_set(env_var_name: &str) -> bool {
242− let output = child.wait_with_output()?;
208+ std::env::var_os(env_var_name).is_some()
211+ pub fn is_exit_success(program: &str, args: &[&str]) -> bool {
212+ std::process::Command::new(program)
244− if !output.status.success() {
214+ .output()
245− bail!("clipboard provider {} failed", self.prg);
215+ .ok()
216+ .and_then(|out| out.status.success().then(|| ())) // TODO: use then_some when stabilized
248− if pipe_output {
220+ #[derive(Debug)]
249− Ok(Some(String::from_utf8(output.stdout)?))
221+ pub struct Config {
250− } else {
222+ pub prg: &'static str,
251− Ok(None)
223+ pub args: &'static [&'static str],
256− #[derive(Debug)]
226+ impl Config {
257− pub struct CommandProvider {
227+ fn execute(&self, input: Option<&str>, pipe_output: bool) -> Result<Option<String>> {
258− pub get_cmd: CommandConfig,
228+ use std::io::Write;
259− pub set_cmd: CommandConfig,
229+ use std::process::{Command, Stdio};
260− pub get_primary_cmd: Option<CommandConfig>,
230+
261− pub set_primary_cmd: Option<CommandConfig>,
231+ let stdin = input.map(|_| Stdio::piped()).unwrap_or_else(Stdio::null);
232+ let stdout = pipe_output.then(Stdio::piped).unwrap_or_else(Stdio::null);
234+ let mut child = Command::new(self.prg)
238+ .stderr(Stdio::null())
241+ if let Some(input) = input {
242+ let mut stdin = child.stdin.take().context("stdin is missing")?;
244+ .write_all(input.as_bytes())
245+ .context("couldn't write in stdin")?;
264− impl ClipboardProvider for CommandProvider {
248+ // TODO: add timer?
265− fn name(&self) -> Cow<str> {
249+ let output = child.wait_with_output()?;
266− if self.get_cmd.prg != self.set_cmd.prg {
250+
267− Cow::Owned(format!("{}+{}", self.get_cmd.prg, self.set_cmd.prg))
251+ if !output.status.success() {
252+ bail!("clipboard provider {} failed", self.prg);
256+ Ok(Some(String::from_utf8(output.stdout)?))
268− } else {
257+ } else {
269− Cow::Borrowed(self.get_cmd.prg)
258+ Ok(None)
273− fn get_contents(&self, clipboard_type: ClipboardType) -> Result<String> {
263+ #[derive(Debug)]
274− match clipboard_type {
264+ pub struct Provider {
275− ClipboardType::Clipboard => Ok(self
276− .get_cmd
265+ pub get_cmd: Config,
277− .execute(None, true)?
266+ pub set_cmd: Config,
278− .context("output is missing")?),
279− ClipboardType::Selection => {
280− if let Some(cmd) = &self.get_primary_cmd {
267+ pub get_primary_cmd: Option<Config>,
281− return cmd.execute(None, true)?.context("output is missing");
268+ pub set_primary_cmd: Option<Config>,
284− Ok(String::new())
271+ impl ClipboardProvider for Provider {
272+ fn name(&self) -> Cow<str> {
273+ if self.get_cmd.prg != self.set_cmd.prg {
274+ Cow::Owned(format!("{}+{}", self.get_cmd.prg, self.set_cmd.prg))
276+ Cow::Borrowed(self.get_cmd.prg)
289− fn set_contents(&mut self, value: String, clipboard_type: ClipboardType) -> Result<()> {
280+ fn get_contents(&self, clipboard_type: ClipboardType) -> Result<String> {
290− let cmd = match clipboard_type {
281+ match clipboard_type {
291− ClipboardType::Clipboard => &self.set_cmd,
282+ ClipboardType::Clipboard => Ok(self
284+ .execute(None, true)?
285+ .context("output is missing")?),
292− ClipboardType::Selection => {
286+ ClipboardType::Selection => {
293− if let Some(cmd) = &self.set_primary_cmd {
287+ if let Some(cmd) = &self.get_primary_cmd {
294− cmd
288+ return cmd.execute(None, true)?.context("output is missing");
296+ fn set_contents(&mut self, value: String, clipboard_type: ClipboardType) -> Result<()> {
297+ let cmd = match clipboard_type {
298+ ClipboardType::Clipboard => &self.set_cmd,
299+ ClipboardType::Selection => {
300+ if let Some(cmd) = &self.set_primary_cmd {
300− cmd.execute(Some(&value), false).map(|_| ())
307+ cmd.execute(Some(&value), false).map(|_| ())