Skip to content

Commit

Permalink
ENH: completion can use different todo.sh locations and configurations.
Browse files Browse the repository at this point in the history
This was previously not supported; todo_completion always used plain todo.sh from the PATH. Now it's possible to override the used executable (and configuration used through _todo_sh, set from a completion function wrapper.

Cp. http://tech.groups.yahoo.com/group/todotxt/message/4003
  • Loading branch information
inkarkat committed Feb 29, 2012
1 parent 3d0a4d0 commit e600a21
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 7 deletions.
45 changes: 45 additions & 0 deletions tests/t6080-completion-path.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash
#

test_description='Bash completion with different path functionality
This test checks that todo_completion can use a different path to todo.sh when
it is not accessible through PATH.
'
. ./test-lib.sh

cat > todo.txt <<EOF
(B) smell the +roses @outside @outdoor +shared
notice the sunflowers +sunflowers @outside @garden +shared +landscape
stop
EOF

mv bin/todo.sh bin/todo2.sh
test_expect_success 'todo2.sh executable' 'todo2.sh list'

# Define a second completion function that injects the different executable. In
# real use, this would be installed via
# complete -F _todo2 todo2.sh
_todo2()
{
local _todo_sh='todo2.sh'
_todo "$@"
}

test_todo_custom_completion _todo2 'all todo2 contexts' 'todo2 list @' '@garden @outdoor @outside'



# Remove the test environment's bin directory from the PATH, so that our test
# executable must be launched with an explicit path.
PATH=${PATH##"${PWD}/bin:"}
test_expect_code 127 'todo2.sh executable not in PATH' 'todo2.sh'

_todo2path()
{
local _todo_sh='./bin/todo2.sh'
_todo "$@"
}
test_todo_custom_completion _todo2path 'all todo2 contexts' 'todo2 list @' '@garden @outdoor @outside'

test_done
62 changes: 62 additions & 0 deletions tests/t6090-completion-aliases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash
#

test_description='Bash completion with different aliases functionality
This test checks that todo_completion can use a different configuration
when another todo.sh alias is defined that uses that configuration.
'
. ./test-lib.sh

cat > todo.txt <<EOF
(B) smell the +roses @outside @outdoor +shared
notice the sunflowers +sunflowers @outside @garden +shared +landscape
stop
EOF
cat > todo2.txt <<EOF
+herbs @oriental buy spices
+slack @home watch tv
EOF

cp todo.cfg todo2.cfg
cat >> todo2.cfg <<'EOF'
export TODO_FILE="$TODO_DIR/todo2.txt"
EOF

# Note: We cannot use aliases within the test framework, but functions are
# equivalent and work fine.
todo1()
{
todo.sh "$@"
}
todo2()
{
todo.sh -d "$HOME/todo2.cfg" "$@"
}

# Ensure that the test fixture works as planned.
test_todo_session 'todo 1 and 2 contexts' <<EOF
>>> todo1 listcon
@garden
@outdoor
@outside
>>> todo2 listcon
@home
@oriental
EOF


# Define a second completion function that injects the different configuration
# file. In real use, this would be installed via
# complete -F _todo2 todo2
_todo2()
{
local _todo_sh='todo.sh -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_done
36 changes: 29 additions & 7 deletions todo_completion
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,34 @@ _todo()
# 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 _todo_sh=${_todo_sh:-todo.sh}
local completions
if [ $COMP_CWORD -eq 1 ]; then
completions="$allCommands $OPTS"
elif [[ $COMP_CWORD -gt 2 && ( \
"${COMP_WORDS[COMP_CWORD-2]}" =~ ^(move|mv)$ || \
"${COMP_WORDS[COMP_CWORD-3]}" =~ ^(move|mv)$ ) ]]; then
# "move ITEM# DEST [SRC]" has file arguments on positions 2 and 3.
completions=$(TODOTXT_VERBOSE=0 todo.sh command listfile)
completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listfile)
else
case "$prev" in
command)
completions=$COMMANDS;;
addto|listfile|lf)
completions=$(TODOTXT_VERBOSE=0 todo.sh command listfile);;
completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listfile);;
-*) completions="$allCommands $OPTS";;
*) case "$cur" in
+*) completions=$(TODOTXT_VERBOSE=0 todo.sh command listproj)
+*) completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listproj)
COMPREPLY=( $( compgen -W "$completions" -- $cur ))
[ ${#COMPREPLY[@]} -gt 0 ] && return 0
# Fall back to projects extracted from done tasks.
completions=$(TODOTXT_VERBOSE=0 TODOTXT_SOURCEVAR=\$DONE_FILE todo.sh command listproj)
completions=$(eval 'TODOTXT_VERBOSE=0 TODOTXT_SOURCEVAR=\$DONE_FILE' $_todo_sh command listproj)
;;
@*) completions=$(TODOTXT_VERBOSE=0 todo.sh command listcon)
@*) completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listcon)
COMPREPLY=( $( compgen -W "$completions" -- $cur ))
[ ${#COMPREPLY[@]} -gt 0 ] && return 0
# Fall back to contexts extracted from done tasks.
completions=$(TODOTXT_VERBOSE=0 TODOTXT_SOURCEVAR=\$DONE_FILE todo.sh command listcon)
completions=$(eval 'TODOTXT_VERBOSE=0 TODOTXT_SOURCEVAR=\$DONE_FILE' $_todo_sh command listcon)
;;
*) if [[ "$cur" =~ ^[0-9]+$ ]]; then
# Remove the (padded) task number; we prepend the
Expand All @@ -60,7 +61,7 @@ _todo()
# Finally, limit the output to a single line just as
# a safety check of the ls action output.
local todo=$( \
TODOTXT_VERBOSE=0 todo.sh -@ -+ -p -x command ls "^ *${cur} " | \
eval TODOTXT_VERBOSE=0 $_todo_sh '-@ -+ -p -x command ls "^ *${cur} "' | \
sed -e 's/^ *[0-9]\+ //' -e 's/\((.) \)[0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} /\1/' \
-e 's/\([xX] \)\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{1,2\}/\1/' \
-e 's/[[:space:]]*$//' \
Expand All @@ -84,6 +85,27 @@ _todo()
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

# 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()
#{
# local _todo_sh='/path/to/todo2.sh'
# _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
# fron 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

0 comments on commit e600a21

Please sign in to comment.