Skip to content

Instantly share code, notes, and snippets.

@doituikhovai
Last active March 15, 2022 04:34
Show Gist options
  • Save doituikhovai/f13315f29a47d392622822a9e7bd1af4 to your computer and use it in GitHub Desktop.
Save doituikhovai/f13315f29a47d392622822a9e7bd1af4 to your computer and use it in GitHub Desktop.
#!/bin/bash
_ws_completions()
{
local cur prev firstword lastword complete_options complete_words
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
firstword=$(_get_firstword)
lastword=$(_get_lastword)
GLOBAL_COMMAND="\
goto\
run\
create\
git\
install\
clone"
GLOBAL_OPTIONS="\
-u"
GOTO_COMMAND="\
beeketing\
sitekit\
capp\
go"
RUN_CAPP_COMMAND="\
capp-sdk
capp-dashboard\
capp-landing\
capp-blog"
RUN_BEEKETING_COMMAND="\
beeketing-dashboard\
beeketing-sdk"
RUN_SITEKIT_COMMAND="\
sitekit-dashboard\
sitekit-landing\
sitekit-sdk"
complete_options="$GLOBAL_OPTIONS"
complete_words="$GLOBAL_COMMAND"
case "$firstword" in
run)
complete_words="$GOTO_COMMAND"
case "$lastword" in
capp)
complete_words="$RUN_CAPP_COMMAND"
if [[ $prev == -* ]]; then
complete_words="$GLOBAL_OPTIONS"
fi
;;
beeketing)
complete_words="$RUN_BEEKETING_COMMAND"
if [[ $prev == -* ]]; then
complete_words="$GLOBAL_OPTIONS"
fi
;;
sitekit)
complete_words="$RUN_SITEKIT_COMMAND"
if [[ $prev == -* ]]; then
complete_words="$GLOBAL_OPTIONS"
fi
;;
esac
;;
install)
complete_words="go"
;;
goto)
complete_words="$GOTO_COMMAND"
case "$lastword" in
capp)
complete_words="$RUN_CAPP_COMMAND"
;;
beeketing)
complete_words="$RUN_BEEKETING_COMMAND"
;;
sitekit)
complete_words="$RUN_SITEKIT_COMMAND"
;;
esac
;;
clone|create)
complete_words="$GOTO_COMMAND"
;;
esac
if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W "$complete_options" -- $cur))
else
COMPREPLY=($(compgen -W "$complete_words" -- $cur))
fi
}
_get_firstword() {
local firstword i
firstword=
for ((i = 1; i < ${#COMP_WORDS[@]}; ++i)); do
if [[ ${COMP_WORDS[i]} != -* ]]; then
firstword=${COMP_WORDS[i]}
break
fi
done
echo $firstword
}
_get_lastword() {
local lastword i
lastword=
for ((i = 1; i < ${#COMP_WORDS[@]}; ++i)); do
if [[ ${COMP_WORDS[i]} != -* ]] && [[ -n ${COMP_WORDS[i]} ]] && [[ ${COMP_WORDS[i]} != $cur ]]; then
lastword=${COMP_WORDS[i]}
fi
done
echo $lastword
}
complete -F _ws_completions ws
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment