Skip to content

Commit

Permalink
Merge pull request #2 from rpearce/fix/shellcheck
Browse files Browse the repository at this point in the history
Fix shellcheck warnings
  • Loading branch information
rpearce authored Aug 30, 2020
2 parents 3b9367e + 6ed6a89 commit 8953bc2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
77 changes: 43 additions & 34 deletions bashcards
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set -eou pipefail

pname="bashcards"
ext="bcrds"
version="0.1.2"
version="0.1.3"

function version {
echo "$version"
Expand Down Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -91,54 +94,60 @@ 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
updated_cards["$key"]="${cards[$key]}"
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
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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;;
Expand Down
2 changes: 1 addition & 1 deletion bashcards.8
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" Manpage for bashcards.
.\" Contact [email protected] 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
Expand Down

0 comments on commit 8953bc2

Please sign in to comment.