From 5e4486826155a45b31ddfd07436310fd534036fe Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Mon, 5 Jul 2010 11:24:19 +0200 Subject: [PATCH] ENH: 'prepend' and 'replace' actions keep prepended date. Generalized and simplified the logic that already kept an existing priority to also keep a date added via todo.sh -t / TODOTXT_DATE_ON_ADD (unless the replaced text also starts with a date). --- tests/t1100-replace.sh | 41 +++++++++++++++++++++++++++++++++++++++++ tests/t1400-prepend.sh | 34 ++++++++++++++++++++++++++++++++++ todo.sh | 36 +++++++++++++++++++++--------------- 3 files changed, 96 insertions(+), 15 deletions(-) diff --git a/tests/t1100-replace.sh b/tests/t1100-replace.sh index e796052..a9f489f 100755 --- a/tests/t1100-replace.sh +++ b/tests/t1100-replace.sh @@ -77,6 +77,7 @@ replaced with replaced with 4: (A) collect the eggs EOF + test_todo_session 'replace with &' << EOF >>> todo.sh replace 3 "thrash the hay & thresh the wheat" 3: jump on hay @@ -90,4 +91,44 @@ test_todo_session 'replace error' << EOF 10: No such task. EOF +cat /dev/null > todo.txt +test_todo_session 'replace handling prepended date on add' <>> todo.sh -t add "new task" +1: 2009-02-13 new task +TODO: 1 added. + +>>> todo.sh replace 1 this is just a new one +1: 2009-02-13 new task +replaced with +1: 2009-02-13 this is just a new one + +>>> todo.sh replace 1 2010-07-04 this also has a new date +1: 2009-02-13 this is just a new one +replaced with +1: 2010-07-04 this also has a new date +EOF + +cat /dev/null > todo.txt +test_todo_session 'replace handling priority and prepended date on add' <>> todo.sh -t add "new task" +1: 2009-02-13 new task +TODO: 1 added. + +>>> todo.sh pri 1 A +1: (A) 2009-02-13 new task +TODO: 1 prioritized (A). + +>>> todo.sh replace 1 this is just a new one +1: (A) 2009-02-13 new task +replaced with +1: (A) 2009-02-13 this is just a new one +EOF + +test_todo_session 'replace with prepended date replaces existing date' <>> todo.sh replace 1 2010-07-04 this also has a new date +1: (A) 2009-02-13 this is just a new one +replaced with +1: (A) 2010-07-04 this also has a new date +EOF + test_done diff --git a/tests/t1400-prepend.sh b/tests/t1400-prepend.sh index 5a19c0b..4219b09 100755 --- a/tests/t1400-prepend.sh +++ b/tests/t1400-prepend.sh @@ -57,4 +57,38 @@ test_todo_session 'prepend with &' < todo.txt +test_todo_session 'prepend handling prepended date on add' <>> todo.sh -t add "new task" +1: 2009-02-13 new task +TODO: 1 added. + +>>> todo.sh prepend 1 "this is just a" +1: 2009-02-13 this is just a new task +EOF + +cat /dev/null > todo.txt +test_todo_session 'prepend handling priority and prepended date on add' <>> todo.sh -t add "new task" +1: 2009-02-13 new task +TODO: 1 added. + +>>> todo.sh pri 1 A +1: (A) 2009-02-13 new task +TODO: 1 prioritized (A). + +>>> todo.sh prepend 1 "this is just a" +1: (A) 2009-02-13 this is just a new task +EOF + +cat /dev/null > todo.txt +test_todo_session 'prepend with prepended date keeps both' <>> todo.sh -t add "new task" +1: 2009-02-13 new task +TODO: 1 added. + +>>> todo.sh prepend 1 "2010-07-04 this is just a" +1: 2009-02-13 2010-07-04 this is just a new task +EOF + test_done diff --git a/todo.sh b/todo.sh index df30a51..8b30d10 100755 --- a/todo.sh +++ b/todo.sh @@ -288,8 +288,14 @@ replaceOrPrepend() { action=$1; shift case "$action" in - replace) backref=;; - prepend) backref=' &';; + replace) + backref= + querytext="Replacement: " + ;; + prepend) + backref=' &' + querytext="Prepend: " + ;; esac shift; item=$1; shift @@ -300,27 +306,27 @@ replaceOrPrepend() [ -z "$todo" ] && die "$item: No such task." if [[ -z "$1" && $TODOTXT_FORCE = 0 ]]; then - case "$action" in - replace) echo -n "Replacement: ";; - prepend) echo -n "Prepend: ";; - esac + echo -n "$querytext" read input 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}') + # Retrieve existing priority and prepended date + priority=$(sed -e "$item!d" -e $item's/^\(([A-Z]) \)\{0,1\}\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{0,1\}.*/\1/' "$TODO_FILE") + prepdate=$(sed -e "$item!d" -e $item's/^\(([A-Z]) \)\{0,1\}\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{0,1\}.*/\2/' "$TODO_FILE") + + if [ "$prepdate" -a "$action" = "replace" ] && [ "$(echo "$input"|sed -e 's/^\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\}\)\{0,1\}.*/\1/')" ]; then + # If the replaced text starts with a date, it will replace the existing + # date, too. + prepdate= fi - # If priority isn't set change task, if it is remove priority, change then add priority again - if [ -z $priority ]; then - sed -i.bak $item" s|^.*|${input}${backref}|" "$TODO_FILE" - else - sed -i.bak -e "$item s/^(.) //" -e "$item s|^.*|\($priority\) ${input}${backref}|" "$TODO_FILE" - fi + # Temporarily remove any existing priority and prepended date, perform the + # change (replace/prepend) and re-insert the existing priority and prepended + # date again. + sed -i.bak -e "$item s/^${priority}${prepdate}//" -e "$item s|^.*|${priority}${prepdate}${input}${backref}|" "$TODO_FILE" if [ $TODOTXT_VERBOSE -gt 0 ]; then newtodo=$(sed "$item!d" "$TODO_FILE") case "$action" in