From 585f8e6336c7e109cd0550adab33879699e17f0e Mon Sep 17 00:00:00 2001 From: Elias Dalbeck Date: Thu, 5 Sep 2024 19:27:52 +0200 Subject: [PATCH 1/7] clipboard as a feature for android and server support --- cli/Cargo.toml | 3 ++- cli/src/main.rs | 41 ++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/cli/Cargo.toml b/cli/Cargo.toml index de23ec8d..fe74af49 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -39,7 +39,7 @@ color-eyre = { workspace = true } number_prefix = { workspace = true } ctrlc = { workspace = true } qr2term = { workspace = true } -arboard = { workspace = true, features = ["wayland-data-control"] } # Wayland by default, fallback to X11. +arboard = { optional = true, workspace = true, features = ["wayland-data-control"] } # Wayland by default, fallback to X11. tracing = { workspace = true, features = ["log", "log-always"] } tracing-subscriber = { workspace=true, features = ["env-filter"] } @@ -47,6 +47,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"] diff --git a/cli/src/main.rs b/cli/src/main.rs index c2f12834..ec431b21 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}; @@ -41,7 +43,6 @@ fn install_ctrlc_handler( }) }) .context("Error setting Ctrl-C handler")?; - Ok(move || { /* Transform the notification into a future that waits */ let notifier = notifier.clone(); @@ -279,11 +280,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 { @@ -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 { @@ -492,7 +484,6 @@ async fn main() -> eyre::Result<()> { false, app_config, None, - clipboard.as_mut(), ) .await?; @@ -578,7 +569,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 +613,18 @@ 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 +745,20 @@ fn sender_print_code( is_leader: false, } .to_string(); + + #[cfg(feature = "clipboard")] writeln!( term, "\nThis wormhole's code is: {} (it has been copied to your clipboard)", style(&code).bold() )?; + #[cfg(not(feature = "clipboard"))] + 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")?; From 73790ef24653241a4eda35de149aa63129f1ed5e Mon Sep 17 00:00:00 2001 From: Elias Dalbeck Date: Thu, 5 Sep 2024 19:29:15 +0200 Subject: [PATCH 2/7] cargo fmt --- cli/src/main.rs | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index ec431b21..05fe0cd2 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -280,7 +280,6 @@ async fn main() -> eyre::Result<()> { .init(); }; - match app.command { WormholeCommand::Send { common, @@ -476,16 +475,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, - ) - .await?; + let (wormhole, _code, relay_hints) = + parse_and_connect(&mut term, common, code, None, false, app_config, None).await?; let offer = forwarding::connect( wormhole, @@ -613,7 +604,8 @@ async fn parse_and_connect( /* Print code and also copy it to clipboard */ if is_send { - #[cfg(feature = "clipboard")] { + #[cfg(feature = "clipboard")] + { let clipboard = Clipboard::new() .map_err(|err| { tracing::warn!("Failed to initialize clipboard support: {}", err); @@ -753,12 +745,8 @@ fn sender_print_code( style(&code).bold() )?; #[cfg(not(feature = "clipboard"))] - writeln!( - term, - "\nThis wormhole's code is: {}", - style(&code).bold() - )?; - + 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")?; From cb72ef0c8da71b1cf63c71cf37ff0cf1d79b25e8 Mon Sep 17 00:00:00 2001 From: Elias Dalbeck Date: Thu, 5 Sep 2024 20:55:14 +0200 Subject: [PATCH 3/7] improved wormhole code display for serve and send --- cli/src/main.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cli/src/main.rs b/cli/src/main.rs index 05fe0cd2..2e91a412 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -771,7 +771,15 @@ fn server_print_code( code: &magic_wormhole::Code, _: &Option, ) -> eyre::Result<()> { + #[cfg(feature = "clipboard")] + writeln!( + term, + "\nThis wormhole's code is: {} (it has been copied to your clipboard)", + style(&code).bold() + )?; + #[cfg(not(feature = "clipboard"))] 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" From b1c792853ecd7e369d2a0a382a86189f8a94c93c Mon Sep 17 00:00:00 2001 From: Elias Dalbeck Date: Fri, 6 Sep 2024 17:25:53 +0200 Subject: [PATCH 4/7] cargo fmt --- cli/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 2e91a412..2b30071e 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -779,7 +779,7 @@ fn server_print_code( )?; #[cfg(not(feature = "clipboard"))] 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" From 39585e873357e599258545e29fc3fef6e8f069e3 Mon Sep 17 00:00:00 2001 From: Elias Dalbeck Date: Tue, 10 Sep 2024 17:59:10 +0200 Subject: [PATCH 5/7] clipboard as default feature --- cli/Cargo.toml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cli/Cargo.toml b/cli/Cargo.toml index fe74af49..1deadbf4 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -39,9 +39,11 @@ color-eyre = { workspace = true } number_prefix = { workspace = true } ctrlc = { workspace = true } qr2term = { workspace = true } -arboard = { optional = true, workspace = true, features = ["wayland-data-control"] } # Wayland by default, fallback to X11. +arboard = { optional = true, workspace = true, features = [ + "wayland-data-control", +] } # Wayland by default, fallback to X11. tracing = { workspace = true, features = ["log", "log-always"] } -tracing-subscriber = { workspace=true, features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } [dev-dependencies] trycmd = { workspace = true } @@ -56,5 +58,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"] From 4affcc9d6b9df029783b49f7ad960cde161c35a3 Mon Sep 17 00:00:00 2001 From: Elias Dalbeck Date: Tue, 10 Sep 2024 18:05:21 +0200 Subject: [PATCH 6/7] improved clipboard conditional wormhole code message --- cli/src/main.rs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 2b30071e..47cb34e8 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -738,14 +738,15 @@ fn sender_print_code( } .to_string(); - #[cfg(feature = "clipboard")] - writeln!( - term, - "\nThis wormhole's code is: {} (it has been copied to your clipboard)", - style(&code).bold() - )?; - #[cfg(not(feature = "clipboard"))] - 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, "This is equivalent to the following link: \u{001B}]8;;{}\u{001B}\\{}\u{001B}]8;;\u{001B}\\", &uri, &uri)?; let qr = @@ -771,14 +772,15 @@ fn server_print_code( code: &magic_wormhole::Code, _: &Option, ) -> eyre::Result<()> { - #[cfg(feature = "clipboard")] - writeln!( - term, - "\nThis wormhole's code is: {} (it has been copied to your clipboard)", - style(&code).bold() - )?; - #[cfg(not(feature = "clipboard"))] - 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, From 63e92c75829576ef834fa256da8ad383f0efd21b Mon Sep 17 00:00:00 2001 From: Elias Dalbeck Date: Tue, 10 Sep 2024 18:09:09 +0200 Subject: [PATCH 7/7] added whitespace for readability --- cli/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/src/main.rs b/cli/src/main.rs index 47cb34e8..ebe3ba5a 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -43,6 +43,7 @@ fn install_ctrlc_handler( }) }) .context("Error setting Ctrl-C handler")?; + Ok(move || { /* Transform the notification into a future that waits */ let notifier = notifier.clone();