From a0da78c885afd9023f06430dfdc0574d3bdf76f8 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Mon, 16 Sep 2024 11:57:24 +0000 Subject: [PATCH] prompt fn --- src/main.rs | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index f2b7aac..4bed79a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,6 +90,25 @@ fn list() -> io:: Result<()> { Ok(()) } +enum PROMPT { + YES, + NO +} + +fn get_prompt(question: &str) -> io::Result { + print!("{} [Y/n] ", question); + io::stdout().flush()?; + let mut input = String::new(); + io::stdin().read_line(&mut input)?; + let input = input.trim().to_lowercase(); + match input.as_str() { + "y" => Ok(PROMPT::YES), + "n" => Ok(PROMPT::NO), + _ => Ok(PROMPT::YES), + } + +} + fn main() -> io::Result<()> { let cli = Cli::parse(); match &cli.command { @@ -104,23 +123,15 @@ fn main() -> io::Result<()> { } None => { if !is_inited() { - print!("Do you want to init your storage? [Y/n] "); - io::stdout().flush().expect("Failed to flush stdout"); - let mut input = String::new(); - io::stdin() - .read_line(&mut input) - .expect("Failed to read answer"); - let input = input.trim().to_lowercase(); - match input.as_str() { - "y" => init()?, - "n" => { + match get_prompt("Do you want to init your storage?")? { + PROMPT::YES => init()?, + PROMPT::NO => { println!("mps can work only when storage inited."); println!("Hint: you cant do"); println!(" git clone {}", STORAGE_FOLDER); println!("to init manually your storage and config") - }, - _ => init()?, - } + }, + } } else { println!("login, not implemented yet") }