-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflash
More file actions
44 lines (38 loc) · 1.24 KB
/
flash
File metadata and controls
44 lines (38 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/sh
#
# flash - minimalistic flashcards for the terminal
# Parse command-line arguments. See also: `man getopts`.
while getopts hr opt; do
case "$opt" in
h)
man 1 flash
exit;;
r)
reverse=1;;
?)
# Print help instructions to standard error and exit with code 2.
printf "Try 'man flash' for more information.\n" >&2
exit 2;;
esac
done
# Read a line of input and split it on `FLASH_DELIMITER`, defaulting to `:`.
while IFS=${FLASH_DELIMITER:-':'} read q a; do
# Remove leading whitespace.
q="${q#${q%%[![:space:]]*}}"
a="${a#${a%%[![:space:]]*}}"
# Ignore blank lines.
[ -z "$q" ] && [ -z "$a" ] && continue
# Reverse the question order if the corresponding flag is set.
[ "$reverse" = 1 ] && tmp=$q && q=$a && a=$tmp
# Print the question.
printf '%s' "$q"
# Wait for user input, then print the answer. This initially attempts to
# use non-POSIX features to improve ergonomics, but gracefully falls back
# to POSIX compatibility mode on failure.
if read -rsn 1 < /dev/tty 2> /dev/null; then
printf '\n%s\n\n' "$a"
else
read -r _ < /dev/tty
printf '%s\n\n' "$a"
fi
done