From c7415478a829162c7fc0382f5675d83e4d156cf5 Mon Sep 17 00:00:00 2001 From: Leonard Steppy Date: Wed, 15 Jan 2025 13:38:56 +0100 Subject: [PATCH] Add input macro similar to pythons input function and replace previous usages of input acquisition --- src/main.rs | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7bb2d88..2fd604a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,6 +119,22 @@ enum OldVersionPolicy { Delete, } +#[macro_export] +macro_rules! input { + ($prompt: expr) => {{ + print!("{}", $prompt); + io::stdout().flush().expect("failed to flush stdout"); + let mut buf = String::new(); + io::stdin() + .read_line(&mut buf) + .expect("failed to read stdin"); + buf.trim().to_string() + }}; + () => { + input!() + }; +} + fn main() -> Result<(), String> { let args = Args::parse(); @@ -250,13 +266,7 @@ fn main() -> Result<(), String> { } if !no_confirm { - log!(logger, "Continue? [Y|n] "); - std::io::stdout().flush().expect("failed to flush stdout"); - let mut buffer = String::new(); - std::io::stdin() - .read_line(&mut buffer) - .expect("failed to read stdin"); - match buffer.to_lowercase().trim() { + match input!("Continue? [Y|n]").to_lowercase().as_str() { "n" | "no" => { log!(logger, "Aborting..."); return Ok(()); @@ -343,13 +353,16 @@ fn main() -> Result<(), String> { .any(|entry| entry.file_name() == file_name) { //TODO ask user whether they want to override, unless silent flag is set - let duplication_notification = format!("A file with the name {} already exists in {}", file_name.to_string_lossy(), working_directory.to_string_lossy()); - + let duplication_notification = format!( + "A file with the name {} already exists in {}", + file_name.to_string_lossy(), + working_directory.to_string_lossy() + ); + if !args.quiet { print!("{duplication_notification}. Do you want to replace it? [N|y]"); - } - + return Err(format!( "A file with the name {} already exists in {}. You can override it with --override or -f", file_name.to_string_lossy(), @@ -397,14 +410,6 @@ fn main() -> Result<(), String> { Ok(()) } -fn get_user_input(prompt: S) -> String where S: Into { - print!("{}", prompt.into()); - io::stdout().flush().expect("failed to flush stdout"); - let mut buf = String::new(); - io::stdin().read_line(&mut buf).expect("failed to read stdin"); - buf -} - fn require_non_empty_servers(servers: &[Server]) -> Result<(), String> { if servers.is_empty() { Err("You did not provide any servers for this operation. Please see --help".to_string())