From 602d3a708fa86b13e0c01e99f2892d81fa0611cc Mon Sep 17 00:00:00 2001 From: Teemu Ikonen Date: Thu, 15 Mar 2018 12:42:14 +0200 Subject: [PATCH] Add default priority variable TODOTXT_PRIORITY_ON_ADD. --- tests/t1040-add-priority.sh | 33 +++++++++++++++++++++++++++++++++ todo.sh | 14 ++++++++++++++ 2 files changed, 47 insertions(+) create mode 100755 tests/t1040-add-priority.sh diff --git a/tests/t1040-add-priority.sh b/tests/t1040-add-priority.sh new file mode 100755 index 00000000..3adaec61 --- /dev/null +++ b/tests/t1040-add-priority.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +test_description='test the priority on add feature' +. ./test-lib.sh + +## Normal use case +echo "export TODOTXT_PRIORITY_ON_ADD=A" >> todo.cfg + +test_todo_session 'config file priority' <>> todo.sh add take out the trash +1 (A) take out the trash +TODO: 1 added. + +>>> todo.sh -p list +1 (A) take out the trash +-- +TODO: 1 of 1 tasks shown +EOF + +## Wrong value in config var +echo "export TODOTXT_PRIORITY_ON_ADD=1" >> todo.cfg + +test_todo_session 'config file wrong priority' <>> todo.sh add fail to take out the trash +=== 1 +TODOTXT_PRIORITY_ON_ADD should be a capital letter from A to Z (it is now "1"). + +>>> todo.sh -p list +=== 1 +TODOTXT_PRIORITY_ON_ADD should be a capital letter from A to Z (it is now "1"). +EOF + +test_done diff --git a/todo.sh b/todo.sh index bd581c38..911cea24 100755 --- a/todo.sh +++ b/todo.sh @@ -142,6 +142,7 @@ help() TODOTXT_PRESERVE_LINE_NUMBERS is same as option -n (0)/-N (1) TODOTXT_PLAIN is same as option -p (1)/-c (0) TODOTXT_DATE_ON_ADD is same as option -t (1)/-T (0) + TODOTXT_PRIORITY_ON_ADD=pri default priority A-Z TODOTXT_VERBOSE=1 is same as option -v TODOTXT_DISABLE_FILTER=1 is same as option -x TODOTXT_DEFAULT_ACTION="" run this when called with no arguments @@ -482,6 +483,7 @@ OVR_TODOTXT_FORCE="$TODOTXT_FORCE" OVR_TODOTXT_PRESERVE_LINE_NUMBERS="$TODOTXT_PRESERVE_LINE_NUMBERS" OVR_TODOTXT_PLAIN="$TODOTXT_PLAIN" OVR_TODOTXT_DATE_ON_ADD="$TODOTXT_DATE_ON_ADD" +OVR_TODOTXT_PRIORITY_ON_ADD="$TODOTXT_PRIORITY_ON_ADD" OVR_TODOTXT_DISABLE_FILTER="$TODOTXT_DISABLE_FILTER" OVR_TODOTXT_VERBOSE="$TODOTXT_VERBOSE" OVR_TODOTXT_DEFAULT_ACTION="$TODOTXT_DEFAULT_ACTION" @@ -601,6 +603,7 @@ TODOTXT_FORCE=${TODOTXT_FORCE:-0} TODOTXT_PRESERVE_LINE_NUMBERS=${TODOTXT_PRESERVE_LINE_NUMBERS:-1} TODOTXT_AUTO_ARCHIVE=${TODOTXT_AUTO_ARCHIVE:-1} TODOTXT_DATE_ON_ADD=${TODOTXT_DATE_ON_ADD:-0} +TODOTXT_PRIORITY_ON_ADD=${TODOTXT_PRIORITY_ON_ADD:-} TODOTXT_DEFAULT_ACTION=${TODOTXT_DEFAULT_ACTION:-} TODOTXT_SORT_COMMAND=${TODOTXT_SORT_COMMAND:-env LC_COLLATE=C sort -f -k2} TODOTXT_DISABLE_FILTER=${TODOTXT_DISABLE_FILTER:-} @@ -740,6 +743,9 @@ fi if [ -n "$OVR_TODOTXT_DATE_ON_ADD" ] ; then TODOTXT_DATE_ON_ADD="$OVR_TODOTXT_DATE_ON_ADD" fi +if [ -n "$OVR_TODOTXT_PRIORITY_ON_ADD" ] ; then + TODOTXT_PRIORITY_ON_ADD="$OVR_TODOTXT_PRIORITY_ON_ADD" +fi if [ -n "$OVR_TODOTXT_DISABLE_FILTER" ] ; then TODOTXT_DISABLE_FILTER="$OVR_TODOTXT_DISABLE_FILTER" fi @@ -761,6 +767,9 @@ ACTION=${1:-$TODOTXT_DEFAULT_ACTION} [ -z "$ACTION" ] && usage [ -d "$TODO_DIR" ] || mkdir -p $TODO_DIR 2> /dev/null || dieWithHelp "$1" "Fatal Error: $TODO_DIR is not a directory" ( cd "$TODO_DIR" ) || dieWithHelp "$1" "Fatal Error: Unable to cd to $TODO_DIR" +[ -z "$TODOTXT_PRIORITY_ON_ADD" ] \ + || echo "$TODOTXT_PRIORITY_ON_ADD" | grep -q "^[A-Z]$" \ + || die "TODOTXT_PRIORITY_ON_ADD should be a capital letter from A to Z (it is now \"$TODOTXT_PRIORITY_ON_ADD\")." [ -f "$TODO_FILE" -o -c "$TODO_FILE" ] || > "$TODO_FILE" [ -f "$DONE_FILE" -o -c "$DONE_FILE" ] || > "$DONE_FILE" @@ -790,6 +799,11 @@ _addto() { now=$(date '+%Y-%m-%d') input=$(echo "$input" | sed -e 's/^\(([A-Z]) \)\{0,1\}/\1'"$now /") fi + if [[ -n "$TODOTXT_PRIORITY_ON_ADD" ]]; then + if ! echo "$input" | grep -q '^([A-Z])'; then + input=$(echo -n "($TODOTXT_PRIORITY_ON_ADD) " ; echo "$input") + fi + fi echo "$input" >> "$file" if [ $TODOTXT_VERBOSE -gt 0 ]; then TASKNUM=$(sed -n '$ =' "$file")