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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
use anki_bridge::{AnkiClient, AnkiRequestable, prelude::*};
use crossterm::{
cursor,
event::{self, Event, KeyCode},
execute,
style::*,
terminal::*,
};
use nanohtml2text::html2text;
use std::io::stdout;
const GOOD: char = '3';
const AGAIN: char = '1';
fn main() {
let anki = AnkiClient::default();
init(&anki);
loop {
prompt(&anki);
}
}
fn init(anki: &AnkiClient) {
clear_screen();
let decks = anki.request(DeckNamesRequest {}).unwrap();
for (index, title) in decks.clone().into_iter().enumerate() {
// 0th index is Default, which is not predictable
if index > 0 {
println!("{:?}: {}", index, title);
}
}
display_prompt_text("deck index:");
let mut input = "".to_string();
loop {
match event::read().unwrap() {
Event::Key(e) => match e.code {
KeyCode::Char(c) => input = input + &c.to_string(),
KeyCode::Enter => break,
_ => (),
},
_ => (),
}
}
clear_screen();
display_text("loading...");
anki.request(GuiDeckReviewRequest {
name: decks[input.parse::<usize>().unwrap()].clone(),
})
.unwrap();
}
fn prompt(anki: &AnkiClient) {
// needs to be done twice to account for lag on the other end
anki.request(GuiCurrentCardRequest {}).unwrap();
let card = anki.request(GuiCurrentCardRequest {}).unwrap();
clear_with_bar(&anki, &card);
display_html(&card.question);
loop {
match event::read().unwrap() {
Event::Key(e) => match e.code {
KeyCode::Enter => break,
_ => (),
},
_ => (),
};
}
anki.request(GuiShowAnswerRequest {}).unwrap();
clear_with_bar(&anki, &card);
{
let length = html2text(&card.question).len();
let text = &html2text(&card.answer)[(2 + length)..];
display_text(&text);
}
display_prompt_text(":");
let ease = loop {
match event::read().unwrap() {
Event::Key(e) => match e.code {
KeyCode::Char(AGAIN) => break Ease::Again,
KeyCode::Char(GOOD) => break Ease::Good,
_ => (),
},
_ => (),
};
};
anki.request(GuiAnswerCardRequest { ease: ease }).unwrap();
event::read().unwrap();
}
fn clear_with_bar(anki: &AnkiClient, current_card: &GuiCurrentCardResponse) {
let deck_name = ¤t_card.deck_name;
let deck_stats = anki
.request(GetDeckStatsRequest {
decks: vec![deck_name.to_string()],
})
.unwrap()
.into_iter()
.next()
.unwrap()
.1;
clear_screen();
execute!(
stdout(),
SetForegroundColor(Color::Blue),
Print(&format!("{:?} ", deck_stats.new_count)),
SetForegroundColor(Color::Red),
Print(&format!("{:?} ", deck_stats.learn_count)),
SetForegroundColor(Color::Green),
Print(&format!("{:?}\n", deck_stats.review_count)),
ResetColor
)
.unwrap();
}
fn clear_screen() {
execute!(
stdout(),
Clear(ClearType::All),
cursor::MoveTo(0, 0),
cursor::Hide
)
.unwrap();
}
fn display_prompt_text(text: &str) {
execute!(
stdout(),
SetForegroundColor(Color::Blue),
Print(text),
ResetColor
)
.unwrap();
}
fn display_html(html: &str) {
let text = html2text(html);
display_text(&text);
}
fn display_text(text: &str) {
execute!(
stdout(),
SetForegroundColor(Color::DarkYellow),
Print(text),
ResetColor
)
.unwrap();
}
|