From 8f9ae82e6a6635db7e617d03f58321816ea46cde Mon Sep 17 00:00:00 2001 From: Leonard Steppy Date: Wed, 8 Jan 2025 18:34:59 +0100 Subject: [PATCH 1/4] [WIP] add input function and ask user whether he wants to override files --- src/main.rs | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index b9918a4..7bb2d88 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use std::io::Write; use std::iter::once; use std::path::PathBuf; use std::str::FromStr; -use std::{env, fs}; +use std::{env, fs, io}; const SERVERS_ENV_VAR: &str = "MSSH_SERVERS"; const EDITOR_ENV_VAR: &str = "MSSH_EDITOR"; @@ -333,19 +333,29 @@ fn main() -> Result<(), String> { let file_name = file .file_name() .ok_or("can only edit files, not directories")?; - if !override_existing - && fs::read_dir(&working_directory) - .map_err(|e| format!("failed to open working directory: {e}"))? - .collect::, _>>() - .map_err(|e| format!("error while querying working directory contents: {e}"))? - .iter() - .any(|entry| entry.file_name() == file_name) - { - return Err(format!( + 'duplicate_check: { + if !override_existing + && fs::read_dir(&working_directory) + .map_err(|e| format!("failed to open working directory: {e}"))? + .collect::, _>>() + .map_err(|e| format!("error while querying working directory contents: {e}"))? + .iter() + .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()); + + 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(), working_directory.to_string_lossy() )); + } } require_non_empty_servers(&servers)?; @@ -387,6 +397,14 @@ 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()) From c7415478a829162c7fc0382f5675d83e4d156cf5 Mon Sep 17 00:00:00 2001 From: Leonard Steppy Date: Wed, 15 Jan 2025 13:38:56 +0100 Subject: [PATCH 2/4] 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()) From 0829590486c6e52c5516796e17ad673215cec5f8 Mon Sep 17 00:00:00 2001 From: Leonard Steppy Date: Wed, 15 Jan 2025 13:45:49 +0100 Subject: [PATCH 3/4] Add confirmation dialoge when file already exists in download directory --- src/main.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2fd604a..07e7e80 100644 --- a/src/main.rs +++ b/src/main.rs @@ -121,8 +121,8 @@ enum OldVersionPolicy { #[macro_export] macro_rules! input { - ($prompt: expr) => {{ - print!("{}", $prompt); + ($prompt: tt) => {{ + print!($prompt); io::stdout().flush().expect("failed to flush stdout"); let mut buf = String::new(); io::stdin() @@ -352,7 +352,6 @@ fn main() -> Result<(), String> { .iter() .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(), @@ -360,14 +359,18 @@ fn main() -> Result<(), String> { ); if !args.quiet { - print!("{duplication_notification}. Do you want to replace it? [N|y]"); + match input!("{duplication_notification}. Do you want to replace it? [N|y]") + .to_lowercase() + .as_str() + { + "y" | "yes" => break 'duplicate_check, + _ => {} + } } return Err(format!( - "A file with the name {} already exists in {}. You can override it with --override or -f", - file_name.to_string_lossy(), - working_directory.to_string_lossy() - )); + "{duplication_notification}. You can override it with --override or -f" + )); } } From 25286daea9d739b7ffcc8b1f40d48dace8b74ac1 Mon Sep 17 00:00:00 2001 From: Leonard Steppy Date: Wed, 15 Jan 2025 18:18:01 +0100 Subject: [PATCH 4/4] Add spaces to confirm prompts --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 07e7e80..56066c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -266,7 +266,7 @@ fn main() -> Result<(), String> { } if !no_confirm { - match input!("Continue? [Y|n]").to_lowercase().as_str() { + match input!("Continue? [Y|n] ").to_lowercase().as_str() { "n" | "no" => { log!(logger, "Aborting..."); return Ok(()); @@ -359,7 +359,7 @@ fn main() -> Result<(), String> { ); if !args.quiet { - match input!("{duplication_notification}. Do you want to replace it? [N|y]") + match input!("{duplication_notification}. Do you want to replace it? [N|y] ") .to_lowercase() .as_str() {