modcli/input/
input_builder.rs

1use rpassword::read_password;
2use std::io::{stdin, stdout, Write};
3
4/// Prompt for plain text input with optional default fallback
5pub fn prompt_text(prompt: &str, default: Option<&str>) -> String {
6    print!(
7        "{prompt}{} ",
8        default.map_or(String::new(), |d| format!(" [{d}]"))
9    );
10    stdout().flush().unwrap();
11
12    let mut input = String::new();
13    stdin().read_line(&mut input).unwrap();
14    let trimmed = input.trim();
15
16    if trimmed.is_empty() {
17        default.unwrap_or("").to_string()
18    } else {
19        trimmed.to_string()
20    }
21}
22
23/// Prompt for a yes/no confirmation
24pub fn confirm(prompt: &str, default_yes: bool) -> bool {
25    let yes_hint = if default_yes { "[Y/n]" } else { "[y/N]" };
26    print!("{prompt} {yes_hint} ");
27    stdout().flush().unwrap();
28
29    let mut input = String::new();
30    stdin().read_line(&mut input).unwrap();
31    let trimmed = input.trim().to_lowercase();
32
33    match trimmed.as_str() {
34        "y" | "yes" => true,
35        "n" | "no" => false,
36        "" => default_yes,
37        _ => default_yes, // Fallback to default if unrecognized
38    }
39}
40
41/// Prompt for a hidden password
42pub fn prompt_password(prompt: &str) -> String {
43    print!("{prompt} ");
44    stdout().flush().unwrap();
45    read_password().unwrap_or_default()
46}