fla.sh/flash

126 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
#==============================================================#
#
# AUTHOR:
# Bryan Jenks
# - www.bryanjenks.xyz
# - https://github.com/tallguyjenks/flash.sh
#
# SOURCE:
# This file is based off of the one presented in this YouTube Video by nixcasts:
# - https://www.youtube.com/watch?v=lX8jqo70r1I
#
# PURPOSE:
# To have a command line flash card tool with minimal code, plain text input and output
#
# VISION:
# The goal i have for this script is a basic level emulation of ANKI to where i have a way to
# keep track of a score for each item in each selected deck so that it can pick from a selection
# of the lowest scoring items and shuf them to the user for reenforcement of active recall.
#
# DEPENDENCIES:
# fzf - https://github.com/junegunn/fzf
# bat - https://github.com/sharkdp/bat
#==============================================================#
PWD="$(pwd)" # Remember User's Starting Directory
DIR="$HOME/.local/share/flash" # Where Decks Are Located
EXAMPLE_DECK="$HOME/.local/share/flash/deck.csv"
COUNTER=0
DIR_MADE_MSG="
Your '~/.local/share/flash' directory has been made and
your 'deck.csv' file is ready for you to enter your flashcard data
"
NO_DECKS="
No decks were found, please make a new deck
using ':' as a delimiter in a '.csv' file in
the '.local/share/flash' directory.
"
DECK_TEMPLATE="category1:question1:answer1:0
category2:question2:answer2:0
category3:question3:answer3:0
"
if [ ! -d $DIR ];then # If Directory does NOT exist
echo "No '.local/share/flash' directory, make it? y/n"
read RESPONSE
case "$RESPONSE" in
[Nn]) exit ;;
[Qq]) exit ;;
[Yy]) mkdir "$DIR" && touch $EXAMPLE_DECK && echo "$DECK_TEMPLATE" >> $EXAMPLE_DECK && echo "$DIR_MADE_MSG" ;;
*) echo "invalid choice, please select either 'y' or 'n'" ;;
esac
fi
cd "$DIR"
if [ "$(find -maxdepth 999 -iname "*.csv" | wc -l)" = 0 ]; then # If there are no flashcard decks available
echo "$NO_DECKS" && cd "$PWD" && exit
fi
# Show pretty FZF preview of decks using BAT
DECK="$(find -maxdepth 999 -iname "*.csv" | fzf --preview='bat --theme="Solarized (dark)" --style=numbers --color=always {} | head -500')"
main(){
IFS=$':'; read -a q <<<$(sort "$DECK" -n --field-separator=: --key=4 | head | shuf -n 1)
if [ $COUNTER -gt 0 ]; then # If not the first flash card then pause before screen clearing
read _
fi
clear
echo "===================================================="
echo "Cards Reviewed: $COUNTER"
echo "Category: ${q[0]}"
echo "Question: ${q[1]}"
echo ''
read -sn 1 NEXT
if [ "$NEXT" = q ]; then
cd "$PWD" && exit
fi
echo "Answer: ${q[2]}"
echo ''
echo "===================================================="
echo ''
echo "How Difficult Was This Question?"
echo ''
echo "Hard [1] Difficult [2] Normal [3] Mild [4] Easy [5]"
echo ''
read -n 1 -p "Answer: " DIFFICULTY_SCORE
COUNTER="$(($COUNTER+1))" # Increment count for above test and card review count increment
if [ "${q[3]}" = 0 ]; then
NEW_ITEM_SCORE=0
case "$DIFFICULTY_SCORE" in
[123]) NEW_ITEM_SCORE=0 ;;#HARD DIFFICULTY & NORMAL
4) NEW_ITEM_SCORE=1 ;;#MILD
5) NEW_ITEM_SCORE=2 ;;#EASY
*) NEW_ITEM_SCORE=0 ;;#INVALID
esac
elif [ "${q[3]}" = 1 ]; then
case "$DIFFICULTY_SCORE" in
1) NEW_ITEM_SCORE=0 ;;#HARD
2) NEW_ITEM_SCORE=0 ;;#DIFFICULT
3) NEW_ITEM_SCORE=1 ;;#NORMAL
4) NEW_ITEM_SCORE=2 ;;#MILD
5) NEW_ITEM_SCORE=3 ;;#EASY
*) NEW_ITEM_SCORE=1 ;;#INVALID
esac
else
case "$DIFFICULTY_SCORE" in
1) NEW_ITEM_SCORE="$((${q[3]}-2))" ;;#HARD
2) NEW_ITEM_SCORE="$((${q[3]}-1))" ;;#DIFFICULTY
3) NEW_ITEM_SCORE="${q[3]}" ;;#NORMAL
4) NEW_ITEM_SCORE="$((${q[3]}+1))" ;;#MILD
5) NEW_ITEM_SCORE="$((${q[3]}+2))" ;;#EASY
*) NEW_ITEM_SCORE="${q[3]}" ;;#INVALID
esac
fi
# Update item score for each flashcard item
sed -i "s/${q[0]}:${q[1]}:${q[2]}:${q[3]}/${q[0]}:${q[1]}:${q[2]}:$NEW_ITEM_SCORE/g" "$DECK"
}
while true; do
main
done