Skip to content

Commit 9623f77

Browse files
committed
ENH: Reuse the todo.sh alias for completion
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.
1 parent a916aa9 commit 9623f77

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

tests/t6090-completion-aliases.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,22 @@ test_todo_session 'todo 1 and 2 contexts' <<EOF
4646
EOF
4747

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

5763
test_todo_completion 'all todo1 contexts' 'todo1 list @' '@garden @outdoor @outside'
58-
test_todo_custom_completion _todo2 'all todo2 contexts' 'todo2 list @' '@home @oriental'
64+
test_todo_completion 'all todo2 contexts' 'todo2 list @' '@home @oriental'
65+
test_todo_custom_completion _todo2 'all uppercased todo2 contexts' 'doesNotMatter list @' '@HOME @ORIENTAL'
5966

6067
test_done

todo_completion

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ _todo()
1818
mv prepend prep pri p replace report shorthelp"
1919
local -r MOVE_COMMAND_PATTERN='move|mv'
2020

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

104-
# If you have renamed the todo.sh executable, or if it is not accessible through
105-
# PATH, you need to add and use a wrapper completion function, like this:
106-
#_todoElsewhere()
104+
# The completion uses the alias itself, so any custom arguments (like a custom
105+
# configuration (-d "$HOME/todo2.cfg")) are used there as well.
106+
# If you don't want this, or need to further tweak the todo.sh command that's
107+
# used by the completion, you can add and use a wrapper completion function that
108+
# redefines _todo_sh before invoking _todo():
109+
#_todo_tweak()
107110
#{
108-
# local _todo_sh='/path/to/todo2.sh'
111+
# local _todo_sh='todo.sh -d "$HOME/todo-tweaked.cfg"'
109112
# _todo "$@"
110113
#}
111-
#complete -F _todoElsewhere /path/to/todo2.sh
112-
113-
# If you use aliases to use different configuration(s), you need to add and use
114-
# a wrapper completion function for each configuration if you want to complete
115-
# from the actual configured task locations:
116-
#alias todo2='todo.sh -d "$HOME/todo2.cfg"'
117-
#_todo2()
118-
#{
119-
# local _todo_sh='todo.sh -d "$HOME/todo2.cfg"'
120-
# _todo "$@"
121-
#}
122-
#complete -F _todo2 todo2
114+
#complete -F _todo_tweak todo.sh

0 commit comments

Comments
 (0)