diff --git a/Cargo.toml b/Cargo.toml index 36d165e..67e67b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_util" -version = "0.6.20" +version = "0.6.21" authors = ["Hatter Jiang "] edition = "2018" description = "Hatter's Rust Util" diff --git a/src/util_term.rs b/src/util_term.rs index 7839e5e..29a4a84 100644 --- a/src/util_term.rs +++ b/src/util_term.rs @@ -1,6 +1,25 @@ +use std::io; +use std::io::Write; + pub const RED: &str = "\x1B[91m"; pub const GREEN: &str = "\x1B[92m"; pub const YELLOW: &str = "\x1B[93m"; pub const BOLD: &str = "\x1B[1m"; pub const UNDER: &str = "\x1B[4m"; pub const END: &str = "\x1B[0m"; + +pub fn read_yes_no(hint: &str) -> bool { + loop { + print!("{} (Yes/No): ", hint); + io::stdout().flush().ok(); + let mut buff = String::new(); + let _ = io::stdin().read_line(&mut buff).expect("Read line from stdin"); + let buff = buff.trim().to_lowercase(); + if vec!["y", "yes"].contains(&buff.as_str()) { + return true; + } + if vec!["n", "no"].contains(&buff.as_str()) { + return false; + } + } +}