summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: d586213eb4c012605299d368712df7c63d2197ff (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
use anki_bridge::{AnkiClient, AnkiRequestable, prelude::*};
use crossterm::{
    event::{self, Event, KeyCode},
    execute,
    style::*,
};
use std::io::stdout;

const GOOD: char = '3';
const AGAIN: char = '1';

fn main() {
    // Creates a client to connect to the Anki instance running on the local computer
    let anki = AnkiClient::default();

    // Fetch the names of all the active decks
    let decks = anki.request(DeckNamesRequest {}).unwrap();
    dbg!(&decks);

    // Fetch statistics about the decks above
    let deck_stats = anki.request(GetDeckStatsRequest { decks }).unwrap();
    dbg!(&deck_stats);

    execute!(
        stdout(),
        SetForegroundColor(Color::DarkMagenta),
        Print("Welcome to anki-cli\n"),
        ResetColor
    )
    .unwrap();
    loop {
        prompt(&anki);
    }
}

fn prompt(anki: &AnkiClient) {
    let card = anki.request(GuiCurrentCardRequest {}).unwrap();
    execute!(
        stdout(),
        SetForegroundColor(Color::DarkYellow),
        Print(card.question),
        Print("\n"),
        ResetColor
    );
    loop {
        match event::read().unwrap() {
            Event::Key(e) => match e.code {
                KeyCode::Char(' ') => break,
                _ => (),
            },
            e => (),
        };
    }
    execute!(
        stdout(),
        SetForegroundColor(Color::DarkYellow),
        Print(card.answer),
        SetForegroundColor(Color::Blue),
        Print("\nEnter the answer:\n"),
        ResetColor
    );
    let ease = loop {
        let ease = match event::read().unwrap() {
            Event::Key(e) => match e.code {
                KeyCode::Char(AGAIN) => break Ease::Again,
                KeyCode::Char(GOOD) => break Ease::Good,
                _ => (),
            },
            e => (),
        };
    };
    anki.request(GuiShowAnswerRequest {}).unwrap();
    anki.request(GuiAnswerCardRequest { ease: ease }).unwrap();
}