Make clipboard support an optional compilation feature

This commit is contained in:
timvisee 2018-03-19 15:33:06 +01:00
parent 523cbc3a19
commit c646cf7938
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
4 changed files with 31 additions and 10 deletions

View file

@ -8,8 +8,11 @@ workspace = ".."
path = "src/main.rs" path = "src/main.rs"
name = "ffsend" name = "ffsend"
[features]
default = ["clipboard"]
[dependencies] [dependencies]
clap = "2.31" clap = "2.31"
clipboard = "0.4" clipboard = { version = "0.4", optional = true }
ffsend-api = { version = "*", path = "../api" } ffsend-api = { version = "*", path = "../api" }
open = "1" open = "1"

View file

@ -4,7 +4,9 @@ use ffsend_api::action::upload::Upload as ApiUpload;
use ffsend_api::reqwest::Client; use ffsend_api::reqwest::Client;
use cmd::cmd_upload::CmdUpload; use cmd::cmd_upload::CmdUpload;
use util::{set_clipboard, open_url}; use util::open_url;
#[cfg(feature = "clipboard")]
use util::set_clipboard;
/// A file upload action. /// A file upload action.
pub struct Upload<'a> { pub struct Upload<'a> {
@ -44,10 +46,13 @@ impl<'a> Upload<'a> {
} }
// Copy the URL in the user's clipboard // Copy the URL in the user's clipboard
#[cfg(feature = "clipboard")]
{
if self.cmd.copy() { if self.cmd.copy() {
// TODO: do not expect, but return an error // TODO: do not expect, but return an error
set_clipboard(url.as_str().to_owned()) set_clipboard(url.as_str().to_owned())
.expect("failed to put download URL in user clipboard"); .expect("failed to put download URL in user clipboard");
} }
} }
}
} }

View file

@ -13,7 +13,9 @@ pub struct CmdUpload<'a> {
impl<'a: 'b, 'b> CmdUpload<'a> { impl<'a: 'b, 'b> CmdUpload<'a> {
/// Build the sub command definition. /// Build the sub command definition.
pub fn build<'y, 'z>() -> App<'y, 'z> { pub fn build<'y, 'z>() -> App<'y, 'z> {
SubCommand::with_name("upload") // Build the subcommand
#[allow(unused_mut)]
let mut cmd = SubCommand::with_name("upload")
.about("Upload files") .about("Upload files")
.visible_alias("u") .visible_alias("u")
.visible_alias("up") .visible_alias("up")
@ -31,11 +33,17 @@ impl<'a: 'b, 'b> CmdUpload<'a> {
.arg(Arg::with_name("open") .arg(Arg::with_name("open")
.long("open") .long("open")
.short("o") .short("o")
.help("Open the share link in your browser")) .help("Open the share link in your browser"));
.arg(Arg::with_name("copy")
// Optional clipboard support
#[cfg(feature = "clipboard")] {
cmd = cmd.arg(Arg::with_name("copy")
.long("copy") .long("copy")
.short("c") .short("c")
.help("Copy the share link to your clipboard")) .help("Copy the share link to your clipboard"));
}
cmd
} }
/// Parse CLI arguments, from the given parent command matches. /// Parse CLI arguments, from the given parent command matches.
@ -85,6 +93,7 @@ impl<'a: 'b, 'b> CmdUpload<'a> {
} }
/// Check whether to copy the file URL in the user's clipboard. /// Check whether to copy the file URL in the user's clipboard.
#[cfg(feature = "clipboard")]
pub fn copy(&self) -> bool { pub fn copy(&self) -> bool {
self.matches.is_present("copy") self.matches.is_present("copy")
} }

View file

@ -1,10 +1,13 @@
#[cfg(feature = "clipboard")]
extern crate clipboard; extern crate clipboard;
extern crate open; extern crate open;
#[cfg(feature = "clipboard")]
use std::error::Error; use std::error::Error;
use std::io::Error as IoError; use std::io::Error as IoError;
use std::process::{exit, ExitStatus}; use std::process::{exit, ExitStatus};
#[cfg(feature = "clipboard")]
use self::clipboard::{ClipboardContext, ClipboardProvider}; use self::clipboard::{ClipboardContext, ClipboardProvider};
use ffsend_api::url::Url; use ffsend_api::url::Url;
@ -31,6 +34,7 @@ pub fn open_path(path: &str) -> Result<ExitStatus, IoError> {
} }
/// Set the clipboard of the user to the given `content` string. /// Set the clipboard of the user to the given `content` string.
#[cfg(feature = "clipboard")]
pub fn set_clipboard(content: String) -> Result<(), Box<Error>> { pub fn set_clipboard(content: String) -> Result<(), Box<Error>> {
let mut context: ClipboardContext = ClipboardProvider::new()?; let mut context: ClipboardContext = ClipboardProvider::new()?;
context.set_contents(content) context.set_contents(content)