Skip to content

Commit

Permalink
completion support for helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
cxreg committed Apr 8, 2012
1 parent 4ffe143 commit 2c0c1c0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
7 changes: 6 additions & 1 deletion download-and-install
Expand Up @@ -60,8 +60,13 @@ for file in \
lib/core/completion \
helper/path/script \
helper/path/meta \
helper/path/completion \
helper/history/script \
helper/history/meta
helper/history/meta \
helper/history/completion \
helper/perlbrew/script \
helper/perlbrew/meta \
helper/perlbrew/completion
do
download_file "$file"
done
Expand Down
8 changes: 8 additions & 0 deletions helper/history/completion
@@ -0,0 +1,8 @@
# $@ contains the command line after "smartcd helper run history"
# $SMARTCD_COMP_CWORD contains the numbered argument which is currently being completed
# Set words="" to what you want to suggest

case $SMARTCD_COMP_CWORD in
1) words="localize also-read";;
*) ;; # Path completion is the default
esac
8 changes: 8 additions & 0 deletions helper/path/completion
@@ -0,0 +1,8 @@
# $@ contains the command line after "smartcd helper run path"
# $SMARTCD_COMP_CWORD contains the numbered argument which is currently being completed
# Set words="" to what you want to suggest

case $SMARTCD_COMP_CWORD in
1) words="append prepend";;
*) ;; # Path completion is the default
esac
8 changes: 8 additions & 0 deletions helper/perlbrew/completion
@@ -0,0 +1,8 @@
# $@ contains the command line after "smartcd helper run perlbrew"
# $SMARTCD_COMP_CWORD contains the numbered argument which is currently being completed
# Set words="" to what you want to suggest

case $SMARTCD_COMP_CWORD in
1) words="init";;
*) ;; # Path completion is the default
esac
33 changes: 22 additions & 11 deletions lib/core/completion
Expand Up @@ -14,6 +14,7 @@ function _smartcd_completion() {

local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local base=$(_smartcd_base)

local prev_ifs="$IFS"
IFS=$' \t\n'
Expand All @@ -28,18 +29,28 @@ function _smartcd_completion() {
edit|show|filename) words="enter leave";;
helper) words="run";;
esac
elif [[ $COMP_CWORD == 3 && "${COMP_WORDS[COMP_CWORD-2]}" == "template" ]]; then
case $prev in
edit|show|run|install|delete) words="$(smartcd template list)";;
esac
elif [[ $COMP_CWORD == 3 && "${COMP_WORDS[COMP_CWORD-2]}" == "helper" ]]; then
case $prev in
run) local _old_ifs="$IFS"
IFS=$'\n'
words="$(command ls $(_smartcd_base)/helper)"
IFS="$_old_ifs"
;;
elif [[ $COMP_CWORD == 3 ]]; then
case "${COMP_WORDS[COMP_CWORD-2]}" in
template) case $prev in
edit|show|run|install|delete) words="$(smartcd template list)";;
esac
;;
helper) case $prev in
run) local _old_ifs="$IFS"
IFS=$'\n'
words="$(command ls $base/helper)"
IFS="$_old_ifs"
;;
esac
;;
esac
elif (( $COMP_CWORD >= 4 )) && [[ ${COMP_WORDS[1]} == "helper" && ${COMP_WORDS[2]} == "run" ]]; then
local helper="${COMP_WORDS[3]}"
if [[ -f "$base/helper/$helper/completion" ]]; then
# Pass unparsed tokens as arguments
local SMARTCD_COMP_CWORD=$(( $COMP_CWORD - 3))
source "$base/helper/$helper/completion" "${COMP_WORDS[@]:4}"
fi
fi

COMPREPLY=( $(compgen -W "$words" -- "$cur") )
Expand Down

0 comments on commit 2c0c1c0

Please sign in to comment.