diff options
author | Joris | 2022-11-27 15:38:39 +0100 |
---|---|---|
committer | Joris | 2022-11-27 15:38:39 +0100 |
commit | d3fb69cf129fe70c932a5d82fdd1bcc613544b5b (patch) | |
tree | 6a098c7900f96791489b8548979fb71ccea03e59 /src/gui | |
parent | 765ba94cdd65184bd4b443c3a946d0d96dc805d6 (diff) |
Speed up deck synchronization
Apply changes for what has been inserted, updated, removed.
Also use transactions to speed up multi-writing.
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/message.rs | 2 | ||||
-rw-r--r-- | src/gui/mod.rs | 30 | ||||
-rw-r--r-- | src/gui/question.rs | 10 |
3 files changed, 10 insertions, 32 deletions
diff --git a/src/gui/message.rs b/src/gui/message.rs index 1ae10a5..29b5d8a 100644 --- a/src/gui/message.rs +++ b/src/gui/message.rs @@ -27,7 +27,7 @@ pub fn show<B: Backend>( let d1 = util::title(title); f.render_widget(d1, chunks[0]); - let message = Paragraph::new(util::center_vertically(chunks[1], &message.to_string())) + let message = Paragraph::new(util::center_vertically(chunks[1], message)) .alignment(Alignment::Center); f.render_widget(message, chunks[1]); })?; diff --git a/src/gui/mod.rs b/src/gui/mod.rs index 858b30d..358e4b5 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -2,12 +2,10 @@ 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 crate::{db, space_repetition, util::event::Events, util::time}; use anyhow::Result; use rusqlite::Connection; -use std::{fs, io, time::Duration}; +use std::io; use termion::{raw::IntoRawMode, raw::RawTerminal, screen::AlternateScreen}; use tui::{backend::TermionBackend, Terminal}; @@ -20,28 +18,6 @@ pub fn terminal() -> Result<Term> { 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; @@ -66,7 +42,7 @@ pub fn start(conn: &Connection, term: &mut Term, events: &Events, deck_name: &st 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, events, &title, &message, true); diff --git a/src/gui/question.rs b/src/gui/question.rs index 71c3ea2..2aa6e65 100644 --- a/src/gui/question.rs +++ b/src/gui/question.rs @@ -1,6 +1,6 @@ use crate::{ gui::util, - model::{card::Card, difficulty, difficulty::Difficulty}, + model::{difficulty, difficulty::Difficulty, Card}, util::event::{Event, Events}, util::serialization, }; @@ -150,7 +150,9 @@ pub fn ask<B: Backend>( Key::Char(c) => { state.input.push(c); if is_correct(&state.input, &card.responses) { - state.answer = Answer::Difficulty { difficulty: Difficulty::Good } + state.answer = Answer::Difficulty { + difficulty: Difficulty::Good, + } } } Key::Backspace => { @@ -161,12 +163,12 @@ pub fn ask<B: Backend>( } Key::Ctrl('w') => { let mut words = state.input.split_whitespace().collect::<Vec<&str>>(); - if words.len() > 0 { + if !words.is_empty() { words.truncate(words.len() - 1); state.input = format!( "{}{}", words.join(" "), - if words.len() > 0 { " " } else { "" } + if !words.is_empty() { " " } else { "" } ); } } |