Skip to content

Commit

Permalink
Merge pull request todotxt#56 from inkarkat/todo_completion
Browse files Browse the repository at this point in the history
Incorporate Bash completion from the Wiki page into the distribution.
  • Loading branch information
ginatrapani committed Dec 7, 2011
2 parents 417e86a + d2e0f6e commit ac090fa
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ VERSION-FILE: .FORCE-VERSION-FILE
todo.sh: VERSION-FILE

# For packaging
DISTFILES := todo.cfg
DISTFILES := todo.cfg todo_completion

DISTNAME=todo.txt_cli-$(VERSION)
dist: $(DISTFILES) todo.sh
Expand Down
64 changes: 64 additions & 0 deletions todo_completion
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

0 comments on commit ac090fa

Please sign in to comment.