-
Notifications
You must be signed in to change notification settings - Fork 0
/
goto.sh
168 lines (151 loc) · 4.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
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
#!/usr/bin/env bash
## Get project's directory
__GOTO_SOURCE="${BASH_SOURCE[0]}"
while [ -h "${__GOTO_SOURCE}" ]; do
__GOTO_DIR="$( cd -P "$( dirname "${__GOTO_SOURCE}" )" >/dev/null 2>&1 && pwd )"
__GOTO_SOURCE="$(readlink "${__GOTO_SOURCE}")"
[[ ${__GOTO_SOURCE} != /* ]] && __GOTO_SOURCE="${__GOTO_DIR}/${__GOTO_SOURCE}"
done
__GOTO_WORKDIR="$( cd -P "$( dirname "${__GOTO_SOURCE}" )" >/dev/null 2>&1 && pwd )"
__GOTO_SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")
## Function for printing multiple strings
function goto_user_info() {
for m in "${@}"; do
echo "${m}"
done
}
## Project's direcctory
## Default: ${__GOTO_WORKDIR}. To change this, issue goto set-pdir /some/path
export _goto_workdir="$(python3 ${__GOTO_WORKDIR}/state.py get_workdir)"
export _goto_projectdir="$(python3 ${__GOTO_WORKDIR}/state.py get_workdir)"
## Simple help menu
function __goto_help() {
echo -e "Goto -- help menu\n"
echo -e " alias\t\tChange directory to the given alias name"
echo -e " set <alias>\tSet an alias to this location"
echo -e " rm <alias>\tRemove alias from list"
echo -e " list\t\tList saved aliases"
echo -e " show-projects\tGet projects"
echo -e " project\tNavigate to an available project"
echo -e " set-pdir\tSet a new project path"
echo -e " help\t\tShow this message and exit"
echo -e "\n"
}
## _goto cd function
function _goto() {
is_alias="$(python3 ${__GOTO_WORKDIR}/state.py check ${1})"
if [[ "${is_alias%%::::*}" == "True" ]]; then
cd "${is_alias##*::::}"
else
subpath="$(python3 "${__GOTO_WORKDIR}/state.py" "check_paths" "${1}")"
if [[ ! -d "${_goto_projectdir}/${subpath}" ]]; then
echo "Error: ${subpath} is not a directory or it does not exist"
return 1
fi
echo "Changing directory to --- ${_goto_workdir}/${subpath}"
cd "${_goto_projectdir}/${subpath}"
fi
}
## goto routine
function goto() {
if [[ "${#}" -eq "0" ]]; then
__goto_help
return 0
fi
while [[ "${#}" -gt "0" ]]; do
case "${1}" in
"alias")
if [[ -z "${2}" ]]; then
echo "You need to give an alias name"
return 1
fi
echo "Changing directory alias --- ${2}"
cd "$(python3 "${__GOTO_WORKDIR}/state.py" "alias" "${2}")"
shift 2;;
"set")
if [[ -z "${2}" ]]; then
echo "You need to give an alias name"
return 1
fi
python3 "${__GOTO_WORKDIR}/state.py" "add" "${2}" "${PWD}"
shift 2;;
"list")
aliases="$(python3 ${__GOTO_WORKDIR}/state.py list)"
echo "Aliases"
_IFS="${IFS}"
IFS=","
for i in ${aliases}; do
echo -e " ${i%%::::*} @ ${i##*::::}"
done
IFS="${_IFS}"
shift 1
return 0;;
"rm")
python3 "${__GOTO_WORKDIR}/state.py" "rm" "${2}"
shift 2;;
"show-projects")
echo "Projects"
goto_user_info $(ls "${_goto_projectdir}")
shift 1;;
"set-pdir")
if [[ -z "${2}" ]]; then
echo "What dir?"
return 1
fi
python3 "${__GOTO_WORKDIR}/state.py" "set_workdir" "${2}"
export _goto_projectdir="$(python3 ${__GOTO_WORKDIR}/state.py get_workdir)"
shift 2;;
"project")
if [[ -z "${2}" ]]; then
echo "Which project?"
return 1
fi
_goto "${2}"
shift 2;;
"help")
__goto_help
return 0;;
*)
_goto "${@}"
return 0;;
esac
done
}
_goto_completions_dir() {
local cur prev
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
aliases="$(python3 ${__GOTO_WORKDIR}/state.py list)"
calianses=""
_IFS="${IFS}"
IFS=","
for i in ${aliases}; do
calianses="${calianses} ${i%%::::*}"
done
IFS="${_IFS}"
case ${COMP_CWORD} in
1)
COMPREPLY=($(compgen -W "alias set list rm project show-projects set-pdir help ${calianses}" -- ${cur}))
;;
2)
case ${prev} in
"project" | "show-projects")
for i in $(ls "${_goto_projectdir}"); do
if [[ -d "${_goto_projectdir}/${i}" ]]; then
COMPREPLY+=($(compgen -W "${i}" -- "${cur}"))
fi
done;;
"rm" | "alias")
aliases="$(python3 ${__GOTO_WORKDIR}/state.py list)"
_IFS="${IFS}"
IFS=","
for i in ${aliases}; do
COMPREPLY+=($(compgen -W "${i%%::::*}" -- "${cur}"))
done
IFS="${_IFS}";;
esac;;
*)
COMPREPLY=();;
esac
}
complete -F _goto_completions_dir goto