Compare commits

...

2 Commits

Author SHA1 Message Date
andromeda
dfe95e2a89 use nanohtml2text fork instead of html2text 2026-04-15 11:30:12 +02:00
andromeda
cdfd807e77 frag nach Name des Stapels, zeig HTML ohne <style> und so 2026-04-15 10:46:39 +02:00
3 changed files with 76 additions and 41 deletions

9
Cargo.lock generated
View File

@@ -8,13 +8,13 @@ version = "0.1.0"
dependencies = [
"anki_bridge",
"crossterm",
"nanohtml2text",
]
[[package]]
name = "anki_bridge"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba6d89e6055f7dbf5f0a70c5c9dd34cf4b37e9ef867ff24f6ce696a02c0c3c85"
source = "git+https://gitlab.com/kerkmann/anki_bridge.git?rev=ae83aab48b53f928d9858471aa621772678973b1#ae83aab48b53f928d9858471aa621772678973b1"
dependencies = [
"async-trait",
"maybe-async",
@@ -367,6 +367,11 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "nanohtml2text"
version = "0.2.1"
source = "git+https://git.mtgmonkey.net/Andromeda/nanohtml2text.git#113b87c8c8d5a51c113b92aa2d8ea1a28080cec9"
[[package]]
name = "num-conv"
version = "0.2.1"

View File

@@ -4,13 +4,17 @@ version = "0.1.0"
edition = "2024"
[dependencies]
[dependencies.nanohtml2text]
git = "https://git.mtgmonkey.net/Andromeda/nanohtml2text.git"
version = "0.2.1"
[dependencies.crossterm]
version = "0.29.0"
default-features = false
features = ["events"]
# minor versions of this package have breaking changes :/
# thus the exact versioning
# bleeding edge version has changes breaking to crates.io
[dependencies.anki_bridge]
version = "=0.10.2"
git = "https://gitlab.com/kerkmann/anki_bridge.git"
rev = "ae83aab48b53f928d9858471aa621772678973b1"
version = "0.10.2"
features = ["ureq_blocking"]

View File

@@ -1,64 +1,63 @@
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() {
// 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_html(&card.question);
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();
{
let length = html2text(&card.question).len();
let text = &html2text(&card.answer)[(2 + length)..];
display_text(&text);
}
display_prompt_text(":");
let ease = loop {
let ease = match event::read().unwrap() {
Event::Key(e) => match e.code {
@@ -69,6 +68,33 @@ 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_html(html: &str) {
let text = html2text(html);
display_text(&text);
}
fn display_text(text: &str) {
execute!(
stdout(),
SetForegroundColor(Color::DarkYellow),
Print(text),
ResetColor
);
}