use std::{
    fmt::Display,
    io::{self, Write, stdin, stdout},
};

pub fn read_line(prompt: &str) -> io::Result<String> {
    print!("{} ", prompt);
    stdout().flush()?;

    let mut input = String::new();
    stdin().read_line(&mut input).unwrap();

    Ok(input.trim().to_string())
}

pub fn read_boolean(question: &str) -> io::Result<bool> {
    let line = read_line(&format!("{} [y/N]", question))?;
    return Ok(line.to_lowercase().starts_with('y'));
}

pub fn print_items<I, D>(items: I)
where
    I: IntoIterator<Item = D>,
    D: Display,
{
    let mut iter = items.into_iter().peekable();

    while let Some(item) = iter.next() {
        if iter.peek().is_some() {
            print!("{} ", item);
        } else {
            print!("{}", item);
        }
    }

    println!();
}
