75 lines
1.9 KiB
Rust
75 lines
1.9 KiB
Rust
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();
|
|
}
|