Compare commits
10 Commits
8c0894e8eb
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
364c022cee | ||
|
|
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 = [
|
dependencies = [
|
||||||
"anki_bridge",
|
"anki_bridge",
|
||||||
"crossterm",
|
"crossterm",
|
||||||
|
"nanohtml2text",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anki_bridge"
|
name = "anki_bridge"
|
||||||
version = "0.10.2"
|
version = "0.10.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "git+https://gitlab.com/kerkmann/anki_bridge.git?rev=ae83aab48b53f928d9858471aa621772678973b1#ae83aab48b53f928d9858471aa621772678973b1"
|
||||||
checksum = "ba6d89e6055f7dbf5f0a70c5c9dd34cf4b37e9ef867ff24f6ce696a02c0c3c85"
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"maybe-async",
|
"maybe-async",
|
||||||
@@ -367,6 +367,11 @@ dependencies = [
|
|||||||
"windows-sys",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nanohtml2text"
|
||||||
|
version = "0.2.1"
|
||||||
|
source = "git+https://git.mtgmonkey.net/Andromeda/nanohtml2text.git#113b87c8c8d5a51c113b92aa2d8ea1a28080cec9"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-conv"
|
name = "num-conv"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
|
|||||||
11
Cargo.toml
11
Cargo.toml
@@ -2,15 +2,20 @@
|
|||||||
name = "anki-cli"
|
name = "anki-cli"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
license = "GPL-3.0+"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
[dependencies.nanohtml2text]
|
||||||
|
git = "https://git.mtgmonkey.net/Andromeda/nanohtml2text.git"
|
||||||
|
version = "0.2.1"
|
||||||
[dependencies.crossterm]
|
[dependencies.crossterm]
|
||||||
version = "0.29.0"
|
version = "0.29.0"
|
||||||
default-features = false
|
default-features = false
|
||||||
features = ["events"]
|
features = ["events"]
|
||||||
|
|
||||||
# minor versions of this package have breaking changes :/
|
# bleeding edge version has changes breaking to crates.io
|
||||||
# thus the exact versioning
|
|
||||||
[dependencies.anki_bridge]
|
[dependencies.anki_bridge]
|
||||||
version = "=0.10.2"
|
git = "https://gitlab.com/kerkmann/anki_bridge.git"
|
||||||
|
rev = "ae83aab48b53f928d9858471aa621772678973b1"
|
||||||
|
version = "0.10.2"
|
||||||
features = ["ureq_blocking"]
|
features = ["ureq_blocking"]
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
toolchain = fenix.packages.x86_64-linux.minimal.toolchain;
|
toolchain = fenix.packages.x86_64-linux.minimal.toolchain;
|
||||||
in {
|
in {
|
||||||
devShells.x86_64-linux.default = pkgs.mkShell {
|
devShells.x86_64-linux.default = pkgs.mkShell {
|
||||||
buildInputs = [toolchain];
|
buildInputs = [toolchain pkgs.xvfb-run];
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
export RUSTUP_TOOLCHAIN=${toolchain}
|
export RUSTUP_TOOLCHAIN=${toolchain}
|
||||||
'';
|
'';
|
||||||
@@ -30,7 +30,12 @@
|
|||||||
cargo = toolchain;
|
cargo = toolchain;
|
||||||
rustc = toolchain;
|
rustc = toolchain;
|
||||||
}).buildPackage {
|
}).buildPackage {
|
||||||
|
nativeBuildInputs = [pkgs.makeWrapper];
|
||||||
src = ./.;
|
src = ./.;
|
||||||
|
postInstall = ''
|
||||||
|
wrapProgram $out/bin/anki-cli \
|
||||||
|
--prefix PATH : ${pkgs.xvfb-run}/bin
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
198
src/main.rs
198
src/main.rs
@@ -1,74 +1,192 @@
|
|||||||
use anki_bridge::{AnkiClient, AnkiRequestable, prelude::*};
|
use anki_bridge::{AnkiClient, AnkiRequestable, prelude::*};
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
|
cursor,
|
||||||
event::{self, Event, KeyCode},
|
event::{self, Event, KeyCode},
|
||||||
execute,
|
execute,
|
||||||
style::*,
|
style::*,
|
||||||
|
terminal::*,
|
||||||
|
};
|
||||||
|
use nanohtml2text::html2text;
|
||||||
|
use std::{
|
||||||
|
io::stdout,
|
||||||
|
process::{Command, Stdio},
|
||||||
|
thread,
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
use std::io::stdout;
|
|
||||||
|
|
||||||
const GOOD: char = '3';
|
const GOOD: char = '3';
|
||||||
const AGAIN: char = '1';
|
const AGAIN: char = '1';
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Creates a client to connect to the Anki instance running on the local computer
|
|
||||||
let anki = AnkiClient::default();
|
let anki = AnkiClient::default();
|
||||||
|
init(&anki);
|
||||||
// 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();
|
|
||||||
loop {
|
loop {
|
||||||
prompt(&anki);
|
prompt(&anki);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prompt(anki: &AnkiClient) {
|
fn init(anki: &AnkiClient) {
|
||||||
let card = anki.request(GuiCurrentCardRequest {}).unwrap();
|
clear_screen();
|
||||||
execute!(
|
let mut decks = anki.request(DeckNamesRequest {});
|
||||||
stdout(),
|
if let Err(_) = decks {
|
||||||
SetForegroundColor(Color::DarkYellow),
|
clear_screen();
|
||||||
Print(card.question),
|
display_text("initializing headless anki...");
|
||||||
Print("\n"),
|
Command::new("bash")
|
||||||
ResetColor
|
.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 {
|
loop {
|
||||||
match event::read().unwrap() {
|
match event::read().unwrap() {
|
||||||
Event::Key(e) => match e.code {
|
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) {
|
||||||
|
flush_read();
|
||||||
|
// 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();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
execute!(
|
anki.request(GuiShowAnswerRequest {}).unwrap();
|
||||||
stdout(),
|
clear_with_bar(&anki, &card);
|
||||||
SetForegroundColor(Color::DarkYellow),
|
{
|
||||||
Print(card.answer),
|
let length = html2text(&card.question).len();
|
||||||
SetForegroundColor(Color::Blue),
|
let text = &html2text(&card.answer)[(2 + length)..];
|
||||||
Print("\nEnter the answer:\n"),
|
display_text(&text);
|
||||||
ResetColor
|
}
|
||||||
);
|
display_prompt_text(":");
|
||||||
let ease = loop {
|
let ease = loop {
|
||||||
let ease = match event::read().unwrap() {
|
match event::read().unwrap() {
|
||||||
Event::Key(e) => match e.code {
|
Event::Key(e) => match e.code {
|
||||||
KeyCode::Char(AGAIN) => break Ease::Again,
|
KeyCode::Char(AGAIN) => break Ease::Again,
|
||||||
KeyCode::Char(GOOD) => break Ease::Good,
|
KeyCode::Char(GOOD) => break Ease::Good,
|
||||||
|
KeyCode::Char('u') => {
|
||||||
|
anki.request(GuiUndoRequest {}).unwrap();
|
||||||
|
return;
|
||||||
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
},
|
},
|
||||||
e => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
anki.request(GuiShowAnswerRequest {}).unwrap();
|
|
||||||
anki.request(GuiAnswerCardRequest { ease: ease }).unwrap();
|
anki.request(GuiAnswerCardRequest { ease: ease }).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn flush_read() {
|
||||||
|
loop {
|
||||||
|
if event::poll(Duration::from_secs(0)).unwrap() {
|
||||||
|
event::read().unwrap();
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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