mj 0.4.3

My Journal - personal tool to capture ideas, work with journals, notes and tasks in your favourite text $EDITOR.
Documentation
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(),
  }
}