aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: ebc2d7e306e436578822c96b89a957851a2d806f (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
mod db;
mod deck;
mod gui;
mod model;
mod space_repetition;
mod sync;
mod util;

use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;

#[derive(Parser)]
#[clap()]
struct Opt {
    /// Path to the deck
    #[clap(long, default_value = "deck.deck")]
    deck: String,

    /// Hide remaining card counts
    #[clap(long)]
    hide_remaining: bool,
}

fn main() -> Result<()> {
    let args = Opt::parse();
    let deck_path = args.deck;
    let mut conn = db::init(db_path(&deck_path))?;
    let deck_name = deck::pp_from_path(&deck_path).unwrap_or_else(|| "Deck".to_string());
    let mut term = gui::setup_terminal()?;

    match sync::run(&mut conn, &deck_path) {
        Ok(()) => {
            let deck_last_sync = util::time::seconds_since_unix_epoch()?;

            match gui::start(
                &mut conn,
                &mut term,
                &deck_path,
                &deck_name,
                deck_last_sync,
                args.hide_remaining,
            ) {
                Ok(()) => (),
                Err(msg) => {
                    // Show errors in TUI, otherwise they are hidden
                    gui::message::show(&mut term, &deck_name, &format!("{msg}"), true)?
                }
            }
        },
        Err(msg) => {
            gui::message::show(&mut term, &deck_name, &format!("{msg}"), true)?
        }
    }


    gui::restore_terminal(&mut term)
}

fn db_path(deck_path: &str) -> String {
    let mut path = PathBuf::from(deck_path);
    path.set_extension("db");
    path.to_string_lossy().to_string()
}