/
dirsrc
193 lines (169 loc) · 3.77 KB
/
dirsrc
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
# vim: syn=sh
#
# FROM:
# http://www.debian-administration.org/articles/130#comment_62
#
# put this in your .bashrc to see how it works
# I tried to make useful comments
# but you are welcome to delete them
# -----SECTION: Moving through directories --------
# declaring variables
export dirarr dircnt dirarr_fhist dircnt_fhist
# dirarr is where I keep visited directories (no duplicates)
# dircnt is its length, ${#arr} was giving me some trouble
# dirarr_fhist a an array of forward history
if [ -z $dircnt ]; then
dircnt="1"
dirarr=("`pwd`")
fi
if [ -z $dircnt_fhist ]; then
dircnt_fhist="0"
dirarr_fhist=
fi
# some aliases that will save us the trouble of too much typing
alias cd='push'
alias p='push'
alias o='pop'
alias c='choose'
alias dir='dir_utility'
# 'cd' command togglers
function plain_dir () { unalias cd; }
function cool_dir () { alias cd='push'; }
# loads and saves history,
# lists and chooses directories
function dir_utility() {
case "$@" in
s|sa|sav|save) save_dirhist ;;
lo|loa|load) load_dirhist ;;
l|li|lis|list) dirl ;;
c|ch|cho|choo|choos|choose) choose ;;
cl|cle|clea|clear) clear_hist ;;
*) \dir "$@" ;;
esac
}
# Used by push() function (see below)
function pop() { popd "$@" > /dev/null; }
# This is where 'cd' command is parsed
function push() {
case "$@" in
"")
pushd ~ > /dev/null
;;
"-") # going backward in history
prevdir="`pwd`"
popd > /dev/null
dirarr_fhist[$dircnt_fhist]=$prevdir
((dircnt_fhist++))
;;
"+"|"=") # going forwards in history
if [ "$dircnt_fhist" == "0" ]; then
echo "Forward list empty"
return 1
fi
((dircnt_fhist--))
dir=${dirarr_fhist[$dircnt_fhist]}
if [ ! -z $dir ]; then
pushd "$dir" > /dev/null
fi
;;
*) # parsing cd
if [ -a "$@" ]; then
pushd "$@" > /dev/null
clear_fhist
curdir="`pwd`"
for ((i="0"; $i < $dircnt; i++)) ; do
if [ "${dirarr[$i]}" == "$curdir" ]; then
return
fi
done
dirarr[$dircnt]="$curdir"
#dircnt=$((++dircnt))
((dircnt++))
else
cdpartial "$@"
fi
;;
esac
export dirarr dircnt
export dirarr_fhist dircnt_fhist
}
# list history
function dirl() {
for ((i="0"; $i < $dircnt; i++)) ; do
if [ "$1" == "-n" ]; then
echo -e "${dirarr[$i]}\n"
else
echo "${dirarr[$i]}"
fi
done
}
# choose a directory from history
function choose() {
if [ ! -z "$1" ]; then
if (( $1 >= 1 && $1 <= $dircnt )); then
num=$1;
((num--))
pushd "${dirarr[$num]}" > /dev/null
clear_fhist
return 0
fi
fi
# make IFS take \n as a separator
IFS="
"
PS3="jump to directory # "
select dir_ans in `dirl -n`
do
if [ ! -z $dir_ans ]; then
pushd "$dir_ans" > /dev/null
clear_fhist
break
fi
done
}
# save history
function save_dirhist() { dirl | sort > ~/.dirhistory; }
#load history
function load_dirhist() {
# this should go to .bash_login
if [ -r ~/.dirhistory ]; then
# TODO Test validity of directories
#cat .dirhistory | while read line
#do
# echo $line
#done
IFS="
"
dirarr=(`cat ~/.dirhistory`)
#dircnt=${#dirarr} #NOT WORKING
dircnt="`cat ~/.dirhistory|wc -l`"
fi
}
# find a directory in History based on a partial string
function cdpartial() {
if [ $dircnt -eq 0 ]; then
return
fi
for ((i="0"; $i < $dircnt; i++)); do
regexp="$@[^/]*$"
if [ `echo ${dirarr[$i]} | grep -i -E $regexp | wc -l` -gt 0 ]; then
\cd ${dirarr[$i]}
clear_fhist
return
fi
done
echo "$@: directory not found"
}
# clears forward history
function clear_fhist() {
dirarr_fhist=
dircnt_fhist="0"
}
# clears history
function clear_hist() {
dirarr=("`pwd`")
dircnt="0"
}
# loads history in .bashrc
load_dirhist
# -----END SECTION: Moving through directories --------