aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/db/db.rs9
-rw-r--r--src/gui/gui.rs20
-rw-r--r--src/gui/message.rs4
-rw-r--r--src/gui/question.rs4
4 files changed, 30 insertions, 7 deletions
diff --git a/src/db/db.rs b/src/db/db.rs
index 4c2d9a2..590f3ea 100644
--- a/src/db/db.rs
+++ b/src/db/db.rs
@@ -108,6 +108,15 @@ pub fn next_ready(conn: &Connection) -> Option<Card> {
})
}
+pub fn count_available(conn: &Connection) -> Option<i32> {
+ let now = time::now().ok()?;
+ let mut stmt = conn.prepare("SELECT COUNT(*) FROM cards WHERE ready <= ? AND deleted IS NULL").ok()?;
+
+ let mut rows = stmt.query([now]).ok()?;
+ let row = rows.next().ok()??;
+ row.get(0).ok()?
+}
+
pub fn update(conn: &Connection, question: &String, state: &space_repetition::State) -> Result<()> {
let now = time::now()?;
let ready = now + state.get_interval_seconds();
diff --git a/src/gui/gui.rs b/src/gui/gui.rs
index 88333bb..94e15d8 100644
--- a/src/gui/gui.rs
+++ b/src/gui/gui.rs
@@ -14,12 +14,16 @@ pub fn start(conn: &Connection, deck_name: &String) -> Result<()> {
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, &card, deck_name)?;
+ let difficulty = question::ask(&mut terminal, &events, &title, &card)?;
+ answers += 1;
db::update(
&conn,
&card.question,
@@ -31,12 +35,12 @@ pub fn start(conn: &Connection, deck_name: &String) -> Result<()> {
"Prochaine carte disponible dans {}.",
time::pp_duration(card.ready - now)
);
- let _ = message::show(&mut terminal, &events, &message, deck_name);
+ 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, &message, deck_name);
+ let _ = message::show(&mut terminal, &events, &title, &message);
break;
}
}
@@ -44,3 +48,13 @@ pub fn start(conn: &Connection, deck_name: &String) -> Result<()> {
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)
+ }
+}
diff --git a/src/gui/message.rs b/src/gui/message.rs
index 158416e..01d124e 100644
--- a/src/gui/message.rs
+++ b/src/gui/message.rs
@@ -12,8 +12,8 @@ use tui::{
pub fn show<B: Backend>(
terminal: &mut Terminal<B>,
events: &Events,
+ title: &String,
message: &String,
- deck_name: &String,
) -> Result<()> {
loop {
terminal.draw(|f| {
@@ -23,7 +23,7 @@ pub fn show<B: Backend>(
.constraints([Constraint::Length(1), Constraint::Percentage(50)].as_ref())
.split(f.size());
- let d1 = util::title(deck_name);
+ let d1 = util::title(title);
f.render_widget(d1, chunks[0]);
let message = Paragraph::new(util::center_vertically(chunks[1], &message))
diff --git a/src/gui/question.rs b/src/gui/question.rs
index e88d6e5..211bcda 100644
--- a/src/gui/question.rs
+++ b/src/gui/question.rs
@@ -28,8 +28,8 @@ enum Answer {
pub fn ask<B: Backend>(
terminal: &mut Terminal<B>,
events: &Events,
+ title: &String,
card: &Card,
- deck_name: &String,
) -> Result<Difficulty> {
let mut state = State {
input: String::new(),
@@ -53,7 +53,7 @@ pub fn ask<B: Backend>(
)
.split(f.size());
- let d1 = util::title(deck_name);
+ let d1 = util::title(title);
f.render_widget(d1, chunks[0]);
let question = Paragraph::new(util::center_vertically(chunks[1], &card.question))