1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::{
    error::Error,
    io::{self, Write},
    str::FromStr,
};

pub struct ZenConsoleInput {
    prompt: Option<String>,
}

impl ZenConsoleInput {
    pub fn new() -> Self {
        Self { prompt: None }
    }

    /// Prompt a message to the user
    pub fn prompt(&self, prompt: &str) -> Self {
        Self {
            prompt: Some(prompt.to_string()),
        }
    }

    fn prompt_and_read_line(&self) -> String {
        if let Some(p) = &self.prompt {
            print!("{p}");
            io::stdout().flush().unwrap();
        }
        let mut input = String::new();
        io::stdin().read_line(&mut input).unwrap();
        input.trim().to_string()
    }

    /// Read single value
    pub fn read_value<T, E>(&self) -> T
    where
        T: FromStr<Err = E>,
        E: Error + 'static,
    {
        match self.prompt_and_read_line().parse() {
            Ok(it) => it,
            Err(err) => {
                eprintln!("{err}");
                return self.read_value();
            }
        }
    }

    /// Read multiple inputs separated by whitespaces
    pub fn read_vector<T, E>(&self) -> Vec<T>
    where
        T: FromStr<Err = E>,
        E: Error + 'static,
    {
        self.prompt_and_read_line()
            .split_whitespace()
            .map(|x| match x.parse() {
                Ok(it) => Ok(it),
                Err(err) => {
                    eprintln!("{err}");
                    Err(err)
                }
            })
            .collect::<Result<Vec<_>, _>>()
            .unwrap_or_else(|_| self.read_vector())
    }
}