diff --git a/Cargo.lock b/Cargo.lock index 31ee415..7a3a612 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,13 +8,13 @@ version = "0.1.0" dependencies = [ "anki_bridge", "crossterm", + "html2text", ] [[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", @@ -149,6 +149,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "fastrand" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6" + [[package]] name = "form_urlencoded" version = "1.2.2" @@ -164,6 +170,29 @@ version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f467dd6dccf739c208452f8014c75c18bb8301b050ad1cfb27153803edb0f51" +[[package]] +name = "html2text" +version = "0.16.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12d23156ea4dbe6b37ad48fab2da56ff27b0f6192fb5db210c44eb07bfe6e787" +dependencies = [ + "html5ever", + "nom", + "tendril", + "thiserror", + "unicode-width", +] + +[[package]] +name = "html5ever" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1054432bae2f14e0061e33d23402fbaa67a921d319d56adc6bcf887ddad1cbc2" +dependencies = [ + "log", + "markup5ever", +] + [[package]] name = "http" version = "1.4.0" @@ -338,6 +367,17 @@ version = "0.4.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" +[[package]] +name = "markup5ever" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8983d30f2915feeaaab2d6babdd6bc7e9ed1a00b66b5e6d74df19aa9c0e91862" +dependencies = [ + "log", + "tendril", + "web_atoms", +] + [[package]] name = "maybe-async" version = "0.2.10" @@ -367,6 +407,21 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "new_debug_unreachable" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" + +[[package]] +name = "nom" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405" +dependencies = [ + "memchr", +] + [[package]] name = "num-conv" version = "0.2.1" @@ -402,6 +457,45 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "phf" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf" +dependencies = [ + "phf_shared", + "serde", +] + +[[package]] +name = "phf_codegen" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49aa7f9d80421bca176ca8dbfebe668cc7a2684708594ec9f3c0db0805d5d6e1" +dependencies = [ + "phf_generator", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "135ace3a761e564ec88c03a77317a7c6b80bb7f7135ef2544dbe054243b89737" +dependencies = [ + "fastrand", + "phf_shared", +] + +[[package]] +name = "phf_shared" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266" +dependencies = [ + "siphasher", +] + [[package]] name = "potential_utf" version = "0.1.5" @@ -417,6 +511,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +[[package]] +name = "precomputed-hash" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" + [[package]] name = "proc-macro2" version = "1.0.106" @@ -558,6 +658,12 @@ dependencies = [ "libc", ] +[[package]] +name = "siphasher" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2aa850e253778c88a04c3d7323b043aeda9d3e30d5971937c1855769763678e" + [[package]] name = "smallvec" version = "1.15.1" @@ -570,6 +676,30 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +[[package]] +name = "string_cache" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a18596f8c785a729f2819c0f6a7eae6ebeebdfffbfe4214ae6b087f690e31901" +dependencies = [ + "new_debug_unreachable", + "parking_lot", + "phf_shared", + "precomputed-hash", +] + +[[package]] +name = "string_cache_codegen" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "585635e46db231059f76c5849798146164652513eb9e8ab2685939dd90f29b69" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", +] + [[package]] name = "syn" version = "2.0.117" @@ -592,6 +722,16 @@ dependencies = [ "syn", ] +[[package]] +name = "tendril" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4790fc369d5a530f4b544b094e31388b9b3a37c0f4652ade4505945f5660d24" +dependencies = [ + "new_debug_unreachable", + "utf-8", +] + [[package]] name = "thiserror" version = "2.0.18" @@ -659,6 +799,12 @@ version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-width" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" + [[package]] name = "ureq" version = "3.3.0" @@ -699,6 +845,12 @@ dependencies = [ "serde", ] +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + [[package]] name = "utf8-zero" version = "0.8.1" @@ -723,6 +875,18 @@ version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +[[package]] +name = "web_atoms" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57a9779e9f04d2ac1ce317aee707aa2f6b773afba7b931222bff6983843b1576" +dependencies = [ + "phf", + "phf_codegen", + "string_cache", + "string_cache_codegen", +] + [[package]] name = "windows-link" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 6f167ee..65bd6f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,13 +4,17 @@ version = "0.1.0" edition = "2024" [dependencies] +[dependencies.html2text] +version = "0.16.7" +features = ["css"] [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"] diff --git a/src/main.rs b/src/main.rs index d586213..9a95134 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 + ); }