From 8b10e0375adb5c91792f9b5adc2a8c8186ed718b Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Tue, 10 Aug 2021 17:32:37 +0000 Subject: [PATCH 01/21] make completions file not executable bash completions files are not supposed to have executable filemode set --- todo_completion | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 todo_completion diff --git a/todo_completion b/todo_completion old mode 100755 new mode 100644 From e0e350db315064d2243f09606955db8aafd2756f Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Tue, 10 Aug 2021 17:35:38 +0000 Subject: [PATCH 02/21] properly track exit values --- todo_completion | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/todo_completion b/todo_completion index b285a63d..ec68ad3d 100644 --- a/todo_completion +++ b/todo_completion @@ -70,8 +70,9 @@ _todo() # a safety check of the ls action output. -e '1q' ) - local todo=$( \ - eval TODOTXT_VERBOSE=0 $_todo_sh '-@ -+ -p -x command ls "^ *${cur} "' 2>/dev/null | \ + local todo + todo=$( \ + eval TODOTXT_VERBOSE=0 "$_todo_sh" '-@ -+ -p -x command ls "^ *${cur} "' 2>/dev/null | \ sed "${sedTransformations[@]}" \ ) # Append task text as a shell comment. This From 17e3e88a78951c6cca676766da4693a024acdab7 Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Tue, 10 Aug 2021 17:40:48 +0000 Subject: [PATCH 03/21] move bash version check to proper place shellcheck already relies on functionality added in bash v4.1, hence better to move the version check to the top of the script rather than checking in just one place also fixes proper invocation of 'read' --- todo.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/todo.sh b/todo.sh index fb8d4bce..317df48f 100755 --- a/todo.sh +++ b/todo.sh @@ -1,5 +1,8 @@ #!/usr/bin/env bash +# Check bash version +(( BASH_VERSINFO[0] > 4 || BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 1 )) || { printf 'Bash 4.1 or newer requried\n' >&2; exit 1; } + # === HEAVY LIFTING === shopt -s extglob extquote @@ -365,11 +368,8 @@ confirm() [ $TODOTXT_FORCE = 0 ] || return 0 printf %s "${1:?}? (y/n) " - local readArgs=(-e -r) - [ -n "${BASH_VERSINFO:-}" ] && [ \( ${BASH_VERSINFO[0]} -eq 4 -a ${BASH_VERSINFO[1]} -ge 1 \) -o ${BASH_VERSINFO[0]} -gt 4 ] && - readArgs+=(-N 1) # Bash 4.1+ supports -N nchars local answer - read "${readArgs[@]}" answer + read -r -e -N 1 answer echo [ "$answer" = "y" ] } From 35e8e52412c0d662f4c14c5dd6fd8ed8430a74d3 Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Tue, 10 Aug 2021 17:55:28 +0000 Subject: [PATCH 04/21] use explicit comparisons --- todo.sh | 41 +++++++++++++++++++++-------------------- todo_completion | 2 +- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/todo.sh b/todo.sh index 317df48f..351385c8 100755 --- a/todo.sh +++ b/todo.sh @@ -306,7 +306,7 @@ addonHelp() for action in "$TODO_ACTIONS_DIR"/* do if [ -f "$action" ] && [ -x "$action" ]; then - if [ ! "$didPrintAddonActionsHeader" ]; then + if [ -z "$didPrintAddonActionsHeader" ]; then cat <<-EndAddonActionsHeader Add-on Actions: EndAddonActionsHeader @@ -314,7 +314,7 @@ addonHelp() fi "$action" usage elif [ -d "$action" ] && [ -x "$action"/"$(basename "$action")" ]; then - if [ ! "$didPrintAddonActionsHeader" ]; then + if [ -z "$didPrintAddonActionsHeader" ]; then cat <<-EndAddonActionsHeader Add-on Actions: EndAddonActionsHeader @@ -337,7 +337,7 @@ actionUsage() "$action"/"$(basename "$action")" usage else builtinActionUsage=$(actionsHelp | sed -n -e "/^ ${actionName//\//\\/} /,/^\$/p" -e "/^ ${actionName//\//\\/}$/,/^\$/p") - if [ "$builtinActionUsage" ]; then + if [ -n "$builtinActionUsage" ]; then echo "$builtinActionUsage" echo else @@ -414,11 +414,12 @@ getTodo() local item=$1 [ -z "$item" ] && die "$errmsg" - [ "${item//[0-9]/}" ] && die "$errmsg" + [ -n "${item//[0-9]/}" ] && die "$errmsg" todo=$(sed "$item!d" "${2:-$TODO_FILE}") [ -z "$todo" ] && die "$(getPrefix "$2"): No task $item." } + getNewtodo() { # Parameters: $1: task number @@ -428,7 +429,7 @@ getNewtodo() local item=$1 [ -z "$item" ] && die "Programming error: $item should exist." - [ "${item//[0-9]/}" ] && die "Programming error: $item should be numeric." + [ -n "${item//[0-9]/}" ] && die "Programming error: $item should be numeric." newtodo=$(sed "$item!d" "${2:-$TODO_FILE}") [ -z "$newtodo" ] && die "$(getPrefix "$2"): No updated task $item." @@ -462,7 +463,7 @@ replaceOrPrepend() priority=$(sed -e "$item!d" -e "${item}s/${priAndDateExpr}.*/\\1/" "$TODO_FILE") prepdate=$(sed -e "$item!d" -e "${item}s/${priAndDateExpr}.*/\\2/" "$TODO_FILE") - if [ "$prepdate" ] && [ "$action" = "replace" ] && [ "$(echo "$input"|sed -e "s/${priAndDateExpr}.*/\\1\\2/")" ]; then + if [ -n "$prepdate" ] && [ "$action" = "replace" ] && [ -n "$(echo "$input" | sed -e "s/${priAndDateExpr}.*/\\1\\2/")" ]; then # If the replaced text starts with a [priority +] date, it will replace # the existing date, too. prepdate= @@ -797,8 +798,8 @@ if [ $TODOTXT_PLAIN = 1 ]; then COLOR_META=$NONE fi -[[ "$HIDE_PROJECTS_SUBSTITUTION" ]] && COLOR_PROJECT="$NONE" -[[ "$HIDE_CONTEXTS_SUBSTITUTION" ]] && COLOR_CONTEXT="$NONE" +[[ -n "$HIDE_PROJECTS_SUBSTITUTION" ]] && COLOR_PROJECT="$NONE" +[[ -n "$HIDE_CONTEXTS_SUBSTITUTION" ]] && COLOR_CONTEXT="$NONE" _addto() { file="$1" @@ -916,7 +917,7 @@ _format() TODOTXT_FINAL_FILTER="cat" fi items=$( - if [ "$FILE" ]; then + if [ -n "$FILE" ]; then sed = "$FILE" else sed = @@ -931,7 +932,7 @@ _format() ## Build and apply the filter. filter_command=$(filtercommand "${pre_filter_command:-}" "${post_filter_command:-}" "$@") - if [ "${filter_command}" ]; then + if [ -n "${filter_command}" ]; then filtered_items=$(echo -n "$items" | eval "${filter_command}") else filtered_items=$items @@ -1009,7 +1010,7 @@ _format() ''' \ | eval ${TODOTXT_FINAL_FILTER} \ ) - [ "$filtered_items" ] && echo "$filtered_items" + [ -n "$filtered_items" ] && echo "$filtered_items" if [ "$TODOTXT_VERBOSE" -gt 0 ]; then NUMTASKS=$( echo -n "$filtered_items" | sed -n '$ =' ) @@ -1026,14 +1027,14 @@ listWordsWithSigil() shift FILE=$TODO_FILE - [ "$TODOTXT_SOURCEVAR" ] && eval "FILE=$TODOTXT_SOURCEVAR" - eval "$(filtercommand 'cat "${FILE[@]}"' '' "$@")" \ - | grep -o "[^ ]*${sigil}[^ ]\\+" \ - | sed -n \ - -e "s#^${TODOTXT_SIGIL_BEFORE_PATTERN//#/\\#}##" \ - -e "s#${TODOTXT_SIGIL_AFTER_PATTERN//#/\\#}\$##" \ - -e "/^${sigil}${TODOTXT_SIGIL_VALID_PATTERN//\//\\/}$/p" \ - | sort -u + [ -n "$TODOTXT_SOURCEVAR" ] && eval "FILE=$TODOTXT_SOURCEVAR" + eval "$(filtercommand 'cat "${FILE[@]}"' '' "$@")" \ + | grep -o "[^ ]*${sigil}[^ ]\\+" \ + | sed -n \ + -e "s#^${TODOTXT_SIGIL_BEFORE_PATTERN//#/\\#}##" \ + -e "s#${TODOTXT_SIGIL_AFTER_PATTERN//#/\\#}\$##" \ + -e "/^${sigil}${TODOTXT_SIGIL_VALID_PATTERN//\//\\/}$/p" \ + | sort -u } export -f cleaninput getPrefix getTodo getNewtodo shellquote filtercommand _list listWordsWithSigil getPadding _format die @@ -1397,7 +1398,7 @@ note: PRIORITY must be anywhere from A to Z." getNewtodo "$item" echo "$item $newtodo" if [ "$oldpri" != "$newpri" ]; then - if [ "$oldpri" ]; then + if [ -n "$oldpri" ]; then echo "TODO: $item re-prioritized from ($oldpri) to ($newpri)." else echo "TODO: $item prioritized ($newpri)." diff --git a/todo_completion b/todo_completion index ec68ad3d..24d29faa 100644 --- a/todo_completion +++ b/todo_completion @@ -78,7 +78,7 @@ _todo() # Append task text as a shell comment. This # completion can be a safety check before a # destructive todo.txt operation. - [ "$todo" ] && COMPREPLY[0]="$cur # $todo" + [ -n "$todo" ] && COMPREPLY[0]="$cur # $todo" return 0 else return 0 From e56fddc4d5e18d11e2d6fb6262eccf2cd408e00b Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Tue, 10 Aug 2021 17:58:11 +0000 Subject: [PATCH 05/21] use explicit file truncation --- todo.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/todo.sh b/todo.sh index 351385c8..e6ef21be 100755 --- a/todo.sh +++ b/todo.sh @@ -780,9 +780,9 @@ ACTION=${1:-$TODOTXT_DEFAULT_ACTION} [ -z "$DONE_FILE" ] && DONE_FILE="$TODO_DIR/done.txt" [ -z "$REPORT_FILE" ] && REPORT_FILE="$TODO_DIR/report.txt" -[ -f "$TODO_FILE" ] || [ -c "$TODO_FILE" ] || > "$TODO_FILE" -[ -f "$DONE_FILE" ] || [ -c "$DONE_FILE" ] || > "$DONE_FILE" -[ -f "$REPORT_FILE" ] || [ -c "$REPORT_FILE" ] || > "$REPORT_FILE" +[ -f "$TODO_FILE" ] || [ -c "$TODO_FILE" ] || : > "$TODO_FILE" +[ -f "$DONE_FILE" ] || [ -c "$DONE_FILE" ] || : > "$DONE_FILE" +[ -f "$REPORT_FILE" ] || [ -c "$REPORT_FILE" ] || : > "$REPORT_FILE" if [ $TODOTXT_PLAIN = 1 ]; then for clr in ${!PRI_@}; do From 60248b7abb628d65ccebbacdeff1bfd8f860d58a Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Tue, 10 Aug 2021 18:06:54 +0000 Subject: [PATCH 06/21] use quotes consistently everywhere also fixes a rare bug when directory name contains a space --- GEN-VERSION-FILE | 6 +++--- todo.sh | 14 +++++++------- todo_completion | 24 ++++++++++++------------ 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/GEN-VERSION-FILE b/GEN-VERSION-FILE index 6308d3ef..25e729a2 100755 --- a/GEN-VERSION-FILE +++ b/GEN-VERSION-FILE @@ -24,13 +24,13 @@ fi VN=$(expr "$VN" : v*'\(.*\)') -if test -r $VF +if test -r "$VF" then - VC=$(sed -e 's/^VERSION=//' <$VF) + VC=$(sed -e 's/^VERSION=//' <"$VF") else VC=unset fi test "$VN" = "$VC" || { echo >&2 "VERSION=$VN" - echo "VERSION=$VN" >$VF + echo "VERSION=$VN" >"$VF" } diff --git a/todo.sh b/todo.sh index e6ef21be..1d3cf59b 100755 --- a/todo.sh +++ b/todo.sh @@ -365,7 +365,7 @@ die() confirm() { - [ $TODOTXT_FORCE = 0 ] || return 0 + [ "$TODOTXT_FORCE" = 0 ] || return 0 printf %s "${1:?}? (y/n) " local answer @@ -784,7 +784,7 @@ ACTION=${1:-$TODOTXT_DEFAULT_ACTION} [ -f "$DONE_FILE" ] || [ -c "$DONE_FILE" ] || : > "$DONE_FILE" [ -f "$REPORT_FILE" ] || [ -c "$REPORT_FILE" ] || : > "$REPORT_FILE" -if [ $TODOTXT_PLAIN = 1 ]; then +if [ "$TODOTXT_PLAIN" = 1 ]; then for clr in ${!PRI_@}; do export "$clr"="$NONE" done @@ -1007,8 +1007,8 @@ _format() s/'"${HIDE_PROJECTS_SUBSTITUTION:-^}"'//g s/'"${HIDE_CONTEXTS_SUBSTITUTION:-^}"'//g s/'"${HIDE_CUSTOM_SUBSTITUTION:-^}"'//g - ''' \ - | eval ${TODOTXT_FINAL_FILTER} \ + ''' \ + | eval "${TODOTXT_FINAL_FILTER}" \ ) [ -n "$filtered_items" ] && echo "$filtered_items" @@ -1159,7 +1159,7 @@ case $action in if [ -z "$3" ]; then if confirm "Delete '$todo'"; then - if [ $TODOTXT_PRESERVE_LINE_NUMBERS = 0 ]; then + if [ "$TODOTXT_PRESERVE_LINE_NUMBERS" = 0 ]; then # delete line (changes line numbers) sed -i.bak -e "${item}s/^.*//" -e '/./!d' "$TODO_FILE" else @@ -1349,7 +1349,7 @@ case $action in getTodo "$item" "$src" [ -z "$todo" ] && die "$item: No such item in $src." if confirm "Move '$todo' from $src to $dest"; then - if [ $TODOTXT_PRESERVE_LINE_NUMBERS = 0 ]; then + if [ "$TODOTXT_PRESERVE_LINE_NUMBERS" = 0 ]; then # delete line (changes line numbers) sed -i.bak -e "${item}s/^.*//" -e '/./!d' "$src" else @@ -1476,7 +1476,7 @@ note: PRIORITY must be anywhere from A to Z." newTaskNum=$( sed -e '/./!d' "$TODO_FILE" | sed -n '$ =' ) deduplicateNum=$(( originalTaskNum - newTaskNum )) - if [ $deduplicateNum -eq 0 ]; then + if [ "$deduplicateNum" -eq 0 ]; then echo "TODO: No duplicate tasks found" else echo "TODO: $deduplicateNum duplicate task(s) removed" diff --git a/todo_completion b/todo_completion index 24d29faa..493d528c 100644 --- a/todo_completion +++ b/todo_completion @@ -21,34 +21,34 @@ _todo() local _todo_sh=${_todo_sh:-todo.sh} local completions if [ "$COMP_CWORD" -eq 1 ]; then - completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 $_todo_sh command listaddons 2>/dev/null) $OPTS" + completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 "$_todo_sh" command listaddons 2>/dev/null) $OPTS" elif [[ $COMP_CWORD -gt 2 && ( \ "${COMP_WORDS[COMP_CWORD-2]}" =~ ^($MOVE_COMMAND_PATTERN${_todo_file2_actions:+|${_todo_file2_actions}})$ || \ "${COMP_WORDS[COMP_CWORD-3]}" =~ ^($MOVE_COMMAND_PATTERN${_todo_file3_actions:+|${_todo_file3_actions}})$ ) ]]; then # "move ITEM# DEST [SRC]" has file arguments on positions 2 and 3. - completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listfile 2>/dev/null) + completions=$(eval TODOTXT_VERBOSE=0 "$_todo_sh" command listfile 2>/dev/null) else case "$prev" in command) completions=$COMMANDS;; help) - completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 $_todo_sh command listaddons 2>/dev/null)";; - -*) completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 $_todo_sh command listaddons 2>/dev/null) $OPTS";; + completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 "$_todo_sh" command listaddons 2>/dev/null)";; + -*) completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 "$_todo_sh" command listaddons 2>/dev/null) $OPTS";; *) if [[ "$prev" =~ ^(addto|listfile|lf${_todo_file1_actions:+|${_todo_file1_actions}})$ ]]; then - completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listfile 2>/dev/null) + completions=$(eval TODOTXT_VERBOSE=0 "$_todo_sh" command listfile 2>/dev/null) else case "$cur" in - +*) completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listproj 2>/dev/null) - COMPREPLY=( $( compgen -W "$completions" -- $cur )) + +*) completions=$(eval TODOTXT_VERBOSE=0 "$_todo_sh" command listproj 2>/dev/null) + COMPREPLY=( $(compgen -W "$completions" -- "$cur")) [ ${#COMPREPLY[@]} -gt 0 ] && return 0 # Fall back to projects extracted from done tasks. - completions=$(eval 'TODOTXT_VERBOSE=0 TODOTXT_SOURCEVAR=\$DONE_FILE' $_todo_sh command listproj 2>/dev/null) + completions=$(eval 'TODOTXT_VERBOSE=0 TODOTXT_SOURCEVAR=\$DONE_FILE' "$_todo_sh" command listproj 2>/dev/null) ;; - @*) completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listcon 2>/dev/null) - COMPREPLY=( $( compgen -W "$completions" -- $cur )) + @*) completions=$(eval TODOTXT_VERBOSE=0 "$_todo_sh" command listcon 2>/dev/null) + COMPREPLY=( $(compgen -W "$completions" -- "$cur")) [ ${#COMPREPLY[@]} -gt 0 ] && return 0 # Fall back to contexts extracted from done tasks. - completions=$(eval 'TODOTXT_VERBOSE=0 TODOTXT_SOURCEVAR=\$DONE_FILE' $_todo_sh command listcon 2>/dev/null) + completions=$(eval 'TODOTXT_VERBOSE=0 TODOTXT_SOURCEVAR=\$DONE_FILE' "$_todo_sh" command listcon 2>/dev/null) ;; *) if [[ "$cur" =~ ^[0-9]+$ ]]; then declare -a sedTransformations=( @@ -90,7 +90,7 @@ _todo() esac fi - COMPREPLY=( $( compgen -W "$completions" -- $cur )) + COMPREPLY=( $(compgen -W "$completions" -- "$cur")) return 0 } complete -F _todo todo.sh From 84d027ea296df20023fb982c0dae07f7421457b3 Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Tue, 10 Aug 2021 18:16:17 +0000 Subject: [PATCH 07/21] use consistent code style for if/then and while/do --- todo.sh | 58 ++++++++++++++++++++------------------------------------- 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/todo.sh b/todo.sh index 1d3cf59b..e19e46c4 100755 --- a/todo.sh +++ b/todo.sh @@ -303,8 +303,7 @@ addonHelp() { if [ -d "$TODO_ACTIONS_DIR" ]; then didPrintAddonActionsHeader= - for action in "$TODO_ACTIONS_DIR"/* - do + for action in "$TODO_ACTIONS_DIR"/*; do if [ -f "$action" ] && [ -x "$action" ]; then if [ -z "$didPrintAddonActionsHeader" ]; then cat <<-EndAddonActionsHeader @@ -328,8 +327,7 @@ addonHelp() actionUsage() { - for actionName - do + for actionName; do action="${TODO_ACTIONS_DIR}/${actionName}" if [ -f "$action" ] && [ -x "$action" ]; then "$action" usage @@ -501,8 +499,7 @@ uppercasePriority() # Postcondition: Modifies $input. lower=( {a..z} ) upper=( {A..Z} ) - for ((i=0; i<26; i++)) - do + for ((i=0; i<26; i++)); do upperPriority="${upperPriority};s/^[(]${lower[i]}[)]/(${upper[i]})/" done input=$(echo "$input" | sed "$upperPriority") @@ -525,8 +522,7 @@ OVR_TODOTXT_FINAL_FILTER="$TODOTXT_FINAL_FILTER" export GREP_OPTIONS="" # == PROCESS OPTIONS == -while getopts ":fhpcnNaAtTvVx+@Pd:" Option -do +while getopts ":fhpcnNaAtTvVx+@Pd:" Option; do case $Option in '@') ## HIDE_CONTEXT_NAMES starts at zero (false); increment it to one @@ -535,8 +531,7 @@ do ## number shows context names and an odd number hides context ## names. : $(( HIDE_CONTEXT_NAMES++ )) - if [ $(( HIDE_CONTEXT_NAMES % 2 )) -eq 0 ] - then + if [ $(( HIDE_CONTEXT_NAMES % 2 )) -eq 0 ]; then ## Zero or even value -- show context names unset HIDE_CONTEXTS_SUBSTITUTION else @@ -551,8 +546,7 @@ do ## number shows project names and an odd number hides project ## names. : $(( HIDE_PROJECT_NAMES++ )) - if [ $(( HIDE_PROJECT_NAMES % 2 )) -eq 0 ] - then + if [ $(( HIDE_PROJECT_NAMES % 2 )) -eq 0 ]; then ## Zero or even value -- show project names unset HIDE_PROJECTS_SUBSTITUTION else @@ -598,8 +592,7 @@ do ## number shows priority labels and an odd number hides priority ## labels. : $(( HIDE_PRIORITY_LABELS++ )) - if [ $(( HIDE_PRIORITY_LABELS % 2 )) -eq 0 ] - then + if [ $(( HIDE_PRIORITY_LABELS % 2 )) -eq 0 ]; then ## Zero or even value -- show priority labels unset HIDE_PRIORITY_SUBSTITUTION else @@ -700,27 +693,22 @@ configFileLocations=( "$TODOTXT_GLOBAL_CFG_FILE" ) -[ -e "$TODOTXT_CFG_FILE" ] || for CFG_FILE_ALT in "${configFileLocations[@]}" -do - if [ -e "$CFG_FILE_ALT" ] - then +[ -e "$TODOTXT_CFG_FILE" ] || for CFG_FILE_ALT in "${configFileLocations[@]}"; do + if [ -e "$CFG_FILE_ALT" ]; then TODOTXT_CFG_FILE="$CFG_FILE_ALT" break fi done -if [ -z "$TODO_ACTIONS_DIR" ] || [ ! -d "$TODO_ACTIONS_DIR" ] -then +if [ -z "$TODO_ACTIONS_DIR" ] || [ ! -d "$TODO_ACTIONS_DIR" ]; then TODO_ACTIONS_DIR="$HOME/.todo/actions" export TODO_ACTIONS_DIR fi [ -d "$TODO_ACTIONS_DIR" ] || for TODO_ACTIONS_DIR_ALT in \ "$HOME/.todo.actions.d" \ - "${XDG_CONFIG_HOME:-$HOME/.config}/todo/actions" -do - if [ -d "$TODO_ACTIONS_DIR_ALT" ] - then + "${XDG_CONFIG_HOME:-$HOME/.config}/todo/actions"; do + if [ -d "$TODO_ACTIONS_DIR_ALT" ]; then TODO_ACTIONS_DIR="$TODO_ACTIONS_DIR_ALT" break fi @@ -837,11 +825,9 @@ filtercommand() post_filter=${1:-} shift - for search_term - do + for search_term; do ## See if the first character of $search_term is a dash - if [ "${search_term:0:1}" != '-' ] - then + if [ "${search_term:0:1}" != '-' ]; then ## First character isn't a dash: hide lines that don't match ## this $search_term filter="${filter:-}${filter:+ | }grep -i $(shellquote "$search_term")" @@ -1046,18 +1032,15 @@ action=$( printf "%s\n" "$ACTION" | tr '[:upper:]' '[:lower:]' ) ## using todo.sh builtins. ## Else, run a actions script with the name of the command if it exists ## or fallback to using a builtin -if [ "$action" == command ] -then +if [ "$action" == command ]; then ## Get rid of "command" from arguments list shift ## Reset action to new first argument - action=$( printf "%s\n" "$1" | tr '[:upper:]' '[:lower:]' ) -elif [ -d "$TODO_ACTIONS_DIR/$action" ] && [ -x "$TODO_ACTIONS_DIR/$action/$action" ] -then + action=$(printf "%s\n" "$1" | tr '[:upper:]' '[:lower:]') +elif [ -d "$TODO_ACTIONS_DIR/$action" ] && [ -x "$TODO_ACTIONS_DIR/$action/$action" ]; then "$TODO_ACTIONS_DIR/$action/$action" "$@" exit $? -elif [ -d "$TODO_ACTIONS_DIR" ] && [ -x "$TODO_ACTIONS_DIR/$action" ] -then +elif [ -d "$TODO_ACTIONS_DIR" ] && [ -x "$TODO_ACTIONS_DIR/$action" ]; then "$TODO_ACTIONS_DIR/$action" "$@" exit $? fi @@ -1244,7 +1227,7 @@ case $action in fi done - if [ $TODOTXT_AUTO_ARCHIVE = 1 ]; then + if [ "$TODOTXT_AUTO_ARCHIVE" = 1 ]; then # Recursively invoke the script to allow overriding of the archive # action. "$TODO_FULL_SH" archive @@ -1486,8 +1469,7 @@ note: PRIORITY must be anywhere from A to Z." "listaddons" ) if [ -d "$TODO_ACTIONS_DIR" ]; then cd "$TODO_ACTIONS_DIR" || exit $? - for action in * - do + for action in *; do if [ -f "$action" ] && [ -x "$action" ]; then echo "$action" elif [ -d "$action" ] && [ -x "$action/$action" ]; then From 584ac89a5aa56499c0f299cdf6607db6680975fd Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Tue, 10 Aug 2021 18:18:02 +0000 Subject: [PATCH 08/21] fix and unify whitespace --- todo.sh | 189 ++++++++++++++++++++++++------------------------ todo_completion | 4 +- 2 files changed, 98 insertions(+), 95 deletions(-) diff --git a/todo.sh b/todo.sh index e19e46c4..7dc92c85 100755 --- a/todo.sh +++ b/todo.sh @@ -6,16 +6,17 @@ # === HEAVY LIFTING === shopt -s extglob extquote -# NOTE: Todo.sh requires a configuration file to run. +# NOTE: Todo.sh requires a configuration file to run. # Place it in one of the default locations or use the -d option for a custom location. [ -f VERSION-FILE ] && . VERSION-FILE || VERSION="@DEV_VERSION@" -version() { +version() +{ cat <<-EndVersion TODO.TXT Command Line Interface v$VERSION - Homepage: http://todotxt.org - Code repository: https://github.com/todotxt/todo.txt-cli/ + Homepage: http://todotxt.org/ + Code repository: https://github.com/todotxt/todo.txt-cli Contributors: https://github.com/todotxt/todo.txt-cli/graphs/contributors License: https://github.com/todotxt/todo.txt-cli/blob/master/LICENSE EndVersion @@ -159,8 +160,8 @@ $indentedJoinedConfigFileLocations TODOTXT_SIGIL_AFTER_PATTERN="" optionally allow chars after +p / @c EndVerboseHelp - actionsHelp - addonHelp + actionsHelp + addonHelp } actionsHelp() @@ -232,7 +233,7 @@ actionsHelp() listall [TERM...] lsa [TERM...] Displays all the lines in todo.txt AND done.txt that contain TERM(s) - sorted by priority with line numbers. Hides all tasks that + sorted by priority with line numbers. Hides all tasks that contain TERM(s) preceded by a minus sign (i.e. -TERM). If no TERM specified, lists entire todo.txt AND done.txt concatenated and sorted. @@ -248,7 +249,7 @@ actionsHelp() listfile [SRC [TERM...]] lf [SRC [TERM...]] Displays all the lines in SRC file located in the todo.txt directory, - sorted by priority with line numbers. If TERM specified, lists + sorted by priority with line numbers. If TERM specified, lists all lines that contain TERM(s) in SRC file. Hides all tasks that contain TERM(s) preceded by a minus sign (i.e. -TERM). Without any arguments, the names of all text files in the todo.txt @@ -307,16 +308,16 @@ addonHelp() if [ -f "$action" ] && [ -x "$action" ]; then if [ -z "$didPrintAddonActionsHeader" ]; then cat <<-EndAddonActionsHeader - Add-on Actions: - EndAddonActionsHeader + Add-on Actions: + EndAddonActionsHeader didPrintAddonActionsHeader=1 fi "$action" usage elif [ -d "$action" ] && [ -x "$action"/"$(basename "$action")" ]; then if [ -z "$didPrintAddonActionsHeader" ]; then cat <<-EndAddonActionsHeader - Add-on Actions: - EndAddonActionsHeader + Add-on Actions: + EndAddonActionsHeader didPrintAddonActionsHeader=1 fi "$action"/"$(basename "$action")" usage @@ -355,6 +356,7 @@ dieWithHelp() die "$@" } + die() { echo "$*" @@ -505,7 +507,7 @@ uppercasePriority() input=$(echo "$input" | sed "$upperPriority") } -#Preserving environment variables so they don't get clobbered by the config file +# Preserving environment variables so they don't get clobbered by the config file OVR_TODOTXT_AUTO_ARCHIVE="$TODOTXT_AUTO_ARCHIVE" OVR_TODOTXT_FORCE="$TODOTXT_FORCE" OVR_TODOTXT_PRESERVE_LINE_NUMBERS="$TODOTXT_PRESERVE_LINE_NUMBERS" @@ -635,9 +637,9 @@ TODOTXT_SORT_COMMAND=${TODOTXT_SORT_COMMAND:-env LC_COLLATE=C sort -f -k2} TODOTXT_DISABLE_FILTER=${TODOTXT_DISABLE_FILTER:-} TODOTXT_FINAL_FILTER=${TODOTXT_FINAL_FILTER:-cat} TODOTXT_GLOBAL_CFG_FILE=${TODOTXT_GLOBAL_CFG_FILE:-/etc/todo/config} -TODOTXT_SIGIL_BEFORE_PATTERN=${TODOTXT_SIGIL_BEFORE_PATTERN:-} # Allow any other non-whitespace entity before +project and @context; should be an optional match; example: \(w:\)\{0,1\} to allow w:@context. -TODOTXT_SIGIL_VALID_PATTERN=${TODOTXT_SIGIL_VALID_PATTERN:-.*} # Limit the valid characters (from the default any non-whitespace sequence) for +project and @context; example: [a-zA-Z]\{3,\} to only allow alphabetic ones that are at least three characters long. -TODOTXT_SIGIL_AFTER_PATTERN=${TODOTXT_SIGIL_AFTER_PATTERN:-} # Allow any other non-whitespace entity after +project and @context; should be an optional match; example: )\{0,1\} to allow (with the corresponding TODOTXT_SIGIL_BEFORE_PATTERN) enclosing in parentheses. +TODOTXT_SIGIL_BEFORE_PATTERN=${TODOTXT_SIGIL_BEFORE_PATTERN:-} # Allow any other non-whitespace entity before +project and @context; should be an optional match; example: \(w:\)\{0,1\} to allow w:@context. +TODOTXT_SIGIL_VALID_PATTERN=${TODOTXT_SIGIL_VALID_PATTERN:-.*} # Limit the valid characters (from the default any non-whitespace sequence) for +project and @context; example: [a-zA-Z]\{3,\} to only allow alphabetic ones that are at least three characters long. +TODOTXT_SIGIL_AFTER_PATTERN=${TODOTXT_SIGIL_AFTER_PATTERN:-} # Allow any other non-whitespace entity after +project and @context; should be an optional match; example: )\{0,1\} to allow (with the corresponding TODOTXT_SIGIL_BEFORE_PATTERN) enclosing in parentheses. # Export all TODOTXT_* variables export "${!TODOTXT_@}" @@ -714,52 +716,51 @@ fi fi done - # === SANITY CHECKS (thanks Karl!) === [ -r "$TODOTXT_CFG_FILE" ] || dieWithHelp "$1" "Fatal Error: Cannot read configuration file ${TODOTXT_CFG_FILE:-${configFileLocations[0]}}" . "$TODOTXT_CFG_FILE" # === APPLY OVERRIDES -if [ -n "$OVR_TODOTXT_AUTO_ARCHIVE" ] ; then - TODOTXT_AUTO_ARCHIVE="$OVR_TODOTXT_AUTO_ARCHIVE" +if [ -n "$OVR_TODOTXT_AUTO_ARCHIVE" ]; then + TODOTXT_AUTO_ARCHIVE="$OVR_TODOTXT_AUTO_ARCHIVE" fi -if [ -n "$OVR_TODOTXT_FORCE" ] ; then - TODOTXT_FORCE="$OVR_TODOTXT_FORCE" +if [ -n "$OVR_TODOTXT_FORCE" ]; then + TODOTXT_FORCE="$OVR_TODOTXT_FORCE" fi -if [ -n "$OVR_TODOTXT_PRESERVE_LINE_NUMBERS" ] ; then - TODOTXT_PRESERVE_LINE_NUMBERS="$OVR_TODOTXT_PRESERVE_LINE_NUMBERS" +if [ -n "$OVR_TODOTXT_PRESERVE_LINE_NUMBERS" ]; then + TODOTXT_PRESERVE_LINE_NUMBERS="$OVR_TODOTXT_PRESERVE_LINE_NUMBERS" fi -if [ -n "$OVR_TODOTXT_PLAIN" ] ; then - TODOTXT_PLAIN="$OVR_TODOTXT_PLAIN" +if [ -n "$OVR_TODOTXT_PLAIN" ]; then + TODOTXT_PLAIN="$OVR_TODOTXT_PLAIN" fi -if [ -n "$OVR_TODOTXT_DATE_ON_ADD" ] ; then - TODOTXT_DATE_ON_ADD="$OVR_TODOTXT_DATE_ON_ADD" +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 +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" +if [ -n "$OVR_TODOTXT_DISABLE_FILTER" ]; then + TODOTXT_DISABLE_FILTER="$OVR_TODOTXT_DISABLE_FILTER" fi -if [ -n "$OVR_TODOTXT_VERBOSE" ] ; then - TODOTXT_VERBOSE="$OVR_TODOTXT_VERBOSE" +if [ -n "$OVR_TODOTXT_VERBOSE" ]; then + TODOTXT_VERBOSE="$OVR_TODOTXT_VERBOSE" fi -if [ -n "$OVR_TODOTXT_DEFAULT_ACTION" ] ; then - TODOTXT_DEFAULT_ACTION="$OVR_TODOTXT_DEFAULT_ACTION" +if [ -n "$OVR_TODOTXT_DEFAULT_ACTION" ]; then + TODOTXT_DEFAULT_ACTION="$OVR_TODOTXT_DEFAULT_ACTION" fi -if [ -n "$OVR_TODOTXT_SORT_COMMAND" ] ; then - TODOTXT_SORT_COMMAND="$OVR_TODOTXT_SORT_COMMAND" +if [ -n "$OVR_TODOTXT_SORT_COMMAND" ]; then + TODOTXT_SORT_COMMAND="$OVR_TODOTXT_SORT_COMMAND" fi -if [ -n "$OVR_TODOTXT_FINAL_FILTER" ] ; then - TODOTXT_FINAL_FILTER="$OVR_TODOTXT_FINAL_FILTER" +if [ -n "$OVR_TODOTXT_FINAL_FILTER" ]; then + TODOTXT_FINAL_FILTER="$OVR_TODOTXT_FINAL_FILTER" fi 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 "$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\")." @@ -801,7 +802,7 @@ _addto() { 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") + input=$(echo -n "($TODOTXT_PRIORITY_ON_ADD) "; echo "$input") fi fi fixMissingEndOfLine "$file" @@ -877,12 +878,14 @@ _list() { 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 @@ -907,7 +910,7 @@ _format() sed = "$FILE" else sed = - fi \ + fi \ | sed -e ''' N s/^/ / @@ -924,7 +927,7 @@ _format() filtered_items=$items fi filtered_items=$( - echo -n "$filtered_items" \ + echo -n "$filtered_items" \ | sed ''' s/^ /00000/; s/^ /0000/; @@ -934,7 +937,7 @@ _format() ''' \ | eval "${TODOTXT_SORT_COMMAND}" \ | awk ''' - function highlight(colorVar, color) { + function highlight(colorVar, color) { color = ENVIRON[colorVar] gsub(/\\+033/, "\033", color) return color @@ -988,7 +991,7 @@ _format() } printf "%s\n", end_clr } - ''' \ + ''' \ | sed ''' s/'"${HIDE_PROJECTS_SUBSTITUTION:-^}"'//g s/'"${HIDE_CONTEXTS_SUBSTITUTION:-^}"'//g @@ -999,8 +1002,8 @@ _format() [ -n "$filtered_items" ] && echo "$filtered_items" if [ "$TODOTXT_VERBOSE" -gt 0 ]; then - NUMTASKS=$( echo -n "$filtered_items" | sed -n '$ =' ) - TOTALTASKS=$( echo -n "$items" | sed -n '$ =' ) + NUMTASKS=$(echo -n "$filtered_items" | sed -n '$ =') + TOTALTASKS=$(echo -n "$items" | sed -n '$ =') fi if [ "$TODOTXT_VERBOSE" -gt 1 ]; then echo "TODO DEBUG: Filter Command was: ${filter_command:-cat}" @@ -1026,7 +1029,7 @@ listWordsWithSigil() export -f cleaninput getPrefix getTodo getNewtodo shellquote filtercommand _list listWordsWithSigil getPadding _format die # == HANDLE ACTION == -action=$( printf "%s\n" "$ACTION" | tr '[:upper:]' '[:lower:]' ) +action=$(printf "%s\n" "$ACTION" | tr '[:upper:]' '[:lower:]') ## If the first argument is "command", run the rest of the arguments ## using todo.sh builtins. @@ -1075,13 +1078,13 @@ case $action in IFS=$'\n' # Treat each line separately - for line in $input ; do + for line in $input; do _addto "$TODO_FILE" "$line" done IFS=$SAVEIFS ;; -"addto" ) +"addto") [ -z "$2" ] && die "usage: $TODO_SH addto DEST \"TODO ITEM\"" dest="$TODO_DIR/$2" [ -z "$3" ] && die "usage: $TODO_SH addto DEST \"TODO ITEM\"" @@ -1096,7 +1099,7 @@ case $action in fi ;; -"append" | "app" ) +"append" | "app") errmsg="usage: $TODO_SH append ITEM# \"TEXT TO APPEND\"" shift; item=$1; shift getTodo "$item" @@ -1123,7 +1126,7 @@ case $action in fi ;; -"archive" ) +"archive") # defragment blank lines sed -i.bak -e '/./!d' "$TODO_FILE" [ "$TODOTXT_VERBOSE" -gt 0 ] && grep "^x " "$TODO_FILE" @@ -1134,7 +1137,7 @@ case $action in fi ;; -"del" | "rm" ) +"del" | "rm") # replace deleted line with a blank line when TODOTXT_PRESERVE_LINE_NUMBERS is 1 errmsg="usage: $TODO_SH del ITEM# [TERM]" item=$2 @@ -1177,7 +1180,7 @@ case $action in fi ;; -"depri" | "dp" ) +"depri" | "dp") errmsg="usage: $TODO_SH depri ITEM#[, ITEM#, ITEM#, ...]" shift; [ $# -eq 0 ] && die "$errmsg" @@ -1187,20 +1190,20 @@ case $action in for item in ${*//,/ }; do getTodo "$item" - if [[ "$todo" = \(?\)\ * ]]; then - sed -i.bak -e "${item}s/^(.) //" "$TODO_FILE" - if [ "$TODOTXT_VERBOSE" -gt 0 ]; then - getNewtodo "$item" - echo "$item $newtodo" - echo "TODO: $item deprioritized." - fi - else - echo "TODO: $item is not prioritized." - fi + if [[ "$todo" = \(?\)\ * ]]; then + sed -i.bak -e "${item}s/^(.) //" "$TODO_FILE" + if [ "$TODOTXT_VERBOSE" -gt 0 ]; then + getNewtodo "$item" + echo "$item $newtodo" + echo "TODO: $item deprioritized." + fi + else + echo "TODO: $item is not prioritized." + fi done ;; -"do" | "done" ) +"do" | "done") errmsg="usage: $TODO_SH do ITEM#[, ITEM#, ITEM#, ...]" # shift so we get arguments to the do request shift; @@ -1234,13 +1237,13 @@ case $action in fi ;; -"help" ) +"help") shift ## Was help; new $1 is first help topic / action name if [ $# -gt 0 ]; then # Don't use PAGER here; we don't expect much usage output from one / few actions. actionUsage "$@" else - if [ -t 1 ] ; then # STDOUT is a TTY + if [ -t 1 ]; then # STDOUT is a TTY if command -v "${PAGER:-less}" >/dev/null 2>&1; then # we have a working PAGER (or less as a default) help | "${PAGER:-less}" && exit 0 @@ -1250,8 +1253,8 @@ case $action in fi ;; -"shorthelp" ) - if [ -t 1 ] ; then # STDOUT is a TTY +"shorthelp") + if [ -t 1 ]; then # STDOUT is a TTY if command -v "${PAGER:-less}" >/dev/null 2>&1; then # we have a working PAGER (or less as a default) shorthelp | "${PAGER:-less}" && exit 0 @@ -1260,22 +1263,22 @@ case $action in shorthelp # just in case something failed above, we go ahead and just spew to STDOUT ;; -"list" | "ls" ) +"list" | "ls") shift ## Was ls; new $1 is first search term _list "$TODO_FILE" "$@" ;; -"listall" | "lsa" ) +"listall" | "lsa") shift ## Was lsa; new $1 is first search term - TOTAL=$( sed -n '$ =' "$TODO_FILE" ) + TOTAL=$(sed -n '$ =' "$TODO_FILE") PADDING=${#TOTAL} post_filter_command="${post_filter_command:-}${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" ) + TDONE=$(sed -n '$ =' "$DONE_FILE") 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 "--" @@ -1285,7 +1288,7 @@ case $action in fi ;; -"listfile" | "lf" ) +"listfile" | "lf") shift ## Was listfile, next $1 is file name if [ $# -eq 0 ]; then [ "$TODOTXT_VERBOSE" -gt 0 ] && echo "Files in the todo.txt directory:" @@ -1298,17 +1301,17 @@ case $action in fi ;; -"listcon" | "lsc" ) +"listcon" | "lsc") shift listWordsWithSigil '@' "$@" ;; -"listproj" | "lsprj" ) +"listproj" | "lsprj") shift listWordsWithSigil '+' "$@" ;; -"listpri" | "lsp" ) +"listpri" | "lsp") shift ## was "listpri", new $1 is priority to list or first TERM pri=$(printf "%s\n" "$1" | tr '[:lower:]' '[:upper:]' | grep -e '^[A-Z]$' -e '^[A-Z]-[A-Z]$') && shift || pri="A-Z" @@ -1316,7 +1319,7 @@ case $action in _list "$TODO_FILE" "$@" ;; -"move" | "mv" ) +"move" | "mv") # replace moved line with a blank line when TODOTXT_PRESERVE_LINE_NUMBERS is 1 errmsg="usage: $TODO_SH mv ITEM# DEST [SRC]" item=$2 @@ -1351,16 +1354,16 @@ case $action in fi ;; -"prepend" | "prep" ) +"prepend" | "prep") errmsg="usage: $TODO_SH prepend ITEM# \"TEXT TO PREPEND\"" replaceOrPrepend 'prepend' "$@" ;; -"pri" | "p" ) +"pri" | "p") shift - while [ "$#" -gt 0 ] ; do + while [ "$#" -gt 0 ]; do item=$1 - newpri=$( printf "%s\n" "$2" | tr '[:lower:]' '[:upper:]' ) + newpri=$(printf "%s\n" "$2" | tr '[:lower:]' '[:upper:]') errmsg="usage: $TODO_SH pri ITEM# PRIORITY[, ITEM# PRIORITY, ...] note: PRIORITY must be anywhere from A to Z." @@ -1395,19 +1398,19 @@ note: PRIORITY must be anywhere from A to Z." done ;; -"replace" ) +"replace") errmsg="usage: $TODO_SH replace ITEM# \"UPDATED ITEM\"" replaceOrPrepend 'replace' "$@" ;; -"report" ) +"report") # archive first # Recursively invoke the script to allow overriding of the archive # action. "$TODO_FULL_SH" archive - TOTAL=$( sed -n '$ =' "$TODO_FILE" ) - TDONE=$( sed -n '$ =' "$DONE_FILE" ) + TOTAL=$(sed -n '$ =' "$TODO_FILE") + TDONE=$(sed -n '$ =' "$DONE_FILE") NEWDATA="${TOTAL:-0} ${TDONE:-0}" LASTREPORT=$(sed -ne '$p' "$REPORT_FILE") LASTDATA=${LASTREPORT#* } # Strip timestamp. @@ -1422,8 +1425,8 @@ note: PRIORITY must be anywhere from A to Z." fi ;; -"deduplicate" ) - if [ $TODOTXT_PRESERVE_LINE_NUMBERS = 0 ]; then +"deduplicate") + if [ "$TODOTXT_PRESERVE_LINE_NUMBERS" = 0 ]; then deduplicateSedCommand='d' else deduplicateSedCommand='s/^.*//; p' @@ -1431,7 +1434,7 @@ note: PRIORITY must be anywhere from A to Z." # To determine the difference when deduplicated lines are preserved, only # non-empty lines must be counted. - originalTaskNum=$( sed -e '/./!d' "$TODO_FILE" | sed -n '$ =' ) + originalTaskNum=$(sed -e '/./!d' "$TODO_FILE" | sed -n '$ =') # Look for duplicate lines and discard the second occurrence. # We start with an empty hold space on the first line. For each line: @@ -1457,7 +1460,7 @@ note: PRIORITY must be anywhere from A to Z." -e "$deduplicateSedCommand" \ "$TODO_FILE" - newTaskNum=$( sed -e '/./!d' "$TODO_FILE" | sed -n '$ =' ) + newTaskNum=$(sed -e '/./!d' "$TODO_FILE" | sed -n '$ =') deduplicateNum=$(( originalTaskNum - newTaskNum )) if [ "$deduplicateNum" -eq 0 ]; then echo "TODO: No duplicate tasks found" @@ -1466,7 +1469,7 @@ note: PRIORITY must be anywhere from A to Z." fi ;; -"listaddons" ) +"listaddons") if [ -d "$TODO_ACTIONS_DIR" ]; then cd "$TODO_ACTIONS_DIR" || exit $? for action in *; do @@ -1479,6 +1482,6 @@ note: PRIORITY must be anywhere from A to Z." fi ;; -* ) +*) usage;; esac diff --git a/todo_completion b/todo_completion index 493d528c..9c069845 100644 --- a/todo_completion +++ b/todo_completion @@ -12,8 +12,8 @@ _todo() local -r OPTS="-@ -@@ -+ -++ -d -f -h -p -P -PP -a -n -t -v -vv -V -x" local -r COMMANDS="\ - add a addto addm append app archive command del \ - rm depri dp do help list ls listaddons listall lsa listcon \ + add a addto addm append app archive command del \ + rm depri dp do help list ls listaddons listall lsa listcon \ lsc listfile lf listpri lsp listproj lsprj move \ mv prepend prep pri p replace report shorthelp" local -r MOVE_COMMAND_PATTERN='move|mv' From 581de8d1b2bb61a5d93fedb1d8ea0f13b94ba7a8 Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Tue, 10 Aug 2021 19:33:46 +0000 Subject: [PATCH 09/21] Revert "move bash version check to proper place" Breaks on macOS This reverts commit 17e3e88a78951c6cca676766da4693a024acdab7. --- todo.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/todo.sh b/todo.sh index 7dc92c85..30773914 100755 --- a/todo.sh +++ b/todo.sh @@ -1,8 +1,5 @@ #!/usr/bin/env bash -# Check bash version -(( BASH_VERSINFO[0] > 4 || BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 1 )) || { printf 'Bash 4.1 or newer requried\n' >&2; exit 1; } - # === HEAVY LIFTING === shopt -s extglob extquote @@ -368,8 +365,11 @@ confirm() [ "$TODOTXT_FORCE" = 0 ] || return 0 printf %s "${1:?}? (y/n) " + local readArgs=(-e -r) + [ -n "${BASH_VERSINFO:-}" ] && [ \( ${BASH_VERSINFO[0]} -eq 4 -a ${BASH_VERSINFO[1]} -ge 1 \) -o ${BASH_VERSINFO[0]} -gt 4 ] && + readArgs+=(-N 1) # Bash 4.1+ supports -N nchars local answer - read -r -e -N 1 answer + read "${readArgs[@]}" answer echo [ "$answer" = "y" ] } From 3c73313d0736ce8acdb94fc4607d9095815300b8 Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Tue, 10 Aug 2021 19:39:32 +0000 Subject: [PATCH 10/21] use proper bash version check needed due to old version of bash on macOS --- todo.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/todo.sh b/todo.sh index 30773914..4f4f57ef 100755 --- a/todo.sh +++ b/todo.sh @@ -365,11 +365,12 @@ confirm() [ "$TODOTXT_FORCE" = 0 ] || return 0 printf %s "${1:?}? (y/n) " - local readArgs=(-e -r) - [ -n "${BASH_VERSINFO:-}" ] && [ \( ${BASH_VERSINFO[0]} -eq 4 -a ${BASH_VERSINFO[1]} -ge 1 \) -o ${BASH_VERSINFO[0]} -gt 4 ] && - readArgs+=(-N 1) # Bash 4.1+ supports -N nchars local answer - read "${readArgs[@]}" answer + if (( BASH_VERSINFO[0] > 4 || BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 1 )); then + read -r -e -N 1 answer + else + read -r -e answer + fi echo [ "$answer" = "y" ] } From f1b68e3418fec4c0e9fea253aebf647178d7f31c Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Tue, 10 Aug 2021 20:21:06 +0000 Subject: [PATCH 11/21] use better comparison syntax --- todo.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/todo.sh b/todo.sh index 4f4f57ef..d8eb7777 100755 --- a/todo.sh +++ b/todo.sh @@ -534,7 +534,7 @@ while getopts ":fhpcnNaAtTvVx+@Pd:" Option; do ## number shows context names and an odd number hides context ## names. : $(( HIDE_CONTEXT_NAMES++ )) - if [ $(( HIDE_CONTEXT_NAMES % 2 )) -eq 0 ]; then + if (( HIDE_CONTEXT_NAMES % 2 == 0 )); then ## Zero or even value -- show context names unset HIDE_CONTEXTS_SUBSTITUTION else @@ -549,7 +549,7 @@ while getopts ":fhpcnNaAtTvVx+@Pd:" Option; do ## number shows project names and an odd number hides project ## names. : $(( HIDE_PROJECT_NAMES++ )) - if [ $(( HIDE_PROJECT_NAMES % 2 )) -eq 0 ]; then + if (( HIDE_PROJECT_NAMES % 2 == 0 )); then ## Zero or even value -- show project names unset HIDE_PROJECTS_SUBSTITUTION else @@ -595,7 +595,7 @@ while getopts ":fhpcnNaAtTvVx+@Pd:" Option; do ## number shows priority labels and an odd number hides priority ## labels. : $(( HIDE_PRIORITY_LABELS++ )) - if [ $(( HIDE_PRIORITY_LABELS % 2 )) -eq 0 ]; then + if (( HIDE_PRIORITY_LABELS % 2 == 0 )); then ## Zero or even value -- show priority labels unset HIDE_PRIORITY_SUBSTITUTION else From 006f9784c3d9a041c6689fb6c6cd4d156cffc177 Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Tue, 10 Aug 2021 21:51:51 +0000 Subject: [PATCH 12/21] fix grammar --- .github/ISSUE_TEMPLATE.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 34020845..0112da62 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -10,7 +10,7 @@ **What is the expected behavior?** -**Which versions todo.sh are you using?** +**Which version of todo.sh are you using?** > Run `todo.sh -V` @@ -19,4 +19,3 @@ **Which version of bash are you using?** > Run `bash --version` - From 1c619cabc1dbb4a9a9b535e5bbc9bdf9dc6f28cd Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Wed, 11 Aug 2021 23:33:29 +0000 Subject: [PATCH 13/21] make version flag -V not exit with an error code --- todo.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/todo.sh b/todo.sh index d8eb7777..23345f49 100755 --- a/todo.sh +++ b/todo.sh @@ -17,7 +17,7 @@ version() Contributors: https://github.com/todotxt/todo.txt-cli/graphs/contributors License: https://github.com/todotxt/todo.txt-cli/blob/master/LICENSE EndVersion - exit 1 + exit 0 } # Set script name and full path early. From ac13085748e3279ecbb92cd1704a23ee1f9dc0ab Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Thu, 12 Aug 2021 02:31:02 +0000 Subject: [PATCH 14/21] unify commenting code style --- todo.sh | 106 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/todo.sh b/todo.sh index 23345f49..b0508ca0 100755 --- a/todo.sh +++ b/todo.sh @@ -528,32 +528,32 @@ export GREP_OPTIONS="" while getopts ":fhpcnNaAtTvVx+@Pd:" Option; do case $Option in '@') - ## HIDE_CONTEXT_NAMES starts at zero (false); increment it to one - ## (true) the first time this flag is seen. Each time the flag - ## is seen after that, increment it again so that an even - ## number shows context names and an odd number hides context - ## names. + # HIDE_CONTEXT_NAMES starts at zero (false); increment it to one + # (true) the first time this flag is seen. Each time the flag + # is seen after that, increment it again so that an even + # number shows context names and an odd number hides context + # names. : $(( HIDE_CONTEXT_NAMES++ )) if (( HIDE_CONTEXT_NAMES % 2 == 0 )); then - ## Zero or even value -- show context names + # Zero or even value -- show context names unset HIDE_CONTEXTS_SUBSTITUTION else - ## One or odd value -- hide context names + # One or odd value -- hide context names export HIDE_CONTEXTS_SUBSTITUTION='[[:space:]]@[[:graph:]]\{1,\}' fi ;; '+') - ## HIDE_PROJECT_NAMES starts at zero (false); increment it to one - ## (true) the first time this flag is seen. Each time the flag - ## is seen after that, increment it again so that an even - ## number shows project names and an odd number hides project - ## names. + # HIDE_PROJECT_NAMES starts at zero (false); increment it to one + # (true) the first time this flag is seen. Each time the flag + # is seen after that, increment it again so that an even + # number shows project names and an odd number hides project + # names. : $(( HIDE_PROJECT_NAMES++ )) if (( HIDE_PROJECT_NAMES % 2 == 0 )); then - ## Zero or even value -- show project names + # Zero or even value -- show project names unset HIDE_PROJECTS_SUBSTITUTION else - ## One or odd value -- hide project names + # One or odd value -- hide project names export HIDE_PROJECTS_SUBSTITUTION='[[:space:]][+][[:graph:]]\{1,\}' fi ;; @@ -589,17 +589,17 @@ while getopts ":fhpcnNaAtTvVx+@Pd:" Option; do OVR_TODOTXT_PLAIN=1 ;; P) - ## HIDE_PRIORITY_LABELS starts at zero (false); increment it to one - ## (true) the first time this flag is seen. Each time the flag - ## is seen after that, increment it again so that an even - ## number shows priority labels and an odd number hides priority - ## labels. + # HIDE_PRIORITY_LABELS starts at zero (false); increment it to one + # (true) the first time this flag is seen. Each time the flag + # is seen after that, increment it again so that an even + # number shows priority labels and an odd number hides priority + # labels. : $(( HIDE_PRIORITY_LABELS++ )) if (( HIDE_PRIORITY_LABELS % 2 == 0 )); then - ## Zero or even value -- show priority labels + # Zero or even value -- show priority labels unset HIDE_PRIORITY_SUBSTITUTION else - ## One or odd value -- hide priority labels + # One or odd value -- hide priority labels export HIDE_PRIORITY_SUBSTITUTION="([A-Z])[[:space:]]" fi ;; @@ -828,16 +828,16 @@ filtercommand() shift for search_term; do - ## See if the first character of $search_term is a dash + # See if the first character of $search_term is a dash if [ "${search_term:0:1}" != '-' ]; then - ## First character isn't a dash: hide lines that don't match - ## this $search_term + # First character isn't a dash: hide lines that don't match + # this $search_term filter="${filter:-}${filter:+ | }grep -i $(shellquote "$search_term")" else - ## First character is a dash: hide lines that match this - ## $search_term + # First character is a dash: hide lines that match this + # $search_term # - ## Remove the first character (-) before adding to our filter command + # Remove the first character (-) before adding to our filter command filter="${filter:-}${filter:+ | }grep -v -i $(shellquote "${search_term:1}")" fi done @@ -851,26 +851,26 @@ filtercommand() _list() { local FILE="$1" - ## If the file starts with a "/" use absolute path. Otherwise, - ## try to find it in either $TODO_DIR or using a relative path + # If the file starts with a "/" use absolute path. Otherwise, + # try to find it in either $TODO_DIR or using a relative path if [ "${1:0:1}" == / ]; then - ## Absolute path + # Absolute path src="$FILE" elif [ -f "$TODO_DIR/$FILE" ]; then - ## Path relative to todo.sh directory + # Path relative to todo.sh directory src="$TODO_DIR/$FILE" elif [ -f "$FILE" ]; then - ## Path relative to current working directory + # Path relative to current working directory src="$FILE" elif [ -f "$TODO_DIR/${FILE}.txt" ]; then - ## Path relative to todo.sh directory, missing file extension + # Path relative to todo.sh directory, missing file extension src="$TODO_DIR/${FILE}.txt" else die "TODO: File $FILE does not exist." fi - ## Get our search arguments, if any - shift ## was file name, new $1 is first search term + # Get our search arguments, if any + shift # was file name, new $1 is first search term _format "$src" '' "$@" @@ -882,7 +882,7 @@ _list() { getPadding() { - ## We need one level of padding for each power of 10 $LINES uses. + # We need one level of padding for each power of 10 $LINES uses. LINES=$(sed -n '$ =' "${1:-$TODO_FILE}") printf %s ${#LINES} } @@ -897,12 +897,12 @@ _format() FILE=$1 shift - ## Figure out how much padding we need to use, unless this was passed to us. + # 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 + # Number the file, then run the filter command, + # then sort and mangle output some more if [[ $TODOTXT_DISABLE_FILTER = 1 ]]; then TODOTXT_FINAL_FILTER="cat" fi @@ -920,7 +920,7 @@ _format() ''' ) - ## Build and apply the filter. + # Build and apply the filter. filter_command=$(filtercommand "${pre_filter_command:-}" "${post_filter_command:-}" "$@") if [ -n "${filter_command}" ]; then filtered_items=$(echo -n "$items" | eval "${filter_command}") @@ -1032,14 +1032,14 @@ export -f cleaninput getPrefix getTodo getNewtodo shellquote filtercommand _list # == HANDLE ACTION == action=$(printf "%s\n" "$ACTION" | tr '[:upper:]' '[:lower:]') -## If the first argument is "command", run the rest of the arguments -## using todo.sh builtins. -## Else, run a actions script with the name of the command if it exists -## or fallback to using a builtin +# If the first argument is "command", run the rest of the arguments +# using todo.sh builtins. +# Else, run a actions script with the name of the command if it exists +# or fallback to using a builtin if [ "$action" == command ]; then - ## Get rid of "command" from arguments list + # Get rid of "command" from arguments list shift - ## Reset action to new first argument + # Reset action to new first argument action=$(printf "%s\n" "$1" | tr '[:upper:]' '[:lower:]') elif [ -d "$TODO_ACTIONS_DIR/$action" ] && [ -x "$TODO_ACTIONS_DIR/$action/$action" ]; then "$TODO_ACTIONS_DIR/$action/$action" "$@" @@ -1049,7 +1049,7 @@ elif [ -d "$TODO_ACTIONS_DIR" ] && [ -x "$TODO_ACTIONS_DIR/$action" ]; then exit $? fi -## Only run if $action isn't found in .todo.actions.d +# Only run if $action isn't found in .todo.actions.d case $action in "add" | "a") if [[ -z "$2" && $TODOTXT_FORCE = 0 ]]; then @@ -1239,7 +1239,7 @@ case $action in ;; "help") - shift ## Was help; new $1 is first help topic / action name + shift # Was help; new $1 is first help topic / action name if [ $# -gt 0 ]; then # Don't use PAGER here; we don't expect much usage output from one / few actions. actionUsage "$@" @@ -1265,12 +1265,12 @@ case $action in ;; "list" | "ls") - shift ## Was ls; new $1 is first search term + shift # Was ls; new $1 is first search term _list "$TODO_FILE" "$@" ;; "listall" | "lsa") - shift ## Was lsa; new $1 is first search term + shift # Was lsa; new $1 is first search term TOTAL=$(sed -n '$ =' "$TODO_FILE") PADDING=${#TOTAL} @@ -1290,13 +1290,13 @@ case $action in ;; "listfile" | "lf") - shift ## Was listfile, next $1 is file name + shift # Was listfile, next $1 is file name if [ $# -eq 0 ]; then [ "$TODOTXT_VERBOSE" -gt 0 ] && echo "Files in the todo.txt directory:" cd "$TODO_DIR" && ls -1 -- *.txt else FILE="$1" - shift ## Was filename; next $1 is first search term + shift # Was filename; next $1 is first search term _list "$FILE" "$@" fi @@ -1313,7 +1313,7 @@ case $action in ;; "listpri" | "lsp") - shift ## was "listpri", new $1 is priority to list or first TERM + shift # was "listpri", new $1 is priority to list or first TERM pri=$(printf "%s\n" "$1" | tr '[:lower:]' '[:upper:]' | grep -e '^[A-Z]$' -e '^[A-Z]-[A-Z]$') && shift || pri="A-Z" post_filter_command="${post_filter_command:-}${post_filter_command:+ | }grep '^ *[0-9]\+ ([${pri}]) '" From 15befbb8f8a0633a7f7639b554dab0c2ee140702 Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Thu, 12 Aug 2021 02:31:38 +0000 Subject: [PATCH 15/21] fix case where directory name may begin with a dash --- todo.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/todo.sh b/todo.sh index b0508ca0..7fe73ec6 100755 --- a/todo.sh +++ b/todo.sh @@ -761,7 +761,7 @@ 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" +( 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\")." @@ -1293,7 +1293,7 @@ case $action in shift # Was listfile, next $1 is file name if [ $# -eq 0 ]; then [ "$TODOTXT_VERBOSE" -gt 0 ] && echo "Files in the todo.txt directory:" - cd "$TODO_DIR" && ls -1 -- *.txt + cd -- "$TODO_DIR" && ls -1 -- *.txt else FILE="$1" shift # Was filename; next $1 is first search term @@ -1472,7 +1472,7 @@ note: PRIORITY must be anywhere from A to Z." "listaddons") if [ -d "$TODO_ACTIONS_DIR" ]; then - cd "$TODO_ACTIONS_DIR" || exit $? + cd -- "$TODO_ACTIONS_DIR" || exit $? for action in *; do if [ -f "$action" ] && [ -x "$action" ]; then echo "$action" From 7e8ae8b23a06cc2370355ba020466302c771d6c9 Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Thu, 12 Aug 2021 02:32:14 +0000 Subject: [PATCH 16/21] unify function braces style --- todo.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/todo.sh b/todo.sh index 7fe73ec6..b6ef2fb4 100755 --- a/todo.sh +++ b/todo.sh @@ -791,7 +791,8 @@ fi [[ -n "$HIDE_PROJECTS_SUBSTITUTION" ]] && COLOR_PROJECT="$NONE" [[ -n "$HIDE_CONTEXTS_SUBSTITUTION" ]] && COLOR_CONTEXT="$NONE" -_addto() { +_addto() +{ file="$1" input="$2" cleaninput @@ -849,7 +850,8 @@ filtercommand() printf %s "$filter" } -_list() { +_list() +{ local FILE="$1" # If the file starts with a "/" use absolute path. Otherwise, # try to find it in either $TODO_DIR or using a relative path From f2411c1be4057868a035204c998c3337e2e3623e Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Thu, 12 Aug 2021 04:00:34 +0000 Subject: [PATCH 17/21] replace obsolete 'typeset' with 'local' typeset is obsolete, replaced by local or declare also improve legibility of what is happening --- todo.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/todo.sh b/todo.sh index b6ef2fb4..ada772b6 100755 --- a/todo.sh +++ b/todo.sh @@ -818,7 +818,8 @@ _addto() shellquote() { - typeset -r qq=\'; printf %s\\n "'${1//\'/${qq}\\${qq}${qq}}'"; + local -r qq=$'\'' + printf "%s\n" "'${1//\'/${qq}\\${qq}${qq}}'" } filtercommand() From 064483d60b4b3741d83c8dfe9e6ed381ff4ce986 Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Thu, 12 Aug 2021 06:30:41 +0000 Subject: [PATCH 18/21] use local variables to avoid pollution --- todo.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/todo.sh b/todo.sh index ada772b6..20f7b1c9 100755 --- a/todo.sh +++ b/todo.sh @@ -300,7 +300,7 @@ actionsHelp() addonHelp() { if [ -d "$TODO_ACTIONS_DIR" ]; then - didPrintAddonActionsHeader= + local didPrintAddonActionsHeader for action in "$TODO_ACTIONS_DIR"/*; do if [ -f "$action" ] && [ -x "$action" ]; then if [ -z "$didPrintAddonActionsHeader" ]; then @@ -799,6 +799,7 @@ _addto() uppercasePriority if [[ "$TODOTXT_DATE_ON_ADD" -eq 1 ]]; then + local now now=$(date '+%Y-%m-%d') input=$(echo "$input" | sed -e 's/^\(([A-Z]) \)\{0,1\}/\1'"$now /") fi From ec9c1be9d39cf5cd91779d700123baab702abb74 Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Thu, 12 Aug 2021 06:32:21 +0000 Subject: [PATCH 19/21] use consistent indentation with rest of file --- todo.sh | 88 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/todo.sh b/todo.sh index 20f7b1c9..0a2b2fe9 100755 --- a/todo.sh +++ b/todo.sh @@ -438,56 +438,56 @@ getNewtodo() replaceOrPrepend() { - action=$1; shift - case "$action" in - replace) - backref= - querytext="Replacement: " - ;; - prepend) - backref=' &' - querytext="Prepend: " - ;; - esac - shift; item=$1; shift - getTodo "$item" - - if [[ -z "$1" && $TODOTXT_FORCE = 0 ]]; then - echo -n "$querytext" - read -r -i "$todo" -e input - else - input=$* - fi - - # Retrieve existing priority and prepended date - local -r priAndDateExpr='^\((.) \)\{0,1\}\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{0,1\}' - priority=$(sed -e "$item!d" -e "${item}s/${priAndDateExpr}.*/\\1/" "$TODO_FILE") - prepdate=$(sed -e "$item!d" -e "${item}s/${priAndDateExpr}.*/\\2/" "$TODO_FILE") - - if [ -n "$prepdate" ] && [ "$action" = "replace" ] && [ -n "$(echo "$input" | sed -e "s/${priAndDateExpr}.*/\\1\\2/")" ]; then - # If the replaced text starts with a [priority +] date, it will replace - # the existing date, too. - prepdate= - fi - - # Temporarily remove any existing priority and prepended date, perform the - # change (replace/prepend) and re-insert the existing priority and prepended - # date again. - cleaninput "for sed" - sed -i.bak -e "$item s/^${priority}${prepdate}//" -e "$item s|^.*|${priority}${prepdate}${input}${backref}|" "$TODO_FILE" - if [ "$TODOTXT_VERBOSE" -gt 0 ]; then - getNewtodo "$item" + action=$1; shift case "$action" in replace) - echo "$item $todo" - echo "TODO: Replaced task with:" - echo "$item $newtodo" + backref= + querytext="Replacement: " ;; prepend) - echo "$item $newtodo" + backref=' &' + querytext="Prepend: " ;; esac - fi + shift; item=$1; shift + getTodo "$item" + + if [[ -z "$1" && $TODOTXT_FORCE = 0 ]]; then + echo -n "$querytext" + read -r -i "$todo" -e input + else + input=$* + fi + + # Retrieve existing priority and prepended date + local -r priAndDateExpr='^\((.) \)\{0,1\}\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{0,1\}' + priority=$(sed -e "$item!d" -e "${item}s/${priAndDateExpr}.*/\\1/" "$TODO_FILE") + prepdate=$(sed -e "$item!d" -e "${item}s/${priAndDateExpr}.*/\\2/" "$TODO_FILE") + + if [ -n "$prepdate" ] && [ "$action" = "replace" ] && [ -n "$(echo "$input" | sed -e "s/${priAndDateExpr}.*/\\1\\2/")" ]; then + # If the replaced text starts with a [priority +] date, it will replace + # the existing date, too. + prepdate= + fi + + # Temporarily remove any existing priority and prepended date, perform the + # change (replace/prepend) and re-insert the existing priority and prepended + # date again. + cleaninput "for sed" + sed -i.bak -e "$item s/^${priority}${prepdate}//" -e "$item s|^.*|${priority}${prepdate}${input}${backref}|" "$TODO_FILE" + if [ "$TODOTXT_VERBOSE" -gt 0 ]; then + getNewtodo "$item" + case "$action" in + replace) + echo "$item $todo" + echo "TODO: Replaced task with:" + echo "$item $newtodo" + ;; + prepend) + echo "$item $newtodo" + ;; + esac + fi } fixMissingEndOfLine() From 17723d5792e3bb92f402ba6e3cc3054e5ed2beef Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Thu, 12 Aug 2021 06:32:56 +0000 Subject: [PATCH 20/21] explicitly indicate that 'command' is a string --- todo.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/todo.sh b/todo.sh index 0a2b2fe9..f3f4cad2 100755 --- a/todo.sh +++ b/todo.sh @@ -1040,7 +1040,7 @@ action=$(printf "%s\n" "$ACTION" | tr '[:upper:]' '[:lower:]') # using todo.sh builtins. # Else, run a actions script with the name of the command if it exists # or fallback to using a builtin -if [ "$action" == command ]; then +if [ "$action" == "command" ]; then # Get rid of "command" from arguments list shift # Reset action to new first argument From cc73bd40959c0b1c39d2aa5693e55510029d8bab Mon Sep 17 00:00:00 2001 From: a1346054 <36859588+a1346054@users.noreply.github.com> Date: Thu, 12 Aug 2021 06:33:47 +0000 Subject: [PATCH 21/21] use consistent quoting for $# with rest of file $# will always be an integer and never needs quoting --- todo.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/todo.sh b/todo.sh index f3f4cad2..810505d1 100755 --- a/todo.sh +++ b/todo.sh @@ -1212,7 +1212,7 @@ case $action in errmsg="usage: $TODO_SH do ITEM#[, ITEM#, ITEM#, ...]" # shift so we get arguments to the do request shift; - [ "$#" -eq 0 ] && die "$errmsg" + [ $# -eq 0 ] && die "$errmsg" # Split multiple do's, if comma separated change to whitespace separated # Loop the 'do' function for each item @@ -1366,14 +1366,14 @@ case $action in "pri" | "p") shift - while [ "$#" -gt 0 ]; do + while [ $# -gt 0 ]; do item=$1 newpri=$(printf "%s\n" "$2" | tr '[:lower:]' '[:upper:]') errmsg="usage: $TODO_SH pri ITEM# PRIORITY[, ITEM# PRIORITY, ...] note: PRIORITY must be anywhere from A to Z." - [ "$#" -lt 2 ] && die "$errmsg" + [ $# -lt 2 ] && die "$errmsg" [[ "$newpri" = @([A-Z]) ]] || die "$errmsg" getTodo "$item"