aboutsummaryrefslogtreecommitdiff
path: root/src/gui/mod.rs
blob: 92cd94365e77e4939383294b907e2c2e25268610 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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 anyhow::Result;
use rusqlite::Connection;
use std::{fs, io, time::Duration};
use termion::{raw::IntoRawMode, raw::RawTerminal, screen::AlternateScreen};
use tui::{backend::TermionBackend, Terminal};

type Term = Terminal<TermionBackend<AlternateScreen<RawTerminal<io::Stdout>>>>;

pub fn terminal() -> Result<Term> {
    let stdout = io::stdout().into_raw_mode()?;
    let stdout = AlternateScreen::from(stdout);
    let backend = TermionBackend::new(stdout);
    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;

    loop {
        let now = time::seconds_since_unix_epoch()?;
        let title = title(deck_name, answers, db::count_available(conn).unwrap_or(0));

        match db::pick_random_ready(conn) {
            Some(card) => {
                let difficulty = question::ask(term, events, &title, &card)?;
                answers += 1;
                db::update(
                    conn,
                    &card.question,
                    &space_repetition::update(card.state, difficulty),
                )?;
            }
            None => {
                let message = match db::next_ready(conn) {
                    Some(ready) => format!(
                        "Prochaine carte disponible dans {}.",
                        time::pp_duration(ready - now)
                    ),
                    None => "Aucune carte n’est disponible. Votre deck est-il vide ?".to_string(),
                };
                let _ = message::show(term, events, &title, &message, true);
                break;
            }
        }
    }

    Ok(())
}

fn title(deck_name: &str, 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
        )
    }
}