From 229737bd2fa0d8c6d0d502b77a46e954f066f507 Mon Sep 17 00:00:00 2001 From: Paul Mansfield Date: Wed, 2 Sep 2009 23:30:14 +0100 Subject: [PATCH] Bug Fix: Issue 1 Added a cleanitem function, this replaces newlines with a space. Currently this is used in the add, append, prepend and replace actions. --- tests/t2000-multiline.sh | 105 +++++++++++++++++++++++++++++++++++++++ todo.sh | 14 +++++- 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100755 tests/t2000-multiline.sh diff --git a/tests/t2000-multiline.sh b/tests/t2000-multiline.sh new file mode 100755 index 0000000..b9fe7b2 --- /dev/null +++ b/tests/t2000-multiline.sh @@ -0,0 +1,105 @@ +#!/bin/sh + +test_description='Multi-line functionality' + +. ./test-lib.sh + +## Replace test +# Create the expected file +echo "1: smell the cheese +replaced with +1: eat apples eat oranges">$HOME/expect.multi + +test_expect_success 'multiline item replace' ' +( +# Prepare single line todo file +cat /dev/null > $HOME/todo.txt +"$HOME/bin/todo.sh" add smell the cheese + +# Run replace +"$HOME/bin/todo.sh" replace 1 "eat apples +eat oranges" > $HOME/output.multi + +# Test output against expected +diff "$HOME/output.multi" "$HOME/expect.multi" +if [ $? -ne 0 ]; then + exit 1 +else + exit 0 +fi +) +' + +## Add test +# Create the expected file +echo "TODO: 'eat apples eat oranges' added on line 2.">$HOME/expect.multi + +test_expect_success 'multiline item add' ' +( +# Prepare single line todo file +cat /dev/null > $HOME/todo.txt +"$HOME/bin/todo.sh" add smell the cheese + +# Run add +"$HOME/bin/todo.sh" add "eat apples +eat oranges" > $HOME/output.multi + +# Test output against expected +diff "$HOME/output.multi" "$HOME/expect.multi" +if [ $? -ne 0 ]; then + exit 1 +else + exit 0 +fi +) +' + +## Append test +# Create the expected file +echo "1: smell the cheese eat apples eat oranges">$HOME/expect.multi + +test_expect_success 'multiline item append' ' +( +# Prepare single line todo file +cat /dev/null > $HOME/todo.txt +"$HOME/bin/todo.sh" add smell the cheese + +# Run append +"$HOME/bin/todo.sh" append 1 "eat apples +eat oranges" > $HOME/output.multi + +# Test output against expected +diff "$HOME/output.multi" "$HOME/expect.multi" +if [ $? -ne 0 ]; then + exit 1 +else + exit 0 +fi +) +' + +## Prepend test +# Create the expected file +echo "1: eat apples eat oranges smell the cheese">$HOME/expect.multi + +test_expect_success 'multiline item prepend' ' +( +# Prepare single line todo file +cat /dev/null > $HOME/todo.txt +"$HOME/bin/todo.sh" add smell the cheese + +# Run prepend +"$HOME/bin/todo.sh" prepend 1 "eat apples +eat oranges" > $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 778862d..4593286 100755 --- a/todo.sh +++ b/todo.sh @@ -240,6 +240,13 @@ cleanup() exit 0 } +cleaninput() +{ + # Cleanup the input + # Replace newlines with spaces + input=`echo $input | tr -d '\n'` +} + archive() { #defragment blank lines @@ -579,6 +586,7 @@ case $action in shift input=$* fi + cleaninput $input if [[ $TODOTXT_DATE_ON_ADD = 1 ]]; then now=`date '+%Y-%m-%d'` @@ -620,6 +628,8 @@ case $action in else input=$* fi + cleaninput $input + if sed -i.bak $item" s|^.*|& $input|" "$TODO_FILE"; then newtodo=$(sed "$item!d" "$TODO_FILE") [ $TODOTXT_VERBOSE -gt 0 ] && echo "$item: $newtodo" @@ -847,7 +857,8 @@ case $action in else input=$* fi - + cleaninput $input + # Test for then set priority if [ `sed "$item!d" "$TODO_FILE"|grep -c "^(\\w)"` -eq 1 ]; then priority=$(sed "$item!d" "$TODO_FILE" | awk -F '\\(|\\)' '{print $2}') @@ -917,6 +928,7 @@ note: PRIORITY must be anywhere from A to Z." else input=$* fi + cleaninput $input # If priority isn't set replace, if it is remove priority, replace then add priority again if [ -z $priority ]; then