forked from todotxt/todo.txt-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request todotxt#56 from inkarkat/todo_completion
Incorporate Bash completion from the Wiki page into the distribution.
- Loading branch information
Showing
2 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/bin/bash source-this-script | ||
[ "$BASH_VERSION" ] || return | ||
|
||
_todo() | ||
{ | ||
local cur prev opts | ||
COMPREPLY=() | ||
cur="${COMP_WORDS[COMP_CWORD]}" | ||
prev="${COMP_WORDS[COMP_CWORD-1]}" | ||
|
||
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 listall lsa listcon \ | ||
lsc listfile lf listpri lsp listproj lsprj move \ | ||
mv prepend prep pri p replace report shorthelp" | ||
|
||
# Add custom commands from add-ons, if installed. | ||
# TODO: Filter for executable flag of files found in $TODO_ACTIONS_DIR. | ||
local allCommands="$COMMANDS $('ls' "${TODO_ACTIONS_DIR:-$HOME/.todo.actions.d}/" 2>/dev/null)" | ||
|
||
local completions | ||
if [ $COMP_CWORD -eq 1 ]; then | ||
completions="$allCommands $OPTS" | ||
else | ||
case "$prev" in | ||
command) | ||
completions=$COMMANDS;; | ||
-*) completions="$allCommands $OPTS";; | ||
*) case "$cur" in | ||
+*) completions=$(TODOTXT_VERBOSE=0 todo.sh command listproj);; | ||
@*) completions=$(TODOTXT_VERBOSE=0 todo.sh command listcon);; | ||
*) if [[ "$cur" =~ ^[0-9]+$ ]]; then | ||
local item=$(TODOTXT_VERBOSE=0 todo.sh -@ -+ -p -x command ls "^ *${cur} " | head -n 1) | ||
|
||
# Remove the (padded) task number; we prepend the | ||
# user-provided $cur. | ||
item=${item#* } | ||
|
||
# Remove the timestamp prepended by the -t option; | ||
# there's no todo.txt option for that yet. | ||
item=${item#[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] } | ||
|
||
# Append task text as a shell comment. This | ||
# completion can be a safety check before a | ||
# destructive todo.txt operation. | ||
[ "$item" ] && COMPREPLY[0]="$cur # $item" | ||
return 0 | ||
else | ||
return 0 | ||
fi | ||
;; | ||
esac | ||
;; | ||
esac | ||
fi | ||
|
||
COMPREPLY=( $( compgen -W "$completions" -- $cur )) | ||
return 0 | ||
} | ||
complete -F _todo todo.sh | ||
# If you define an alias (e.g. "t") to todo.sh, you need to explicitly enable | ||
# completion for it, too: | ||
#complete -F _todo t |