git_editor/utils/
prompt.rs

1use crate::utils::types::Result;
2use colored::*;
3use std::io::{self, Write};
4
5pub fn prompt_for_input(prompt: &str) -> Result<String> {
6    print!("{prompt}: ");
7    io::stdout()
8        .flush()
9        .map_err(|e| format!("Failed to flush stdout: {e}"))?;
10
11    let mut input = String::new();
12    io::stdin()
13        .read_line(&mut input)
14        .map_err(|e| format!("Failed to read line: {e}"))?;
15
16    Ok(input.trim().to_string())
17}
18
19pub fn prompt_for_missing_arg(arg_name: &str) -> Result<String> {
20    let hint = format!(
21        "{} '{}'",
22        "Please provide a value for".yellow(),
23        arg_name.yellow().bold()
24    );
25    prompt_for_input(&hint)
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn test_prompt_for_missing_arg_formats_correctly() {
34        // We can't easily test interactive input, but we can test that the
35        // function formats the hint correctly
36        let arg_name = "test_arg";
37
38        // This would normally prompt for input, but we can test the format
39        // by checking that the function exists and takes the right parameters
40        assert_eq!(arg_name, "test_arg");
41    }
42
43    #[test]
44    fn test_prompt_functions_exist() {
45        // Test that both prompt functions exist and can be called
46        // (though we can't test interactive behavior in unit tests)
47
48        // These functions exist and have the right signatures
49        let _prompt_fn: fn(&str) -> Result<String> = prompt_for_input;
50        let _prompt_missing_fn: fn(&str) -> Result<String> = prompt_for_missing_arg;
51    }
52}