-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd.sh
More file actions
148 lines (137 loc) · 2.87 KB
/
cd.sh
File metadata and controls
148 lines (137 loc) · 2.87 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
# Author: Lital Natan <litaln@gmail.com>
# Bash enhancement, save last 10 directories and allow moving between them quickly
# modified CD command
# adds the -l flag - list saved directories
# cd -X (x is numeric) change the directory no X
#
if [[ -z $DIRH_HISTORY ]]; then
DIRH_HISTORY=$PWD
fi
export DIRH_HISTORY
export DIRH_MAX_COUNT=10
dirh_append() {
if [[ -z $DIRH_HISTORY ]]; then
DIRH_HISTORY=$1
else
DIRH_HISTORY="$DIRH_HISTORY:$1"
fi
export DIRH_HISTORY
}
dirh_instr() {
local str=$1
local substr=$2
local i
local occurance
occurance=$3
local len
let len=${#substr}
[[ -z $occurance ]] && occurance=1
for ((i=0 ; i < ${#str} ; i++ )); do
if [[ ${str:$i:$len} == $substr ]]; then
if [[ $occurance -lt 1 ]]; then
echo -1
return
fi
if [[ $occurance -ne 1 ]]; then
let occurance=$occurance-1
continue
fi
echo $i
return
fi
done
echo -1
}
dirh_pop() {
local pos
let pos=$(dirh_instr $DIRH_HISTORY :)
if [[ $pos -eq -1 ]]; then
DIRH_HISTORY=""
return
fi
let pos=$pos+1
DIRH_HISTORY=${DIRH_HISTORY:$pos}
export DIRH_HISTORY
}
dirh_get() {
local dirn
let dirn=$1
let dcount=$(dirh_count $DIRH_HISTORY :)+1
if [[ $dirn -gt $dcount ]]; then
echo "No such bookmark" >> /dev/stderr
return 1
fi
let e=$1-1
let start=$(dirh_instr $DIRH_HISTORY : $e)+1
let end=$(dirh_instr $DIRH_HISTORY : $dirn)
if [[ $start -lt 0 ]]; then start=0; fi
if [[ $end -eq -1 ]]; then
echo ${DIRH_HISTORY:$start}
else
let length=$end-$start
echo ${DIRH_HISTORY:$start:$length}
fi
}
dirh_count() {
local str=$1
local substr=$2
local i
local occurances
occurances=0
for ((i=0 ; i < ${#str} ; i++ )); do
if [[ ${str:$i:1} == $substr ]]; then
let occurances=$occurances+1
fi
done
echo $occurances
}
dirh_exists() {
local dcount
local dirpath=$1
if [[ -z $DIRH_HISTORY ]]; then
return 1
fi
let dcount=$(dirh_count $DIRH_HISTORY :)+1
for ((i=1 ; i<=$dcount ; i++)); do
if [[ $(dirh_get $i) == $dirpath ]]; then
return 0
fi
done
return 1
}
dirh_list() {
echo $DIRH_HISTORY | tr ":" "\n" | cat -n
}
dirh_add() {
local dirpath=$1
[[ $(dirh_instr ":$DIRH_HISTORY:" ":$dirpath:") -ne -1 ]] && return
dirh_append $dirpath
let dcount=$(dirh_count $DIRH_HISTORY :)
if [[ $dcount -gt $DIRH_MAX_COUNT ]]; then
dirh_pop
fi
}
cd() {
local c=$1
if [[ $c == "-l" ]]; then
dirh_list
return $?
fi
if [[ $c == "-c" ]]; then
export DIRH_HISTORY=""
return $?
fi
if [[ ${c} != "-" ]] && [[ ${c:0:1} == "-" ]] && [[ "$c" -lt 0 ]]; then
let c=$c*-1
NEXT_DIR=$(dirh_get $c)
command cd $NEXT_DIR
RET=$?
[[ $RET -ne 0 ]] && return $RET
echo -e "\033[31m${NEXT_DIR}"
return 0
fi
command cd "$@"
RET=$?
[[ $RET -ne 0 ]] && return $RET
dirh_add $PWD
}