aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/sd.bash
blob: 4dc72f31da1b9c8ce7ebeb6ea6ab1c625232beb4 (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
# Load _completion_ignore_case helper function
if ! declare -F _completion_ignore_case >/dev/null ; then
    source "$HOME"/.bash_completion.d/_completion_ignore_case.bash
fi

# Completion function for sd; any sibling directories, excluding the self
_sd() {

    # Build list of matching sibling directories
    local ci comp
    while IFS= read -d / -r comp ; do
        COMPREPLY[ci++]=$comp
    done < <(

        # Make globs expand appropriately
        shopt -s dotglob nullglob
        if _completion_ignore_case ; then
            shopt -s nocasematch 2>/dev/null
        fi

        # Print matching sibling dirs that are not the current dir
        for sib in ../*/ ; do
            # Strip leading ../
            sib=${sib#../}
            # Strip trailing slash
            sib=${sib%/}
            # Skip self
            [[ $sib != "${PWD##*/}" ]] || continue
            # Check the quoted and unquoted word for matching
            for match in "$sib" "$(printf '%q' "$sib")" ; do
                # Print any match, slash-terminated
                case $match in
                    ("$2"*)
                        printf '%s/' "$sib"
                        continue
                        ;;
                esac
            done
        done
    )
}
complete -F _sd -o filenames sd