mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-27 07:30:05 +08:00
feat: add read_yes_no
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "rust_util"
|
||||
version = "0.6.20"
|
||||
version = "0.6.21"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
description = "Hatter's Rust Util"
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user