Add input macro similar to pythons input function and replace previous usages of input acquisition

This commit is contained in:
Leonard Steppy 2025-01-15 13:38:56 +01:00
parent 8f9ae82e6a
commit c7415478a8

View File

@ -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<S>(prompt: S) -> String where S: Into<String> {
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())