-
Notifications
You must be signed in to change notification settings - Fork 0
/
gotoimp.bash
460 lines (410 loc) · 12.2 KB
/
gotoimp.bash
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#
# GoToImp
# BASH directory traversal enhancement
#
# @author RuneImp <[email protected]>
#
#####
# ChangeLog
# ---------
# 2019-06-03 v0.6.0 Cleaned up data path code and added edit options
# 2019-02-19 v0.5.2 Updated and moved terminal wipe code to function term_wipe
# 2018-06-25 v0.5.1 Updated docs
# 2018-06-25 v0.5.0 Updated fresh install initialization and BASH completion
# 2018-06-18 v0.4.0 Added tmux support
# 2018-05-10 v0.3.0 Initial BASH Completion added
# 2018-05-?? v0.2.0 Added Update switch
# 2018-04-?? v0.1.0 Initial script creation
#
#
# CONSTANTS'ish
#
# declare -r and readonly is noisy...
#
declare _GOTO_APP_NAME='GoToImp'
declare _GOTO_APP_VERSION='0.6.0'
#
# VARIABLES
#
declare -a _gotoimp_alias_lines
declare -a _gotoimp_alias_names
declare -a _gotoimp_alias_paths
declare -a _gotoimp_list_titles
declare -i _gotoimp_alias_length=0
declare -i _gotoimp_name_width=0
declare -i _gotoimp_path_width=0
declare _gotoimp_alias_db=''
declare _gotoimp_data_path=''
declare _gotoimp_title_db=''
#
# FUNCTIONS
#
_gotoimp_bash_completion()
{
local list="$(goto -l)"
local words
local -i line_count=$(echo "$list" | wc -l)
# echo "\$list: $list"
# echo "\$line_count: $line_count"
if [[ $line_count -gt 1 ]]; then
local words="$(echo "$list" | tail -n +3 | grep -E -v '^\s*$' | awk '{print $1}' | tr "\n" " ")"
words="${words%%[[:space:]]}" # Remove trailing spaces
words="${words##[[:space:]]}" # Remove leading spaces
# echo complete -W "$words" goto
complete -W "$words" goto
else
echo "$list"
fi
}
_gotoimp_help()
{
cat <<-EOH
gotoimp v${_GOTO_APP_VERSION}
Command for storing and utilizing aliases to directories
OPTIONS:
-a | --add <alias> <path> Add a alias path
-at | --add-title <alias> <path> <title> Add an alias path with title
-d | --del | --delete <alias> Delete a goto alias
-e | --edit Display configuration paths for editing
-h | --help Display this help info
-l | --list List goto aliases
-t | --title <alias> <title> Add an alias title
-u | --up | --update Update a goto alias
-ut | --update-title <alias> <path> <title> Update a goto alias path with title
-v | --version Show the goto version
EOH
}
_gotoimp_load_list()
{
local -i found_space=1
local -i i=0
local -i j=0
local line=""
local goto_name=""
local goto_path=""
local goto_term=""
IFS='
'
_gotoimp_alias_lines=( $(cat "${_gotoimp_alias_db}") )
_gotoimp_title_lines=( $(cat "${_gotoimp_title_db}") )
IFS=' '
_gotoimp_alias_length=${#_gotoimp_alias_lines[@]}
_gotoimp_alias_names=( )
_gotoimp_alias_paths=( )
_gotoimp_name_width=0
_gotoimp_title_length=${#_gotoimp_title_lines[@]}
_gotoimp_title_names=( )
_gotoimp_title_terms=( )
i=0
while [[ $i -lt $_gotoimp_alias_length ]]; do
found_space=1
j=0
line=${_gotoimp_alias_lines[$i]}
while [[ $j -lt ${#line} ]]; do
# echo "line: ${line} ($j) | ${line:$j:1} ($j)"
if [[ $found_space -eq 0 ]]; then
goto_path="${goto_path}${line:$j:1}"
if [[ ${#goto_path} -gt ${_gotoimp_path_width} ]]; then
_gotoimp_path_width=${#goto_path}
fi
else
if [[ ${line:$j:1} != ' ' ]]; then
goto_name="${goto_name}${line:$j:1}"
if [[ ${#goto_name} -gt ${_gotoimp_name_width} ]]; then
_gotoimp_name_width=${#goto_name}
fi
else
found_space=0
fi
fi
let "j += 1"
done
# echo "goto_name: $goto_name | goto_path: $goto_path"
_gotoimp_alias_names=( "${_gotoimp_alias_names[@]}" "$goto_name" )
_gotoimp_alias_paths=( "${_gotoimp_alias_paths[@]}" "$goto_path" )
goto_name=""
goto_path=""
# echo "${i}: ${_gotoimp_alias_lines[$i]} (${_gotoimp_name_width})"
let "i += 1"
done
i=0
while [[ $i -lt $_gotoimp_title_length ]]; do
found_space=1
j=0
line=${_gotoimp_title_lines[$i]}
while [[ $j -lt ${#line} ]]; do
# echo "line: ${line} ($j) | ${line:$j:1} ($j)"
if [[ $found_space -eq 0 ]]; then
goto_term="${goto_term}${line:$j:1}"
else
if [[ ${line:$j:1} != ' ' ]]; then
goto_name="${goto_name}${line:$j:1}"
else
found_space=0
fi
fi
let "j += 1"
done
# echo "goto_name: $goto_name | goto_term: $goto_term"
_gotoimp_title_names=( "${_gotoimp_title_names[@]}" "$goto_name" )
_gotoimp_title_terms=( "${_gotoimp_title_terms[@]}" "$goto_term" )
goto_name=""
goto_term=""
# echo "${i}: ${_gotoimp_title_lines[$i]} (${_gotoimp_name_width})"
let "i += 1"
done
}
goto()
{
local -i i=0
local -i j=0
local -i not_found=0
local goto_path=''
local goto_name=''
local goto_path=''
local term_title=''
until [[ $# -eq 0 ]]; do
case "$1" in
-a | --add)
goto_name="$2"
goto_path="$3"
_gotoimp_load_list
if [[ $_gotoimp_alias_length -eq 0 ]]; then
echo " Adding first gotoimp alias!"
# touch "${_gotoimp_alias_db}"
fi
echo " gotoimp '${goto_name}' alias added to ${_gotoimp_alias_db}"
echo "${goto_name} ${goto_path}" >> "${_gotoimp_alias_db}"
# touch ~/.gotoimp.tmp
cat "${_gotoimp_alias_db}" | sort > ~/.gotoimp.tmp
mv ~/.gotoimp.tmp "${_gotoimp_alias_db}"
_gotoimp_bash_completion
shift
shift
;;
-at | --add-title)
goto_name="$2"
goto_path="$3"
goto_term="$4"
goto --add "$goto_name" "$goto_path"
goto --title "$goto_name" "$goto_term"
# if [[ ! -f "${_gotoimp_title_db}" ]]; then
# echo " Adding first gotoimp alias title!"
# touch "${_gotoimp_title_db}"
# fi
# echo " gotoimp '${goto_term}' alias title added to ${_gotoimp_title_db}"
# echo "${goto_name} ${goto_term}" >> "${_gotoimp_title_db}"
# cat "${_gotoimp_title_db}" | sort > ~/.gotoimp.tmp
# mv ~/.gotoimp.tmp "${_gotoimp_title_db}"
shift
shift
shift
;;
-d | --del | --delete | -r | --remove)
goto_name="$2"
if [[ -f "${_gotoimp_title_db}" ]]; then
echo " gotoimp '${goto_name}' alias removed"
cat "${_gotoimp_alias_db}" | grep -Ev "^$goto_name.*" > ~/.gotoimp.tmp
mv ~/.gotoimp.tmp "${_gotoimp_alias_db}"
else
echo " No gotoimp alias shorcuts defined yet"
fi
if [[ -f "${_gotoimp_title_db}" ]]; then
cat "${_gotoimp_title_db}" | grep -Ev "^$goto_name.*" > ~/.gotoimp.tmp
mv ~/.gotoimp.tmp "${_gotoimp_title_db}"
else
echo " No gotoimp alias titles defined yet"
fi
shift
;;
-e | --edit)
echo "$_GOTO_APP_NAME Data Path = '$_gotoimp_data_path'"
echo "$_GOTO_APP_NAME Alias DB = '$_gotoimp_alias_db'"
echo "$_GOTO_APP_NAME Title DB = '$_gotoimp_title_db'"
;;
-h | --help)
_gotoimp_help
;;
-l | --list)
# echo "\$_gotoimp_alias_length: $_gotoimp_alias_length"
_gotoimp_load_list
if [[ $_gotoimp_alias_length -eq 0 ]]; then
echo " No gotoimp aliases yet"
else
echo " gotoimp aliases:"
printf "%-${_gotoimp_name_width}s %-${_gotoimp_path_width}s %s\n" Alias Path Title
i=0
while [[ $i -lt $_gotoimp_alias_length ]]; do
term_title=""
j=0
while [[ $j -lt $_gotoimp_title_length ]]; do
if [[ "${_gotoimp_title_names[$j]}" == "${_gotoimp_alias_names[$i]}" ]]; then
term_title="'${_gotoimp_title_terms[$j]}'"
fi
let "j += 1"
done
printf "%-${_gotoimp_name_width}s %-${_gotoimp_path_width}s %s\n" "${_gotoimp_alias_names[$i]}" "${_gotoimp_alias_paths[$i]}" "$term_title"
let "i += 1"
done
echo
fi
;;
-t | --title)
goto_name="$2"
goto_term="$3"
if [[ ! -f "${_gotoimp_title_db}" ]]; then
echo " Adding first gotoimp alias title!"
touch "${_gotoimp_title_db}"
fi
echo " gotoimp '${goto_term}' alias title added to ${_gotoimp_title_db}"
echo "${goto_name} ${goto_term}" >> "${_gotoimp_title_db}"
cat "${_gotoimp_title_db}" | sort > ~/.gotoimp.tmp
mv ~/.gotoimp.tmp "${_gotoimp_title_db}"
shift
shift
;;
-v | --version)
echo "gotoimp v${_GOTO_APP_VERSION}"
;;
-u | --update)
goto_name="$2"
goto_path="$3"
tmp=$(goto -d "${goto_name}" 2>&1 /dev/null)
tmp=$(goto -a "${goto_name}" "${goto_path}" 2>&1 /dev/null)
echo " gotoimp '${goto_name}' alias updated in ${_gotoimp_alias_db}"
shift
shift
;;
-ut | --update-title)
goto_name="$2"
goto_path="$3"
goto_term="$4"
tmp=$(goto -d "${goto_name}" 2>&1 /dev/null)
tmp=$(goto -at "${goto_name}" "${goto_path}" "${goto_term}" 2>&1 /dev/null)
echo " gotoimp '${goto_name}' alias updated in ${_gotoimp_alias_db} and ${_gotoimp_title_db}"
shift
shift
shift
;;
*)
goto_name="$1"
_gotoimp_load_list
i=0
while [[ $i -lt $_gotoimp_alias_length ]]; do
if [[ "${_gotoimp_alias_names[$i]}" == "$goto_name" ]]; then
# echo "Going to: '${goto_name}'"
goto_path="${_gotoimp_alias_paths[$i]}"
# echo "\$goto_path: $goto_path"
goto_path="${goto_path/#\~/$HOME}"
# echo "\$goto_path: $goto_path"
cd "$goto_path"
not_found=1
break
fi
let "i += 1"
done
if [[ $not_found -eq 1 ]]; then
term_wipe
i=0
# echo "\$goto_name: $goto_name"
while [[ $i -lt $_gotoimp_title_length ]]; do
if [[ "${_gotoimp_title_names[$i]}" == "$goto_name" ]]; then
# echo "Going to: '${goto_name}'"
goto_term="${_gotoimp_title_terms[$i]}"
# echo "\$goto_term: $goto_term ($i)"
printf "\033]0;${goto_term}\007"
break
fi
let "i += 1"
done
fi
if [[ $not_found -eq 0 ]]; then
echo "gotoimp '$goto_name' alias not found"
fi
;;
esac
shift
done
}
term_wipe()
{
if [[ ${#VISUAL_STUDIO_CODE} -gt 0 ]]; then
clear
elif [[ $KITTY_WINDOW_ID -gt 0 ]] || [[ ${#TMUX} -gt 0 ]] || [[ "$TERM_PROGRAM" = 'vscode' ]]; then
printf '\033c'
elif [[ "$(uname)" == 'Darwin' ]] || [[ "$TERM_PROGRAM" = 'Apple_Terminal' ]] || [[ "$TERM_PROGRAM" = 'iTerm.app' ]]; then
osascript -e 'tell application "System Events" to keystroke "k" using command down'
elif [[ -x "$(which tput)" ]]; then
tput reset
elif [[ -x "$(which reset)" ]]; then
reset
else
clear
fi
}
#
# INIT
#
# Check for existing Shorcut Alias DB
if [[ -d "${XDG_DATA_HOME}/gotoimp" ]]; then
_gotoimp_data_path="${XDG_DATA_HOME}/gotoimp"
elif [[ -d ~/.local/share/gotoimp ]]; then
_gotoimp_data_path=~/.local/share/gotoimp
elif [[ -f ~/.local/gotoimp ]]; then
_gotoimp_data_path=~/.local/gotoimp
elif [[ -d ~/.gotoimp ]]; then
_gotoimp_data_path=~/.gotoimp
fi
# Define Path to Alias DB
if [[ ${#_gotoimp_data_path} -gt 0 ]]; then
_gotoimp_alias_db="${_gotoimp_data_path}/alias_db.txt"
if [[ -f "${_gotoimp_data_path}/alias_db.txt" ]]; then
# All good in the hood
:
elif [[ -f ~/.goto ]]; then
# NOTE: This is a bit magical. Should probably get user input.
_gotoimp_alias_db=~/.goto
echo "${_GOTO_APP_NAME} copying '"~/.goto"' to '${_gotoimp_data_path}/alias_db.txt'"
cp ~/.goto "${_gotoimp_data_path}/alias_db.txt"
else
echo "${_GOTO_APP_NAME} creating missing '${_gotoimp_data_path}/alias_db.txt'"
touch "${_gotoimp_data_path}/alias_db.txt"
fi
elif [[ -f ~/.goto ]]; then
_gotoimp_alias_db=~/.goto
fi
# Define Data Path if data path not found
if [[ ${#_gotoimp_data_path} -eq 0 ]]; then
if [[ -d "${XDG_DATA_HOME}" ]]; then
_gotoimp_data_path="${XDG_DATA_HOME}/gotoimp"
elif [[ -d ~/.local ]]; then
if [[ -d ~/.local/share ]]; then
_gotoimp_data_path=~/.local/share/gotoimp
else
_gotoimp_data_path=~/.local/gotoimp
fi
else
_gotoimp_data_path=~/.gotoimp
fi
if [[ ! -d "${_gotoimp_data_path}" ]]; then
mkdir -p "${_gotoimp_data_path}"
fi
fi
# Define path to Shorcut Alias DB
if [[ ${#_gotoimp_alias_db} -eq 0 ]]; then
_gotoimp_alias_db="${_gotoimp_data_path}/alias_db.txt"
if [[ ! -f "${_gotoimp_alias_db}" ]]; then
touch "${_gotoimp_alias_db}"
fi
fi
# Define path to Terminal Title DB
if [[ ${#_gotoimp_title_db} -eq 0 ]]; then
_gotoimp_title_db="${_gotoimp_data_path}/title_db.txt"
if [[ ! -f "${_gotoimp_title_db}" ]]; then
touch "${_gotoimp_title_db}"
fi
fi
# echo "_gotoimp_data_path = '$_gotoimp_data_path'"
# echo "_gotoimp_alias_db = '$_gotoimp_alias_db'"
# echo "_gotoimp_title_db = '$_gotoimp_title_db'"
_gotoimp_bash_completion