wd.sh 4.58 KB
Newer Older
Markus Færevaag's avatar
Markus Færevaag committed
1
2
#!/bin/zsh

3
4
# WARP DIRECTORY
# ==============
Markus Færevaag's avatar
Markus Færevaag committed
5
6
7
8
9
10
11
# Jump to custom directories in terminal
# because `cd` takes too long...
#
# @github.com/mfaerevaag/wd


## variables
12
readonly CONFIG=$HOME/.warprc
Markus Færevaag's avatar
Markus Færevaag committed
13

14
15
16
17
18
19
# colors
readonly BLUE="\033[96m"
readonly GREEN="\033[92m"
readonly YELLOW="\033[93m"
readonly RED="\033[91m"
readonly NOC="\033[m"
Markus Færevaag's avatar
Markus Færevaag committed
20
21


22
23
## init

24
# check if config file exists
25
if [ ! -e $CONFIG ]
26
then
27
28
    # if not, create config file
    touch $CONFIG
29
30
fi

31
# load warp points
Markus Færevaag's avatar
Markus Færevaag committed
32
typeset -A points
33
while read -r line
Markus Færevaag's avatar
Markus Færevaag committed
34
35
36
37
38
39
40
41
42
43
44
45
46
do
    arr=(${(s,:,)line})
    key=${arr[1]}
    val=${arr[2]}

    points[$key]=$val
done < $CONFIG


## functions

wd_warp()
{
47
48
49
    local point=$1

    if [[ $point =~ "^\.+$" ]]
Markus Færevaag's avatar
Markus Færevaag committed
50
    then
51
        if [ $#1 < 2 ]
Markus Færevaag's avatar
Markus Færevaag committed
52
53
54
55
56
57
        then
            wd_print_msg $YELLOW "Warping to current directory?"
        else
            (( n = $#1 - 1 ))
            cd -$n > /dev/null
        fi
58
    elif [[ ${points[$point]} != "" ]]
Markus Færevaag's avatar
Markus Færevaag committed
59
    then
60
        cd ${points[$point]}
Markus Færevaag's avatar
Markus Færevaag committed
61
    else
62
        wd_print_msg $RED "Unknown warp point '${point}'"
Markus Færevaag's avatar
Markus Færevaag committed
63
64
65
66
67
    fi
}

wd_add()
{
68
69
70
71
72
73
74
    local force=$1
    local point=$2

    if [[ $point =~ "^[\.]+$" ]]
    then
        wd_print_msg $RED "Warp point cannot be just dots"
    elif [[ $point =~ "(\s|\ )+" ]]
Markus Færevaag's avatar
Markus Færevaag committed
75
    then
76
77
        wd_print_msg $RED "Warp point should not contain whitespace"
    elif [[ $point == *:* ]]
Markus Færevaag's avatar
Markus Færevaag committed
78
    then
79
80
81
82
83
84
85
86
87
        wd_print_msg $RED "Warp point cannot contain colons"
    elif [[ $point == "" ]]
    then
        wd_print_msg $RED "Warp point cannot be empty"
    elif [[ ${points[$2]} == "" ]] || $force
    then
        wd_remove $point > /dev/null
        printf "%q:%q\n" "${point}" "${PWD}" >> $CONFIG

Markus Færevaag's avatar
Markus Færevaag committed
88
89
        wd_print_msg $GREEN "Warp point added"
    else
90
        wd_print_msg $YELLOW "Warp point '${point}' already exists. Use 'add!' to overwrite."
Markus Færevaag's avatar
Markus Færevaag committed
91
92
93
94
95
    fi
}

wd_remove()
{
96
97
98
    local point=$1

    if [[ ${points[$point]} != "" ]]
Markus Færevaag's avatar
Markus Færevaag committed
99
    then
100
        if sed -i.bak "s,^${point}:.*$,,g" $CONFIG
Markus Færevaag's avatar
Markus Færevaag committed
101
102
103
        then
            wd_print_msg $GREEN "Warp point removed"
        else
104
            wd_print_msg $RED "Something bad happened! Sorry."
Markus Færevaag's avatar
Markus Færevaag committed
105
106
107
108
109
110
111
112
113
        fi
    else
        wd_print_msg $RED "Warp point was not found"
    fi
}

wd_list_all()
{
    wd_print_msg $BLUE "All warp points:"
114
115

    while IFS= read -r line
Markus Færevaag's avatar
Markus Færevaag committed
116
117
118
119
120
121
122
    do
        if [[ $line != "" ]]
        then
            arr=(${(s,:,)line})
            key=${arr[1]}
            val=${arr[2]}

123
            printf "%20s  ->  %s\n" $key $val
Markus Færevaag's avatar
Markus Færevaag committed
124
        fi
125
126
127
128
129
130
131
132
133
    done <<< $(sed "s:${HOME}:~:g" $CONFIG)
}

wd_show()
{
    local cwd=$(print $PWD | sed "s:^${HOME}:~:")

    wd_print_msg $BLUE "Warp points to current directory:"
    wd_list_all | grep -e "${cwd}$"
Markus Færevaag's avatar
Markus Færevaag committed
134
135
136
137
}

wd_print_msg()
{
138
139
140
141
    local color=$1
    local msg=$2

    if [[ $color == "" || $msg == "" ]]
Markus Færevaag's avatar
Markus Færevaag committed
142
    then
143
        print " ${RED}*${NOC} Could not print message. Sorry!"
Markus Færevaag's avatar
Markus Færevaag committed
144
    else
145
        print " ${color}*${NOC} ${msg}"
Markus Færevaag's avatar
Markus Færevaag committed
146
147
148
149
150
    fi
}

wd_print_usage()
{
151
152
153
154
155
156
157
158
159
160
161
    cat <<- EOF
Usage: wd [add|-a|--add] [rm|-r|--remove] <point>

Commands:
	add	Adds the current working directory to your warp points
	add!	Overwrites existing warp point
	rm	Removes the given warp point
	show	Outputs warp points to current directory
	ls	Outputs all stored warp points
	help	Show this extremely helpful text
EOF
Markus Færevaag's avatar
Markus Færevaag committed
162
163
164
165
166
167
}


## run

# get opts
168
args=$(getopt -o a:r:lhs -l add:,rm:,ls,help,show -- $*)
Markus Færevaag's avatar
Markus Færevaag committed
169

170
# check if no arguments were given
Markus Færevaag's avatar
Markus Færevaag committed
171
172
173
174
if [[ $? -ne 0 || $#* -eq 0 ]]
then
    wd_print_usage

175
# check if config file is writeable
176
elif [ ! -w $CONFIG ]
177
then
178
    # do nothing
179
    # can't run `exit`, as this would exit the executing shell
180
    wd_print_msg $RED "\'$CONFIG\' is not writeable."
181
182

else
183
    for o
Markus Færevaag's avatar
Markus Færevaag committed
184
    do
185
        case "$o"
Markus Faerevaag's avatar
Markus Faerevaag committed
186
187
188
            in
            -a|--add|add)
                wd_add false $2
Markus Færevaag's avatar
Markus Færevaag committed
189
190
191
                break
                ;;
            -a!|--add!|add!)
Markus Faerevaag's avatar
Markus Faerevaag committed
192
                wd_add true $2
Markus Færevaag's avatar
Markus Færevaag committed
193
194
                break
                ;;
Markus Faerevaag's avatar
Markus Faerevaag committed
195
196
            -r|--remove|rm)
                wd_remove $2
Markus Færevaag's avatar
Markus Færevaag committed
197
198
                break
                ;;
Markus Faerevaag's avatar
Markus Faerevaag committed
199
200
            -l|--list|ls)
                wd_list_all
Markus Færevaag's avatar
Markus Færevaag committed
201
202
                break
                ;;
Markus Faerevaag's avatar
Markus Faerevaag committed
203
204
            -h|--help|help)
                wd_print_usage
Markus Færevaag's avatar
Markus Færevaag committed
205
206
                break
                ;;
Markus Faerevaag's avatar
Markus Faerevaag committed
207
208
            -s|--show|show)
                wd_show
Markus Færevaag's avatar
Markus Færevaag committed
209
210
211
                break
                ;;
            *)
212
                wd_warp $o
Markus Færevaag's avatar
Markus Færevaag committed
213
214
                break
                ;;
Markus Faerevaag's avatar
Markus Faerevaag committed
215
216
217
218
            --)
                break
                ;;
        esac
Markus Færevaag's avatar
Markus Færevaag committed
219
220
221
222
223
224
    done
fi

## garbage collection
# if not, next time warp will pick up variables from this run
# remember, there's no sub shell
225
226
227
228
229
230
231
232
233

unset wd_warp
unset wd_add
unset wd_remove
unset wd_show
unset wd_list_all
unset wd_print_msg
unset wd_print_usage

Markus Faerevaag's avatar
Markus Faerevaag committed
234
unset args
235
unset points
Markus Faerevaag's avatar
Markus Faerevaag committed
236
unset val &> /dev/null # fixes issue #1