git_editor/utils/
prompt.rs1use 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 let arg_name = "test_arg";
37
38 assert_eq!(arg_name, "test_arg");
41 }
42
43 #[test]
44 fn test_prompt_functions_exist() {
45 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}