Skip to content

Commit

Permalink
completions/{cap,homesick,rake,sdkman,ssh}: Fix coding styles
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Sep 30, 2023
1 parent e6572f4 commit cd8b4d2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 68 deletions.
9 changes: 4 additions & 5 deletions completions/capistrano.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/}

function _omb_completion_cap {
if [ -f Capfile ]; then
recent=`ls -t .cap_tasks~ Capfile **/*.cap 2> /dev/null | head -n 1`
if [[ -f Capfile ]]; then
local recent=$(ls -t .cap_tasks~ Capfile **/*.cap 2> /dev/null | head -n 1)
if [[ $recent != '.cap_tasks~' ]]; then
cap --version | grep 'Capistrano v2.' > /dev/null
if [ $? -eq 0 ]; then
if cap --version | grep 'Capistrano v2.' > /dev/null; then
# Capistrano 2.x
cap --tool --verbose --tasks | cut -d " " -f 2 > .cap_tasks~
else
# Capistrano 3.x
cap --all --tasks | cut -d " " -f 2 > .cap_tasks~
fi
fi
COMPREPLY=($(compgen -W "`cat .cap_tasks~`" -- ${COMP_WORDS[COMP_CWORD]}))
COMPREPLY=($(compgen -W '$(< .cap_tasks)' -- "${COMP_WORDS[COMP_CWORD]}"))
return 0
fi
}
Expand Down
35 changes: 17 additions & 18 deletions completions/homesick.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,58 @@
#
# The homebrew bash completion script was used as inspiration.
# Originally from https://github.com/liborw/homesick-completion
# https://github.com/liborw/homesick-completion/blob/904d121d1b8f81629f473915a10c9144fdd416dc/homesick_bash_completion.sh

_omb_completion_homesick()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
local cur=${COMP_WORDS[COMP_CWORD]}
local options="--skip --force --pretend --quiet"
local actions="cd clone commit destroy diff generate help list open pull push rc show_path status symlink track unlink version"
local repos=$(\ls ~/.homesick/repos)

# Subcommand list
[[ ${COMP_CWORD} -eq 1 ]] && {
COMPREPLY=( $(compgen -W "${options} ${actions}" -- ${cur}) )
if ((COMP_CWORD == 1)); then
COMPREPLY=( $(compgen -W "${options} ${actions}" -- "$cur") )
return
}
fi

# Find the first non-switch word
local prev_index=1
local prev="${COMP_WORDS[prev_index]}"
local prev=${COMP_WORDS[prev_index]}
while [[ $prev == -* ]]; do
prev_index=$((++prev_index))
prev="${COMP_WORDS[prev_index]}"
((++prev_index))
prev=${COMP_WORDS[prev_index]}
done

# Find the number of non-"--" commands
local num=0
for word in ${COMP_WORDS[@]}
do
for word in "${COMP_WORDS[@]}"; do
if [[ $word != -* ]]; then
num=$((++num))
((++num))
fi
done

case "$prev" in
case $prev in
# Commands that take a castle
cd|commit|destroy|diff|open|pull|push|rc|show_path|status|symlink|unlink)
COMPREPLY=( $(compgen -W "${repos}" -- ${cur}) )
COMPREPLY=( $(compgen -W "${repos}" -- "$cur") )
return
;;
# Commands that take command
help)
COMPREPLY=( $(compgen -W "${actions}" -- ${cur}) )
COMPREPLY=( $(compgen -W "${actions}" -- "$cur") )
return
;;
# Track command take file and repo
track)
if [[ "$num" -eq 2 ]]; then
COMPREPLY=( $(compgen -X -f ${cur}) )
elif [[ "$num" -ge 3 ]]; then
COMPREPLY=( $(compgen -W "${repos}" -- ${cur}) )
if ((num == 2)); then
COMPREPLY=( $(compgen -X -f "$cur") )
elif ((num >= 3)); then
COMPREPLY=( $(compgen -W "${repos}" -- "$cur") )
fi
return
;;
esac
}

complete -o bashdefault -o default -F _omb_completion_homesick homesick

6 changes: 3 additions & 3 deletions completions/rake.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/}

function _omb_completion_rake {
if [ -f Rakefile ]; then
recent=`ls -t .rake_tasks~ Rakefile **/*.rake 2> /dev/null | head -n 1`
if [[ -f Rakefile ]]; then
local recent=$(ls -t .rake_tasks~ Rakefile **/*.rake 2> /dev/null | head -n 1)
if [[ $recent != '.rake_tasks~' ]]; then
rake --silent --tasks --all | cut -d " " -f 2 > .rake_tasks~
fi
COMPREPLY=($(compgen -W "`cat .rake_tasks~`" -- ${COMP_WORDS[COMP_CWORD]}))
COMPREPLY=($(compgen -W '$(< .rake_tasks~)' -- "${COMP_WORDS[COMP_CWORD]}"))
return 0
fi
}
Expand Down
59 changes: 28 additions & 31 deletions completions/sdkman.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ if ! declare -F __sdkman_build_version_csv &>/dev/null; then
# sdkman/sdkman-cli".
# https://github.com/sdkman/sdkman-cli/blob/19e5c081297d6a8d1ce8a8b54631bb3f8e8e861b/src/main/bash/sdkman-list.sh#L51
function __sdkman_build_version_csv() {
local candidate versions_csv
candidate="$1"
versions_csv=""
if [[ -d "${SDKMAN_CANDIDATES_DIR}/${candidate}" ]]; then
local candidate=$1
local versions_csv=""
if [[ -d ${SDKMAN_CANDIDATES_DIR}/${candidate} ]]; then
for version in $(find "${SDKMAN_CANDIDATES_DIR}/${candidate}" -maxdepth 1 -mindepth 1 \( -type l -o -type d \) -exec basename '{}' \; | sort -r); do
if [[ "$version" != 'current' ]]; then
versions_csv="${version},${versions_csv}"
if [[ $version != 'current' ]]; then
versions_csv=${version},${versions_csv}
fi
done
versions_csv=${versions_csv%?}
Expand All @@ -26,36 +25,36 @@ fi

_omb_completion_sdkman()
{
local CANDIDATES
local CANDIDATE_VERSIONS

local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=()

if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=( $(compgen -W "install uninstall rm list ls use current outdated version default selfupdate broadcast offline help flush" -- ${COMP_WORDS[COMP_CWORD]}) )
elif [ $COMP_CWORD -eq 2 ]; then
case "${COMP_WORDS[COMP_CWORD-1]}" in
if ((COMP_CWORD == 1)); then
COMPREPLY=( $(compgen -W "install uninstall rm list ls use current outdated version default selfupdate broadcast offline help flush" -- "$cur") )
elif ((COMP_CWORD == 2)); then
case ${COMP_WORDS[COMP_CWORD-1]} in
"install" | "uninstall" | "rm" | "list" | "ls" | "use" | "current" | "outdated" )
CANDIDATES=$(echo "${SDKMAN_CANDIDATES_CSV}" | tr ',' ' ')
COMPREPLY=( $(compgen -W "$CANDIDATES" -- ${COMP_WORDS[COMP_CWORD]}) )
local candidates
candidates=$(echo "${SDKMAN_CANDIDATES_CSV}" | tr ',' ' ')
COMPREPLY=( $(compgen -W "$candidates" -- "$cur") )
;;
"offline" )
COMPREPLY=( $(compgen -W "enable disable" -- ${COMP_WORDS[COMP_CWORD]}) )
COMPREPLY=( $(compgen -W "enable disable" -- "$cur") )
;;
"selfupdate" )
COMPREPLY=( $(compgen -W "force" -P "[" -S "]" -- ${COMP_WORDS[COMP_CWORD]}) )
COMPREPLY=( $(compgen -W "force" -P "[" -S "]" -- "$cur") )
;;
"flush" )
COMPREPLY=( $(compgen -W "candidates broadcast archives temp" -- ${COMP_WORDS[COMP_CWORD]}) )
COMPREPLY=( $(compgen -W "candidates broadcast archives temp" -- "$cur") )
;;
*)
;;
esac
elif [ $COMP_CWORD -eq 3 ]; then
case "${COMP_WORDS[COMP_CWORD-2]}" in
elif ((COMP_CWORD == 3)); then
case ${COMP_WORDS[COMP_CWORD-2]} in
"install" | "uninstall" | "rm" | "use" | "default" )
_omb_completion_sdkman__candidate_versions ${COMP_WORDS[COMP_CWORD-1]}
COMPREPLY=( $(compgen -W "$CANDIDATE_VERSIONS" -- ${COMP_WORDS[COMP_CWORD]}) )
local candidate_versions
_omb_completion_sdkman__candidate_versions "${COMP_WORDS[COMP_CWORD-1]}"
COMPREPLY=( $(compgen -W "$candidate_versions" -- "$cur") )
;;
*)
;;
Expand All @@ -66,21 +65,19 @@ _omb_completion_sdkman()
}

function _omb_completion_sdkman__candidate_versions {

CANDIDATE_LOCAL_VERSIONS=$(_omb_completion_sdkman__cleanup_local_versions $1)
if [ "$SDKMAN_OFFLINE_MODE" = "true" ]; then
CANDIDATE_VERSIONS=$CANDIDATE_LOCAL_VERSIONS
local local_versions=$(_omb_completion_sdkman__cleanup_local_versions "$1")
if [[ $SDKMAN_OFFLINE_MODE = "true" ]]; then
candidate_versions=$local_versions
else
CANDIDATE_ONLINE_VERSIONS="$(curl -s "${SDKMAN_SERVICE}/candidates/$1" | tr ',' ' ')"
CANDIDATE_VERSIONS="$(echo $CANDIDATE_ONLINE_VERSIONS $CANDIDATE_LOCAL_VERSIONS |sort | uniq ) "
local online_versions="$(curl -s "${SDKMAN_SERVICE}/candidates/$1" | tr ',' ' ')"
candidate_versions="$(echo $online_versions $local_versions |sort | uniq ) "
fi

}

function _omb_completion_sdkman__cleanup_local_versions {
__sdkman_build_version_csv $1
echo $CSV | tr ',' ' '

__sdkman_build_version_csv "$1"
tr ',' ' ' <<< "$CSV"
}

complete -F _omb_completion_sdkman sdk
21 changes: 10 additions & 11 deletions completions/ssh.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@
export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/}

function _omb_completion_ssh {
local CURRENT_PROMPT="${COMP_WORDS[COMP_CWORD]}"
if [[ ${CURRENT_PROMPT} == *@* ]] ; then
local OPTIONS="-P ${CURRENT_PROMPT/@*/}@ -- ${CURRENT_PROMPT/*@/}"
local cur=${COMP_WORDS[COMP_CWORD]}
if [[ $cur == *@* ]] ; then
local -a options=(-P "${cur%%@*}@" -- "${cur#*@}")
else
local OPTIONS=" -- ${CURRENT_PROMPT}"
local -a options=(-- "$cur")
fi


# parse all defined hosts from .ssh/config
if [ -r "$HOME/.ssh/config" ]; then
COMPREPLY=($(compgen -W "$(grep ^Host "$HOME/.ssh/config" | awk '{for (i=2; i<=NF; i++) print $i}' )" ${OPTIONS}) )
if [[ -r $HOME/.ssh/config ]]; then
COMPREPLY=($(compgen -W "$(grep ^Host "$HOME/.ssh/config" | awk '{for (i=2; i<=NF; i++) print $i}' )" "${options[@]}"))
fi

# parse all hosts found in .ssh/known_hosts
if [ -r "$HOME/.ssh/known_hosts" ]; then
if [[ -r $HOME/.ssh/known_hosts ]]; then
if grep -v -q -e '^ ssh-rsa' "$HOME/.ssh/known_hosts" ; then
COMPREPLY=( ${COMPREPLY[@]} $(compgen -W "$( awk '{print $1}' "$HOME/.ssh/known_hosts" | grep -v ^\| | cut -d, -f 1 | sed -e 's/\[//g' | sed -e 's/\]//g' | cut -d: -f1 | grep -v ssh-rsa)" ${OPTIONS}) )
COMPREPLY+=($(compgen -W "$( awk '{print $1}' "$HOME/.ssh/known_hosts" | grep -v ^\| | cut -d, -f 1 | sed -e 's/\[//g' | sed -e 's/\]//g' | cut -d: -f1 | grep -v ssh-rsa)" "${options[@]}"))
fi
fi

# parse hosts defined in /etc/hosts
if [ -r /etc/hosts ]; then
COMPREPLY=( ${COMPREPLY[@]} $(compgen -W "$( grep -v '^[[:space:]]*$' /etc/hosts | grep -v '^#' | awk '{for (i=2; i<=NF; i++) print $i}' )" ${OPTIONS}) )
if [[ -r /etc/hosts ]]; then
COMPREPLY+=($(compgen -W "$( grep -v '^[[:space:]]*$' /etc/hosts | grep -v '^#' | awk '{for (i=2; i<=NF; i++) print $i}' )" "${options[@]}"))
fi

return 0
Expand Down

0 comments on commit cd8b4d2

Please sign in to comment.