diff --git a/README.md b/README.md index 1c45f3a..7c53f07 100644 --- a/README.md +++ b/README.md @@ -37,3 +37,26 @@ use the simple bash installation script in the repository: ``` curl -L https://winklerfamilie.eu/git/efelon/shell_sequencer/raw/branch/master/install.sh | bash ``` + +## Bash-completion + +The included optional bash-completion script `sqnall-completion.bash` provides aliases to all available sequences as well as completion for sequencer.sh options and steps (including aliases) individually for each sequence. The aliases have the prefix `sqn_` which stands for _sequence name_. + +After executing + +``` +source /opt/sequencer/sqnall-completion.bash +``` + +e.g. `/opt/sequencer/seqs/kodi.sh` can be started anyhere with `sqn_kodi` + + +### Automatic setup after login + +To automatically provide aliases and bash-completion after login, the following bash script needs to be executed once for each user separately: + +``` +source /opt/sequencer/installCompletion.sh +``` + +This sources the bash-completion script in the users .bashrc file. diff --git a/installCompletion.sh b/installCompletion.sh new file mode 100755 index 0000000..e4255d9 --- /dev/null +++ b/installCompletion.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +sequencerCompletion() { + local SEQBASE="/opt/sequencer" + local SEQCOMP_LOC="$SEQBASE/sqnall-completion.bash" + local SEQCOMP_LOADER="$(realpath ~/.bashrc)" + local SEQCOMP_SOURCE="source \"$SEQCOMP_LOC\"" + + grep "$SEQCOMP_SOURCE" "$SEQCOMP_LOADER" >>/dev/null 2>&1 + if [ $? -eq 0 ]; then + echo " [I] Completion already installed ($SEQCOMP_LOADER)" + else + echo "$SEQCOMP_SOURCE" >>"$SEQCOMP_LOADER" + [ $? -eq 0 ] && echo " [I] Sequence bash-completion installed" + fi + source "$SEQCOMP_LOC" +} + +sequencerCompletion + diff --git a/sequencer/sequencer.sh b/sequencer/sequencer.sh index 43fd486..79b7576 100755 --- a/sequencer/sequencer.sh +++ b/sequencer/sequencer.sh @@ -8,8 +8,8 @@ ## Version information VERSION_REV=13 -VERSION_MAJOR=1 -VERSION_MINOR=1 +VERSION_MAJOR=2 +VERSION_MINOR=0 ## Start of generic script part @@ -59,23 +59,24 @@ helpSequencer() { Usage: ${0##*/} [OPTIONS] [STEP NUMBER(s) or ALIAS] [STEP ARGUMENTS] [OPTIONS] - --config, -c : Open sequence configuration file (also sets -qq) - --dry-run, -d : Only print to console what would be done - ! Attention - Sequence must support this - --help, -h : Display help - --helpapi, -ha : Display help about build-in supporting functions - (e.g. exe,addconf,echerr,...) - --profile, -p : Sequence configuration profile name (default: "default") - (if supported by sequence) - -pl : List available profiles - --quiet, -q : Don't ask for permission to execute steps - If called without starting step number, only this help is shown - -qq : Same as --quiet but suppresses regular sequencer.sh output - --single, -s : Execute only one step - If more than one step is requested, only the first will be executed - --verbose, -v : Verbose output (use exe() function to call shell commands in seqs) - ( e.g.: exe apt update ) - --version : Display version of sequencer and revision of sequence + --config, -c : Open sequence configuration file (also sets -qq) + --dry-run, -d : Only print to console what would be done + ! Attention - Sequence must support this + --help, -h : Display help + --helpapi, -ha : Display help about build-in supporting functions + (e.g. exe,addconf,echerr,...) + --liststeps, -ls : List all step numbers and alias + --profile, -p : Sequence configuration profile name (default: "default") + (if supported by sequence) + -pl : List available profiles + --quiet, -q : Don't ask for permission to execute steps + If called without starting step number, only this help is shown + -qq : Same as --quiet but suppresses regular sequencer.sh output + --single, -s : Execute only one step + If more than one step is requested, only the first will be executed + --verbose, -v : Verbose output (use exe() function to call shell commands in seqs) + ( e.g.: exe apt update ) + --version : Display version of sequencer and revision of sequence [STEP NUMBER"(s)" 1-${MAX_STEP} or ALIAS] No STEP or ALIAS : assume 1 as starting point @@ -995,6 +996,38 @@ displayHelp() { CONTEXT_HELP=0 } +# listSteps [FILTER STRING] +# [FILTER STRING] +# show only steps and aliases starting with [FILTER STRING] +listSteps() { + local aList=() + local aSearch="$1" + local locAlias= + + for ((i=1; i<=${MAX_STEP}; i++)); do + # Display step reference in help if step function exists + existsFunction step_${i} + [ $? -ne 0 ] && continue + + # Display alias if exists + existsFunction step_${i}_alias + if [ $? -eq 0 ] ; then + step_${i}_alias + locAlias=("$ALIAS") + else + locAlias=("$i") + fi + + if [ -z "$aSearch" ]; then + aList+=("$locAlias") + elif [[ "$locAlias" =~ ^$aSearch ]]; then + aList+=("$locAlias") + fi + done + + [ ${#aList[@]} -ne 0 ] && echo "${aList[@]}" +} + # showVersion showVersion() { echo "Sequencer ${VERSION_STRING}" @@ -1056,6 +1089,10 @@ main() { --helpapi|-ha) #show build-in functions helpApi exit 0;; + --liststeps|-ls) + shift + listSteps "$1" + exit 0;; --profile|-p) # seq profile name SEQUENCER_ARGS+=" $1 $2" shift diff --git a/sqnall-completion.bash b/sqnall-completion.bash new file mode 100755 index 0000000..c8b7fe1 --- /dev/null +++ b/sqnall-completion.bash @@ -0,0 +1,40 @@ +#/usr/bin/env bash + +_sqnall_completions() +{ + # 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="$(alias "$1")" + curCmd="${curCmd#*=\'}" + curCmd="${curCmd%\'*}" + COMPREPLY=() + case "$cur" in + -*) + COMPREPLY=( $( compgen -W '-c -d -h -ha -ls -p -pl -q -qq -s -v --version' -- $cur ) );; + *) + # Stop after step selection (last argument which is not an option; starting with "-") + (( COMP_CWORD > 1 )) && [[ ! "${COMP_WORDS[((COMP_CWORD - 1))]}" =~ ^- ]] && return + # sequencer.sh provides creation of a step list with search functionality + COMPREPLY=( $( "$curCmd" -ls "$cur" ) );; + esac + return 0 +} + +installCompletion() { + local SEQBASE="/opt/sequencer/seqs" + local SEQPREFIX="sqn_" + local SEQSHORT=() + # Create aliases and command (alias) list for "complete" command" + SEQLIST=($(ls "$SEQBASE/$cur"*.sh)) + for i in "${!SEQLIST[@]}"; do + SEQLIST[$i]="${SEQLIST[$i]##*/}" + SEQLIST[$i]="${SEQLIST[$i]%.*}" + SEQSHORT[$i]="${SEQPREFIX}${SEQLIST[$i]}" + alias ${SEQSHORT[$i]}="$SEQBASE/${SEQLIST[$i]}.sh" + done + complete -o nosort -o bashdefault -o default -F _sqnall_completions ${SEQSHORT[@]} +} + +installCompletion