pub mod message; pub mod question; pub mod util; use crate::deck; use crate::util::time; use crate::{db, space_repetition, util::event::Events}; use anyhow::Result; use rusqlite::Connection; use std::{fs, io, time::Duration}; use termion::{raw::IntoRawMode, raw::RawTerminal, screen::AlternateScreen}; use tui::{backend::TermionBackend, Terminal}; type Term = Terminal>>>; pub fn terminal() -> Result { let stdout = io::stdout().into_raw_mode()?; let stdout = AlternateScreen::from(stdout); let backend = TermionBackend::new(stdout); Ok(Terminal::new(backend)?) } pub fn synchronize( conn: &Connection, term: &mut Term, events: &Events, deck_path: &str, deck_name: &str, ) -> Result<()> { let last_modified = time::seconds_since_unix_epoch_of(fs::metadata(deck_path)?.modified()?)?; let last_deck_read = db::last_deck_read(conn); let must_synchronize = last_deck_read.map(|r| r < last_modified).unwrap_or(true); if must_synchronize { let _ = message::show(term, events, deck_name, "Synchronization du deck…", false); time::wait_at_least( || db::synchronize(conn, deck::read(deck_path)?), Duration::from_secs(1), )?; } Ok(()) } pub fn start(conn: &Connection, term: &mut Term, events: &Events, deck_name: &str) -> Result<()> { let mut answers = 0; loop { let now = time::seconds_since_unix_epoch()?; let title = title(deck_name, answers, db::count_available(conn).unwrap_or(0)); match db::pick_random_ready(conn) { Some(card) => { let difficulty = question::ask(term, events, &title, &card)?; answers += 1; db::update( conn, &card.question, &space_repetition::update(card.state, difficulty), )?; } None => { let message = match db::next_ready(conn) { Some(ready) => format!( "Prochaine carte disponible dans {}.", time::pp_duration(ready - now) ), None => "Aucune carte n’est disponible. Votre deck est-il vide ?".to_string(), }; let _ = message::show(term, events, &title, &message, true); break; } } } Ok(()) } fn title(deck_name: &str, answers: i32, available_cards: i32) -> String { if answers == 0 && available_cards == 0 { deck_name.to_string() } else if available_cards == 0 { format!( "{} ({} / {})", deck_name, answers, answers + available_cards ) } else { format!( "{} ({} / {})", deck_name, answers + 1, answers + available_cards ) } }