From 040e4229dc4ecb62609e7674c7de41be105c0738 Mon Sep 17 00:00:00 2001 From: kernelPanic0x <66633224+kernelPanic0x@users.noreply.github.com> Date: Fri, 13 Sep 2024 01:55:10 +0200 Subject: [PATCH] cli: Clipboard as an optional feature (#259) cli: Clipboard as an optional feature For android and server support --- cli/Cargo.toml | 5 ++-- cli/src/main.rs | 72 ++++++++++++++++++++++++++----------------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/cli/Cargo.toml b/cli/Cargo.toml index b85ec191..2bb5ea74 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -38,7 +38,7 @@ color-eyre = { workspace = true } number_prefix = { workspace = true } ctrlc = { workspace = true } qr2term = { workspace = true } -arboard = { workspace = true, features = [ +arboard = { optional = true, workspace = true, features = [ "wayland-data-control", ] } # Wayland by default, fallback to X11. tracing = { workspace = true, features = ["log", "log-always"] } @@ -48,6 +48,7 @@ tracing-subscriber = { workspace = true, features = ["env-filter"] } trycmd = { workspace = true } [features] +clipboard = ["dep:arboard"] # TLS implementations for websocket connections via async-tungstenite # required for optional wss connection to the mailbox server tls = ["magic-wormhole/tls"] @@ -56,5 +57,5 @@ native-tls = ["magic-wormhole/native-tls"] experimental-transfer-v2 = ["magic-wormhole/experimental-transfer-v2"] experimental = ["experimental-transfer-v2"] -default = ["magic-wormhole/default", "magic-wormhole/forwarding"] +default = ["clipboard", "magic-wormhole/default", "magic-wormhole/forwarding"] all = ["default", "magic-wormhole/native-tls"] diff --git a/cli/src/main.rs b/cli/src/main.rs index c2f12834..ebe3ba5a 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -3,7 +3,6 @@ mod util; use std::time::{Duration, Instant}; -use arboard::Clipboard; use async_std::sync::Arc; use clap::{Args, CommandFactory, Parser, Subcommand}; use color_eyre::{eyre, eyre::Context}; @@ -18,6 +17,9 @@ use magic_wormhole::{ use std::{io::Write, path::PathBuf}; use tracing_subscriber::EnvFilter; +#[cfg(feature = "clipboard")] +use arboard::Clipboard; + fn install_ctrlc_handler( ) -> eyre::Result futures::future::BoxFuture<'static, ()> + Clone> { use async_std::sync::{Condvar, Mutex}; @@ -279,12 +281,6 @@ async fn main() -> eyre::Result<()> { .init(); }; - let mut clipboard = Clipboard::new() - .map_err(|err| { - tracing::warn!("Failed to initialize clipboard support: {}", err); - }) - .ok(); - match app.command { WormholeCommand::Send { common, @@ -304,7 +300,6 @@ async fn main() -> eyre::Result<()> { true, transfer::APP_CONFIG, Some(&sender_print_code), - clipboard.as_mut(), )), ctrl_c(), ) @@ -342,7 +337,6 @@ async fn main() -> eyre::Result<()> { true, transfer::APP_CONFIG, Some(&sender_print_code), - clipboard.as_mut(), )); match futures::future::select(connect_fut, ctrl_c()).await { Either::Left((result, _)) => result?, @@ -382,7 +376,6 @@ async fn main() -> eyre::Result<()> { false, transfer::APP_CONFIG, None, - clipboard.as_mut(), )); match futures::future::select(connect_fut, ctrl_c()).await { Either::Left((result, _)) => result?, @@ -456,7 +449,6 @@ async fn main() -> eyre::Result<()> { true, app_config, Some(&server_print_code), - clipboard.as_mut(), )); let (wormhole, _code, relay_hints) = match futures::future::select(connect_fut, ctrl_c()).await { @@ -484,17 +476,8 @@ async fn main() -> eyre::Result<()> { tracing::warn!("This is an unstable feature. Make sure that your peer is running the exact same version of the program as you. Also, please report all bugs and crashes."); let mut app_config = forwarding::APP_CONFIG; app_config.app_version.transit_abilities = parse_transit_args(&common); - let (wormhole, _code, relay_hints) = parse_and_connect( - &mut term, - common, - code, - None, - false, - app_config, - None, - clipboard.as_mut(), - ) - .await?; + let (wormhole, _code, relay_hints) = + parse_and_connect(&mut term, common, code, None, false, app_config, None).await?; let offer = forwarding::connect( wormhole, @@ -578,7 +561,6 @@ async fn parse_and_connect( is_send: bool, mut app_config: magic_wormhole::AppConfig, print_code: Option<&PrintCodeFn>, - clipboard: Option<&mut Clipboard>, ) -> eyre::Result<(Wormhole, magic_wormhole::Code, Vec)> { // TODO handle relay servers with multiple endpoints better let mut relay_hints: Vec = common_args @@ -623,10 +605,19 @@ async fn parse_and_connect( /* Print code and also copy it to clipboard */ if is_send { - if let Some(clipboard) = clipboard { - match clipboard.set_text(mailbox_connection.code().to_string()) { - Ok(()) => tracing::info!("Code copied to clipboard"), - Err(err) => tracing::warn!("Failed to copy code to clipboard: {}", err), + #[cfg(feature = "clipboard")] + { + let clipboard = Clipboard::new() + .map_err(|err| { + tracing::warn!("Failed to initialize clipboard support: {}", err); + }) + .ok(); + + if let Some(mut clipboard) = clipboard { + match clipboard.set_text(mailbox_connection.code().to_string()) { + Ok(()) => tracing::info!("Code copied to clipboard"), + Err(err) => tracing::warn!("Failed to copy code to clipboard: {}", err), + } } } @@ -747,11 +738,17 @@ fn sender_print_code( is_leader: false, } .to_string(); - writeln!( - term, - "\nThis wormhole's code is: {} (it has been copied to your clipboard)", - style(&code).bold() - )?; + + if cfg!(feature = "clipboard") { + writeln!( + term, + "\nThis wormhole's code is: {} (it has been copied to your clipboard)", + style(&code).bold() + )?; + } else { + writeln!(term, "\nThis wormhole's code is: {}", style(&code).bold())?; + } + writeln!(term, "This is equivalent to the following link: \u{001B}]8;;{}\u{001B}\\{}\u{001B}]8;;\u{001B}\\", &uri, &uri)?; let qr = qr2term::generate_qr_string(&uri).context("Failed to generate QR code for send link")?; @@ -776,7 +773,16 @@ fn server_print_code( code: &magic_wormhole::Code, _: &Option, ) -> eyre::Result<()> { - writeln!(term, "\nThis wormhole's code is: {}", style(&code).bold())?; + if cfg!(feature = "clipboard") { + writeln!( + term, + "\nThis wormhole's code is: {} (it has been copied to your clipboard)", + style(&code).bold() + )?; + } else { + writeln!(term, "\nThis wormhole's code is: {}", style(&code).bold())?; + } + writeln!( term, "On the other side, enter that code into a Magic Wormhole client\n"