@@ -7,7 +7,7 @@ set -eou pipefail
7
7
8
8
pname=" bashcards"
9
9
ext=" bcrds"
10
- version=" 0.1.2 "
10
+ version=" 0.1.3 "
11
11
12
12
function version {
13
13
echo " $version "
@@ -40,8 +40,11 @@ function dir_not_found {
40
40
41
41
# to_filename transforms `/path/to/file.ext` to `file`
42
42
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% .* } "
45
48
46
49
echo " $filename "
47
50
@@ -59,23 +62,23 @@ function make_card {
59
62
local vborder_length=" $2 "
60
63
shift
61
64
shift
62
- local content=" $@ "
65
+ local content=" $* "
63
66
64
67
echo " "
65
- printf ' –%.0s' $( seq 1 $hborder_length )
68
+ printf ' –%.0s' $( seq 1 " $hborder_length " )
66
69
echo " "
67
70
printf " |"
68
- printf ' %.0s' $( seq 1 $vborder_length )
71
+ printf ' %.0s' $( seq 1 " $vborder_length " )
69
72
printf " |"
70
73
echo " "
71
74
72
75
echo " | $content |"
73
76
74
77
printf " |"
75
- printf ' %.0s' $( seq 1 $vborder_length )
78
+ printf ' %.0s' $( seq 1 " $vborder_length " )
76
79
printf " |"
77
80
echo " "
78
- printf ' –%.0s' $( seq 1 $hborder_length )
81
+ printf ' –%.0s' $( seq 1 " $hborder_length " )
79
82
echo " "
80
83
81
84
return 0
@@ -91,54 +94,60 @@ function run_cards {
91
94
clearscreen
92
95
93
96
local cards_name=$1
94
- eval " declare -A cards=" ${2#* =}
97
+ eval " declare -A cards=" " ${2#* =} "
95
98
local -A updated_cards
96
99
97
100
local cards_keys=(" ${! cards[@]} " )
98
101
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 )) "
100
103
local random_cards_key=" ${cards_keys[$random_cards_key_index]} "
101
104
102
105
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 )) "
104
107
105
108
local front=" ${selected_key_val[$random_selected_index]} "
106
109
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 ))
109
112
110
- local back=" ${selected_key_val[$(( $ random_selected_index == 0 ? 1 : 0))]} "
113
+ local back=" ${selected_key_val[$(( random_selected_index == 0 ? 1 : 0))]} "
111
114
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 ))
114
117
115
118
# print cards name
116
- echo " $( to_filename $cards_name ) "
119
+ local output_name
120
+ output_name=" $( to_filename " $cards_name " ) "
121
+ echo " $output_name "
117
122
118
123
# 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 "
120
127
121
- read -p " (Press return to flip)" _
128
+ read -rp " (Press return to flip)" _
122
129
123
130
# 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 "
125
134
126
135
for key in " ${! cards[@]} " ; do
127
136
if [ " $key " != " $random_cards_key " ]; then
128
137
updated_cards[" $key " ]=" ${cards[$key]} "
129
138
fi
130
139
done
131
140
132
- if [ ! -v updated_cards[@] ]; then
141
+ if [[ ! -v updated_cards[@] ] ]; then
133
142
echo " "
134
143
echo " All done!"
135
- read -p " (Press return to exit)" _
144
+ read -rp " (Press return to exit)" _
136
145
exit 0
137
146
else
138
- read -p " (Press return for next card)" _
147
+ read -rp " (Press return for next card)" _
139
148
fi
140
149
141
- run_cards $cards_name " $( declare -p updated_cards) "
150
+ run_cards " $cards_name " " $( declare -p updated_cards) "
142
151
143
152
return 0
144
153
}
@@ -150,7 +159,7 @@ function select_file {
150
159
local -a opts
151
160
152
161
for file in " ${files[@]} " ; do
153
- opts+=(" $( to_filename $file ) " )
162
+ opts+=(" $( to_filename " $file " ) " )
154
163
done
155
164
156
165
clearscreen
@@ -168,23 +177,23 @@ function select_file {
168
177
fi
169
178
170
179
# get the input
171
- read -p " > " opt
180
+ read -rp " > " opt
172
181
173
182
# make sure the option is in
174
183
# the list and then continue
175
184
if
176
185
[[ " $opt " =~ ^[0-9]+$ ]] &&
177
- [ $opt -gt 0 ] &&
178
- [ $opt -lt $(( ${# files[@]} + 1 )) ]
186
+ [ " $opt " -gt 0 ] &&
187
+ [ " $opt " -lt $(( ${# files[@]} + 1 )) ]
179
188
then
180
189
local selected_file=" ${files[$opt-1]} "
181
190
local -A cards
182
191
183
- while IFS=\= read key value; do
192
+ while IFS=" = " read -r key value; do
184
193
cards[" $key " ]=" $value "
185
- done < $selected_file
194
+ done < " $selected_file "
186
195
187
- run_cards $selected_file " $( declare -p cards) "
196
+ run_cards " $selected_file " " $( declare -p cards) "
188
197
else
189
198
select_file " (Please select from the options)" " ${files[@]} "
190
199
fi
@@ -195,8 +204,8 @@ function select_file {
195
204
function start {
196
205
local -a files
197
206
198
- if [ -d $1 ]; then
199
- for file in $1 /* .$ext ; do
207
+ if [ -d " $1 " ]; then
208
+ for file in " $1 " /* ." $ext " ; do
200
209
files+=(" $file " )
201
210
done
202
211
@@ -215,7 +224,7 @@ function start {
215
224
# Determine command
216
225
217
226
case " $1 " in
218
- -d|--dir ) [[ ! -n " ${2-} " ]] && dir_not_found; start $2 ;;
227
+ -d|--dir ) [[ -z " ${2-} " ]] && dir_not_found; start " $2 " ;;
219
228
-h|--help ) usage;;
220
229
-v|--version) version;;
221
230
* ) unknown-cmd;;
0 commit comments