2023-03-27 22:23:13 +02:00
|
|
|
function _hs () {
|
2023-03-03 05:00:57 +01:00
|
|
|
local cur prev
|
|
|
|
COMPREPLY=()
|
|
|
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
|
|
|
|
|
|
local settings_file
|
|
|
|
settings_file=~/'.heidisql/portable_settings.txt'
|
|
|
|
if [[ ! -r "${settings_file}" ]]; then
|
|
|
|
COMPREPLY="$(printf -- '%s' '<File '"${settings_file}"' not readable, nothing to auto-complete>')"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local saved_sessions
|
|
|
|
# Find all saved sessions where NetType is set to '2' aka 'MariaDB or
|
|
|
|
# MySQL (SSH tunnel)'
|
|
|
|
saved_sessions="$(grep -Pio -- '(?<=^Servers\\)[^\\]+(?=\\NetType<\|\|\|>[[:digit:]]<\|\|\|>2)' "${settings_file}" | sort | uniq)"
|
|
|
|
|
|
|
|
# Commands below depend on this IFS
|
|
|
|
local IFS=$'\n'
|
|
|
|
|
|
|
|
if [[ "${prev}" != 'hs' ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${cur}" == * ]]; then
|
|
|
|
# Filter our candidates
|
|
|
|
CANDIDATES=($(compgen -W "${saved_sessions[*]}" -- "${cur}"))
|
|
|
|
|
|
|
|
# Correctly set our candidates to COMPREPLY
|
|
|
|
if [[ "${#CANDIDATES[*]}" -eq '0' ]]; then
|
|
|
|
COMPREPLY=()
|
|
|
|
else
|
|
|
|
COMPREPLY=($(printf ''"'"'%s'"'"'\n' "${CANDIDATES[@]}"))
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
2023-03-27 22:23:13 +02:00
|
|
|
|
2023-03-03 05:00:57 +01:00
|
|
|
complete -F _hs hs
|