-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnavigator.sh
executable file
·293 lines (248 loc) · 10.2 KB
/
navigator.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env bash
install_dir="$HOME/.navigator-home"
DEFAULT="\e[0m"
DEFAULTBOLD="\e[1m"
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
MAGENTA="\e[35m"
# To check if correct arguments were passed and to display help.
argument_checker() {
case $1 in
--help)
cat "$install_dir/help.txt"
return 1
;;
-h | --hidden | -a | --all)
return 0
;;
*)
echo "Invalid argument. Do nn --help to view usage."
return 1
;;
esac
}
# To print hidden and non-hidden directory names in different colors.
print_directory_names() {
if [[ $current_shell =~ "zsh" ]]; then
if [[ $d =~ "\..*" ]]; then
echo -e "${YELLOW}$serial\t---------------\t${DEFAULT}${DEFAULTBOLD}${MAGENTA}$d${DEFAULT}"
else
echo -e "${YELLOW}$serial\t---------------\t${DEFAULT}${DEFAULTBOLD}${BLUE}$d${DEFAULT}"
fi
else
if [[ $d =~ ^\..*$ ]]; then
echo -e "${YELLOW}$serial\t---------------\t${DEFAULT}${DEFAULTBOLD}${MAGENTA}$d${DEFAULT}"
else
echo -e "${YELLOW}$serial\t---------------\t${DEFAULT}${DEFAULTBOLD}${BLUE}$d${DEFAULT}"
fi
fi
}
# To print the heading and the back option.
print_heading() {
# Printing the heading-
echo -e "${RED}No.\t \tDirectory${DEFAULT}"
}
# To specify arguments to display hidden or all directories, if needed, and to print directories in different ways for zsh and bash.
directory_chooser() {
local current_shell=$(ps -p $$ -ocomm=)
# If the current directory isn't / then display the option to back using 0.
if [[ "$PWD" != "/" && ! -z $non_hidden_directories ]]; then
echo -e "${YELLOW}0\t---------------\t${DEFAULT}${DEFAULTBOLD}${BLUE}..${DEFAULT}"
fi
# If current shell is zsh, then use */ and .*/ to print directory names.
if [[ $current_shell = 'zsh' ]]; then
if [[ $1 = "-h" || $1 = "--hidden" ]]; then
if [[ -z $hidden_directories ]]; then
echo -e "No hidden directories to navigate to. Use nn to view all non-hidden directories, if any. Use 0 to go back to the previous directory.\n\n"
return 0
fi
for d in .*/; do
echo -e "${YELLOW}$serial\t---------------\t${DEFAULT}${DEFAULTBOLD}${MAGENTA}$d${DEFAULT}"
directory_list+=("$d")
serial=$((serial+1))
done
elif [[ $1 = "-a" || $1 = "--all" ]]; then
if [[ -z $non_hidden_directories && ! -z $hidden_directories ]]; then
for d in .*/; do
# Checking if directory names start from '.' or not to print hidden and non hidden directories in different colors.
print_directory_names
directory_list+=("$d")
serial=$((serial+1))
done
elif [[ ! -z $non_hidden_directories && -z $hidden_directories ]]; then
for d in */; do
# Checking if directory names start from '.' or not to print hidden and non hidden directories in different colors.
print_directory_names
directory_list+=("$d")
serial=$((serial+1))
done
elif [[ -z $non_hidden_directories && -z $hidden_directories ]]; then
echo "No directories to navigate to. Enter 0 to go back to the previous directory."
else
if [[ ! -z $non_hidden_directories && -z $hidden_directories ]]; then
for d in */; do
# Checking if directory names start from '.' or not to print hidden and non hidden directories in different colors.
print_directory_names
directory_list+=("$d")
serial=$((serial+1))
done
else
for d in $(echo */ && echo .*/); do
# Checking if directory names start from '.' or not to print hidden and non hidden directories in different colors.
print_directory_names
directory_list+=("$d")
serial=$((serial+1))
done
fi
fi
else
if [[ -z $non_hidden_directories ]]; then
echo -e "No non hidden directories available. Use nn -h to view hidden directories, if any. Enter 0 to go back to the previous directory."
return 0
fi
for d in */; do
echo -e "${YELLOW}$serial\t---------------\t${DEFAULT}${DEFAULTBOLD}${BLUE}$d${DEFAULT}"
directory_list+=("$d")
serial=$((serial+1))
done
fi
# If current shell is bash, then use the lists made using find to print directory names
else
IFS=$'\n'
if [[ $1 = "-h" || $1 = "--hidden" ]]; then
if [[ -z $hidden_directories ]]; then
echo -e "No directories to navigate to\n"
return 0
fi
for d in $hidden_directories; do
echo -e "${YELLOW}$serial\t---------------\t${DEFAULT}${DEFAULTBOLD}${MAGENTA}$d${DEFAULT}"
directory_list+=("$d")
serial=$((serial+1))
done
elif [[ $1 = "-a" || $1 = "--all" ]]; then
if [[ -z $non_hidden_directories && ! -z $hidden_directories ]]; then
for d in $hidden_directories; do
# Checking if directory names start from '.' or not to print hidden and non hidden directories in different colors.
print_directory_names
directory_list+=("$d")
serial=$((serial+1))
done
elif [[ ! -z $non_hidden_directories && -z $hidden_directories ]]; then
for d in $non_hidden_directories; do
# Checking if directory names start from '.' or not to print hidden and non hidden directories in different colors.
print_directory_names
directory_list+=("$d")
serial=$((serial+1))
done
else
for d in $directories; do
# Checking if directory names start from '.' or not to print hidden and non hidden directories in different colors.
print_directory_names
directory_list+=("$d")
serial=$((serial+1))
done
fi
else
for d in $non_hidden_directories; do
echo -e "${YELLOW}$serial\t---------------\t${DEFAULT}${DEFAULTBOLD}${BLUE}$d${DEFAULT}"
directory_list+=("$d")
serial=$((serial+1))
done
fi
fi
}
navigator() {
local serial=1
local directory_list=("#")
local current_shell=$(ps -p $$ -ocomm=)
# Indicating pwd since it can get confusing during recursive use (powerline style)
echo -e "\n\e[100m\e[97m current directory ${DEFAULT}\e[90m\e[44m${DEFAULT}\e[44m\e[30m$PWD ${DEFAULT}${BLUE}${DEFAULT}\n"
local directories=$(find . -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | sort)
local hidden_directories=$(find . -maxdepth 1 -regex ".*/\.+.*" -type d -exec basename {} \; | sort)
local non_hidden_directories=$(find . -maxdepth 1 -regex ".*/[^\.].+" -type d -exec basename {} \; | sort)
if [[ -z $non_hidden_directories && ! -z $hidden_directories && $# = 0 ]]; then
echo "No non hidden directories available. Use \"nn -a\" or \"nn -h\" to view hidden directories."
return 0
fi
directory_chooser $1 > >( { column -t -s $'\t'; printf "Enter the directory number (s to stop)\n${GREEN}|-> ${DEFAULT}"; } )
read -r number
if [[ "$number" == "s" ]]; then
cd $PWD
return 0;
fi
if [[ "$number" == "h" ]]; then
clear
echo "Use these commands inside the navigator: "
echo -e "c\t\t\tclears the screen"
echo -e "d\t\t\tbrings up a system info dashboard"
echo -e "f\t\t\tshows the files belonging to the current directory"
navigator
fi
if [[ "$number" == "c" ]]; then
clear
navigator
fi
if [[ "$number" == "d" ]]; then
clear
echo -e "\n${GREEN}Memory:${DEFAULT}"
data=$(free -h | awk '{print $2}')
total=$(echo $data | awk '{print $2}')
data=$(free -h | awk '{print $3}')
used=$(echo $data | awk '{print $2}')
data=$(free -h | awk '{print $4}')
free_mem=$(echo $data | awk '{print $2}')
echo -e "${YELLOW}\t\tTotal\t\tUsed\t\tFree${DEFAULT}"
echo -e "\t\t${total}\t\t${used}\t\t${free_mem}"
echo -e "\n${GREEN}Kernel:${DEFAULT}${YELLOW} $(uname -r)${DEFAULT}"
echo -e "\n${GREEN}Shell:${DEFAULT}${YELLOW} $SHELL${DEFAULT}"
echo ""
read -p ""
clear
navigator
fi
if [[ "$number" == "f" ]]; then
files=$(ls -al | grep "drwx" -v | awk '{print $9}')
clear
echo -e "Files in ${BLUE}$(pwd)${DEFAULT}:\n"
for file in $files; do
if [[ -x $file ]]; then
EXEC="Executable"
COLOR=${GREEN}
else
EXEC=" "
COLOR=${DEFAULT}
fi
if [[ -w $file ]]; then
WRT="Writable"
else
WRT=" "
fi
if [[ -r $file ]]; then
READ="Readable"
else
READ=" "
fi
echo -e "${EXEC} - ${WRT} - ${READ} : ${COLOR}$file${DEFAULT}"
done
fi
if [[ "$number" -ge 0 && "$number" -lt $serial ]] 2>/dev/null; then
if [[ "$number" == "0" && "$PWD" != "/" ]]; then
cd ..
else
[[ $current_shell =~ 'zsh' ]] && number=$((number+1))
cd "${directory_list[number]}" 2> /dev/null
fi
else
echo "Invalid choice"
return 1
fi
navigator $1
}
if [[ $# -ne 0 ]]; then
argument_checker $1 && navigator $1
return 0
else
navigator
fi