aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/mysql.bash
blob: d3cc1e7b41f7d522ac7f1c99c5019aeefe18e385 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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
        [[ -n $db ]] || continue
        COMPREPLY[${#COMPREPLY[@]}]=$db
    done < <(

        # Set options so that globs expand correctly
        shopt -s dotglob nullglob

        # Make globbing case-insensitive if appropriate; is there a cleaner way
        # to find this value?
        while read -r _ option value ; do
            case $option in
                completion-ignore-case)
                    case $value in
                        on)
                            shopt -s nocaseglob
                            break
                            ;;
                    esac
            esac
        done < <(bind -v)

        # 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}")

        # Print quoted entries, null-delimited, if there was at least one;
        # otherwise, just print a null character to stop this hanging in Bash
        # 4.4
        if ((${#cnfs[@]})) ; then
            printf '%q\0' "${cnfs[@]}"
        else
            printf '\0'
        fi
    )
}

# bashdefault requires Bash >=3.0
if ((BASH_VERSINFO[0] >= 3)) ; then
    complete -F _mysql -o bashdefault -o default mysql
else
    complete -F _mysql -o default mysql
fi