aboutsummaryrefslogtreecommitdiff
path: root/sh/shrc.d/rd.sh
blob: 3b699c0d6f2ef7072323cbc5d30e726f22910902 (plain)
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
#
# Replace the first instance of the first argument string with the second
# argument string in $PWD, and make that the target of the cd builtin. This is
# to emulate a feature of the `cd` builtin in Zsh that I like, but that I think
# should be a separate command rather than overloading `cd`.
#
#     $ pwd
#     /usr/local/bin
#     $ rd local
#     $ pwd
#     /usr/bin
#     $ rd usr opt
#     $ pwd
#     /opt/bin
#
rd() {

    # Check argument count
    case $# in
        1|2) ;;
        *)
            printf >&2 \
                'rd(): Need a string and optionally a replacement\n'
            return 2
            ;;
    esac

    # Set the positional parameters to an option terminator and what will
    # hopefully end up being the substituted directory name
    set -- "$(

        # Current path: e.g. /foo/ayy/bar/ayy
        cur=$PWD
        # Pattern to replace: e.g. ayy
        pat=$1
        # Text with which to replace pattern: e.g. lmao
        # This can be a null string or unset, in order to *remove* the pattern
        rep=$2

        # /foo/
        curtc=${cur%%"$pat"*}
        # /bar/ayy
        curlc=${cur#*"$pat"}
        # /foo/lmao/bar/ayy
        new=${curtc}${rep}${curlc}

        # Check that a substitution resulted in an actual change and that we
        # ended up with a non-null target, or print an error to stderr
        if [ "$cur" = "$curtc" ] || [ -z "$new" ] ; then
            printf >&2 'rd(): Substitution failed\n'
            exit 1
        fi

        # Print the target with trailing slash to work around newline stripping
        printf '%s/' "${new%/}"
    )"

    # Remove trailing slash
    set -- "${1%/}"

    # If the subshell printed nothing, return with failure
    [ -n "$1" ] || return

    # Try to change into the determined directory
    command cd -- "$@"
}