aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/mysql.bash
diff options
context:
space:
mode:
Diffstat (limited to 'bash/bashrc.d/mysql.bash')
-rw-r--r--bash/bashrc.d/mysql.bash53
1 files changed, 34 insertions, 19 deletions
diff --git a/bash/bashrc.d/mysql.bash b/bash/bashrc.d/mysql.bash
index 5ef6f67c..33231c13 100644
--- a/bash/bashrc.d/mysql.bash
+++ b/bash/bashrc.d/mysql.bash
@@ -23,26 +23,41 @@ mysql() {
# Completion setup for MySQL for configured databases
_mysql() {
- local word
- word=${COMP_WORDS[COMP_CWORD]}
-
- # Check directory exists and has at least one .cnf file
- local dir
- dir=$HOME/.mysql
- if [[ ! -d $dir ]] || (
- shopt -s nullglob dotglob
- declare -a files=("$dir"/*.cnf)
- ((! ${#files[@]}))
- ) ; then
- return 1
- fi
+
+ # The completed results are filenames, and if there are no matches, do
+ # default completion
+ compopt -o filenames -o default
+
+ # Only makes sense for first argument
+ ((COMP_CWORD == 1)) || return 1
+
+ # Bail if directory doesn't exist
+ local dirname
+ dirname=$HOME/.mysql
+ [[ -d $dirname ]] || return 1
# Return the names of the .cnf files sans prefix as completions
- local -a items
- items=("$dir"/*.cnf)
- items=("${items[@]##*/}")
- items=("${items[@]%%.cnf}")
- COMPREPLY=( $(compgen -W "${items[*]}" -- "$word") )
+ local db
+ while IFS= read -d '' -r db ; do
+ [[ $db == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue
+ COMPREPLY=("${COMPREPLY[@]}" "$db")
+ done < <(
+
+ # Set options so that globs expand correctly
+ shopt -s dotglob nullglob
+
+ # Collect all the config file names, strip off leading path and .cnf
+ local -a cnfs
+ cnfs=("$dirname"/*.cnf)
+ cnfs=("${cnfs[@]#$dirname/}")
+ cnfs=("${cnfs[@]%.cnf}")
+
+ # Bail if no files to prevent empty output
+ ((${#cnfs[@]})) || exit 1
+
+ # Print the conf names, null-delimited
+ printf '%s\0' "${cnfs[@]}"
+ )
}
-complete -F _mysql -o default mysql
+complete -F _mysql mysql