Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completions/{ssh,rake,capistrano}: Do not rewrite COMP_WORDBREAKS #480

Merged
merged 5 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions completions/capistrano.completion.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
#! bash oh-my-bash.module
# Bash completion support for Capistrano.

export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/}
_omb_module_require lib:omb-completion

function _capcomplete {
if [ -f Capfile ]; then
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
# 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]}))
return 0
function _omb_completion_cap {
local cur
_omb_completion_reassemble_breaks :

if [[ -f Capfile ]]; then
local recent=$(ls -t .cap_tasks~ Capfile **/*.cap 2> /dev/null | head -n 1)
if [[ $recent != '.cap_tasks~' ]]; 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 '$(< .cap_tasks)' -- "$cur"))
fi

_omb_completion_resolve_breaks
}

complete -o default -o nospace -F _capcomplete cap
complete -o default -o nospace -F _omb_completion_cap cap
95 changes: 47 additions & 48 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

_homesick_complete()
_omb_completion_homesick()
{
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)
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}) )
return
}
# Subcommand list
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]}"
while [[ $prev == -* ]]; do
prev_index=$((++prev_index))
prev="${COMP_WORDS[prev_index]}"
done
# Find the first non-switch word
local prev_index=1
local prev=${COMP_WORDS[prev_index]}
while [[ $prev == -* ]]; do
((++prev_index))
prev=${COMP_WORDS[prev_index]}
done

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

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}) )
return
;;
# Commands that take command
help)
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}) )
fi
return
;;
esac
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") )
return
;;
# Commands that take command
help)
COMPREPLY=( $(compgen -W "${actions}" -- "$cur") )
return
;;
# Track command take file and repo
track)
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 _homesick_complete homesick

complete -o bashdefault -o default -F _omb_completion_homesick homesick
24 changes: 14 additions & 10 deletions completions/rake.completion.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#! bash oh-my-bash.module
# Bash completion support for Rake, Ruby Make.

export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/}
_omb_module_require lib:omb-completion

function _rakecomplete {
if [ -f Rakefile ]; then
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]}))
return 0
function _omb_completion_rake {
local cur
_omb_completion_reassemble_breaks :

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 '$(< .rake_tasks~)' -- "$cur"))
fi

_omb_completion_resolve_breaks
}

complete -o default -o nospace -F _rakecomplete rake
complete -o default -o nospace -F _omb_completion_rake rake
81 changes: 51 additions & 30 deletions completions/sdkman.completion.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,60 @@
#! bash oh-my-bash.module
_sdkman_complete()
{
local CANDIDATES
local CANDIDATE_VERSIONS

if ! declare -F __sdkman_build_version_csv &>/dev/null; then
# @fn __sdkman_build_version_csv
# Copyright 2021 Marco Vermeulen.
# Licensed under the Apache License, Version 2.0 (the "License");
#
# This function was taken from "main/bash/sdkman-list.sh @
# 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=$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}
fi
done
versions_csv=${versions_csv%?}
fi
echo "$versions_csv"
}
fi

_omb_completion_sdkman()
{
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" )
_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 @@ -40,23 +64,20 @@ _sdkman_complete()
return 0
}

function _sdkman_candidate_versions {

CANDIDATE_LOCAL_VERSIONS=$(__sdkman_cleanup_local_versions $1)
if [ "$SDKMAN_OFFLINE_MODE" = "true" ]; then
CANDIDATE_VERSIONS=$CANDIDATE_LOCAL_VERSIONS
function _omb_completion_sdkman__candidate_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 __sdkman_cleanup_local_versions {

__sdkman_build_version_csv $1
echo $CSV | tr ',' ' '

function _omb_completion_sdkman__cleanup_local_versions {
__sdkman_build_version_csv "$1"
tr ',' ' ' <<< "$CSV"
}

complete -F _sdkman_complete sdk
complete -F _omb_completion_sdkman sdk
55 changes: 28 additions & 27 deletions completions/ssh.completion.sh
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
#! bash oh-my-bash.module
# Bash completion support for ssh.

export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/}

function _sshcomplete {
local CURRENT_PROMPT="${COMP_WORDS[COMP_CWORD]}"
if [[ ${CURRENT_PROMPT} == *@* ]] ; then
local OPTIONS="-P ${CURRENT_PROMPT/@*/}@ -- ${CURRENT_PROMPT/*@/}"
else
local OPTIONS=" -- ${CURRENT_PROMPT}"
_omb_module_require lib:omb-completion

function _omb_completion_ssh {
local cur
_omb_completion_reassemble_breaks :

if [[ $cur == *@* ]] ; then
local -a options=(-P "${cur%%@*}@" -- "${cur#*@}")
else
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[@]}"))
fi

# parse all hosts found in .ssh/known_hosts
if [[ -r $HOME/.ssh/known_hosts ]]; then
if grep -v -q -e '^ ssh-rsa' "$HOME/.ssh/known_hosts" ; then
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+=($(compgen -W "$( grep -v '^[[:space:]]*$' /etc/hosts | grep -v '^#' | awk '{for (i=2; i<=NF; i++) print $i}' )" "${options[@]}"))
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}) )
fi

# parse all hosts found in .ssh/known_hosts
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}) )
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}) )
fi

return 0
_omb_completion_resolve_breaks
}

complete -o default -o nospace -F _sshcomplete ssh scp
complete -o default -o nospace -F _omb_completion_ssh ssh scp
Loading
Loading