aboutsummaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
authorJoris2022-02-12 16:57:19 +0100
committerJoris2022-02-12 16:57:19 +0100
commit3adf3f9697c4e2beb10e652947046d5fddda2ed4 (patch)
tree689e704cf4665cc4cd1949c06d4a7b0553d07501 /src/gui
parenta2ba205df12332a360d89991fe508f964d88c73f (diff)
Say when the next card will be available
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/gui.rs24
-rw-r--r--src/gui/message.rs41
-rw-r--r--src/gui/mod.rs2
-rw-r--r--src/gui/question.rs27
-rw-r--r--src/gui/util.rs21
5 files changed, 89 insertions, 26 deletions
diff --git a/src/gui/gui.rs b/src/gui/gui.rs
index fdf686b..88333bb 100644
--- a/src/gui/gui.rs
+++ b/src/gui/gui.rs
@@ -1,11 +1,12 @@
-use crate::{db::db, gui::question, space_repetition, util::event::Events};
+use crate::util::time;
+use crate::{db::db, gui::message, gui::question, space_repetition, util::event::Events};
use anyhow::Result;
use rusqlite::Connection;
use std::io;
use termion::{raw::IntoRawMode, screen::AlternateScreen};
use tui::{backend::TermionBackend, Terminal};
-pub fn start(conn: &Connection) -> Result<()> {
+pub fn start(conn: &Connection, deck_name: &String) -> Result<()> {
let stdout = io::stdout().into_raw_mode()?;
let stdout = AlternateScreen::from(stdout);
let backend = TermionBackend::new(stdout);
@@ -14,17 +15,28 @@ pub fn start(conn: &Connection) -> Result<()> {
let events = Events::new();
loop {
- match db::pick_ready(&conn) {
- Some(card) => {
- let difficulty =
- question::ask(&mut terminal, &events, &card, "German".to_string())?;
+ let now = time::now()?;
+
+ match db::next_ready(&conn) {
+ Some(card) if card.ready <= now => {
+ let difficulty = question::ask(&mut terminal, &events, &card, deck_name)?;
db::update(
&conn,
&card.question,
&space_repetition::update(card.state, difficulty),
)?;
}
+ Some(card) => {
+ let message = format!(
+ "Prochaine carte disponible dans {}.",
+ time::pp_duration(card.ready - now)
+ );
+ let _ = message::show(&mut terminal, &events, &message, deck_name);
+ break;
+ }
None => {
+ let message = format!("Aucune carte n’est disponible. Votre deck est-il vide ?");
+ let _ = message::show(&mut terminal, &events, &message, deck_name);
break;
}
}
diff --git a/src/gui/message.rs b/src/gui/message.rs
new file mode 100644
index 0000000..158416e
--- /dev/null
+++ b/src/gui/message.rs
@@ -0,0 +1,41 @@
+use crate::gui::util;
+use crate::util::event::{Event, Events};
+use anyhow::Result;
+use termion::event::Key;
+use tui::{
+ backend::Backend,
+ layout::{Alignment, Constraint, Direction, Layout},
+ widgets::Paragraph,
+ Terminal,
+};
+
+pub fn show<B: Backend>(
+ terminal: &mut Terminal<B>,
+ events: &Events,
+ message: &String,
+ deck_name: &String,
+) -> Result<()> {
+ loop {
+ terminal.draw(|f| {
+ let chunks = Layout::default()
+ .direction(Direction::Vertical)
+ .margin(2)
+ .constraints([Constraint::Length(1), Constraint::Percentage(50)].as_ref())
+ .split(f.size());
+
+ let d1 = util::title(deck_name);
+ f.render_widget(d1, chunks[0]);
+
+ let message = Paragraph::new(util::center_vertically(chunks[1], &message))
+ .alignment(Alignment::Center);
+ f.render_widget(message, chunks[1]);
+ })?;
+
+ if let Event::Input(key) = events.next()? {
+ match key {
+ Key::Char('q') => return Ok(()),
+ _ => (),
+ }
+ }
+ }
+}
diff --git a/src/gui/mod.rs b/src/gui/mod.rs
index cbf9675..f351eba 100644
--- a/src/gui/mod.rs
+++ b/src/gui/mod.rs
@@ -1,2 +1,4 @@
pub mod gui;
+pub mod message;
pub mod question;
+pub mod util;
diff --git a/src/gui/question.rs b/src/gui/question.rs
index 64589d9..e88d6e5 100644
--- a/src/gui/question.rs
+++ b/src/gui/question.rs
@@ -1,4 +1,5 @@
use crate::{
+ gui::util,
model::{card::Card, difficulty, difficulty::Difficulty},
util::event::{Event, Events},
util::serialization,
@@ -7,7 +8,7 @@ use anyhow::Result;
use termion::event::Key;
use tui::{
backend::Backend,
- layout::{Alignment, Constraint, Direction, Layout, Rect},
+ layout::{Alignment, Constraint, Direction, Layout},
style::{Color, Modifier, Style},
text::{Span, Spans, Text},
widgets::{Block, Borders, Paragraph, Wrap},
@@ -28,7 +29,7 @@ pub fn ask<B: Backend>(
terminal: &mut Terminal<B>,
events: &Events,
card: &Card,
- deck: String,
+ deck_name: &String,
) -> Result<Difficulty> {
let mut state = State {
input: String::new(),
@@ -52,16 +53,10 @@ pub fn ask<B: Backend>(
)
.split(f.size());
- let d1 = Paragraph::new(format!("{}", deck))
- .alignment(Alignment::Center)
- .style(
- Style::default()
- .fg(Color::Blue)
- .add_modifier(Modifier::BOLD),
- );
+ let d1 = util::title(deck_name);
f.render_widget(d1, chunks[0]);
- let question = Paragraph::new(center_vertically(chunks[1], &card.question))
+ let question = Paragraph::new(util::center_vertically(chunks[1], &card.question))
.style(match state.answer {
Answer::Write => {
if state.input == "" {
@@ -75,7 +70,7 @@ pub fn ask<B: Backend>(
.alignment(Alignment::Center);
f.render_widget(question, chunks[1]);
- let answer = Paragraph::new(center_vertically(chunks[2], &state.input))
+ let answer = Paragraph::new(util::center_vertically(chunks[2], &state.input))
.style(match state.answer {
Answer::Write => Style::default(),
Answer::Difficulty { difficulty: _ } => {
@@ -96,7 +91,7 @@ pub fn ask<B: Backend>(
difficulty: selected,
} => {
if !is_correct(&state.input, &card.responses) || card.responses.len() > 1 {
- let paragraph = Paragraph::new(center_vertically(
+ let paragraph = Paragraph::new(util::center_vertically(
chunks[3],
&serialization::words_to_line(&card.responses),
))
@@ -176,14 +171,6 @@ pub fn ask<B: Backend>(
}
}
-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
.iter()
diff --git a/src/gui/util.rs b/src/gui/util.rs
new file mode 100644
index 0000000..38ed1e7
--- /dev/null
+++ b/src/gui/util.rs
@@ -0,0 +1,21 @@
+use tui::{
+ layout::{Alignment, Rect},
+ style::{Color, Modifier, Style},
+ widgets::Paragraph,
+};
+
+pub fn title(str: &str) -> Paragraph {
+ Paragraph::new(str).alignment(Alignment::Center).style(
+ Style::default()
+ .fg(Color::Blue)
+ .add_modifier(Modifier::BOLD),
+ )
+}
+
+pub 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)
+}