aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/bd.bash
blob: 3850e9735cf76a6a1519588b74525ebb2b8a72cd (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Move back up the directory tree to the first directory matching the name
bd() {

    # For completeness' sake, we'll pass any options to cd
    local arg
    local -a opts
    for arg ; do
        case $arg in
            --)
                shift
                break
                ;;
            -*)
                shift
                opts[${#opts[@]}]=$arg
                ;;
            *)
                break
                ;;
        esac
    done

    # We should have zero or one arguments after all that, bail if there are
    # more
    if (($# > 1)) ; then
        printf 'bash: %s: usage: %s [PATH]\n' \
            "$FUNCNAME" "$FUNCNAME" >&2
        return 2
    fi

    # The requested pattern is the first argument; strip trailing slashes if
    # there are any
    local req=$1
    [[ $req != / ]] || req=${req%/}

    # What to do now depends on the request
    local dirname
    case $req in

        # If no argument at all, just go up one level
        '')
            dirname=..
            ;;

        # Just go straight to the root or dot directories if asked
        /|.|..)
            dirname=$req
            ;;

        # Anything else with a leading / needs to anchor to the start of the
        # path
        /*)
            dirname=$req
            if [[ $PWD != "$dirname"/* ]] ; then
                printf 'bash: %s: Directory name not in path\n' \
                    "$FUNCNAME" >&2
                return 1
            fi
            ;;

        # In all other cases, iterate through the directory tree to find a
        # match, or whittle the dirname down to an empty string trying
        *)
            dirname=${PWD%/*}
            while [[ -n $dirname && $dirname != */"$req" ]] ; do
                dirname=${dirname%/*}
            done
            if [[ -z $dirname ]] ; then
                printf 'bash: %s: Directory name not in path\n' \
                    "$FUNCNAME" >&2
                return 1
            fi
            ;;
    esac

    # Try to change into the determined directory
    builtin cd "${opts[@]}" -- "$dirname"
}

# Completion setup for bd
_bd() {

    # Only makes sense for the first argument
    ((COMP_CWORD == 1)) || return 1

    # Build a list of dirnames in $PWD
    local -a dirnames
    IFS=/ read -d '' -a dirnames < <(printf '%s\0' "${PWD#/}")

    # Remove the last element in the array (the current directory)
    ((${#dirnames[@]})) || return 1
    dirnames=("${dirnames[@]:0:$((${#dirnames[@]}-1))}")

    # Add the matching dirnames to the reply
    local dirname
    for dirname in "${dirnames[@]}" ; do
        [[ $dirname == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue
        COMPREPLY=("${COMPREPLY[@]}" "$(printf %q "$dirname")")
    done
}
complete -F _bd bd