-
Notifications
You must be signed in to change notification settings - Fork 2
/
goto.sh
65 lines (48 loc) · 1.22 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
goto()
{
local name path
[ $# -ne 1 ] && echo "Usage: goto <label>" >&2 && return 1
name=__goto_$1
eval path=\$$name
if [ -n "$path" ]; then
cd "$path"
else
echo "goto: undefined path label '$name'" >&2
return 1
fi
}
addgoto()
{
local label oldpath path
[ $# -ne 2 ] && echo "Usage: addgoto <label> <path>" >&2 && return 1
if [ ! -r ${HOME}/.gotos ]; then
touch ${HOME}/.gotos
fi
label=$1
path=$(realpath $2)
eval oldpath=\$__goto_$label
[ -n "$oldpath" ] && echo "addgoto: redefining label from '$oldpath'" >&2
sed -i'' -e "/^[[:space:]]*${label}[[:space:]][[:space:]]*/d" ${HOME}/.gotos
echo "$label $path" >> ${HOME}/.gotos
sourcegotos
}
listgotos()
{
cat ${HOME}/.gotos
}
sourcegotos()
{
local i label path
[ $# -ne 0 ] && echo "sourcegotos: unexpected arguments" >&2 && return 1
[ -r ${HOME}/.gotos ] || return
i=1
while read label path; do
if [ -z "$label" -o -z "$path" ]; then
echo "sourcegotos: warning: invalid goto definition on line $i" >&2
continue
fi
eval __goto_${label}=$path
i=$(($i + 1))
done < ${HOME}/.gotos
}
sourcegotos