diff --git a/CHANGELOG.md b/CHANGELOG.md index 60dfb11..5ee63b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,23 +1,34 @@ # Changelog + All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [0.1.3] - 2020-08-30 + +### Fixed + +* shellcheck warnings + ## [0.1.2] - 2020-05-09 ### Fixed -Include missing version information + +* include missing version information ### Fixed + * use `printf` instead of `clear` to clear the screen ## [0.1.1] - 2020-05-09 ### Fixed + * use `printf` instead of `clear` to clear the screen ## [0.1.0] - 2020-05-09 ### Added + * all the things diff --git a/bashcards b/bashcards index a96c56d..2a7fa70 100755 --- a/bashcards +++ b/bashcards @@ -7,7 +7,7 @@ set -eou pipefail pname="bashcards" ext="bcrds" -version="0.1.2" +version="0.1.3" function version { echo "$version" @@ -40,8 +40,11 @@ function dir_not_found { # to_filename transforms `/path/to/file.ext` to `file` function to_filename { - local full_filename=$(basename "$1") - local filename="${full_filename%.*}" + local full_filename + local filename + + full_filename=$(basename "$1") + filename="${full_filename%.*}" echo "$filename" @@ -59,23 +62,23 @@ function make_card { local vborder_length="$2" shift shift - local content="$@" + local content="$*" echo "" - printf '–%.0s' $(seq 1 $hborder_length) + printf '–%.0s' $(seq 1 "$hborder_length") echo "" printf "|" - printf ' %.0s' $(seq 1 $vborder_length) + printf ' %.0s' $(seq 1 "$vborder_length") printf "|" echo "" echo "| $content |" printf "|" - printf ' %.0s' $(seq 1 $vborder_length) + printf ' %.0s' $(seq 1 "$vborder_length") printf "|" echo "" - printf '–%.0s' $(seq 1 $hborder_length) + printf '–%.0s' $(seq 1 "$hborder_length") echo "" return 0 @@ -91,37 +94,43 @@ function run_cards { clearscreen local cards_name=$1 - eval "declare -A cards="${2#*=} + eval "declare -A cards=""${2#*=}" local -A updated_cards local cards_keys=("${!cards[@]}") local cards_keys_count=${#cards_keys[@]} - local random_cards_key_index="$[$RANDOM % $cards_keys_count]" + local random_cards_key_index="$(( RANDOM % cards_keys_count ))" local random_cards_key="${cards_keys[$random_cards_key_index]}" local selected_key_val=("$random_cards_key" "${cards[$random_cards_key]}") - local random_selected_index="$[$RANDOM % 2]" + local random_selected_index="$(( RANDOM % 2 ))" local front="${selected_key_val[$random_selected_index]}" local front_length=${#front} - local front_hborder_length=$(( $front_length + 6 )) - local front_vborder_length=$(( $front_length + 4 )) + local front_hborder_length=$(( front_length + 6 )) + local front_vborder_length=$(( front_length + 4 )) - local back="${selected_key_val[$(( $random_selected_index == 0 ? 1 : 0))]}" + local back="${selected_key_val[$(( random_selected_index == 0 ? 1 : 0))]}" local back_length=${#back} - local back_hborder_length=$(( $back_length + 6 )) - local back_vborder_length=$(( $back_length + 4 )) + local back_hborder_length=$(( back_length + 6 )) + local back_vborder_length=$(( back_length + 4 )) # print cards name - echo "$(to_filename $cards_name)" + local output_name + output_name="$(to_filename "$cards_name")" + echo "$output_name" # print front - echo "$(make_card $front_hborder_length $front_vborder_length $front)" + local output_front + output_front="$(make_card $front_hborder_length $front_vborder_length "$front")" + echo "$output_front" - read -p "(Press return to flip)" _ + read -rp "(Press return to flip)" _ # print back - echo "$(make_card $back_hborder_length $back_vborder_length $back)" + local output_back + output_back="$(make_card $back_hborder_length $back_vborder_length "$back")" + echo "$output_back" for key in "${!cards[@]}"; do if [ "$key" != "$random_cards_key" ]; then @@ -129,16 +138,16 @@ function run_cards { fi done - if [ ! -v updated_cards[@] ]; then + if [[ ! -v updated_cards[@] ]]; then echo "" echo "All done!" - read -p "(Press return to exit)" _ + read -rp "(Press return to exit)" _ exit 0 else - read -p "(Press return for next card)" _ + read -rp "(Press return for next card)" _ fi - run_cards $cards_name "$(declare -p updated_cards)" + run_cards "$cards_name" "$(declare -p updated_cards)" return 0 } @@ -150,7 +159,7 @@ function select_file { local -a opts for file in "${files[@]}"; do - opts+=("$(to_filename $file)") + opts+=("$(to_filename "$file")") done clearscreen @@ -168,23 +177,23 @@ function select_file { fi # get the input - read -p "> " opt + read -rp "> " opt # make sure the option is in # the list and then continue if [[ "$opt" =~ ^[0-9]+$ ]] && - [ $opt -gt 0 ] && - [ $opt -lt $((${#files[@]} + 1)) ] + [ "$opt" -gt 0 ] && + [ "$opt" -lt $((${#files[@]} + 1)) ] then local selected_file="${files[$opt-1]}" local -A cards - while IFS=\= read key value; do + while IFS="=" read -r key value; do cards["$key"]="$value" - done < $selected_file + done < "$selected_file" - run_cards $selected_file "$(declare -p cards)" + run_cards "$selected_file" "$(declare -p cards)" else select_file "(Please select from the options)" "${files[@]}" fi @@ -195,8 +204,8 @@ function select_file { function start { local -a files - if [ -d $1 ]; then - for file in $1/*.$ext; do + if [ -d "$1" ]; then + for file in "$1"/*."$ext"; do files+=("$file") done @@ -215,7 +224,7 @@ function start { # Determine command case "$1" in - -d|--dir ) [[ ! -n "${2-}" ]] && dir_not_found; start $2;; + -d|--dir ) [[ -z "${2-}" ]] && dir_not_found; start "$2";; -h|--help ) usage;; -v|--version) version;; *) unknown-cmd;; diff --git a/bashcards.8 b/bashcards.8 index 3890856..bc53ca6 100644 --- a/bashcards.8 +++ b/bashcards.8 @@ -1,6 +1,6 @@ .\" Manpage for bashcards. .\" Contact me@robertwpearce.com to correct errors or typos. -.TH man 8 "08 May 2020" "0.1.0" "bashcards man page" +.TH man 8 "30 August 2020" "0.1.3" "bashcards man page" .SH NAME bashcards \- Practice flashcards in bash .SH SYNOPSIS