-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgup.sh
225 lines (187 loc) · 4.71 KB
/
gup.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
#!/bin/bash
# Gup version.
__GUP_VERSION="1.6"
# Gup entry-point.
gup() {
local method="exec"
# Verbose mode.
__GUP_VERBOSE=false
# Target.
local target=""
local force_number=""
# Parse arguments.
while [ $# -ne 0 ]
do
case "$1" in
--verbose|-v) __GUP_VERBOSE=true ;;
--help) method="help" ;;
--interactive|-i) method="interactive" ;;
--version) method="version" ;;
--number|-n) force_number=true ;;
*) target="$1"; break ;;
esac
shift
done
# Call method as per command.
__gup_log "Calling __gup_$method()"
case "$method" in
"exec") __gup_exec "$target" $force_number ;;
"interactive") __gup_interactive ;;
"version") __gup_version ;;
"help") __gup_help ;;
esac
}
# Executes gup on an argument.
#
# Usage: gup <target> <force-number>
__gup_exec() {
# Get the current working directory.
local pwd_old="$PWD"
__gup_log "Old PWD: $pwd_old"
# Determine target.
local target="$1"
# Is the target forceably a number?
local force_number="$2"
# If target is empty, force it as the number "1".
if [[ -z "$target" ]]; then
target=1
force_number=true
fi
__gup_log "Target is: $target."
# Treat target as a string first, unless it is forced to be a number.
# This looks up ancestor directories with numeric names, if any.
if [[ -z "$force_number" ]]; then
__gup_log "Treating target as a string."
__gup_by_string "$target"
else
__gup_log "--number flag detected."
fi
# If treating the argument as a string doesn't have any effect,
# then treat the argument as a number.
if [[ "$pwd_old" == "$PWD" ]]; then
__gup_log "Treating target as a number."
__gup_by_number $target
fi
__gup_log "New PWD: $PWD"
}
# Runs gup with numeric argument.
__gup_by_number() {
# Cast argument to integer.
local -i count="${1:-1}"
local command
if (( count < 0 )); then
__gup_log "Argument cannot be negative."
return 1
fi
# If target is a string it will result in 0. Alternatively, the user
# might've entered "0" as the target. When argument is 0, we do nothing.
if (( count != 0 )); then
__gup_log "Going up $count directories."
command="cd "
for _ in $(seq 1 "$count"); do
command="$command../"
done
__gup_eval "$command"
fi
}
# Runs gup with string argument.
__gup_by_string() {
local target="$1"
local dest
local curdir=""
# Look for the nearest parent directory named "$target".
dest=$(dirname "$PWD")
while [ "$dest" != "/" ]
do
curdir=$(basename "$dest")
__gup_log "Analyzing: \"$curdir\""
if [[ "$curdir" == "$target" ]]; then
__gup_log "Ancestor directory \"$target\" found"
break
else
__gup_log "Moving up: \"$curdir\" != \"$target\""
fi
dest=$(dirname "$dest")
done
# If a matching directory was found, go to it. However,
# if a match was not found, we should be at "/" right now.
if [[ "$dest" == "/" ]]; then
__gup_log "Ancestor directory \"$target\" not found."
else
__gup_eval "cd \"$dest\""
fi
}
# Displays version information.
#
# Usage: __gup_version
__gup_version() {
echo "gup $__GUP_VERSION"
if [[ $__GUP_VERBOSE == true ]]; then
echo "Author: Jigarius | jigarius.com"
echo "GitHub: github.com/jigarius/gup"
fi
}
# Allows user to choose the directory they want to go up to.
#
# Usage: __gup_interactive
__gup_interactive() {
local dest=""
local curdir=""
local -a choices=()
local -i reply=0
dest=$(dirname "$PWD")
# Collect all directory names in $PWD.
while [ "$dest" != "/" ]
do
curdir=$(basename "$dest")
choices+=( "$curdir" )
dest=$(dirname "$dest")
done
# Generate a menu.
echo "Choose a destination directory:"
select choice in "${choices[@]}"; do
# If the choice is invalid, do nothing.
if [[ -z $choice ]]; then
__gup_log "Choice invalid. Doing nothing."
return 1
fi
# Determine the number of levels we have to go up.
reply="$REPLY"
__gup_log "Option chosen: $REPLY ($choice), i.e. go up $reply directories."
# Execute gup with the number of levels to go up.
__gup_exec "$reply" true
return 0
done
}
# Executes a command.
#
# Usage: __gup_eval [command]
__gup_eval() {
local command
command="$1"
if [[ -n "$command" ]]; then
__gup_log "Running: $command"
eval "$command"
fi
}
# Logs a message.
#
# Usage: __gup_log $MESSAGE
__gup_log() {
local message=$1
if [ "$__GUP_VERBOSE" = true ]; then
echo "$message"
fi
}
# Show user manual.
__gup_help() {
# If the "man" command doesn't exist, we can't do anything.
if ! command -v man &> /dev/null; then
return
fi
local dir
local cmd
dir=$(dirname "$0")
cmd="man \"$dir/gup.groff\""
__gup_eval "$cmd"
}