shfmt formatting & if-else shortened (minor) (#33)
* Applied shfmt formatting * Minor optimizations Some if-else flows only contained 1 statement, in shell, a shorter version of those can be achieved by using the conditional operators, among other tiny edits. * Extra safety for multiple conditions This is probably unnecessary, but just for extra safety, braces can be added.
This commit is contained in:
parent
81ca45379b
commit
55b8b9a896
49
flash
49
flash
|
|
@ -114,11 +114,12 @@ Science:What is the distance between the Earth and Sol called?:An Astronomical U
|
||||||
Programming:What is the best operating system?:Arch, because BTW i run Arch:999"
|
Programming:What is the best operating system?:Arch, because BTW i run Arch:999"
|
||||||
|
|
||||||
# Define setup process in a function and create necessary files for user
|
# Define setup process in a function and create necessary files for user
|
||||||
setup(){\
|
setup() {
|
||||||
mkdir "$DIR" && \
|
mkdir "$DIR" &&
|
||||||
touch {"$EXAMPLE_DECK","$HIGH_SCORE","$REVIEW_LOG"} && \
|
touch {"$EXAMPLE_DECK","$HIGH_SCORE","$REVIEW_LOG"} &&
|
||||||
echo "$DECK_TEMPLATE" >> "$EXAMPLE_DECK" && \
|
echo "$DECK_TEMPLATE" >>"$EXAMPLE_DECK" &&
|
||||||
echo -e "$DIR_MADE_MSG" ;}
|
echo -e "$DIR_MADE_MSG"
|
||||||
|
}
|
||||||
|
|
||||||
# Test if .local/share exists and wether to offer setup process
|
# Test if .local/share exists and wether to offer setup process
|
||||||
if [ ! -d "$DIR" ]; then
|
if [ ! -d "$DIR" ]; then
|
||||||
|
|
@ -137,19 +138,14 @@ fi
|
||||||
|
|
||||||
# If there are no flashcard decks available return user to starting location
|
# If there are no flashcard decks available return user to starting location
|
||||||
# while also displaying explanatory text of issue
|
# while also displaying explanatory text of issue
|
||||||
if [ "$(find . -maxdepth "$SEARCH_DEPTH" -iname "*.csv" | wc -l)" = 0 ]; then
|
[ "$(find . -maxdepth "$SEARCH_DEPTH" -iname "*.csv" | wc -l)" = 0 ] &&
|
||||||
echo -e "$NO_DECKS" && cd "$PWD" && exit 1
|
echo -e "$NO_DECKS" && cd "$PWD" && exit 1
|
||||||
fi
|
|
||||||
|
|
||||||
# if highscore file was removed, remake it.
|
# if highscore file was removed, remake it.
|
||||||
if [ ! -e "$HIGH_SCORE" ]; then
|
[ ! -e "$HIGH_SCORE" ] && touch "$HIGH_SCORE"
|
||||||
touch "$HIGH_SCORE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# if reviewed file was removed, remake it.
|
# if reviewed file was removed, remake it.
|
||||||
if [ ! -e "$REVIEW_LOG" ]; then
|
[ ! -e "$REVIEW_LOG" ] && touch "$REVIEW_LOG"
|
||||||
touch "$REVIEW_LOG"
|
|
||||||
fi
|
|
||||||
|
|
||||||
print_usage() {
|
print_usage() {
|
||||||
echo -e "\n${LCYAN}fla.sh --- Flash card system by Bryan Jenks${NC} ${LBLUE}<BryanJenks@protonmail.com>${NC}\n\n${YELLOW}${BOLD}Usage:${NC}\n\t${GREEN}flash -h:${NC} Print this help text\n\t${GREEN}flash -i:${NC} Print Information about the flashcard system\n\t${GREEN}flash -v:${NC} Print version Number\n\t${GREEN}flash -p [BINARY]:${NC} Change the previewer when selecting decks.\n\t\tDefault: ${GREEN}bat${NC}\n\t\tSupported: ${GREEN}bat${NC}, ${GREEN}cat${NC}\n"
|
echo -e "\n${LCYAN}fla.sh --- Flash card system by Bryan Jenks${NC} ${LBLUE}<BryanJenks@protonmail.com>${NC}\n\n${YELLOW}${BOLD}Usage:${NC}\n\t${GREEN}flash -h:${NC} Print this help text\n\t${GREEN}flash -i:${NC} Print Information about the flashcard system\n\t${GREEN}flash -v:${NC} Print version Number\n\t${GREEN}flash -p [BINARY]:${NC} Change the previewer when selecting decks.\n\t\tDefault: ${GREEN}bat${NC}\n\t\tSupported: ${GREEN}bat${NC}, ${GREEN}cat${NC}\n"
|
||||||
|
|
@ -203,12 +199,11 @@ while getopts 'hivp:' flag; do
|
||||||
h) print_usage && exit 0 ;;
|
h) print_usage && exit 0 ;;
|
||||||
i) print_info && exit 0 ;;
|
i) print_info && exit 0 ;;
|
||||||
v) echo -e "\n${YELLOW}fla.sh Current Version:${NC} ${RED}1.1${NC}\n" && exit 0 ;;
|
v) echo -e "\n${YELLOW}fla.sh Current Version:${NC} ${RED}1.1${NC}\n" && exit 0 ;;
|
||||||
p) if [[ $(command -v "$OPTARG" 2>&1) ]]; then PREVIEWER=$OPTARG; else echo "Unable to find previewer $OPTARG. Exiting..." && exit 1; fi ;;
|
p) { [[ $(command -v "$OPTARG" 2>&1) ]] && PREVIEWER=$OPTARG; } || echo "Unable to find previewer $OPTARG. Exiting..." && exit 1 ;;
|
||||||
*) print_usage && exit 1 ;;
|
*) print_usage && exit 1 ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
||||||
# Set some parameters for preview command used by FZF
|
# Set some parameters for preview command used by FZF
|
||||||
while [ -z "$PREVIEWER_PARAMTERS" ]; do
|
while [ -z "$PREVIEWER_PARAMTERS" ]; do
|
||||||
case "$PREVIEWER" in
|
case "$PREVIEWER" in
|
||||||
|
|
@ -242,7 +237,7 @@ ${ORANGE}${BOLD}Cards Reviewed:${BOLD}${NC}\t$COUNTER
|
||||||
${LGREY}Category:${NC} ${q[0]}
|
${LGREY}Category:${NC} ${q[0]}
|
||||||
___________________________________________________________
|
___________________________________________________________
|
||||||
|
|
||||||
$(if [ ${#QUESTION} -lt 59 ]; then printf "%$(( ( 59 - ${#QUESTION} ) / 2 ))s"; fi)$QUESTION"
|
$([ ${#QUESTION} -lt 59 ] && printf "%$(((59 - ${#QUESTION}) / 2))s")$QUESTION"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -259,9 +254,7 @@ add_usage_entry(){
|
||||||
# If no deck is selected in fzf menu
|
# If no deck is selected in fzf menu
|
||||||
# return user to start location
|
# return user to start location
|
||||||
# and exit quietly
|
# and exit quietly
|
||||||
if [ -z "$DECK" ]; then
|
[ -z "$DECK" ] && clear && cd "$PWD" && exit 1
|
||||||
clear && cd "$PWD" && exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
IFS=$':'
|
IFS=$':'
|
||||||
|
|
@ -279,15 +272,13 @@ ${LGREY}──────────────── Press [Enter] to contin
|
||||||
done
|
done
|
||||||
clear
|
clear
|
||||||
print_head
|
print_head
|
||||||
if [ "$NEXT" = q ] || [ "$NEXT" = Q ]; then
|
{ [ "$NEXT" = q ] || [ "$NEXT" = Q ]; } && add_usage_entry && cd "$PWD" && clear && exit 0
|
||||||
add_usage_entry && cd "$PWD" && clear && exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
ANSWER=$(echo "${q[2]}" | fold -w 59)
|
ANSWER=$(echo "${q[2]}" | fold -w 59)
|
||||||
|
|
||||||
echo -e "___________________________________________________________
|
echo -e "___________________________________________________________
|
||||||
|
|
||||||
$(if [ ${#ANSWER} -lt 59 ]; then printf "%$(( ( 59 - ${#ANSWER} ) / 2 ))s"; fi)$ANSWER
|
$([ ${#ANSWER} -lt 59 ] && printf "%$(((59 - ${#ANSWER}) / 2))s")$ANSWER
|
||||||
|
|
||||||
${WHITEBG}${WHITE}===========================================================${NC}
|
${WHITEBG}${WHITE}===========================================================${NC}
|
||||||
|
|
||||||
|
|
@ -302,9 +293,8 @@ ${LGREY}Select a number to continue, or${NC} ${LRED}Q${NC} ${LGREY}to quit...${N
|
||||||
# shellcheck disable=SC2162
|
# shellcheck disable=SC2162
|
||||||
read -sn 1 DIFFICULTY_SCORE
|
read -sn 1 DIFFICULTY_SCORE
|
||||||
done
|
done
|
||||||
if [ "$DIFFICULTY_SCORE" = q ] || [ "$DIFFICULTY_SCORE" = Q ]; then
|
{ [ "$DIFFICULTY_SCORE" = q ] || [ "$DIFFICULTY_SCORE" = Q ]; } &&
|
||||||
add_usage_entry && cd "$PWD" && clear && exit 0
|
add_usage_entry && cd "$PWD" && clear && exit 0
|
||||||
fi
|
|
||||||
clear
|
clear
|
||||||
|
|
||||||
COUNTER="$((COUNTER + 1))" # Increment count for card review count increment
|
COUNTER="$((COUNTER + 1))" # Increment count for card review count increment
|
||||||
|
|
@ -344,15 +334,10 @@ ${LGREY}Select a number to continue, or${NC} ${LRED}Q${NC} ${LGREY}to quit...${N
|
||||||
ANSWER_REGEX=$(sed "s/\(\/\|\[\|\]\|\*\)/\\\\\1/g" <<<"${q[2]}")
|
ANSWER_REGEX=$(sed "s/\(\/\|\[\|\]\|\*\)/\\\\\1/g" <<<"${q[2]}")
|
||||||
sed -i "s/${q[0]}:$QUESTION_REGEX:$ANSWER_REGEX:${q[3]}/${q[0]}:$QUESTION_REGEX:$ANSWER_REGEX:$NEW_ITEM_SCORE/g" "$DECK"
|
sed -i "s/${q[0]}:$QUESTION_REGEX:$ANSWER_REGEX:${q[3]}/${q[0]}:$QUESTION_REGEX:$ANSWER_REGEX:$NEW_ITEM_SCORE/g" "$DECK"
|
||||||
# If no highscore currently set, set it.
|
# If no highscore currently set, set it.
|
||||||
if [ -z "$(cat "$HIGH_SCORE")" ]; then
|
[ -z "$(cat "$HIGH_SCORE")" ] && echo "$COUNTER" >"$HIGH_SCORE"
|
||||||
echo "$COUNTER" > "$HIGH_SCORE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# If Cards Reviewed > Current High Score, Update
|
# If Cards Reviewed > Current High Score, Update
|
||||||
if [ "$COUNTER" -gt "$(cat "$HIGH_SCORE")" ]; then
|
[ "$COUNTER" -gt "$(cat "$HIGH_SCORE")" ] && echo "$COUNTER" >"$HIGH_SCORE"
|
||||||
echo "$COUNTER" > "$HIGH_SCORE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue