Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

WIP #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

WIP #23

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 69 additions & 45 deletions src/learn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use file::read_cards;
use prompt::{self, Command, CmdOption};
use qa::Qa;
use std::io;

#[derive(PartialEq, Clone)]
enum UserAction {
Expand All @@ -27,44 +28,78 @@ enum UserAction {
}

#[derive(Clone)]
struct LoopOption {
letter: char,
doc: String,
action: UserAction,
struct YesQuit {
Yes,
Quit
}

struct AssessmentOption {
q: u8,
doc: &'static str,
}


static YES_NO: [LoopOption; ]

static ASSESSMENTS: [AssessmentOption; 6] = [
AssessmentOption {
q: 0,
doc: "complete blackout"
},
AssessmentOption {
q: 1,
doc: "incorrect response; the correct one remembered"
},
AssessmentOption {
q: 2,
doc: "incorrect response; where the correct one seemed easy to recall"
},
AssessmentOption {
q: 3,
doc: "correct response recalled with serious difficulty"
},
AssessmentOption {
q: 4,
doc: "correct response after a hesitation"
},
AssessmentOption {
q: 5,
doc: "perfect response"
},
];


let yes = LoopOption {
letter:'y',
doc: "yes".to_string(),
action: UserAction::Continue,
};
let quit = LoopOption {
letter: 'q',
doc: "quit".to_string(),
action: UserAction::Quit,
};
let options = vec![yes, quit];



impl CmdOption for LoopOption {
fn letter(&self) -> char {
self.letter
match self {
LoopOption::Yes => 'y',
LoopOption::Quit => 'q',
}
}

fn doc(&self) -> &str {
&self.doc
match self {
LoopOption::Yes => "yes",
LoopOption::Quit => "quit",
}
}
}

struct AssessmentOption {
q: u8,
doc: &'static str,
}

lazy_static! {
static ref ASSESSMENTS: Vec<AssessmentOption> = {
vec![
AssessmentOption {q: 0, doc: "complete blackout"},
AssessmentOption {q: 1, doc: "incorrect response; the correct one \
remembered"},
AssessmentOption {q: 2, doc: "incorrect response; where the \
correct one seemed easy to recall"},
AssessmentOption {q: 3, doc: "correct response recalled with \
serious difficulty"},
AssessmentOption {q: 4, doc: "correct response after a \
hesitation"},
AssessmentOption {q: 5, doc: "perfect response"},
]
};
}

impl CmdOption for AssessmentOption {
fn letter(&self) -> char {
Expand Down Expand Up @@ -109,34 +144,23 @@ fn read_option(command: &Command<LoopOption>) -> UserAction {
}

fn show_card(qa: &mut Qa) -> UserAction {
let yes = LoopOption {
letter:'y',
doc: "yes".to_string(),
action: UserAction::Continue,
};
let quit = LoopOption {
letter: 'q',
doc: "quit".to_string(),
action: UserAction::Quit,
};
let options = vec![yes, quit];

{
let card = qa.current_card();
println!("Q: {}", card.question());
let command = Command::new("Show answer", &options);
if read_option(&command) == UserAction::Quit {
return UserAction::Quit;
{
// show answer after user presses RET
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
}

println!("A: {}", card.answer());
}

let command = Command::new("How difficult was it", &*ASSESSMENTS);

let command = Command::new("How difficult was it", &ASSESSMENTS, None);
let q = prompt::prompt(&command).expect("Invalid option.").q;
qa.assess_current(q);

let command = Command::new("Continue with another card", &options);
let command = Command::new("Continue with another card", &options, Some(0));
read_option(&command)
}

Expand All @@ -159,7 +183,7 @@ fn ask_for_more(qa: &Qa) -> UserAction {

let options = vec![yes, quit];
let command = Command::new("No more items planned for today, add more",
&options);
&options, None);
return read_option(&command);
}

16 changes: 14 additions & 2 deletions src/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,32 @@ pub trait CmdOption {
pub struct Command<'a, T> where T: 'a + CmdOption {
question: &'a str,
options: &'a Vec<T>,
default: Option<usize>,
}

impl<'a, T> Command<'a, T> where T: CmdOption {
/// # Panics
///
/// When list of options is empty.
pub fn new(question: &'a str, options: &'a Vec<T>) -> Self {
/// * When list of options is empty.
/// * When `default`
pub fn new(
question: &'a str,
options: &'a Vec<T>,
default: Option<usize>
) -> Self {
if options.is_empty() {
panic!("Got empty list of options.");
}
if let Some(index) = default {
if index > options.len() {
panic!("Default is not contained in list of options.");
}
}

Command {
question: question,
options: options,
default: default,
}
}

Expand Down