Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clipboard as an optional feature #157 #259

Merged
merged 8 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -48,6 +48,7 @@ tracing-subscriber = { workspace = true, features = ["env-filter"] }
trycmd = { workspace = true }

[features]
clipboard = ["dep:arboard"]
kernelPanic0x marked this conversation as resolved.
Show resolved Hide resolved
# TLS implementations for websocket connections via async-tungstenite
# required for optional wss connection to the mailbox server
tls = ["magic-wormhole/tls"]
Expand All @@ -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"]
72 changes: 39 additions & 33 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<impl Fn() -> futures::future::BoxFuture<'static, ()> + Clone> {
use async_std::sync::{Condvar, Mutex};
Expand Down Expand Up @@ -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,
Expand All @@ -304,7 +300,6 @@ async fn main() -> eyre::Result<()> {
true,
transfer::APP_CONFIG,
Some(&sender_print_code),
clipboard.as_mut(),
)),
ctrl_c(),
)
Expand Down Expand Up @@ -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?,
Expand Down Expand Up @@ -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?,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -578,7 +561,6 @@ async fn parse_and_connect(
is_send: bool,
mut app_config: magic_wormhole::AppConfig<impl serde::Serialize + Send + Sync + 'static>,
print_code: Option<&PrintCodeFn>,
clipboard: Option<&mut Clipboard>,
) -> eyre::Result<(Wormhole, magic_wormhole::Code, Vec<transit::RelayHint>)> {
// TODO handle relay servers with multiple endpoints better
let mut relay_hints: Vec<transit::RelayHint> = common_args
Expand Down Expand Up @@ -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),
}
}
}

Expand Down Expand Up @@ -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")?;
Expand All @@ -776,7 +773,16 @@ fn server_print_code(
code: &magic_wormhole::Code,
_: &Option<url::Url>,
) -> 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"
Expand Down