anki-i3blocks

an i3blocks block to display Anki stats
Log | Files | Refs | README | LICENSE

commit 336d341102c8b2dabc12c55573594af9a7eb8bb6
parent 0c28e64fed2884d1d1468c1877118e3ca83f4693
Author: Stefan Koch <programming@stefan-koch.name>
Date:   Sat,  8 Jan 2022 12:14:40 +0100

allow multiple decks

Diffstat:
MREADME.md | 12++++++++++++
Manki | 41++++++++++++++++++++++++++---------------
2 files changed, 38 insertions(+), 15 deletions(-)

diff --git a/README.md b/README.md @@ -11,3 +11,15 @@ command=~/anki Spanish label=ES interval=600 ``` + +Deck names must not include the character `'`. + +You can also query multiple decks at once by separating the deck names +with semicolon. + +``` +[wanikani] +command=~/anki "Spanish;Spanish Books" +label=ES +interval=600 +``` diff --git a/anki b/anki @@ -1,25 +1,36 @@ #!/usr/bin/env bash -deck=$1 +decks=$1 if [[ -z $ANKI_COLLECTION_PATH ]]; then ANKI_COLLECTION_PATH="$HOME/.local/share/Anki2/User 1/collection.anki2" fi -learning_query="SELECT COUNT(*) FROM cards c JOIN decks d ON c.did = d.id WHERE d.name = '$deck' COLLATE NOCASE AND c.ivl != 0;" -all_query="SELECT COUNT(*) FROM cards c JOIN decks d ON c.did = d.id WHERE d.name = '$deck' COLLATE NOCASE;" -today=$(date +%Y-%m-%d) -duration_query="SELECT SUM(time) FROM revlog r JOIN cards c ON r.cid = c.id JOIN decks d ON c.did = d.id WHERE d.name = '$deck' COLLATE NOCASE AND r.id >= strftime('%s', '$today') * 1000;" +learning_count=0 +all_count=0 +duration_min=0 -# TODO: Is there really no other way to work around the exclusive lock that -# Anki or AnkiConnect seems to hold than copying the whole database? -uuid=$(uuidgen) -tmp_db=/tmp/anki-${uuid}.sqlite3 -cp "$ANKI_COLLECTION_PATH" $tmp_db -learning_count=$(sqlite3 -readonly $tmp_db "$learning_query") -all_count=$(sqlite3 -readonly $tmp_db "$all_query") -duration_ms=$(sqlite3 -readonly $tmp_db "$duration_query") -duration_min=$((duration_ms / 60000)) -rm $tmp_db +OLDIFS="$IFS" +IFS=";" +for deck in $decks; do + deck=$(echo $deck | sed "s/\'//g") + + learning_query="SELECT COUNT(*) FROM cards c JOIN decks d ON c.did = d.id WHERE d.name = '$deck' COLLATE NOCASE AND c.ivl != 0;" + all_query="SELECT COUNT(*) FROM cards c JOIN decks d ON c.did = d.id WHERE d.name = '$deck' COLLATE NOCASE;" + today=$(date +%Y-%m-%d) + duration_query="SELECT SUM(time) FROM revlog r JOIN cards c ON r.cid = c.id JOIN decks d ON c.did = d.id WHERE d.name = '$deck' COLLATE NOCASE AND r.id >= strftime('%s', '$today') * 1000;" + + # TODO: Is there really no other way to work around the exclusive lock that + # Anki or AnkiConnect seems to hold than copying the whole database? + uuid=$(uuidgen) + tmp_db=/tmp/anki-${uuid}.sqlite3 + cp "$ANKI_COLLECTION_PATH" $tmp_db + learning_count=$(($learning_count + $(sqlite3 -readonly $tmp_db "$learning_query"))) + all_count=$(($all_count + $(sqlite3 -readonly $tmp_db "$all_query"))) + duration_ms=$(sqlite3 -readonly $tmp_db "$duration_query") + duration_min=$(($duration_min + duration_ms / 60000)) + rm $tmp_db +done +IFS=$OLDIFS echo "$learning_count/$all_count ($duration_min min)"