From 3b1847d8820d73c1f80defadbbcd5acc2a999c48 Mon Sep 17 00:00:00 2001 From: John Doty Date: Wed, 31 Jul 2024 15:06:20 -0700 Subject: [PATCH] Require a file name for 'clip', use '-' for stdin This makes argument parsing more reliable: to `fwd` to a server named `clip` just leave off the file name. --- src/main.rs | 18 ++++++++++-------- src/reverse.rs | 18 +++++++----------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9988c13..6bd877e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ connect to. On a server that already has a client connected to it you can use `fwd browse ` to open `` in the default browser of the client. -On a server that already has a client connected to it you can use `fwd clip` +On a server that already has a client connected to it you can use `fwd clip -` to read stdin and send it to the clipboard of the client, or `fwd clip ` to send the the contents of `file`. @@ -36,7 +36,7 @@ enum Args { Server, Client(String, bool), Browse(String), - Clip(Option), + Clip(String), Error, } @@ -77,9 +77,9 @@ fn parse_args(args: Vec) -> Args { } } else if rest[0] == "clip" { if rest.len() == 1 { - Args::Clip(None) + Args::Client(rest[0].to_string(), sudo.unwrap_or(false)) } else if rest.len() == 2 { - Args::Clip(Some(rest[1].to_string())) + Args::Clip(rest[1].to_string()) } else { Args::Error } @@ -98,8 +98,8 @@ async fn browse_url(url: &str) { } } -async fn clip_file(file: Option) { - if let Err(e) = fwd::clip_file(file.as_deref()).await { +async fn clip_file(file: String) { + if let Err(e) = fwd::clip_file(&file).await { eprintln!("Unable to copy to the clipboard"); eprintln!("{}", e); std::process::exit(1); @@ -177,9 +177,11 @@ mod tests { assert_arg_parse!(&["foo.com"], Args::Client(_, false)); assert_arg_parse!(&["a"], Args::Client(_, false)); assert_arg_parse!(&["browse"], Args::Client(_, false)); + assert_arg_parse!(&["clip"], Args::Client(_, false)); assert_arg_parse!(&["foo.com", "--sudo"], Args::Client(_, true)); assert_arg_parse!(&["a", "-s"], Args::Client(_, true)); assert_arg_parse!(&["-s", "browse"], Args::Client(_, true)); + assert_arg_parse!(&["-s", "clip"], Args::Client(_, true)); } #[test] @@ -189,8 +191,8 @@ mod tests { #[test] fn clip() { - assert_arg_parse!(&["clip"], Args::Clip(None)); - assert_arg_parse!(&["clip", "garbage"], Args::Clip(Some(_))); + assert_arg_parse!(&["clip", "garbage"], Args::Clip(_)); + assert_arg_parse!(&["clip", "-"], Args::Clip(_)); } #[test] diff --git a/src/reverse.rs b/src/reverse.rs index 8e0b8ed..76a89d4 100644 --- a/src/reverse.rs +++ b/src/reverse.rs @@ -59,17 +59,13 @@ async fn clip_reader(reader: &mut T) -> Result<()> { } #[inline] -pub async fn clip_file(file: Option<&str>) -> Result<()> { - // send_reverse_message(Message::Browse(url.to_string())).await - match file { - Some(path) => { - let mut file = tokio::fs::File::open(path).await?; - clip_reader(&mut file).await?; - } - None => { - let mut stdin = tokio::io::stdin(); - clip_reader(&mut stdin).await?; - } +pub async fn clip_file(file: &str) -> Result<()> { + if file == "-" { + let mut stdin = tokio::io::stdin(); + clip_reader(&mut stdin).await?; + } else { + let mut file = tokio::fs::File::open(file).await?; + clip_reader(&mut file).await?; } Ok(())