From 3adf3f9697c4e2beb10e652947046d5fddda2ed4 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 12 Feb 2022 16:57:19 +0100 Subject: Say when the next card will be available --- src/gui/gui.rs | 24 ++++++++++++++++++------ src/gui/message.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/gui/mod.rs | 2 ++ src/gui/question.rs | 27 +++++++-------------------- src/gui/util.rs | 21 +++++++++++++++++++++ 5 files changed, 89 insertions(+), 26 deletions(-) create mode 100644 src/gui/message.rs create mode 100644 src/gui/util.rs (limited to 'src/gui') 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( + terminal: &mut Terminal, + 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( terminal: &mut Terminal, events: &Events, card: &Card, - deck: String, + deck_name: &String, ) -> Result { let mut state = State { input: String::new(), @@ -52,16 +53,10 @@ pub fn ask( ) .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( .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( 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( } } -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) -> 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) +} -- cgit v1.2.3