From 28ec5a06f21b598ab88d0c0895097779a42ce2f3 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Thu, 26 Jan 2012 14:48:29 +0100 Subject: [PATCH 1/3] Get rid of cleanup, only use TMP_FILE in listall. After the recent refactorings, the temporary file is only needed for the listall action. Therefore, the creation-checks and eventual cleanup can be restricted to the listall action, which should slightly speed up the overall script execution. --- todo.sh | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/todo.sh b/todo.sh index 89062f0..45948a5 100755 --- a/todo.sh +++ b/todo.sh @@ -302,12 +302,6 @@ die() exit 1 } -cleanup() -{ - [ -f "$TMP_FILE" ] && rm "$TMP_FILE" - return 0 -} - cleaninput() { # Parameters: When $1 = "for sed", performs additional escaping for use @@ -670,7 +664,6 @@ ACTION=${1:-$TODOTXT_DEFAULT_ACTION} [ -d "$TODO_DIR" ] || die "Fatal Error: $TODO_DIR is not a directory" ( cd "$TODO_DIR" ) || die "Fatal Error: Unable to cd to $TODO_DIR" -[ -w "$TMP_FILE" ] || echo -n > "$TMP_FILE" || die "Fatal Error: Unable to write to $TMP_FILE" [ -f "$TODO_FILE" ] || cp /dev/null "$TODO_FILE" [ -f "$DONE_FILE" ] || cp /dev/null "$DONE_FILE" [ -f "$REPORT_FILE" ] || cp /dev/null "$REPORT_FILE" @@ -854,9 +847,7 @@ then elif [ -d "$TODO_ACTIONS_DIR" -a -x "$TODO_ACTIONS_DIR/$action" ] then "$TODO_ACTIONS_DIR/$action" "$@" - status=$? - cleanup - exit $status + exit $? fi ## Only run if $action isn't found in .todo.actions.d @@ -1082,12 +1073,15 @@ case $action in "listall" | "lsa" ) shift ## Was lsa; new $1 is first search term + [ -w "$TMP_FILE" ] || echo -n > "$TMP_FILE" || die "Fatal Error: Unable to write to $TMP_FILE" cat "$TODO_FILE" "$DONE_FILE" > "$TMP_FILE" TOTAL=$( sed -n '$ =' "$TODO_FILE" ) post_filter_command="awk -v TOTAL=$TOTAL -v PADDING=${#TOTAL} '{ \$1 = sprintf(\"%\" PADDING \"d\", (\$1 > TOTAL ? 0 : \$1)); print }' " TODOTXT_VERBOSE=0 _list "$TMP_FILE" "$@" + [ -f "$TMP_FILE" ] && rm "$TMP_FILE" + if [ $TODOTXT_VERBOSE -gt 0 ]; then TDONE=$( sed -n '$ =' "$DONE_FILE" ) TASKNUM=$(TODOTXT_PLAIN=1 TODOTXT_VERBOSE=0 _list "$TODO_FILE" "$@" | sed -n '$ =') @@ -1283,5 +1277,3 @@ note: PRIORITY must be anywhere from A to Z." * ) usage;; esac - -cleanup From cf7f7531be849eca431a9935862cad9ed1f34625 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Thu, 26 Jan 2012 15:53:46 +0100 Subject: [PATCH 2/3] Break up _list(), get rid of TMP_FILE. Extract a new function _format() (and getPadding(), both also exported for add-ons) from _list(), which includes the main formatting and filtering pipeline, without the file handling and verbose summary. This can receive the todo file via stdin, so the listall action is able to format the concatenated files without going through a temporary file. Eventually, after further refactorings, _format() could be used for actual formatted verbose messages in all commands; currently, the raw, unformatted task is printed. --- todo.sh | 58 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/todo.sh b/todo.sh index 45948a5..95fdabd 100755 --- a/todo.sh +++ b/todo.sh @@ -753,13 +753,32 @@ _list() { ## Get our search arguments, if any shift ## was file name, new $1 is first search term - ## Build the filter. - filter_command=$(filtercommand "${pre_filter_command:-}" "${post_filter_command:-}" "$@") + _format "$src" '' "$@" - ## Figure out how much padding we need to use - ## We need one level of padding for each power of 10 $LINES uses - LINES=$( sed -n '$ =' "$src" ) - PADDING=${#LINES} + if [ $TODOTXT_VERBOSE -gt 0 ]; then + echo "--" + echo "$(getPrefix "$src"): ${NUMTASKS:-0} of ${TOTALTASKS:-0} tasks shown" + fi +} +getPadding() +{ + ## We need one level of padding for each power of 10 $LINES uses. + LINES=$(sed -n '$ =' "${1:-$TODO_FILE}") + printf %s ${#LINES} +} +_format() +{ + # Parameters: $1: todo input file; when empty formats stdin + # $2: ITEM# number width; if empty auto-detects from $1 / $TODO_FILE. + # Precondition: None + # Postcondition: $NUMTASKS and $TOTALTASKS contain statistics (unless $TODOTXT_VERBOSE=0). + + FILE=$1 + shift + + ## Figure out how much padding we need to use, unless this was passed to us. + PADDING=${1:-$(getPadding "$FILE")} + shift ## Number the file, then run the filter command, ## then sort and mangle output some more @@ -767,7 +786,11 @@ _list() { TODOTXT_FINAL_FILTER="cat" fi items=$( - sed = "$src" \ + if [ "$FILE" ]; then + sed = "$FILE" + else + sed = + fi \ | sed -e ''' N s/^/ / @@ -775,6 +798,9 @@ _list() { /^[ 0-9]\{1,\} *$/d ''' ) + + ## Build and apply the filter. + filter_command=$(filtercommand "${pre_filter_command:-}" "${post_filter_command:-}" "$@") if [ "${filter_command}" ]; then filtered_items=$(echo -n "$items" | eval "${filter_command}") else @@ -820,16 +846,13 @@ _list() { if [ $TODOTXT_VERBOSE -gt 0 ]; then NUMTASKS=$( echo -n "$filtered_items" | sed -n '$ =' ) TOTALTASKS=$( echo -n "$items" | sed -n '$ =' ) - - echo "--" - echo "$(getPrefix "$FILE"): ${NUMTASKS:-0} of ${TOTALTASKS:-0} tasks shown" fi if [ $TODOTXT_VERBOSE -gt 1 ]; then echo "TODO DEBUG: Filter Command was: ${filter_command:-cat}" fi } -export -f cleaninput getPrefix getTodo getNewtodo shellquote filtercommand _list die +export -f cleaninput getPrefix getTodo getNewtodo shellquote filtercommand _list getPadding _format die # == HANDLE ACTION == action=$( printf "%s\n" "$ACTION" | tr 'A-Z' 'a-z' ) @@ -1073,19 +1096,16 @@ case $action in "listall" | "lsa" ) shift ## Was lsa; new $1 is first search term - [ -w "$TMP_FILE" ] || echo -n > "$TMP_FILE" || die "Fatal Error: Unable to write to $TMP_FILE" - cat "$TODO_FILE" "$DONE_FILE" > "$TMP_FILE" TOTAL=$( sed -n '$ =' "$TODO_FILE" ) + PADDING=${#TOTAL} - post_filter_command="awk -v TOTAL=$TOTAL -v PADDING=${#TOTAL} '{ \$1 = sprintf(\"%\" PADDING \"d\", (\$1 > TOTAL ? 0 : \$1)); print }' " - TODOTXT_VERBOSE=0 _list "$TMP_FILE" "$@" - - [ -f "$TMP_FILE" ] && rm "$TMP_FILE" + post_filter_command="awk -v TOTAL=$TOTAL -v PADDING=$PADDING '{ \$1 = sprintf(\"%\" PADDING \"d\", (\$1 > TOTAL ? 0 : \$1)); print }' " + cat "$TODO_FILE" "$DONE_FILE" | TODOTXT_VERBOSE=0 _format '' "$PADDING" "$@" if [ $TODOTXT_VERBOSE -gt 0 ]; then TDONE=$( sed -n '$ =' "$DONE_FILE" ) - TASKNUM=$(TODOTXT_PLAIN=1 TODOTXT_VERBOSE=0 _list "$TODO_FILE" "$@" | sed -n '$ =') - DONENUM=$(TODOTXT_PLAIN=1 TODOTXT_VERBOSE=0 _list "$DONE_FILE" "$@" | sed -n '$ =') + TASKNUM=$(TODOTXT_PLAIN=1 TODOTXT_VERBOSE=0 _format "$TODO_FILE" 1 "$@" | sed -n '$ =') + DONENUM=$(TODOTXT_PLAIN=1 TODOTXT_VERBOSE=0 _format "$DONE_FILE" 1 "$@" | sed -n '$ =') echo "--" echo "$(getPrefix "$TODO_FILE"): ${TASKNUM:-0} of ${TOTAL:-0} tasks shown" echo "$(getPrefix "$DONE_FILE"): ${DONENUM:-0} of ${TDONE:-0} tasks shown" From be0a0265d11565eaf28ef1429b2ee0d63bfe3808 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Thu, 26 Jan 2012 16:21:46 +0100 Subject: [PATCH 3/3] Also get rid of TMP_FILE in todo.cfg. There's a slight chance that some add-on has used this (undocumented, unofficial) configuration value for its own purposes (and maybe also relied on the unexposed cleanup() infrastructure), but detecting and fixing that problem (by moving the cleanup into the add-on itself) is pretty straightforward. --- todo.cfg | 1 - 1 file changed, 1 deletion(-) diff --git a/todo.cfg b/todo.cfg index 546a640..8504388 100644 --- a/todo.cfg +++ b/todo.cfg @@ -8,7 +8,6 @@ export TODO_DIR=`dirname "$0"` export TODO_FILE="$TODO_DIR/todo.txt" export DONE_FILE="$TODO_DIR/done.txt" export REPORT_FILE="$TODO_DIR/report.txt" -export TMP_FILE="$TODO_DIR/todo.tmp" # You can customize your actions directory location #export TODO_ACTIONS_DIR="$HOME/.todo.actions.d"