-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbettercd.source
More file actions
226 lines (197 loc) · 5.46 KB
/
bettercd.source
File metadata and controls
226 lines (197 loc) · 5.46 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env bash
export BETTERCDROOT=$HOME/projects
export BETTERCDVERBOSE=0
export BETTERCDCACHE=$HOME/.bettercd/bettercd.cache
export BETTERCDLWD=$HOME/.bettercd/bettercd.lwd
function _bettercd_save_lwd(){
#Saves the last working directory to $BETTERCDLWD
echo $PWD >> ${BETTERCDLWD}
}
function _bettercd_cd_to_lwd(){
#Changes the working dir to the last working directory
LWD=$(tail -1 ${BETTERCDLWD})
_bettercd_save_lwd
builtin cd $LWD
}
function _bettercd_cd_to_home(){
#Changes the working dir to $HOME
_bettercd_save_lwd
_bettercd_log "cd to $HOME"
builtin cd $HOME
}
function _bettercd_regen_cache(){
#Regenerates Bettercd Cache
echo "Regenerating bettercd cache"
find $HOME -type d 2>/dev/null > $HOME/.bettercd/bettercd.cache
}
function _bettercd_log(){
#Outputs some info if $BETTERCDVERBOSE is 1
if [[ $BETTERCDVERBOSE -eq 1 ]]; then
echo "$2"
fi
}
function _bettercd_update_root() {
if [[ ! -z $2 ]]; then
#sed -i 's/export BETTERCDROOT\=.*/export BETTERCDROOT\=\$HOME/g' $HOME/.bettercd/bettercd.source
if [[ -d $2 ]]; then
echo "Updating bettercd root DIR to $2"
export BETTERCDROOT=$2
sed -i "0,/export BETTERCDROOT\=.*/s??export BETTERCDROOT\=$BETTERCDROOT?" $HOME/.bettercd/bettercd.source
echo "Updating cache"
find $BETTERCDROOT -type d 2>/dev/null > $HOME/.bettercd/bettercd.cache
echo "Updating cronjob"
crontab -l | grep -v bettercd | crontab -
echo "*/20 * * * * find $BETTERCDROOT -type d 2>/dev/null > $HOME/.bettercd/bettercd.cache" | crontab -
else
echo "Seems like $2 is not a directory"
fi
else
echo "USAGE:"
echo
echo "cd --updateroot <DIR>"
fi
}
function _bettercd_show_hist() {
NHISTCD=15
re='^[0-9]+$'
if [[ ! -z $2 ]]; then
if [[ "$2" =~ $re ]] ;then
NHISTCD=$2
fi
fi
#HISTCD=$(tail -"$NHISTCD" "$BETTERCDLWD" | sed "s?$BETTERCDROOT?\\\e\[95m$BETTERCDROOT\\\e\[32m?g")
local IT=1
while IFS="" read -r p || [ -n "$p" ]
do
echo "[-$IT] $p"
IT=$((IT+1))
if (($IT > $NHISTCD )); then
break
fi
done < $BETTERCDLWD
}
function _bettercd_create_expr(){
WC="[a-zA-Z\-]*"
EXPR="$WC"
for T in $@
do
if [[ $T != "cd" ]]; then
#statements
EXPR="$EXPR$T$WC"[\\/\\-]"$WC"
fi
done
L=$(echo ${EXPR} | wc -c)-18
EXPR=${EXPR:0:$L}
return $EXPR
}
function c () {
WC="[a-zA-Z\-]*"
EXPR="$WC"
if [[ -z $1 ]]; then
_bettercd_cd_to_home
elif [[ $1 == "--help" ]]; then
man $HOME/.bettercd/bettercd.man
elif [[ $1 == "-" || $1 == "--" ]]; then
_bettercd_cd_to_lwd
elif [[ $1 == "--cache" ]]; then
_bettercd_regen_cache
elif [[ $1 == "--updateroot" ]]; then
_bettercd_update_root $@
elif [[ $1 == "-h" || $1 == "--history" ]]; then
_bettercd_show_hist $@
else # $1 is a DIR or a list of PATERN
_bettercd_save_lwd
builtin cd $1 2>/dev/null
if [ $? -eq 0 ]; then
_bettercd_log "cd to $PWD"
else
if [[ -f $BETTERCDCACHE ]]; then
BETTERLIST=0
for T in $@
do
if [[ $T == "-l" ]]; then
BETTERLIST=1
else
EXPR="$EXPR$T$WC"[\\/\\-]"$WC"
fi
done
L=$(echo ${EXPR} | wc -c)-18
EXPR=${EXPR:0:$L}
if [[ $BETTERLIST -eq 1 ]]; then
LISTCD=$(grep -i "$EXPR" "$BETTERCDCACHE" | awk '{ print length($0) " " $0; }' $file | sort -n | cut -d ' ' -f 2- | head -10 | sed "s?$BETTERCDROOT?\\\e\[95m$BETTERCDROOT\\\e\[32m?g") && echo -e "\e[95mBETTERCDROOT:$BETTERCDROOT\n\n$LISTCD"
else
DIR=$(grep -i "$EXPR" "$BETTERCDCACHE" | awk '{ print length($0) " " $0; }' $file | sort -n | cut -d ' ' -f 2- | head -1)
_bettercd_log "better cd to $DIR"
builtin cd $DIR
fi
else
echo "bettercd cache not found"
echo "please run $HOME/.bettercd/install.sh"
fi
fi
fi
}
function cc () {
WC="[a-zA-Z\-]*"
EXPR="$WC"
CACHE=$HOME/.bettercd/bettercd.cache
if [[ -z $1 ]]; then
_bettercd_cd_to_home
elif [[ $1 == "-h" ]]; then
man $HOME/.bettercd/bettercd.man
else
if [[ -f $BETTERCDCACHE ]]; then
BETTERLIST=0
for T in $@
do
if [[ $T == "-l" ]]; then
BETTERLIST=1
else
EXPR="$EXPR$T$WC"[\\/\\-]"$WC"
fi
done
L=$(echo ${EXPR} | wc -c)-18
EXPR=${EXPR:0:$L}
DIR=$(grep -i $PWD/ $BETTERCDCACHE | grep -i "$EXPR" | awk '{ print length($0) " " $0; }' $file | sort -n | cut -d ' ' -f 2- | head -1)
_bettercd_log "better cd to $DIR"
if [[ $BETTERLIST -eq 1 ]]; then
LISTCDD=$(grep -i "$PWD/" $BETTERCDCACHE | grep -i "$EXPR" | awk '{ print length($0) " " $0; }' $file | sort -n | cut -d ' ' -f 2- | head -10 | sed "s?$PWD?\\\e\[95m$PWD\\\e\[32m?g" | sed "s?$BETTERCDROOT?$BETTERCDROOT\\\e\[96m?g") && echo -e "\e[96mPWD:$PWD\n\e[95mBETTERCDROOT:$BETTERCDROOT\n\n$LISTCDD"
else
if [[ ! -z $DIR ]]; then
builtin cd $DIR
else
_bettercd_log "No directory found to jump under $PWD"
fi
fi
else
echo "bettercd cache not found"
echo "please run $HOME/.bettercd/install.sh"
fi
fi
}
function _bettercd_comp(){
local cur cword prev
N=0
if [[ -z ${words[2]} ]]; then
#statements
return 0
fi
if [[ ${N} == 1 ]]; then
#statements
return 1
fi
WC="[a-zA-Z\-]*"
EXPR="$WC"
for T in ${words}
do
if [[ $T != "cd" ]]; then
#statements
EXPR="$EXPR$T$WC"[\\/\\-]"$WC"
fi
done
L=$(echo ${EXPR} | wc -c)-18
EXPR=${EXPR:0:$L}
LISTCDD=$(grep -i "$EXPR" $BETTERCDCACHE | awk '{ print length($0) " " $0; }' $file | sort -n | cut -d ' ' -f 2- | head -10 | sed "s?$BETTERCDROOT??g") && echo -e "\n$LISTCDD\e[8m"
_arguments '%'
N+=1
}