1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
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, deck_name: &String) -> Result<()> {
let stdout = io::stdout().into_raw_mode()?;
let stdout = AlternateScreen::from(stdout);
let backend = TermionBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let events = Events::new();
let mut answers = 0;
loop {
let now = time::now()?;
let title = title(deck_name, answers, db::count_available(&conn).unwrap_or(0));
match db::next_ready(&conn) {
Some(card) if card.ready <= now => {
let difficulty = question::ask(&mut terminal, &events, &title, &card)?;
answers += 1;
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, &title, &message);
break;
}
None => {
let message = format!("Aucune carte n’est disponible. Votre deck est-il vide ?");
let _ = message::show(&mut terminal, &events, &title, &message);
break;
}
}
}
Ok(())
}
fn title(deck_name: &String, 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)
}
}
|