101 lines
3.2 KiB
Bash
Executable File
101 lines
3.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# shellcheck disable=SC1090 # dynamically sourced file
|
|
|
|
_sqn_completions()
|
|
{
|
|
local tempreply=()
|
|
# Current search string
|
|
local cur=${COMP_WORDS[COMP_CWORD]}
|
|
# Extract alias command with buildins same as:
|
|
# local curCmd="$(alias $1 | sed "s/^alias.*\" \"\(.*\)\"'$/\1/")"
|
|
local curCmd=
|
|
curCmd="$(alias "${1:-}")"
|
|
curCmd="${curCmd##*\" \"}"
|
|
curCmd="${curCmd%\"\'*}"
|
|
COMPREPLY=()
|
|
case "$cur" in
|
|
-*|+*)
|
|
opts=( \
|
|
-- ++\
|
|
-a --all \
|
|
-c --config \
|
|
-d --dry-run \
|
|
-h --help \
|
|
-ha --helpapi \
|
|
-ls --liststeps \
|
|
-p --profile \
|
|
-pl \
|
|
-q --quiet \
|
|
-qq \
|
|
-s --single \
|
|
-v --verbose \
|
|
--version)
|
|
read -r -d '' -a COMPREPLY < <(compgen -W "${opts[*]}" -- "$cur");;
|
|
*)
|
|
case "${COMP_WORDS[((COMP_CWORD-1))]}" in
|
|
# provide completion for option with parameter
|
|
-p|--profile)
|
|
# sequencer.sh provides creation of a profile list with search functionality
|
|
IFS=$'\n' read -r -d '' -a tempreply <<<"$( "$curCmd" -pl "${cur}" 2>/dev/null)"
|
|
for comp in "${tempreply[@]}" ; do
|
|
# Support for profile names with spaces
|
|
COMPREPLY+=("$(printf '%q' "${comp}")")
|
|
done
|
|
;;
|
|
*)
|
|
# Stop after step selection (last argument which is not an option (starting with "-|+")
|
|
# or option paramter (profile name))
|
|
if (( COMP_CWORD > 1 )) && [[ ! "${COMP_WORDS[((COMP_CWORD - 1))]}" =~ ^(-|\+) ]] ; then
|
|
case "${COMP_WORDS[((COMP_CWORD - 2))]}" in
|
|
-p|--profile)
|
|
;;
|
|
*)
|
|
return
|
|
;;
|
|
esac
|
|
fi
|
|
# sequencer.sh provides creation of a step list with search functionality
|
|
#read -r -a COMPREPLY <<<"$( "$curCmd" -ls "$cur" )"
|
|
IFS=$'\n' read -r -d '' -a tempreply <<<"$( "$curCmd" -ls "$cur" )"
|
|
for comp in "${tempreply[@]}" ; do
|
|
# Support for aliases with spaces
|
|
COMPREPLY+=("$(printf '%q' "${comp}")")
|
|
done
|
|
;;
|
|
esac
|
|
;;
|
|
esac
|
|
return 0
|
|
}
|
|
|
|
installCompletion() {
|
|
# Get script working directory
|
|
# (when called from a different directory)
|
|
local script_dir=
|
|
script_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >>/dev/null 2>&1 && pwd)"
|
|
|
|
. "${script_dir}/sequencer.cfg" >/dev/null 2>&1
|
|
|
|
local seq_base="${script_dir}/seqs"
|
|
local seq_prefix="sqn_"
|
|
local seq_short=()
|
|
local seq_list=()
|
|
|
|
# Check for user selected sequences
|
|
[ -e "$SEQUENCER_USER_SEQS" ] && seq_base="$SEQUENCER_USER_SEQS"
|
|
|
|
# Create aliases and command (alias) list for "complete" command"
|
|
IFS=$'\n' read -r -d '' -a seq_list < <(ls -1 "${seq_base}/${cur}"*.sh)
|
|
for i in "${!seq_list[@]}"; do
|
|
seq_list[$i]="${seq_list[$i]##*/}"
|
|
seq_list[$i]="${seq_list[$i]%.*}"
|
|
seq_short[$i]="${seq_prefix}${seq_list[$i]/[[:blank:]]/_}"
|
|
# shellcheck disable=SC2139,SC2086
|
|
# alias should be expanded when defined
|
|
alias ${seq_short[$i]}="_SQN_ALIAS=\"${seq_short[$i]}\" \"${seq_base}/${seq_list[$i]}.sh\""
|
|
done
|
|
complete -o nosort -o bashdefault -o default -F _sqn_completions "${seq_short[@]}"
|
|
}
|
|
|
|
installCompletion
|