aboutsummaryrefslogtreecommitdiff
path: root/src/gui/question.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/question.rs')
-rw-r--r--src/gui/question.rs198
1 files changed, 198 insertions, 0 deletions
diff --git a/src/gui/question.rs b/src/gui/question.rs
new file mode 100644
index 0000000..a22b977
--- /dev/null
+++ b/src/gui/question.rs
@@ -0,0 +1,198 @@
+use crate::{
+ model::{card::Card, difficulty, difficulty::Difficulty},
+ util::event::{Event, Events},
+ util::serialization,
+};
+use anyhow::Result;
+use termion::event::Key;
+use tui::{
+ backend::Backend,
+ layout::{Alignment, Constraint, Direction, Layout, Rect},
+ style::{Color, Modifier, Style},
+ text::{Span, Spans, Text},
+ widgets::{Block, Borders, Paragraph, Wrap},
+ Terminal,
+};
+
+struct State {
+ pub input: String,
+ pub answer: Answer,
+}
+
+enum Answer {
+ Write,
+ Difficulty { difficulty: Difficulty },
+}
+
+pub fn ask<B: Backend>(
+ terminal: &mut Terminal<B>,
+ events: &Events,
+ card: &Card,
+ deck: String,
+) -> Result<Difficulty> {
+ let mut state = State {
+ input: String::new(),
+ answer: Answer::Write,
+ };
+
+ loop {
+ terminal.draw(|f| {
+ let chunks = Layout::default()
+ .direction(Direction::Vertical)
+ .margin(2)
+ .constraints(
+ [
+ Constraint::Length(1),
+ Constraint::Percentage(30),
+ Constraint::Length(5),
+ Constraint::Percentage(30),
+ Constraint::Length(5),
+ ]
+ .as_ref(),
+ )
+ .split(f.size());
+
+ let d1 = Paragraph::new(format!("{}", deck))
+ .alignment(Alignment::Center)
+ .style(
+ Style::default()
+ .fg(Color::Blue)
+ .add_modifier(Modifier::BOLD),
+ );
+ f.render_widget(d1, chunks[0]);
+
+ let question = Paragraph::new(center_vertically(chunks[1], &card.question))
+ .style(match state.answer {
+ Answer::Write => {
+ if state.input == "" {
+ Style::default().fg(Color::Yellow)
+ } else {
+ Style::default()
+ }
+ }
+ _ => Style::default(),
+ })
+ .alignment(Alignment::Center);
+ f.render_widget(question, chunks[1]);
+
+ let answer = Paragraph::new(center_vertically(chunks[2], &state.input))
+ .style(match state.answer {
+ Answer::Write => Style::default(),
+ Answer::Difficulty { difficulty: _ } => {
+ if is_correct(&state.input, &card.responses) {
+ Style::default().fg(Color::Green)
+ } else {
+ Style::default().fg(Color::Red)
+ }
+ }
+ })
+ .alignment(Alignment::Center)
+ .block(Block::default().borders(Borders::ALL).title("Réponse"))
+ .wrap(Wrap { trim: true });
+ f.render_widget(answer, chunks[2]);
+
+ match state.answer {
+ Answer::Difficulty {
+ difficulty: selected,
+ } => {
+ if !is_correct(&state.input, &card.responses) || card.responses.len() > 1 {
+ let paragraph = Paragraph::new(center_vertically(
+ chunks[3],
+ &serialization::words_to_line(&card.responses),
+ ))
+ .alignment(Alignment::Center);
+ f.render_widget(paragraph, chunks[3]);
+ };
+
+ let difficulties = card.state.difficulties();
+ let l = difficulties.len();
+ let sep = Span::styled(" • ", Style::default());
+ let tabs = difficulties
+ .iter()
+ .enumerate()
+ .map(|(i, d)| {
+ let style = if *d == selected {
+ Style::default()
+ .fg(Color::Yellow)
+ .add_modifier(Modifier::UNDERLINED)
+ } else {
+ Style::default().add_modifier(Modifier::DIM)
+ };
+ let d = Span::styled(difficulty::label(*d), style);
+ if i < l - 1 {
+ [d, sep.clone()].to_vec()
+ } else {
+ [d].to_vec()
+ }
+ })
+ .collect::<Vec<Vec<Span>>>()
+ .concat();
+ let p =
+ Paragraph::new(Text::from(Spans::from(tabs))).alignment(Alignment::Center);
+ f.render_widget(p, chunks[4]);
+ }
+ _ => {}
+ }
+ })?;
+
+ if let Event::Input(key) = events.next()? {
+ match state.answer {
+ Answer::Write => match key {
+ Key::Char('\n') => {
+ let difficulty = if is_correct(&state.input, &card.responses) {
+ Difficulty::Good
+ } else {
+ Difficulty::Again
+ };
+ state.answer = Answer::Difficulty { difficulty }
+ }
+ Key::Char(c) => {
+ state.input.push(c);
+ }
+ Key::Backspace => {
+ state.input.pop();
+ }
+ _ => {}
+ },
+ Answer::Difficulty {
+ difficulty: selected,
+ } => match key {
+ Key::Left => {
+ for d in relative_element(&card.state.difficulties(), &selected, -1).iter()
+ {
+ state.answer = Answer::Difficulty { difficulty: *d }
+ }
+ }
+ Key::Right => {
+ for d in relative_element(&card.state.difficulties(), &selected, 1).iter() {
+ state.answer = Answer::Difficulty { difficulty: *d }
+ }
+ }
+ Key::Char('\n') => return Ok(selected),
+ _ => {}
+ },
+ }
+ }
+ }
+}
+
+fn center_vertically(chunk: Rect, text: &String) -> String {
+ let text_lines = text.lines().count();
+ let chunk_inner_lines: usize = (chunk.height - 2).into();
+ let blank_lines = chunk_inner_lines - text_lines;
+ let newlines = "\n".repeat(blank_lines / 2);
+ format!("{}{}", newlines, text)
+}
+
+fn is_correct(input: &String, responses: &Vec<String>) -> bool {
+ responses.contains(input)
+}
+
+fn relative_element<T: Clone + PartialEq>(xs: &Vec<T>, x: &T, ri: i32) -> Option<T> {
+ let i = xs.iter().position(|t| t == x)? as i32 + ri;
+ if i >= 0 && i < xs.len() as i32 {
+ Some(xs[i as usize].clone())
+ } else {
+ None
+ }
+}