Skip to content

Commit 8953bc2

Browse files
authored
Merge pull request #2 from rpearce/fix/shellcheck
Fix shellcheck warnings
2 parents 3b9367e + 6ed6a89 commit 8953bc2

File tree

3 files changed

+56
-36
lines changed

3 files changed

+56
-36
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
# Changelog
2+
23
All notable changes to this project will be documented in this file.
34

45
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
56
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
67

8+
## [0.1.3] - 2020-08-30
9+
10+
### Fixed
11+
12+
* shellcheck warnings
13+
714
## [0.1.2] - 2020-05-09
815

916
### Fixed
10-
Include missing version information
17+
18+
* include missing version information
1119

1220
### Fixed
21+
1322
* use `printf` instead of `clear` to clear the screen
1423

1524
## [0.1.1] - 2020-05-09
1625

1726
### Fixed
27+
1828
* use `printf` instead of `clear` to clear the screen
1929

2030
## [0.1.0] - 2020-05-09
2131

2232
### Added
33+
2334
* all the things

bashcards

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set -eou pipefail
77

88
pname="bashcards"
99
ext="bcrds"
10-
version="0.1.2"
10+
version="0.1.3"
1111

1212
function version {
1313
echo "$version"
@@ -40,8 +40,11 @@ function dir_not_found {
4040

4141
# to_filename transforms `/path/to/file.ext` to `file`
4242
function to_filename {
43-
local full_filename=$(basename "$1")
44-
local filename="${full_filename%.*}"
43+
local full_filename
44+
local filename
45+
46+
full_filename=$(basename "$1")
47+
filename="${full_filename%.*}"
4548

4649
echo "$filename"
4750

@@ -59,23 +62,23 @@ function make_card {
5962
local vborder_length="$2"
6063
shift
6164
shift
62-
local content="$@"
65+
local content="$*"
6366

6467
echo ""
65-
printf '–%.0s' $(seq 1 $hborder_length)
68+
printf '–%.0s' $(seq 1 "$hborder_length")
6669
echo ""
6770
printf "|"
68-
printf ' %.0s' $(seq 1 $vborder_length)
71+
printf ' %.0s' $(seq 1 "$vborder_length")
6972
printf "|"
7073
echo ""
7174

7275
echo "| $content |"
7376

7477
printf "|"
75-
printf ' %.0s' $(seq 1 $vborder_length)
78+
printf ' %.0s' $(seq 1 "$vborder_length")
7679
printf "|"
7780
echo ""
78-
printf '–%.0s' $(seq 1 $hborder_length)
81+
printf '–%.0s' $(seq 1 "$hborder_length")
7982
echo ""
8083

8184
return 0
@@ -91,54 +94,60 @@ function run_cards {
9194
clearscreen
9295

9396
local cards_name=$1
94-
eval "declare -A cards="${2#*=}
97+
eval "declare -A cards=""${2#*=}"
9598
local -A updated_cards
9699

97100
local cards_keys=("${!cards[@]}")
98101
local cards_keys_count=${#cards_keys[@]}
99-
local random_cards_key_index="$[$RANDOM % $cards_keys_count]"
102+
local random_cards_key_index="$(( RANDOM % cards_keys_count ))"
100103
local random_cards_key="${cards_keys[$random_cards_key_index]}"
101104

102105
local selected_key_val=("$random_cards_key" "${cards[$random_cards_key]}")
103-
local random_selected_index="$[$RANDOM % 2]"
106+
local random_selected_index="$(( RANDOM % 2 ))"
104107

105108
local front="${selected_key_val[$random_selected_index]}"
106109
local front_length=${#front}
107-
local front_hborder_length=$(( $front_length + 6 ))
108-
local front_vborder_length=$(( $front_length + 4 ))
110+
local front_hborder_length=$(( front_length + 6 ))
111+
local front_vborder_length=$(( front_length + 4 ))
109112

110-
local back="${selected_key_val[$(( $random_selected_index == 0 ? 1 : 0))]}"
113+
local back="${selected_key_val[$(( random_selected_index == 0 ? 1 : 0))]}"
111114
local back_length=${#back}
112-
local back_hborder_length=$(( $back_length + 6 ))
113-
local back_vborder_length=$(( $back_length + 4 ))
115+
local back_hborder_length=$(( back_length + 6 ))
116+
local back_vborder_length=$(( back_length + 4 ))
114117

115118
# print cards name
116-
echo "$(to_filename $cards_name)"
119+
local output_name
120+
output_name="$(to_filename "$cards_name")"
121+
echo "$output_name"
117122

118123
# print front
119-
echo "$(make_card $front_hborder_length $front_vborder_length $front)"
124+
local output_front
125+
output_front="$(make_card $front_hborder_length $front_vborder_length "$front")"
126+
echo "$output_front"
120127

121-
read -p "(Press return to flip)" _
128+
read -rp "(Press return to flip)" _
122129

123130
# print back
124-
echo "$(make_card $back_hborder_length $back_vborder_length $back)"
131+
local output_back
132+
output_back="$(make_card $back_hborder_length $back_vborder_length "$back")"
133+
echo "$output_back"
125134

126135
for key in "${!cards[@]}"; do
127136
if [ "$key" != "$random_cards_key" ]; then
128137
updated_cards["$key"]="${cards[$key]}"
129138
fi
130139
done
131140

132-
if [ ! -v updated_cards[@] ]; then
141+
if [[ ! -v updated_cards[@] ]]; then
133142
echo ""
134143
echo "All done!"
135-
read -p "(Press return to exit)" _
144+
read -rp "(Press return to exit)" _
136145
exit 0
137146
else
138-
read -p "(Press return for next card)" _
147+
read -rp "(Press return for next card)" _
139148
fi
140149

141-
run_cards $cards_name "$(declare -p updated_cards)"
150+
run_cards "$cards_name" "$(declare -p updated_cards)"
142151

143152
return 0
144153
}
@@ -150,7 +159,7 @@ function select_file {
150159
local -a opts
151160

152161
for file in "${files[@]}"; do
153-
opts+=("$(to_filename $file)")
162+
opts+=("$(to_filename "$file")")
154163
done
155164

156165
clearscreen
@@ -168,23 +177,23 @@ function select_file {
168177
fi
169178

170179
# get the input
171-
read -p "> " opt
180+
read -rp "> " opt
172181

173182
# make sure the option is in
174183
# the list and then continue
175184
if
176185
[[ "$opt" =~ ^[0-9]+$ ]] &&
177-
[ $opt -gt 0 ] &&
178-
[ $opt -lt $((${#files[@]} + 1)) ]
186+
[ "$opt" -gt 0 ] &&
187+
[ "$opt" -lt $((${#files[@]} + 1)) ]
179188
then
180189
local selected_file="${files[$opt-1]}"
181190
local -A cards
182191

183-
while IFS=\= read key value; do
192+
while IFS="=" read -r key value; do
184193
cards["$key"]="$value"
185-
done < $selected_file
194+
done < "$selected_file"
186195

187-
run_cards $selected_file "$(declare -p cards)"
196+
run_cards "$selected_file" "$(declare -p cards)"
188197
else
189198
select_file "(Please select from the options)" "${files[@]}"
190199
fi
@@ -195,8 +204,8 @@ function select_file {
195204
function start {
196205
local -a files
197206

198-
if [ -d $1 ]; then
199-
for file in $1/*.$ext; do
207+
if [ -d "$1" ]; then
208+
for file in "$1"/*."$ext"; do
200209
files+=("$file")
201210
done
202211

@@ -215,7 +224,7 @@ function start {
215224
# Determine command
216225

217226
case "$1" in
218-
-d|--dir ) [[ ! -n "${2-}" ]] && dir_not_found; start $2;;
227+
-d|--dir ) [[ -z "${2-}" ]] && dir_not_found; start "$2";;
219228
-h|--help ) usage;;
220229
-v|--version) version;;
221230
*) unknown-cmd;;

bashcards.8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.\" Manpage for bashcards.
22
.\" Contact [email protected] to correct errors or typos.
3-
.TH man 8 "08 May 2020" "0.1.0" "bashcards man page"
3+
.TH man 8 "30 August 2020" "0.1.3" "bashcards man page"
44
.SH NAME
55
bashcards \- Practice flashcards in bash
66
.SH SYNOPSIS

0 commit comments

Comments
 (0)