forked from jestor/cdd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdd.sh
More file actions
210 lines (181 loc) · 3.9 KB
/
Copy pathcdd.sh
File metadata and controls
210 lines (181 loc) · 3.9 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
#!/usr/bin/env bash
# This script is intended to be used as:
# alias cdd=". /<full path>/cder.sh"
level=0
dir_count=0
max_dir_count=0
current_dir=""
initial_dir=`pwd`
old_dir=$OLDPWD
seek_dir="$initial_dir/"
search_dir=0
erase=0
function partial_match {
local tmp_dir=`pwd`
if [ "$tmp_dir" == "/" ]; then
tmp_dir=""
fi
# Test and return a substring match, IE: /ape/moose == /ape/*
[[ "$seek_dir" == "$tmp_dir/$1/"* ]]
}
# Builds a list of all directories in the current directory
function get_dirs {
dirs=( )
dir_count=0
for f in *; do
if [ -d "$f" ]; then
dirs[$dir_count]="$f"
if [ $search_dir == 1 ] && partial_match $f; then
level=$dir_count
search_dir=0
fi
((dir_count++))
fi
done
#store the rewind marker at dir_count + 2
max_dir_count=$(($dir_count+2))
}
# Display the input string $1
# If $erase is set, then erase strlen($1) characters
function display_str {
if [ $erase == 0 ]; then
echo "$1"
else
for ((k = 0; k < ${#1}; k++)); do
echo -ne " "
done
echo
fi
}
# Display the current directory or erase the old directory
function print_dir {
if [ $1 == 1 ] ; then
# Rewind up max_dir_count lines
echo -e "\033[${max_dir_count}A"
else
get_dirs
current_dir=`pwd`
fi
erase=$1
display_str "$current_dir"
for ((j = 0; j < $dir_count; j++)); do
if ((j == level)); then
display_str "* ${dirs[$j]}"
else
display_str " ${dirs[$j]}"
fi
done
if [ $1 == 1 ] ; then
# Rewind up max_dir_count lines
echo -e "\033[${max_dir_count}A"
fi
}
function move_cursor {
local distance=$((dir_count - level))
if [ $1 == 1 ]; then
distance=$((distance + 1))
if [ $distance -gt 0 ]; then echo -ne "\033[${distance}A"; fi
echo -e " "
echo -e "*"
else
if [ $distance -gt 0 ]; then echo -ne "\033[${distance}A"; fi
echo -e "*"
echo -e " "
fi
local remainder=$((distance - 2))
if [ $remainder -gt 0 ]; then echo -ne "\033[${remainder}B"; fi
}
# Display instructions
function print_instructions {
if [ $1 == 1 ] ; then
echo -e "\033[11A"
fi
erase=$1
display_str "cdd is a directory navigation tool."
display_str "The following stdin input options are available:"
display_str " h - Navigate to the parent directory"
display_str " j - Move the directory selection down"
display_str " k - Move the directory selection up"
display_str " l - Enter the selected directory"
display_str " ESC | q - Exit cdd without changing the current directory"
display_str " Enter - Exit cdd at the current directory"
display_str ""
display_str "Press any key to continue."
# Set the next clear operation to clear away help
if [ $1 == 1 ] ; then
echo -e "\033[11A"
fi
}
redraw=1
while [ 1 ]; do
if [ $redraw == 1 ]; then
print_dir 0
fi
redraw=1
clear_dir=1
read -r -s -n 1 C
case $C in
"h") # Left
# we are traversing "backwards", so instruct search directory to occur
search_dir=1
cd ..
level=0
;;
"j") # Down
level=$((level+1))
clear_dir=0
redraw=0
if [ $level -ge $dir_count ]; then
level=$((dir_count-1))
else
move_cursor 1
fi
;;
"k") # Up
level=$((level-1))
clear_dir=0
redraw=0
if [ $level -lt 0 ]; then
level=0
else
move_cursor 0
fi
;;
"l") # Right
enter_dir="$current_dir/${dirs[$level]}"
if [ "$current_dir" == "/" ]; then
enter_dir="/${dirs[$level]}"
fi
# if we are traversing "forwards", ie: Entering /ape with a seek_dir of /ape/moose
if partial_match "${dirs[$level]}"; then
search_dir=1
cd "$enter_dir"
else
cd "$enter_dir"
# This isn't forwards, so set the new seek_dir to `pwd`/
seek_dir="`pwd`/"
fi
#reset the current selection index
level=0
;;
"q" | $'\e')
cd "$initial_dir"
export OLDPWD=$old_dir
break
;;
"")
export OLDPWD=$initial_dir
break
;;
*)
print_dir 1
print_instructions 0
read -r -s -n 1 C
print_instructions 1
clear_dir=0
;;
esac
if [ $clear_dir == 1 ]; then
print_dir 1
fi
done