From 7501b225c2ecbf05b857461a2ac359b88d82d6c0 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Tue, 13 Jul 2010 17:18:31 +0200 Subject: [PATCH] ENH: Improved 'del ITEM# TERM' command. - Condensing whitespace around TERM into a single space and removing leading/trailing spaces if at the beginning/end of the task. - Proper error message if TERM not found. - Aligned verbose removal message with the verbose output of the 'replace' command. In addition, removed (accidental) printing of the task (without leading ITEM#) when checking for existence of the task; using empty check of task text as done elsewhere. --- tests/t1800-del.sh | 112 +++++++++++++++++++++++++++++++++++++++++++++ todo.sh | 59 ++++++++++++++---------- 2 files changed, 148 insertions(+), 23 deletions(-) create mode 100755 tests/t1800-del.sh diff --git a/tests/t1800-del.sh b/tests/t1800-del.sh new file mode 100755 index 0000000..aaf8675 --- /dev/null +++ b/tests/t1800-del.sh @@ -0,0 +1,112 @@ +#!/bin/sh + +test_description='basic del functionality +' +. ./test-lib.sh + +test_todo_session 'del usage' <>> todo.sh del B +usage: todo.sh del ITEM# [TERM] +=== 1 +EOF + +test_todo_session 'del nonexistant item' <>> todo.sh -f del 42 +42: No such task. +=== 1 + +>>> todo.sh -f del 42 Roses +42: No such task. +=== 1 +EOF + +cat > todo.txt <>> 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 + +>>> todo.sh -f del 1 +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 <>> todo.sh -p list +2 (A) notice the sunflowers +1 (B) smell the uppercase Roses +flowers @outside +3 (C) stop +-- +TODO: 3 of 3 tasks shown + +>>> todo.sh del 1 uppercase +1: (B) smell the uppercase Roses +flowers @outside +got 'uppercase' removed to become +1: (B) smell the Roses +flowers @outside + +>>> todo.sh -p list +2 (A) notice the sunflowers +1 (B) smell the Roses +flowers @outside +3 (C) stop +-- +TODO: 3 of 3 tasks shown + +>>> todo.sh del 1 "the Roses" +1: (B) smell the Roses +flowers @outside +got 'the Roses' removed to become +1: (B) smell +flowers @outside + +>>> todo.sh del 1 m +1: (B) smell +flowers @outside +got 'm' removed to become +1: (B) sell +flowers @outside + +>>> todo.sh del 1 @outside +1: (B) sell +flowers @outside +got '@outside' removed to become +1: (B) sell +flowers + +>>> todo.sh del 1 sell +1: (B) sell +flowers +got 'sell' removed to become +1: (B) +flowers +EOF + +cat > todo.txt <>> todo.sh del 1 dung +1: (B) smell the uppercase Roses +flowers @outside +'dung' not found; no removal done. +=== 1 + +>>> todo.sh -p list +2 (A) notice the sunflowers +1 (B) smell the uppercase Roses +flowers @outside +3 (C) stop +-- +TODO: 3 of 3 tasks shown +EOF + +test_done diff --git a/todo.sh b/todo.sh index df3c22e..beb44e8 100755 --- a/todo.sh +++ b/todo.sh @@ -776,37 +776,50 @@ case $action in errmsg="usage: $TODO_SH del ITEM# [TERM]" item=$2 [ -z "$item" ] && die "$errmsg" + [[ "$item" = +([0-9]) ]] || die "$errmsg" + DELETEME=$(sed "$item!d" "$TODO_FILE") + [ -z "$DELETEME" ] && die "$item: No such task." if [ -z "$3" ]; then - - [[ "$item" = +([0-9]) ]] || die "$errmsg" - if sed -ne "$item p" "$TODO_FILE" | grep "^."; then - DELETEME=$(sed "$item!d" "$TODO_FILE") - - if [ $TODOTXT_FORCE = 0 ]; then - echo "Delete '$DELETEME'? (y/n)" - read ANSWER + if [ $TODOTXT_FORCE = 0 ]; then + echo "Delete '$DELETEME'? (y/n)" + read ANSWER + else + ANSWER="y" + fi + if [ "$ANSWER" = "y" ]; then + if [ $TODOTXT_PRESERVE_LINE_NUMBERS = 0 ]; then + # delete line (changes line numbers) + sed -i.bak -e $item"s/^.*//" -e '/./!d' "$TODO_FILE" else - ANSWER="y" + # leave blank line behind (preserves line numbers) + sed -i.bak -e $item"s/^.*//" "$TODO_FILE" fi - if [ "$ANSWER" = "y" ]; then - if [ $TODOTXT_PRESERVE_LINE_NUMBERS = 0 ]; then - # delete line (changes line numbers) - sed -i.bak -e $item"s/^.*//" -e '/./!d' "$TODO_FILE" - else - # leave blank line behind (preserves line numbers) - sed -i.bak -e $item"s/^.*//" "$TODO_FILE" - fi - [ $TODOTXT_VERBOSE -gt 0 ] && echo "TODO: '$DELETEME' deleted." - else - echo "TODO: No tasks were deleted." + if [ $TODOTXT_VERBOSE -gt 0 ]; then + echo "$item: $DELETEME" + echo "TODO: $item deleted." fi else - die "$item: No such task." + echo "TODO: No tasks were deleted." fi else - sed -i.bak -e $item"s/$3/ /g" "$TODO_FILE" - [ $TODOTXT_VERBOSE -gt 0 ] && echo "TODO: $3 removed from $item." + sed -i.bak \ + -e $item"s/^\((.) \)\{0,1\} *$3 */\1/g" \ + -e $item"s/ *$3 *\$//g" \ + -e $item"s/ *$3 */ /g" \ + -e $item"s/ *$3 */ /g" \ + -e $item"s/$3//g" \ + "$TODO_FILE" + newtodo=$(sed "$item!d" "$TODO_FILE") + if [ "$DELETEME" = "$newtodo" ]; then + [ $TODOTXT_VERBOSE -gt 0 ] && echo "$item: $DELETEME" + die "'$3' not found; no removal done." + fi + if [ $TODOTXT_VERBOSE -gt 0 ]; then + echo "$item: $DELETEME" + echo "got '$3' removed to become" + echo "$item: $newtodo" + fi fi ;;