-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoto.sh
201 lines (165 loc) · 5.69 KB
/
goto.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
#!/usr/bin/env bash
# go2 command - love commandline and love bookmarks, this is their love child!
#
# License: Creative Commons License
# (details available here http://creativecommons.org/licenses/by-sa/3.0/deed.en_US)
# Author: Dino Korah
# URL: https://github.com/codemedic/bash-ninja
#
# Description:
# Helps you navigate through source trees using custome book marks and
# comes complete with auto-complete
[ -t 0 ] || return 0
# version control root and bookmark definitions
: "${go_projects_conf:=$HOME/go_bookmarks.conf}"
# debug prints enabled ?
: "${go_debug:=0}"
# bash debug/xtrace enabled ?
: "${go_bash_debug:=0}"
: "${go_bash_debug_file:="$HOME/go-xtrace.txt"}"
# debug print
__d() {
[ "$go_debug" -eq 1 ] || {
# if debug is disables, the pipe form of invocations should still oipe through
[ -n "$1" ] || cat
return
}
: "${go_debug_syslog_tag:=go2}"
if [ -n "$1" ]; then
/usr/bin/logger -t "$go_debug_syslog_tag" -- "$@" </dev/null
else
# pipe through mode
/usr/bin/logger -t "$go_debug_syslog_tag" -s 2>&1 | \
cut -c"$((${#go_debug_syslog_tag}+2))-"
fi
}
__go__load_definitions()
{
# shellcheck disable=SC1090
source "$go_projects_conf"
}
GoRegexBookmarkName='^[a-zA-Z0-9_]*$'
GoRegexBookmarkSuffix='^(([a-zA-Z0-9_]+)#)(.*)$'
__go__find() {
if command -v gfind &>/dev/null; then
gfind "$@"
else
find "$@"
fi
}
__go__get_completions_paths() {
local bookmark="$1" preserve_prefix="$2" suffix="$3"; shift 3
local find_completions=1 name_pattern=()
# e.g cd_root=/
local bookmark_var="cd_${bookmark}"
if declare -p "${bookmark_var}" &> /dev/null; then
local path="${!bookmark_var}" path_offset
# e.g root#dev/
if [[ "$suffix" =~ /$ ]] && [ -d "${path}/${suffix}" ]; then
path_offset="$suffix"
path="${path}/${path_offset}"
# If there is a path in the suffix, look only in the closest directory (a level up) if that exists
# e.g root#dev/sel
elif [[ "$suffix" == */* ]] && [ -d "${path}/${suffix/%+([^\/])}" ]; then
# strip out the suffix
path_offset="${suffix/%+([^\/])}"
path="${path}/${path_offset}"
name_pattern=( -name "$(basename "$suffix")*" )
# a non-empty suffix and has no '/' in it (name pattern cannot have '/' in them)
elif [ -n "$suffix" ] && [[ "$suffix" != */* ]]; then
name_pattern=( -name "${suffix}*" )
elif [ -n "$suffix" ]; then
find_completions=0
fi
if [ "$find_completions" = 1 ] && [ -d "$path" ]; then
while read -r compl_option; do
COMPREPLY+=( "${preserve_prefix}${path_offset}${compl_option}" )
done < <(__go__find -L "$path" -maxdepth 1 -mindepth 1 "${name_pattern[@]}" \( -type d -or -type l -xtype d \) -printf '%P/\n')
fi
fi
}
__go__get_completions() {
__d "param $*"
local cur=${COMP_WORDS[COMP_CWORD]}
__d cur: "$cur"
__go__load_definitions
# enable extended globs option; restored at the end!
local saved_opts; saved_opts="$(shopt -p); $(set +o | grep -v history)"
shopt -s extglob
[ "${go_bash_debug}" = 0 ] || {
exec {BASH_XTRACEFD}>"$go_bash_debug_file"
set -x
}
if [[ "$cur" == /* ]]; then
__go__get_completions_paths root '' "$1"
elif [[ "$cur" =~ $GoRegexBookmarkSuffix ]]; then
__go__get_completions_paths "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}" "${BASH_REMATCH[3]}"
elif [[ "$cur" =~ $GoRegexBookmarkName ]]; then
# shellcheck disable=SC2086
for bookmark in ${!cd_*}; do
[[ "$bookmark" != "cd_${cur}"* ]] || COMPREPLY+=( "${bookmark#cd_}#" )
done
fi
__d "COMPREPLY: ${COMPREPLY[*]}"
# restore saved shell options
set +x; # make sure debug is off
# toggle history around the eval so that it doesn't get added to command-history
set +o history
eval "$saved_opts"
set -o history
}
go2() {
__d params: "$@"
__go__load_definitions
local bookmark;
local suffix;
if [ -n "$1" ]; then
# are we dealing with a bookmark with path suffix
# bookmark#path/to/some/thing
if [[ "$1" =~ $GoRegexBookmarkSuffix ]]; then
bookmark="${1%%#*}"
suffix="${1#*#}"
# or if it is path with no bookmark; i.e using the "root" bookmark
elif [[ "$1" == /* ]]; then
bookmark=root
suffix="$1"
# else assume it is a bookmark name
elif [[ "$1" =~ $GoRegexBookmarkName ]]; then
bookmark="$1"
suffix=''
fi
fi
__d "bookmark: $bookmark"
local bookmark_var="cd_${bookmark}"
declare -p "${bookmark_var}" &> /dev/null ||
echo "Unknown bookmark '${bookmark}'"
local cd_path="${!bookmark_var}";
[ -n "$suffix" ] &&
cd_path="${cd_path}/${suffix}";
{ [ -n "$cd_path" ] && cd "$cd_path"; } ||
echo "GO path could not be found."
}
go2_add()
{
local shortcut dir;
if [ -z "$1" ]; then
echo Shortcut name not given.
return 1
elif ! [[ "$1" =~ ^[a-zA-Z0-9_]+$ ]]; then
echo "Shortcut name must be a valid bash variable name"
return 1;
fi
shortcut="$1"; shift;
dir="${1:-$(pwd)}";
echo "cd_$shortcut=\"$dir\"" >> "${go_projects_conf}";
}
go2_enable_bash_debug() {
go_bash_debug="${1:-1}"
echo "Logging xtrace to ${go_bash_debug_file}" 1>&2
}
if ! declare -p __go__script_loaded &>/dev/null; then
complete -F __go__get_completions -o dirnames -o nospace go2;
alias goto=go2
complete -F __go__get_completions -o dirnames -o nospace goto;
__go__script_loaded=1
fi