Skip to content

Commit

Permalink
ENH: Reuse the todo.sh alias for completion
Browse files Browse the repository at this point in the history
Having to define a completion function wrapper is cumbersome. I had seen the trick of simply using ${COMP_WORDS[0]} (i.e. the used todo.sh command itself) from Paul Mansfield (https://github.com/the1ts/todo.txt-plugins/blob/develop/bash_completion/todo.txt#L7), which neatly avoids this.
By keeping the _todo_sh variable, this is a one-line change and it still allows the old way of using the override in a wrapper function. (So users aren't forced to change their customizations when upgrading.)
Tests are adapted to verify that the alias is used, and still verify the wrapper function as well.
The documentation is simplified because there's normally no need for the completion wrapper function.
  • Loading branch information
inkarkat committed Oct 31, 2024
1 parent a916aa9 commit 9623f77
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
13 changes: 10 additions & 3 deletions tests/t6090-completion-aliases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,22 @@ test_todo_session 'todo 1 and 2 contexts' <<EOF
EOF

# Define a second completion function that injects the different configuration
# file. In real use, this would be installed via
# file and uppercases all output. (This is a silly behavior change that still
# requires a completion function override.)
# In real use, this would be installed via
# complete -F _todo2 todo2
_uppercase_todo()
{
todo.sh "$@" | tr '[:lower:]' '[:upper:]'
}
_todo2()
{
local _todo_sh='todo.sh -d "$HOME/todo2.cfg"'
local _todo_sh='_uppercase_todo -d "$HOME/todo2.cfg"'
_todo "$@"
}

test_todo_completion 'all todo1 contexts' 'todo1 list @' '@garden @outdoor @outside'
test_todo_custom_completion _todo2 'all todo2 contexts' 'todo2 list @' '@home @oriental'
test_todo_completion 'all todo2 contexts' 'todo2 list @' '@home @oriental'
test_todo_custom_completion _todo2 'all uppercased todo2 contexts' 'doesNotMatter list @' '@HOME @ORIENTAL'

test_done
26 changes: 9 additions & 17 deletions todo_completion
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ _todo()
mv prepend prep pri p replace report shorthelp"
local -r MOVE_COMMAND_PATTERN='move|mv'

local _todo_sh=${_todo_sh:-todo.sh}
local _todo_sh=${_todo_sh:-${COMP_WORDS[0]}}
local completions
if [ "$COMP_CWORD" -eq 1 ]; then
completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 $_todo_sh command listaddons 2>/dev/null) $OPTS"
Expand Down Expand Up @@ -101,22 +101,14 @@ complete -F _todo todo.sh
# ~/.bashrc (or wherever else you're defining your alias). If you simply
# uncomment it here, you will need to redo this on every todo.txt update!

# If you have renamed the todo.sh executable, or if it is not accessible through
# PATH, you need to add and use a wrapper completion function, like this:
#_todoElsewhere()
# The completion uses the alias itself, so any custom arguments (like a custom
# configuration (-d "$HOME/todo2.cfg")) are used there as well.
# If you don't want this, or need to further tweak the todo.sh command that's
# used by the completion, you can add and use a wrapper completion function that
# redefines _todo_sh before invoking _todo():
#_todo_tweak()
#{
# local _todo_sh='/path/to/todo2.sh'
# local _todo_sh='todo.sh -d "$HOME/todo-tweaked.cfg"'
# _todo "$@"
#}
#complete -F _todoElsewhere /path/to/todo2.sh

# If you use aliases to use different configuration(s), you need to add and use
# a wrapper completion function for each configuration if you want to complete
# from the actual configured task locations:
#alias todo2='todo.sh -d "$HOME/todo2.cfg"'
#_todo2()
#{
# local _todo_sh='todo.sh -d "$HOME/todo2.cfg"'
# _todo "$@"
#}
#complete -F _todo2 todo2
#complete -F _todo_tweak todo.sh

0 comments on commit 9623f77

Please sign in to comment.