Refactor clipboard to make it easier to feature gate std::process

pull/1872/head
Blaž Hrastnik 3 years ago
parent 4940db3e2d
commit 8611c5b84e
No known key found for this signature in database
GPG Key ID: 1238B9C4AD889640

@ -16,12 +16,12 @@ pub trait ClipboardProvider: std::fmt::Debug {
macro_rules! command_provider {
(paste => $get_prg:literal $( , $get_arg:literal )* ; copy => $set_prg:literal $( , $set_arg:literal )* ; ) => {{
Box::new(provider::CommandProvider {
get_cmd: provider::CommandConfig {
Box::new(provider::command::Provider {
get_cmd: provider::command::Config {
prg: $get_prg,
args: &[ $( $get_arg ),* ],
},
set_cmd: provider::CommandConfig {
set_cmd: provider::command::Config {
prg: $set_prg,
args: &[ $( $set_arg ),* ],
},
@ -35,20 +35,20 @@ macro_rules! command_provider {
primary_paste => $pr_get_prg:literal $( , $pr_get_arg:literal )* ;
primary_copy => $pr_set_prg:literal $( , $pr_set_arg:literal )* ;
) => {{
Box::new(provider::CommandProvider {
get_cmd: provider::CommandConfig {
Box::new(provider::command::Provider {
get_cmd: provider::command::Config {
prg: $get_prg,
args: &[ $( $get_arg ),* ],
},
set_cmd: provider::CommandConfig {
set_cmd: provider::command::Config {
prg: $set_prg,
args: &[ $( $set_arg ),* ],
},
get_primary_cmd: Some(provider::CommandConfig {
get_primary_cmd: Some(provider::command::Config {
prg: $pr_get_prg,
args: &[ $( $pr_get_arg ),* ],
}),
set_primary_cmd: Some(provider::CommandConfig {
set_primary_cmd: Some(provider::command::Config {
prg: $pr_set_prg,
args: &[ $( $pr_set_arg ),* ],
}),
@ -63,6 +63,8 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
#[cfg(target_os = "macos")]
pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
use command::exists;
if exists("pbcopy") && exists("pbpaste") {
command_provider! {
paste => "pbpaste";
@ -75,6 +77,7 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
#[cfg(not(any(windows, target_os = "macos")))]
pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
use provider::command::{env_var_is_set, exists, is_exit_success};
// TODO: support for user-defined provider, probably when we have plugin support by setting a
// variable?
@ -116,26 +119,9 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
}
}
fn exists(executable_name: &str) -> bool {
which::which(executable_name).is_ok()
}
fn env_var_is_set(env_var_name: &str) -> bool {
std::env::var_os(env_var_name).is_some()
}
fn is_exit_success(program: &str, args: &[&str]) -> bool {
std::process::Command::new(program)
.args(args)
.output()
.ok()
.and_then(|out| out.status.success().then(|| ())) // TODO: use then_some when stabilized
.is_some()
}
mod provider {
use super::{ClipboardProvider, ClipboardType};
use anyhow::{bail, Context as _, Result};
use anyhow::Result;
use std::borrow::Cow;
#[cfg(not(target_os = "windows"))]
@ -210,13 +196,34 @@ mod provider {
}
}
pub mod command {
use super::*;
use anyhow::{bail, Context as _, Result};
pub fn exists(executable_name: &str) -> bool {
which::which(executable_name).is_ok()
}
pub fn env_var_is_set(env_var_name: &str) -> bool {
std::env::var_os(env_var_name).is_some()
}
pub fn is_exit_success(program: &str, args: &[&str]) -> bool {
std::process::Command::new(program)
.args(args)
.output()
.ok()
.and_then(|out| out.status.success().then(|| ())) // TODO: use then_some when stabilized
.is_some()
}
#[derive(Debug)]
pub struct CommandConfig {
pub struct Config {
pub prg: &'static str,
pub args: &'static [&'static str],
}
impl CommandConfig {
impl Config {
fn execute(&self, input: Option<&str>, pipe_output: bool) -> Result<Option<String>> {
use std::io::Write;
use std::process::{Command, Stdio};
@ -254,14 +261,14 @@ mod provider {
}
#[derive(Debug)]
pub struct CommandProvider {
pub get_cmd: CommandConfig,
pub set_cmd: CommandConfig,
pub get_primary_cmd: Option<CommandConfig>,
pub set_primary_cmd: Option<CommandConfig>,
pub struct Provider {
pub get_cmd: Config,
pub set_cmd: Config,
pub get_primary_cmd: Option<Config>,
pub set_primary_cmd: Option<Config>,
}
impl ClipboardProvider for CommandProvider {
impl ClipboardProvider for Provider {
fn name(&self) -> Cow<str> {
if self.get_cmd.prg != self.set_cmd.prg {
Cow::Owned(format!("{}+{}", self.get_cmd.prg, self.set_cmd.prg))
@ -301,3 +308,4 @@ mod provider {
}
}
}
}

Loading…
Cancel
Save