aboutsummaryrefslogtreecommitdiff
path: root/src/gui/mod.rs
blob: 3abe2384e2a5c88a5a9016ce9c7c08aa6d2aa257 (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
pub mod message;
pub mod question;
pub mod util;

use crate::sync;
use crate::{db, space_repetition, util::time};
use anyhow::Result;
use crossterm::terminal;
use rusqlite::Connection;
use std::fs;
use std::io::Stdout;
use tui::{backend::CrosstermBackend, 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: &mut Connection,
    term: &mut Term,
    deck_path: &str,
    deck_name: &str,
    mut deck_last_sync: u64,
    hide_remaining: bool,
) -> Result<()> {
    loop {
        // Synchronize deck if necessary
        let deck_last_update =
            time::seconds_since_unix_epoch_of(fs::metadata(deck_path)?.modified()?)?;
        if deck_last_update > deck_last_sync {
            sync::run(conn, deck_path)?;
            deck_last_sync = time::seconds_since_unix_epoch()?;
        }

        let title = title(
            deck_name,
            db::count_available(conn).unwrap_or(0),
            hide_remaining,
        );

        match db::pick_random_ready(conn) {
            Some(card) => match question::ask(term, &title, &card)? {
                question::Response::Aborted => break,
                question::Response::Answered { difficulty } => {
                    db::update(
                        conn,
                        &card.question,
                        &space_repetition::update(card.state, difficulty),
                    )?;
                }
            },
            None => {
                let message = match db::next_ready(conn) {
                    Some(ready) => {
                        let now = time::seconds_since_unix_epoch()?;
                        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, available_cards: i32, hide_remaining: bool) -> String {
    if available_cards == 0 || hide_remaining {
        deck_name.to_string()
    } else {
        format!("{deck_name} ({available_cards})")
    }
}