on master help sign in
Refactor clipboard to make it easier to feature gate std::process
commit8611c5b84e2f87ab7708ed3ca91062f08608775d authorBlaž Hrastnik <blaz@mxxn.io> dateSun 27 Mar 2022, 17:20 (5 years ago) parent4940db3
1 file changed+106−98
helix-view/src/clipboard.rs+106−98
helix-view/src/clipboard.rs+106−98
@@ -16,12 +16,12 @@ pub trait ClipboardProvider: std::fmt::Debug {
1616
1717macro_rules! command_provider {
1818 (paste => $get_prg:literal $( , $get_arg:literal )* ; copy => $set_prg:literal $( , $set_arg:literal )* ; ) => {{
19− Box::new(provider::CommandProvider {
20− get_cmd: provider::CommandConfig {
19+ Box::new(provider::command::Provider {
20+ get_cmd: provider::command::Config {
2121 prg: $get_prg,
2222 args: &[ $( $get_arg ),* ],
2323 },
24− set_cmd: provider::CommandConfig {
24+ set_cmd: provider::command::Config {
2525 prg: $set_prg,
2626 args: &[ $( $set_arg ),* ],
2727 },
@@ -35,20 +35,20 @@ macro_rules! command_provider {
3535 primary_paste => $pr_get_prg:literal $( , $pr_get_arg:literal )* ;
3636 primary_copy => $pr_set_prg:literal $( , $pr_set_arg:literal )* ;
3737 ) => {{
38− Box::new(provider::CommandProvider {
39− get_cmd: provider::CommandConfig {
38+ Box::new(provider::command::Provider {
39+ get_cmd: provider::command::Config {
4040 prg: $get_prg,
4141 args: &[ $( $get_arg ),* ],
4242 },
43− set_cmd: provider::CommandConfig {
43+ set_cmd: provider::command::Config {
4444 prg: $set_prg,
4545 args: &[ $( $set_arg ),* ],
4646 },
47− get_primary_cmd: Some(provider::CommandConfig {
47+ get_primary_cmd: Some(provider::command::Config {
4848 prg: $pr_get_prg,
4949 args: &[ $( $pr_get_arg ),* ],
5050 }),
51− set_primary_cmd: Some(provider::CommandConfig {
51+ set_primary_cmd: Some(provider::command::Config {
5252 prg: $pr_set_prg,
5353 args: &[ $( $pr_set_arg ),* ],
5454 }),
@@ -63,6 +63,8 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
6363
6464#[cfg(target_os = "macos")]
6565pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
66+ use command::exists;
67+
6668 if exists("pbcopy") && exists("pbpaste") {
6769 command_provider! {
6870 paste => "pbpaste";
@@ -75,6 +77,7 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
7577
7678#[cfg(not(any(windows, target_os = "macos")))]
7779pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
80+ use provider::command::{env_var_is_set, exists, is_exit_success};
7881 // TODO: support for user-defined provider, probably when we have plugin support by setting a
7982 // variable?
8083
@@ -116,26 +119,9 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
116119 }
117120}
118121
119−fn exists(executable_name: &str) -> bool {
120− which::which(executable_name).is_ok()
121−}
122−
123−fn env_var_is_set(env_var_name: &str) -> bool {
124− std::env::var_os(env_var_name).is_some()
125−}
126−
127−fn is_exit_success(program: &str, args: &[&str]) -> bool {
128− std::process::Command::new(program)
129− .args(args)
130− .output()
131− .ok()
132− .and_then(|out| out.status.success().then(|| ())) // TODO: use then_some when stabilized
133− .is_some()
134−}
135−
136122mod provider {
137123 use super::{ClipboardProvider, ClipboardType};
138− use anyhow::{bail, Context as _, Result};
124+ use anyhow::Result;
139125 use std::borrow::Cow;
140126
141127 #[cfg(not(target_os = "windows"))]
@@ -210,94 +196,116 @@ mod provider {
210196 }
211197 }
212198
213− #[derive(Debug)]
214− pub struct CommandConfig {
215− pub prg: &'static str,
216− pub args: &'static [&'static str],
217− }
199+ pub mod command {
200+ use super::*;
201+ use anyhow::{bail, Context as _, Result};
218202
219− impl CommandConfig {
220− fn execute(&self, input: Option<&str>, pipe_output: bool) -> Result<Option<String>> {
221− use std::io::Write;
222− use std::process::{Command, Stdio};
223−
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);
226−
227− let mut child = Command::new(self.prg)
228− .args(self.args)
229− .stdin(stdin)
230− .stdout(stdout)
231− .stderr(Stdio::null())
232− .spawn()?;
233−
234− if let Some(input) = input {
235− let mut stdin = child.stdin.take().context("stdin is missing")?;
236− stdin
237− .write_all(input.as_bytes())
238− .context("couldn't write in stdin")?;
239− }
203+ pub fn exists(executable_name: &str) -> bool {
204+ which::which(executable_name).is_ok()
205+ }
240206
241− // TODO: add timer?
242− let output = child.wait_with_output()?;
207+ pub fn env_var_is_set(env_var_name: &str) -> bool {
208+ std::env::var_os(env_var_name).is_some()
209+ }
243210
244− if !output.status.success() {
245− bail!("clipboard provider {} failed", self.prg);
246− }
211+ pub fn is_exit_success(program: &str, args: &[&str]) -> bool {
212+ std::process::Command::new(program)
213+ .args(args)
214+ .output()
215+ .ok()
216+ .and_then(|out| out.status.success().then(|| ())) // TODO: use then_some when stabilized
217+ .is_some()
218+ }
247219
248− if pipe_output {
249− Ok(Some(String::from_utf8(output.stdout)?))
250− } else {
251− Ok(None)
252− }
220+ #[derive(Debug)]
221+ pub struct Config {
222+ pub prg: &'static str,
223+ pub args: &'static [&'static str],
253224 }
254− }
255225
256− #[derive(Debug)]
257− pub struct CommandProvider {
258− pub get_cmd: CommandConfig,
259− pub set_cmd: CommandConfig,
260− pub get_primary_cmd: Option<CommandConfig>,
261− pub set_primary_cmd: Option<CommandConfig>,
262− }
226+ impl Config {
227+ fn execute(&self, input: Option<&str>, pipe_output: bool) -> Result<Option<String>> {
228+ use std::io::Write;
229+ use std::process::{Command, Stdio};
230+
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);
233+
234+ let mut child = Command::new(self.prg)
235+ .args(self.args)
236+ .stdin(stdin)
237+ .stdout(stdout)
238+ .stderr(Stdio::null())
239+ .spawn()?;
240+
241+ if let Some(input) = input {
242+ let mut stdin = child.stdin.take().context("stdin is missing")?;
243+ stdin
244+ .write_all(input.as_bytes())
245+ .context("couldn't write in stdin")?;
246+ }
263247
264− impl ClipboardProvider for CommandProvider {
265− fn name(&self) -> Cow<str> {
266− if self.get_cmd.prg != self.set_cmd.prg {
267− Cow::Owned(format!("{}+{}", self.get_cmd.prg, self.set_cmd.prg))
268− } else {
269− Cow::Borrowed(self.get_cmd.prg)
248+ // TODO: add timer?
249+ let output = child.wait_with_output()?;
250+
251+ if !output.status.success() {
252+ bail!("clipboard provider {} failed", self.prg);
253+ }
254+
255+ if pipe_output {
256+ Ok(Some(String::from_utf8(output.stdout)?))
257+ } else {
258+ Ok(None)
259+ }
270260 }
271261 }
272262
273− fn get_contents(&self, clipboard_type: ClipboardType) -> Result<String> {
274− match clipboard_type {
275− ClipboardType::Clipboard => Ok(self
276− .get_cmd
277− .execute(None, true)?
278− .context("output is missing")?),
279− ClipboardType::Selection => {
280− if let Some(cmd) = &self.get_primary_cmd {
281− return cmd.execute(None, true)?.context("output is missing");
282− }
263+ #[derive(Debug)]
264+ pub struct Provider {
265+ pub get_cmd: Config,
266+ pub set_cmd: Config,
267+ pub get_primary_cmd: Option<Config>,
268+ pub set_primary_cmd: Option<Config>,
269+ }
283270
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))
275+ } else {
276+ Cow::Borrowed(self.get_cmd.prg)
285277 }
286278 }
287− }
288279
289− fn set_contents(&mut self, value: String, clipboard_type: ClipboardType) -> Result<()> {
290− let cmd = match clipboard_type {
291− ClipboardType::Clipboard => &self.set_cmd,
292− ClipboardType::Selection => {
293− if let Some(cmd) = &self.set_primary_cmd {
294− cmd
295− } else {
296− return Ok(());
280+ fn get_contents(&self, clipboard_type: ClipboardType) -> Result<String> {
281+ match clipboard_type {
282+ ClipboardType::Clipboard => Ok(self
283+ .get_cmd
284+ .execute(None, true)?
285+ .context("output is missing")?),
286+ ClipboardType::Selection => {
287+ if let Some(cmd) = &self.get_primary_cmd {
288+ return cmd.execute(None, true)?.context("output is missing");
289+ }
290+
291+ Ok(String::new())
297292 }
298293 }
299− };
300− cmd.execute(Some(&value), false).map(|_| ())
294+ }
295+
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 {
301+ cmd
302+ } else {
303+ return Ok(());
304+ }
305+ }
306+ };
307+ cmd.execute(Some(&value), false).map(|_| ())
308+ }
301309 }
302310 }
303311}