Compare commits
8 Commits
dfe95e2a89
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
364c022cee | ||
|
|
bcd83506ea | ||
|
|
219930f3c6 | ||
|
|
da93183389 | ||
|
|
4770f441d1 | ||
|
|
d24c653bd2 | ||
|
|
2dbe2c7811 | ||
|
|
a6afe8626b |
@@ -2,6 +2,7 @@
|
||||
name = "anki-cli"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license = "GPL-3.0+"
|
||||
|
||||
[dependencies]
|
||||
[dependencies.nanohtml2text]
|
||||
|
||||
@@ -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
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
116
src/main.rs
116
src/main.rs
@@ -1,13 +1,18 @@
|
||||
use anki_bridge::{AnkiClient, AnkiRequestable, prelude::*};
|
||||
use crossterm::{
|
||||
cursor::*,
|
||||
cursor,
|
||||
event::{self, Event, KeyCode},
|
||||
execute,
|
||||
style::*,
|
||||
terminal::*,
|
||||
};
|
||||
use nanohtml2text::html2text;
|
||||
use std::io::stdout;
|
||||
use std::{
|
||||
io::stdout,
|
||||
process::{Command, Stdio},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
const GOOD: char = '3';
|
||||
const AGAIN: char = '1';
|
||||
@@ -22,7 +27,36 @@ fn main() {
|
||||
|
||||
fn init(anki: &AnkiClient) {
|
||||
clear_screen();
|
||||
display_prompt_text("Name des Stapels: ");
|
||||
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() {
|
||||
@@ -34,24 +68,36 @@ fn init(anki: &AnkiClient) {
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
anki.request(GuiDeckReviewRequest { name: input }).unwrap();
|
||||
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_screen();
|
||||
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;
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
e => (),
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
anki.request(GuiShowAnswerRequest {}).unwrap();
|
||||
clear_screen();
|
||||
clear_with_bar(&anki, &card);
|
||||
{
|
||||
let length = html2text(&card.question).len();
|
||||
let text = &html2text(&card.answer)[(2 + length)..];
|
||||
@@ -59,21 +105,65 @@ fn prompt(anki: &AnkiClient) {
|
||||
}
|
||||
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();
|
||||
return;
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
e => (),
|
||||
_ => (),
|
||||
};
|
||||
};
|
||||
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), MoveTo(0, 0));
|
||||
execute!(
|
||||
stdout(),
|
||||
Clear(ClearType::All),
|
||||
cursor::MoveTo(0, 0),
|
||||
cursor::Hide
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn display_prompt_text(text: &str) {
|
||||
@@ -82,7 +172,8 @@ fn display_prompt_text(text: &str) {
|
||||
SetForegroundColor(Color::Blue),
|
||||
Print(text),
|
||||
ResetColor
|
||||
);
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn display_html(html: &str) {
|
||||
@@ -96,5 +187,6 @@ fn display_text(text: &str) {
|
||||
SetForegroundColor(Color::DarkYellow),
|
||||
Print(text),
|
||||
ResetColor
|
||||
);
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user