use std::io::{self, BufRead, Write};
fn prompt<R>(prompt_text: &str, mut reader: R) -> String
where
R: BufRead,
{
print!("{}", prompt_text);
io::stdout().flush().expect("Failed to flush stdout.");
let mut input = String::new();
reader.read_line(&mut input).expect("Failed to read input.");
input.trim().to_lowercase()
}
pub fn prompt_stdin(prompt_text: &str) -> String {
prompt(prompt_text, io::stdin().lock())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_prompt() {
let prompt_text = "Enter some text: ";
let expected = "hello";
let reader = std::io::Cursor::new(format!("{}\n", expected));
let actual = prompt(prompt_text, reader);
assert_eq!(actual, expected);
}
}