Return from user prompt without requiring Enter (#354)
* Tests: Add coverage for del / move without -f, but with prompting Supplying the user confirmation via "yes". * Cosmetics: Align inconsistent spacing for before (y/n) prompt * Refactoring: Extract confirm() function The user confirmation query had been duplicated (once) in the code. * Refactoring: confirm(): Leave early if forced * Return from user prompt without requiring Enter By just reading a single character (y for yes, anything else: no). * Tests: Ensure that only a single "y" concludes the confirmation By switching from "yes" (that endlessly prints newline-separated "y"s) to "printf y". * t1800-del: Add coverage for negative confirmation Negative means "anything but y", so "n", "x", and Enter all apply. * Cosmetics: Add trailing space after (y/n) prompt So that the user's typed answer is not recorded directly after it, but with separation: "Foo? (y/n) y" instead of "Foo? (y/n)y". *Compatibility: "read -N 1" is only available in Bash 4.1+ Mac OS still ships with Bash 3.2 :-( Fall back to the original prompting that requires conclusion via Enter then. Note: Even though the tests use "printf y", this still gets accepted, as there'll be EOF after that. In real use (when stdin from the terminal stays open), a concluding Enter is mandatory, though. Closes #152
This commit is contained in:
@@ -4,6 +4,8 @@ test_description='basic del functionality
|
||||
'
|
||||
. ./test-lib.sh
|
||||
|
||||
SPACE=' '
|
||||
|
||||
test_todo_session 'del usage' <<EOF
|
||||
>>> todo.sh del B
|
||||
usage: todo.sh del ITEM# [TERM]
|
||||
@@ -44,6 +46,50 @@ TODO: 1 deleted.
|
||||
TODO: 2 of 2 tasks shown
|
||||
EOF
|
||||
|
||||
cat > todo.txt <<EOF
|
||||
(B) smell the uppercase Roses +flowers @outside
|
||||
(A) notice the sunflowers
|
||||
stop
|
||||
EOF
|
||||
test_todo_session 'del with confirmation' <<EOF
|
||||
>>> todo.sh -p list
|
||||
2 (A) notice the sunflowers
|
||||
1 (B) smell the uppercase Roses +flowers @outside
|
||||
3 stop
|
||||
--
|
||||
TODO: 3 of 3 tasks shown
|
||||
|
||||
>>> printf n | todo.sh del 1
|
||||
Delete '(B) smell the uppercase Roses +flowers @outside'? (y/n)$SPACE
|
||||
TODO: No tasks were deleted.
|
||||
|
||||
>>> todo.sh -p list
|
||||
2 (A) notice the sunflowers
|
||||
1 (B) smell the uppercase Roses +flowers @outside
|
||||
3 stop
|
||||
--
|
||||
TODO: 3 of 3 tasks shown
|
||||
|
||||
>>> printf x | todo.sh del 1
|
||||
Delete '(B) smell the uppercase Roses +flowers @outside'? (y/n)$SPACE
|
||||
TODO: No tasks were deleted.
|
||||
|
||||
>>> echo | todo.sh del 1
|
||||
Delete '(B) smell the uppercase Roses +flowers @outside'? (y/n)$SPACE
|
||||
TODO: No tasks were deleted.
|
||||
|
||||
>>> printf y | todo.sh del 1
|
||||
Delete '(B) smell the uppercase Roses +flowers @outside'? (y/n)$SPACE
|
||||
1 (B) smell the uppercase Roses +flowers @outside
|
||||
TODO: 1 deleted.
|
||||
|
||||
>>> todo.sh -p list
|
||||
2 (A) notice the sunflowers
|
||||
3 stop
|
||||
--
|
||||
TODO: 2 of 2 tasks shown
|
||||
EOF
|
||||
|
||||
cat > todo.txt <<EOF
|
||||
(B) smell the uppercase Roses +flowers @outside
|
||||
(A) notice the sunflowers
|
||||
|
||||
@@ -5,6 +5,8 @@ test_description='basic move functionality
|
||||
'
|
||||
. ./test-lib.sh
|
||||
|
||||
SPACE=' '
|
||||
|
||||
cat > todo.txt <<EOF
|
||||
(B) smell the uppercase Roses +flowers @outside
|
||||
(A) notice the sunflowers
|
||||
@@ -31,6 +33,33 @@ TODO: 1 of 1 tasks shown
|
||||
DONE: 3 of 3 tasks shown
|
||||
EOF
|
||||
|
||||
cat > todo.txt <<EOF
|
||||
(B) smell the uppercase Roses +flowers @outside
|
||||
(A) notice the sunflowers
|
||||
EOF
|
||||
cat > done.txt <<EOF
|
||||
x 2009-02-13 make the coffee +wakeup
|
||||
x 2009-02-13 smell the coffee +wakeup
|
||||
EOF
|
||||
test_todo_session 'basic move with confirmation' <<EOF
|
||||
>>> printf y | todo.sh move 1 done.txt 2>&1 | sed -e "s#'[^']\{1,\}/\([^/']\{1,\}\)'#'\1'#g" -e 's#from .\{1,\}/\([^/]\{1,\}\) to .\{1,\}/\([^/]\{1,\}\)?#from \1 to \2?#g'
|
||||
Move '(B) smell the uppercase Roses +flowers @outside' from todo.txt to done.txt? (y/n)$SPACE
|
||||
1 (B) smell the uppercase Roses +flowers @outside
|
||||
TODO: 1 moved from 'todo.txt' to 'done.txt'.
|
||||
|
||||
>>> todo.sh -p ls
|
||||
2 (A) notice the sunflowers
|
||||
--
|
||||
TODO: 1 of 1 tasks shown
|
||||
|
||||
>>> todo.sh -p listfile done.txt
|
||||
3 (B) smell the uppercase Roses +flowers @outside
|
||||
1 x 2009-02-13 make the coffee +wakeup
|
||||
2 x 2009-02-13 smell the coffee +wakeup
|
||||
--
|
||||
DONE: 3 of 3 tasks shown
|
||||
EOF
|
||||
|
||||
test_todo_session 'basic move with passed source' <<EOF
|
||||
>>> todo.sh -f move 2 todo.txt done.txt | sed "s#'[^']\{1,\}/\([^/']\{1,\}\)'#'\1'#g"
|
||||
2 x 2009-02-13 smell the coffee +wakeup
|
||||
|
||||
30
todo.sh
30
todo.sh
@@ -362,6 +362,20 @@ die()
|
||||
exit 1
|
||||
}
|
||||
|
||||
confirm()
|
||||
{
|
||||
[ $TODOTXT_FORCE = 0 ] || return 0
|
||||
|
||||
printf %s "${1:?}? (y/n) "
|
||||
local readArgs=(-e -r)
|
||||
[ -n "${BASH_VERSINFO:-}" ] && [ \( ${BASH_VERSINFO[0]} -eq 4 -a ${BASH_VERSINFO[1]} -ge 1 \) -o ${BASH_VERSINFO[0]} -gt 4 ] &&
|
||||
readArgs+=(-N 1) # Bash 4.1+ supports -N nchars
|
||||
local answer
|
||||
read "${readArgs[@]}" answer
|
||||
echo
|
||||
[ "$answer" = "y" ]
|
||||
}
|
||||
|
||||
cleaninput()
|
||||
{
|
||||
# Parameters: When $1 = "for sed", performs additional escaping for use
|
||||
@@ -1146,13 +1160,7 @@ case $action in
|
||||
getTodo "$item"
|
||||
|
||||
if [ -z "$3" ]; then
|
||||
if [ $TODOTXT_FORCE = 0 ]; then
|
||||
echo "Delete '$todo'? (y/n)"
|
||||
read -e -r ANSWER
|
||||
else
|
||||
ANSWER="y"
|
||||
fi
|
||||
if [ "$ANSWER" = "y" ]; then
|
||||
if confirm "Delete '$todo'"; then
|
||||
if [ $TODOTXT_PRESERVE_LINE_NUMBERS = 0 ]; then
|
||||
# delete line (changes line numbers)
|
||||
sed -i.bak -e "${item}s/^.*//" -e '/./!d' "$TODO_FILE"
|
||||
@@ -1342,13 +1350,7 @@ case $action in
|
||||
|
||||
getTodo "$item" "$src"
|
||||
[ -z "$todo" ] && die "$item: No such item in $src."
|
||||
if [ $TODOTXT_FORCE = 0 ]; then
|
||||
echo "Move '$todo' from $src to $dest? (y/n)"
|
||||
read -e -r ANSWER
|
||||
else
|
||||
ANSWER="y"
|
||||
fi
|
||||
if [ "$ANSWER" = "y" ]; then
|
||||
if confirm "Move '$todo' from $src to $dest"; then
|
||||
if [ $TODOTXT_PRESERVE_LINE_NUMBERS = 0 ]; then
|
||||
# delete line (changes line numbers)
|
||||
sed -i.bak -e "${item}s/^.*//" -e '/./!d' "$src"
|
||||
|
||||
Reference in New Issue
Block a user