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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
pub mod message;
pub mod question;
pub mod util;
use crate::{db, space_repetition, util::time};
use anyhow::Result;
use rusqlite::Connection;
use std::io::Stdout;
use tui::{backend::CrosstermBackend, Terminal};
use crossterm::{terminal};
pub type Term = Terminal<CrosstermBackend<Stdout>>;
pub fn setup_terminal() -> Result<Term> {
terminal::enable_raw_mode()?;
let mut stdout = std::io::stdout();
crossterm::execute!(stdout, terminal::EnterAlternateScreen)?;
let backend = CrosstermBackend::new(stdout);
Ok(Terminal::new(backend)?)
}
pub fn restore_terminal(term: &mut Term) -> Result<()> {
terminal::disable_raw_mode()?;
crossterm::execute!(term.backend_mut(), terminal::LeaveAlternateScreen)?;
term.show_cursor()?;
Ok(())
}
pub fn start(
conn: &Connection,
term: &mut Term,
deck_name: &str,
hide_progress: bool,
) -> 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), hide_progress);
match db::pick_random_ready(conn) {
Some(card) => match question::ask(term, &title, &card)? {
question::Response::Aborted => break,
question::Response::Answered { difficulty } => {
answers += 1;
db::update(
conn,
&card.question,
&space_repetition::update(card.state, difficulty),
)?;
}
},
None => {
let message = match db::next_ready(conn) {
Some(ready) => {
let duration = time::pp_duration(ready - now);
format!("Prochaine carte disponible dans {duration}.")
}
None => "Aucune carte n’est disponible. Votre deck est-il vide ?".to_string(),
};
let _ = message::show(term, &title, &message, true);
break;
}
}
}
Ok(())
}
fn title(deck_name: &str, answers: i32, available_cards: i32, hide_progress: bool) -> String {
if answers == 0 && available_cards == 0 || hide_progress {
deck_name.to_string()
} else if available_cards == 0 {
let from = answers;
let to = answers + available_cards;
format!("{deck_name} ({from} / {to})")
} else {
let from = answers + 1;
let to = answers + available_cards;
format!("{deck_name} ({from} / {to})")
}
}
|