-
Notifications
You must be signed in to change notification settings - Fork 0
/
sd.sh
executable file
·189 lines (169 loc) · 3.41 KB
/
sd.sh
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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
function show_usage() {
local sd="sd"
echo "Usage: $sd [-option]"
echo " or: $sd [-option] [arg...]"
echo "where options include:"
echo " -h show this help"
echo " -l list all dirs you've added"
echo " -s [no.] select a dir to switch to, according to seq no."
echo " -a . add current dir"
echo " -a [dir] add absolute dir"
echo " -r [no.] remove absolute dir, according to seq no."
echo " -ri remove invalid dirs"
echo " -R remove all dirs"
}
user_home=`echo ~`
file="$user_home/.switch_dir/dirs.txt"
function read_file() {
if [ -f $file ]; then
while read line
do
echo $line
done < $file
else
touch $file
fi
}
function add_dir() {
local d
if [ "$1" = "." ]; then
d=`pwd`
else
d=$1
fi
if [ ! -d $d ]; then
echo "$d: not a directory or not exist"
return
fi
local array=(`read_file`)
if [ "$array" = "" ]; then
echo $d > $file
else
local exists="false"
for dir in ${array[@]}
do
if [ "$dir" = "$d" ]; then
exists="true"
break
fi
done
if [ "$exists" = "false" ]; then
echo $d >> $file
fi
fi
}
function rem_dir() {
echo $1 | grep -w '[0-9]*' > /dev/null
if [ ! $? = 0 ]; then
echo "a sequence number is expected here"
return
fi
local array=(`read_file`)
local rem_idx=0
let len=$[${#array[@]}+1]
if [ $1 -lt $len -a $1 -gt 0 ]; then
rem_idx=$1
fi
local i=0
if [ "$rem_idx" != "0" ]; then
rem_all_dirs
for dir in ${array[@]}
do
((i++))
if [ "$rem_idx" != "$i" ]; then
echo $dir >> $file
fi
done
fi
}
function list_all_dirs() {
echo "[#] [dir]"
local array=(`read_file`)
local i=0
for dir in ${array[@]}
do
((i++))
echo $i" "$dir
done
}
function sel_dir() {
echo $1 | grep -w '[0-9]*' > /dev/null
if [ ! $? = 0 ]; then
echo "a sequence number is expected here"
return
fi
local array=(`read_file`)
local i=0
local sel_dir
for dir in ${array[@]}
do
((i++))
if [ "$1" = "$i" ]; then
sel_dir=$dir
break
fi
done
if [ "$sel_dir" = "" ]; then
echo "sequence number not valid"
return
fi
sysOS=`uname -s`
if [ $sysOS = "Darwin" ]; then
which pbcopy > /dev/null
if [ $? = 0 ]; then
echo -n "cd $sel_dir" | pbcopy
echo "press cmd+v to switch to $sel_dir"
else
echo "pbcopy not installed, switch to $sel_dir by yourself :)"
fi
else
which xsel > /dev/null
if [ $? = 0 ]; then
echo -n "cd $sel_dir" | xsel -i -b
echo "press ctrl+v to switch to $sel_dir"
else
echo "xsel not installed, switch to $sel_dir by yourself :)"
fi
fi
}
function rem_all_dirs() {
echo -n > $file
}
function rem_invalid_dirs() {
local array=(`read_file`)
rem_all_dirs
for dir in ${array[@]}
do
if [ -d $dir ]; then
echo $dir >> $file
fi
done
}
if [ $# -ne 1 ] && [ $# -ne 2 ]; then
show_usage
fi
if [ $# -eq 1 ]; then
if [ "$1" = "-h" ]; then
show_usage
elif [ "$1" = "-l" ]; then
list_all_dirs
elif [ "$1" = "-R" ]; then
rem_all_dirs
elif [ "$1" = "-ri" ]; then
rem_invalid_dirs
else
show_usage
fi
fi
if [ $# -eq 2 ]; then
if [ "$1" = "-a" ]; then
add_dir $2
elif [ "$1" = "-s" ]; then
sel_dir $2
elif [ "$1" = "-r" ]; then
rem_dir $2
else
show_usage
fi
fi