Compare commits
9 Commits
8c0894e8eb
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bcd83506ea | ||
|
|
219930f3c6 | ||
|
|
da93183389 | ||
|
|
4770f441d1 | ||
|
|
d24c653bd2 | ||
|
|
2dbe2c7811 | ||
|
|
a6afe8626b | ||
|
|
dfe95e2a89 | ||
|
|
cdfd807e77 |
9
Cargo.lock
generated
9
Cargo.lock
generated
@@ -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"
|
||||
|
||||
11
Cargo.toml
11
Cargo.toml
@@ -2,15 +2,20 @@
|
||||
name = "anki-cli"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license = "GPL-3.0+"
|
||||
|
||||
[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"]
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
toolchain = fenix.packages.x86_64-linux.minimal.toolchain;
|
||||
in {
|
||||
devShells.x86_64-linux.default = pkgs.mkShell {
|
||||
buildInputs = [toolchain];
|
||||
buildInputs = [toolchain pkgs.xvfb-run];
|
||||
shellHook = ''
|
||||
export RUSTUP_TOOLCHAIN=${toolchain}
|
||||
'';
|
||||
@@ -30,7 +30,12 @@
|
||||
cargo = toolchain;
|
||||
rustc = toolchain;
|
||||
}).buildPackage {
|
||||
nativeBuildInputs = [pkgs.makeWrapper];
|
||||
src = ./.;
|
||||
postInstall = ''
|
||||
wrapProgram $out/bin/anki-cli \
|
||||
--prefix PATH : ${pkgs.xvfb-run}/bin
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
190
src/main.rs
190
src/main.rs
@@ -1,74 +1,184 @@
|
||||
use anki_bridge::{AnkiClient, AnkiRequestable, prelude::*};
|
||||
use crossterm::{
|
||||
cursor,
|
||||
event::{self, Event, KeyCode},
|
||||
execute,
|
||||
style::*,
|
||||
terminal::*,
|
||||
};
|
||||
use nanohtml2text::html2text;
|
||||
use std::{
|
||||
io::stdout,
|
||||
process::{Command, Stdio},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
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();
|
||||
let mut decks = anki.request(DeckNamesRequest {});
|
||||
if let Err(_) = decks {
|
||||
clear_screen();
|
||||
display_text("initializing headless anki...");
|
||||
Command::new("bash")
|
||||
.arg("-c")
|
||||
.arg("QT_QPA_PLATFORM=xcb xvfb-run -a anki")
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
loop {
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
display_text(".");
|
||||
decks = anki.request(DeckNamesRequest {});
|
||||
if let Ok(_) = decks {
|
||||
clear_screen();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
let decks = decks.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(' ') => break,
|
||||
KeyCode::Char(c) => input = input + &c.to_string(),
|
||||
KeyCode::Enter => break,
|
||||
_ => (),
|
||||
},
|
||||
e => (),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
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,
|
||||
KeyCode::Char('u') => {
|
||||
anki.request(GuiUndoRequest {}).unwrap();
|
||||
event::read().unwrap();
|
||||
return;
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
execute!(
|
||||
stdout(),
|
||||
SetForegroundColor(Color::DarkYellow),
|
||||
Print(card.answer),
|
||||
SetForegroundColor(Color::Blue),
|
||||
Print("\nEnter the answer:\n"),
|
||||
ResetColor
|
||||
);
|
||||
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 {
|
||||
let ease = match event::read().unwrap() {
|
||||
match event::read().unwrap() {
|
||||
Event::Key(e) => match e.code {
|
||||
KeyCode::Char(AGAIN) => break Ease::Again,
|
||||
KeyCode::Char(GOOD) => break Ease::Good,
|
||||
KeyCode::Char('u') => {
|
||||
anki.request(GuiUndoRequest {}).unwrap();
|
||||
event::read().unwrap();
|
||||
return;
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
e => (),
|
||||
_ => (),
|
||||
};
|
||||
};
|
||||
anki.request(GuiShowAnswerRequest {}).unwrap();
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user