frag nach Name des Stapels, zeig HTML ohne <style> und so

This commit is contained in:
andromeda
2026-04-15 10:46:39 +02:00
parent 8c0894e8eb
commit cdfd807e77
3 changed files with 239 additions and 41 deletions

View File

@@ -1,8 +1,10 @@
use anki_bridge::{AnkiClient, AnkiRequestable, prelude::*};
use crossterm::{
cursor::*,
event::{self, Event, KeyCode},
execute,
style::*,
terminal::*,
};
use std::io::stdout;
@@ -10,55 +12,47 @@ 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();
init(&anki);
loop {
prompt(&anki);
}
}
fn prompt(anki: &AnkiClient) {
let card = anki.request(GuiCurrentCardRequest {}).unwrap();
execute!(
stdout(),
SetForegroundColor(Color::DarkYellow),
Print(card.question),
Print("\n"),
ResetColor
);
fn init(anki: &AnkiClient) {
clear_screen();
display_prompt_text("Name des Stapels: ");
let mut input = "".to_string();
loop {
match event::read().unwrap() {
Event::Key(e) => match e.code {
KeyCode::Char(' ') => break,
KeyCode::Char(c) => input = input + &c.to_string(),
KeyCode::Enter => break,
_ => (),
},
_ => (),
}
}
anki.request(GuiDeckReviewRequest { name: input }).unwrap();
}
fn prompt(anki: &AnkiClient) {
let card = anki.request(GuiCurrentCardRequest {}).unwrap();
clear_screen();
display_question(&card);
loop {
match event::read().unwrap() {
Event::Key(e) => match e.code {
KeyCode::Enter => break,
_ => (),
},
e => (),
};
}
execute!(
stdout(),
SetForegroundColor(Color::DarkYellow),
Print(card.answer),
SetForegroundColor(Color::Blue),
Print("\nEnter the answer:\n"),
ResetColor
);
anki.request(GuiShowAnswerRequest {}).unwrap();
clear_screen();
display_answer(&card);
display_prompt_text(":");
let ease = loop {
let ease = match event::read().unwrap() {
Event::Key(e) => match e.code {
@@ -69,6 +63,42 @@ fn prompt(anki: &AnkiClient) {
e => (),
};
};
anki.request(GuiShowAnswerRequest {}).unwrap();
anki.request(GuiAnswerCardRequest { ease: ease }).unwrap();
event::read().unwrap();
}
fn clear_screen() {
execute!(stdout(), Clear(ClearType::All), MoveTo(0, 0));
}
fn display_prompt_text(text: &str) {
execute!(
stdout(),
SetForegroundColor(Color::Blue),
Print(text),
ResetColor
);
}
fn display_question(card: &GuiCurrentCardResponse) {
let text = html2text::from_read(card.question.as_bytes(), 80).unwrap();
execute!(
stdout(),
SetForegroundColor(Color::DarkYellow),
Print(text),
ResetColor
);
}
fn display_answer(card: &GuiCurrentCardResponse) {
let length = html2text::from_read(card.question.as_bytes(), 80)
.unwrap()
.len();
let text = &html2text::from_read(card.answer.as_bytes(), 80).unwrap()[length..];
execute!(
stdout(),
SetForegroundColor(Color::DarkYellow),
Print(text),
ResetColor
);
}