sqn-completion supports seqs with spaces in their names

sqn-completion now shellcheck compliant and correctly indented
This commit is contained in:
2022-03-16 01:05:29 +01:00
parent 48b5965969
commit 739a4b1e7c
2 changed files with 38 additions and 31 deletions

View File

@@ -1,6 +1,6 @@
root = true root = true
[*.{sh,cfg,example}] [*.{sh,bash,cfg,example}]
charset = utf-8 charset = utf-8
indent_style = space indent_style = space
indent_size = 2 indent_size = 2

View File

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