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
use ansi_term::Colour::{Red, Yellow};
use crate::core::*;
use std::io::{self, Write};

#[derive(Debug)]
pub enum YesNo {
  Yes,
  No,
}

pub fn prompt_yes_no(question: &str, default: YesNo) -> Result<YesNo> {
  loop {
    print!(
      "{} [{}] ",
      Yellow.paint(question.to_owned()),
      Red.paint(format_yes_no(&default)),
    );
    io::stdout().flush()?;
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    input.make_ascii_lowercase();
    let answer = input.trim().to_owned();
    if answer == "y" {
      return Ok(YesNo::Yes);
    }
    if answer == "n" {
      return Ok(YesNo::No);
    }
    if answer == "" {
      return Ok(default)
    }
  }
}

fn format_yes_no(value: &YesNo) -> String {
  match value {
    YesNo::Yes => "Y/n".to_owned(),
    YesNo::No => "y/N".to_owned(),
  }
}