modcli/input/
input_builder.rs

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