packager/pkgr/src/util/prompts.rs

33 lines
No EOL
866 B
Rust

use std::io::Write;
use log::trace;
pub fn is_noninteractive() -> bool {
if let Ok(v) = std::env::var("PKGR_NON_INTERACTIVE") {
trace!("PKGR_NON_INTERACTIVE={}", v);
match v.as_str() {
"1" => true,
"true" => true,
_ => false
}
} else { false }
}
pub fn prompt_bool<S: Into<String>>(prompt: S, default: bool) -> bool {
if is_noninteractive() { return default; }
print!("{} [{}]: ", prompt.into(), if default { "Y/n" } else { "y/N" });
match std::io::stdout().flush() {
Ok(_) => (),
_ => println!()
};
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read input.");
let answer = input.trim().to_lowercase();
if answer.is_empty() {
default
} else {
answer == "y" || answer == "yes"
}
}