/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use std::io::{self, Write}; pub fn prompt_string>(prompt: S) -> Option { print!("{}: ", prompt.as_ref()); let _ = io::stdout().flush(); // Don't care if flush fails really. let mut s = String::new(); io::stdin() .read_line(&mut s) .expect("Failed to read line..."); if let Some('\n') = s.chars().next_back() { s.pop(); } if let Some('\r') = s.chars().next_back() { s.pop(); } if s.is_empty() { None } else { Some(s) } } pub fn prompt_password>(prompt: S) -> Option { let result = dialoguer::Password::new() .with_prompt(prompt.as_ref().to_string()) .interact(); match result { Ok(p) => Some(p), Err(e) => { log::warn!("Error getting password: {e}"); None } } } pub fn prompt_char(msg: &str) -> Option { prompt_string(msg).and_then(|r| r.chars().next()) } pub fn prompt_usize>(prompt: S) -> Option { if let Some(s) = prompt_string(prompt) { match s.parse::() { Ok(n) => Some(n), Err(_) => { println!("Couldn't parse!"); None } } } else { None } }