wd.sh 4.28 KB
Newer Older
Markus Færevaag's avatar
Markus Færevaag committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/zsh

# WARP
# ====
# Jump to custom directories in terminal
# because `cd` takes too long...
#
# @github.com/mfaerevaag/wd


## variables
CONFIG=$HOME/.warprc

## colors
BLUE="\033[96m"
GREEN="\033[92m"
YELLOW="\033[93m"
RED="\033[91m"
NOC="\033[m"


22
23
24
25
26
27
28
# check if config file exists
if [[ ! -a $CONFIG ]]
then
  # if not: create config file
  touch $CONFIG
fi

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

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


## functions
# prepended wd_ to not conflict with your environment (no sub shell)

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

wd_add()
{
Markus Faerevaag's avatar
Markus Faerevaag committed
67
    if [[ $2 =~ "^\.+$" || $2 =~ "^\s*$" ]]
Markus Færevaag's avatar
Markus Færevaag committed
68
    then
Markus Faerevaag's avatar
Markus Faerevaag committed
69
70
        wd_print_msg $RED "Illegal warp point (see README)."
    elif [[ ${points[$2]} == "" ]] || $1
Markus Færevaag's avatar
Markus Færevaag committed
71
    then
Markus Faerevaag's avatar
Markus Faerevaag committed
72
73
        wd_remove $2 > /dev/null
        print "$2:$PWD" >> $CONFIG
Markus Færevaag's avatar
Markus Færevaag committed
74
75
        wd_print_msg $GREEN "Warp point added"
    else
Markus Faerevaag's avatar
Markus Faerevaag committed
76
        wd_print_msg $YELLOW "Warp point '$2' already exists. Use 'add!' to overwrite."
Markus Færevaag's avatar
Markus Færevaag committed
77
78
79
80
81
82
83
84
85
    fi
}

wd_remove()
{
    if [[ ${points[$1]} != "" ]]
    then
        if wd_tmp=`sed "/^$1:/d" $CONFIG`
        then
Markus Faerevaag's avatar
Markus Faerevaag committed
86
87
88
            # `>!` forces overwrite
            # we need this if people use `setopt NO_CLOBBER`
            echo $wd_tmp >! $CONFIG
Markus Færevaag's avatar
Markus Færevaag committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
            wd_print_msg $GREEN "Warp point removed"
        else
            wd_print_msg $RED "Warp point unsuccessfully removed. Sorry!"
        fi
    else
        wd_print_msg $RED "Warp point was not found"
    fi
}

wd_show()
{
    wd_print_msg $BLUE "Warp points to current directory:"
    wd_list_all | grep $PWD$
}

wd_list_all()
{
    wd_print_msg $BLUE "All warp points:"
    while read line
    do
        if [[ $line != "" ]]
        then
            arr=(${(s,:,)line})
            key=${arr[1]}
            val=${arr[2]}

            print "\t" $key "\t -> \t" $val
        fi
    done < $CONFIG
}

wd_print_msg()
{
    if [[ $1 == "" || $2 == "" ]]
    then
        print " $RED*$NOC Could not print message. Sorry!"
    else
        print " $1*$NOC $2"
    fi
}

wd_print_usage()
{
132
    print "Usage: wd [add|-a|--add] [rm|-r|--remove] [ls|-l|--list] <point>"
Markus Færevaag's avatar
Markus Færevaag committed
133
134
135
    print "\nCommands:"
    print "\t add \t Adds the current working directory to your warp points"
    print "\t add! \t Overwrites existing warp point"
Markus Faerevaag's avatar
Markus Faerevaag committed
136
    print "\t rm \t  Removes the given warp point"
137
    print "\t show \t Outputs warp points to current directory"
Markus Faerevaag's avatar
Markus Faerevaag committed
138
    print "\t ls \t Outputs all stored warp points"
Markus Færevaag's avatar
Markus Færevaag committed
139
140
141
142
143
144
145
    print "\t help \t Show this extremely helpful text"
}


## run

# get opts
Markus Faerevaag's avatar
Markus Faerevaag committed
146
args=`getopt -o a:r:lhs -l add:,rm:,ls,help,show -- $*`
Markus Færevaag's avatar
Markus Færevaag committed
147

148
# check if no arguments were given
Markus Færevaag's avatar
Markus Færevaag committed
149
150
151
152
if [[ $? -ne 0 || $#* -eq 0 ]]
then
    wd_print_usage

153
154
155
156
157
158
159
160
161
# check if config file is writeable
elif [[ ! -w $CONFIG ]]
then
    wd_print_msg $RED "\'$CONFIG\' is not writeable."
    # do nothing => exit
    # can't run `exit`, as this would exit the executing shell
    # i.e. your terminal

else
Markus Færevaag's avatar
Markus Færevaag committed
162
163
164
165
    #set -- $args # WTF

    for i
    do
Markus Faerevaag's avatar
Markus Faerevaag committed
166
167
168
169
        case "$i"
            in
            -a|--add|add)
                wd_add false $2
Markus Færevaag's avatar
Markus Færevaag committed
170
171
172
                break
                ;;
            -a!|--add!|add!)
Markus Faerevaag's avatar
Markus Faerevaag committed
173
                wd_add true $2
Markus Færevaag's avatar
Markus Færevaag committed
174
175
                break
                ;;
Markus Faerevaag's avatar
Markus Faerevaag committed
176
177
            -r|--remove|rm)
                wd_remove $2
Markus Færevaag's avatar
Markus Færevaag committed
178
179
                break
                ;;
Markus Faerevaag's avatar
Markus Faerevaag committed
180
181
            -l|--list|ls)
                wd_list_all
Markus Færevaag's avatar
Markus Færevaag committed
182
183
                break
                ;;
Markus Faerevaag's avatar
Markus Faerevaag committed
184
185
            -h|--help|help)
                wd_print_usage
Markus Færevaag's avatar
Markus Færevaag committed
186
187
                break
                ;;
Markus Faerevaag's avatar
Markus Faerevaag committed
188
189
            -s|--show|show)
                wd_show
Markus Færevaag's avatar
Markus Færevaag committed
190
191
192
193
194
195
                break
                ;;
            *)
                wd_warp $i
                break
                ;;
Markus Faerevaag's avatar
Markus Faerevaag committed
196
197
198
199
            --)
                break
                ;;
        esac
Markus Færevaag's avatar
Markus Færevaag committed
200
201
202
203
204
205
206
    done
fi


## garbage collection
# if not, next time warp will pick up variables from this run
# remember, there's no sub shell
Markus Faerevaag's avatar
Markus Faerevaag committed
207
208
209
unset points
unset args
unset val &> /dev/null # fixes issue #1