Make startvm command complete only not running vms

This commit is contained in:
2016-03-06 13:31:55 +01:00
parent 6251330e9b
commit e4618d52a5

View File

@@ -102,10 +102,24 @@ _VBoxManage() {
# Issues are the same as in above function. # Issues are the same as in above function.
_vms_comp() { _vms_comp() {
local command=$1 local command=$1
local exclude_running=false
local vms local vms
local running_vms
local item local item
compopt -o filenames compopt -o filenames
if [[ $# == 2 ]]
then
exclude_running=true
running_vms=$(VBoxManage list runningvms | \
awk -F ' {' '{ print $1 }' | \
tr '\n' '|' | \
sed 's/|$//' | \
sed 's/"//g')
IFS='|' read -ra running_vms <<< "$running_vms"
fi
vms=$(VBoxManage list $command | \ vms=$(VBoxManage list $command | \
awk -F ' {' '{ print $1 }' | \ awk -F ' {' '{ print $1 }' | \
tr '\n' '|' | \ tr '\n' '|' | \
@@ -114,6 +128,12 @@ _VBoxManage() {
IFS='|' read -ra vms <<< "$vms" IFS='|' read -ra vms <<< "$vms"
for item in "${vms[@]}" for item in "${vms[@]}"
do do
if $exclude_running
then
_is_in_array "$item" "${running_vms[@]}"
[[ $? == 0 ]] && continue
fi
[[ ${item^^} == ${cur^^}* ]] && COMPREPLY+=("$item") [[ ${item^^} == ${cur^^}* ]] && COMPREPLY+=("$item")
done done
} }
@@ -370,6 +390,15 @@ _VBoxManage() {
done done
} }
_is_in_array() {
local element
for element in "${@:2}"
do
[[ "$element" == "$1" ]] && return 0
done
return 1
}
COMP_WORDBREAKS=${COMP_WORDBREAKS//|/} # remove pipe from comp word breaks COMP_WORDBREAKS=${COMP_WORDBREAKS//|/} # remove pipe from comp word breaks
COMPREPLY=() COMPREPLY=()
@@ -1605,7 +1634,7 @@ _VBoxManage() {
;; ;;
startvm) startvm)
if [[ ${prev} == ${cmd} ]]; then if [[ ${prev} == ${cmd} ]]; then
_vms_comp vms _vms_comp vms 1
elif [[ "${prev}" == "--type" ]]; then elif [[ "${prev}" == "--type" ]]; then
COMPREPLY=( $(compgen -W "gui sdl headless" -- ${cur}) ) COMPREPLY=( $(compgen -W "gui sdl headless" -- ${cur}) )
else else