diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-07-30 01:17:09 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-07-30 02:09:30 +1200 |
commit | 4cbbd121c012b3962f12fdff0f1820c3b8636a44 (patch) | |
tree | 00149270df365ed0b7a1b9e9f4922fe7d5a80eb3 /bash/bash_completion.d/mysql.bash | |
parent | Change ca from Bash func to sh script (diff) | |
download | dotfiles-4cbbd121c012b3962f12fdff0f1820c3b8636a44.tar.gz dotfiles-4cbbd121c012b3962f12fdff0f1820c3b8636a44.zip |
Move bash completion setup into separate dir
Diffstat (limited to 'bash/bash_completion.d/mysql.bash')
-rw-r--r-- | bash/bash_completion.d/mysql.bash | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/bash/bash_completion.d/mysql.bash b/bash/bash_completion.d/mysql.bash new file mode 100644 index 00000000..f64b6f32 --- /dev/null +++ b/bash/bash_completion.d/mysql.bash @@ -0,0 +1,34 @@ +# Completion setup for MySQL for configured databases +_mysql() { + + # 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 db + while IFS= read -rd '' db ; do + 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"/"${COMP_WORDS[COMP_CWORD]}"*.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 '%q\0' "${cnfs[@]}" + ) +} +complete -F _mysql -o default mysql |