From 7a87077dfcdda4eba02c7a6da3c97c31caf1be2b Mon Sep 17 00:00:00 2001 From: Paul Mansfield Date: Sun, 6 Sep 2009 18:53:31 +0100 Subject: [PATCH] Added new action addm This allows addition of multiple todo items by sending multiple lines. --- tests/t2000-multiline.sh | 29 +++++++++++++++++++++++++---- todo.sh | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/tests/t2000-multiline.sh b/tests/t2000-multiline.sh index 40c7e15..1c257ee 100755 --- a/tests/t2000-multiline.sh +++ b/tests/t2000-multiline.sh @@ -10,7 +10,7 @@ echo "1: smell the cheese replaced with 1: eat apples eat oranges drink milk">$HOME/expect.multi -test_expect_success 'multiline item replace' ' +test_expect_success 'multiline squash item replace' ' ( # Prepare single line todo file cat /dev/null > $HOME/todo.txt @@ -35,7 +35,7 @@ fi # Create the expected file echo "TODO: 'eat apples eat oranges drink milk' added on line 2.">$HOME/expect.multi -test_expect_success 'multiline item add' ' +test_expect_success 'multiline squash item add' ' ( # Prepare single line todo file cat /dev/null > $HOME/todo.txt @@ -60,7 +60,7 @@ fi # Create the expected file echo "1: smell the cheese eat apples eat oranges drink milk">$HOME/expect.multi -test_expect_success 'multiline item append' ' +test_expect_success 'multiline squash item append' ' ( # Prepare single line todo file cat /dev/null > $HOME/todo.txt @@ -85,7 +85,7 @@ fi # Create the expected file echo "1: eat apples eat oranges drink milk smell the cheese">$HOME/expect.multi -test_expect_success 'multiline item prepend' ' +test_expect_success 'multiline squash item prepend' ' ( # Prepare single line todo file cat /dev/null > $HOME/todo.txt @@ -106,4 +106,25 @@ fi ) ' +## Multiple line addition +# Create the expected file +echo -e "TODO: 'eat apples' added on line 2.\nTODO: 'eat oranges' added on line 3.\nTODO: 'drink milk' added on line 4.">$HOME/expect.multi + +test_expect_success 'actual multiline add' ' +( +# Run addm +"$HOME/bin/todo.sh" addm "eat apples +eat oranges +drink milk" > $HOME/output.multi + +# Test output against expected +diff "$HOME/output.multi" "$HOME/expect.multi" +if [ $? -ne 0 ]; then + exit 1 +else + exit 0 +fi +) +' + test_done diff --git a/todo.sh b/todo.sh index d25173d..0997148 100755 --- a/todo.sh +++ b/todo.sh @@ -41,6 +41,8 @@ shorthelp() Actions: add|a "THING I NEED TO DO +project @context" addto DEST "TEXT TO ADD" + addm "THINGS I NEED TO DO + MORE THINGS I NEED TO DO" append|app NUMBER "TEXT TO APPEND" archive command [ACTIONS] @@ -77,6 +79,13 @@ help() Project and context notation optional. Quotes optional. + addm "FIRST THING I NEED TO DO +project1 @context + SECOND THING I NEED TO DO +project2 @context" + Adds FIRST THING I NEED TO DO to your todo.txt on its own line and + Adds SECOND THING I NEED TO DO to you todo.txt on its own line. + Project and context notation optional. + Quotes optional. + addto DEST "TEXT TO ADD" Adds a line of text to any file located in the todo.txt directory. For example, addto inbox.txt "decide about vacation" @@ -604,6 +613,34 @@ case $action in [ $TODOTXT_VERBOSE -gt 0 ] && echo "TODO: '$input' added on line $TASKNUM." cleanup;; +"addm") + if [[ -z "$2" && $TODOTXT_FORCE = 0 ]]; then + echo -n "Add: " + read input + else + [ -z "$2" ] && die "usage: $TODO_SH addm \"TODO ITEM\"" + shift + input=$* + fi + + # Set InternalField Seperator as newline so we can + # loop across multiple lines + SAVEIFS=$IFS + IFS=$'\n' + + # Treat each line seperately + for line in $input ; do + if [[ $TODOTXT_DATE_ON_ADD = 1 ]]; then + now=`date '+%Y-%m-%d'` + line="$now $line" + fi + echo "$line" >> "$TODO_FILE" + TASKNUM=$(sed -n '$ =' "$TODO_FILE") + [ $TODOTXT_VERBOSE -gt 0 ] && echo "TODO: '$line' added on line $TASKNUM." + done + IFS=$SAVEIFS + cleanup;; + "addto" ) [ -z "$2" ] && die "usage: $TODO_SH addto DEST \"TODO ITEM\"" dest="$TODO_DIR/$2"