From 87e12144440b9525cbabdca5a9f79038a9a0f0e0 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 12 Feb 2022 17:21:29 +0100 Subject: Show current progress after the deck name --- src/db/db.rs | 9 +++++++++ src/gui/gui.rs | 20 +++++++++++++++++--- src/gui/message.rs | 4 ++-- src/gui/question.rs | 4 ++-- 4 files changed, 30 insertions(+), 7 deletions(-) (limited to 'src') 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 { }) } +pub fn count_available(conn: &Connection) -> Option { + 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( terminal: &mut Terminal, events: &Events, + title: &String, message: &String, - deck_name: &String, ) -> Result<()> { loop { terminal.draw(|f| { @@ -23,7 +23,7 @@ pub fn show( .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( terminal: &mut Terminal, events: &Events, + title: &String, card: &Card, - deck_name: &String, ) -> Result { let mut state = State { input: String::new(), @@ -53,7 +53,7 @@ pub fn ask( ) .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)) -- cgit v1.2.3